Skip to content

Commit

Permalink
add unit tests for addressbook update
Browse files Browse the repository at this point in the history
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
  • Loading branch information
call-me-matt committed Aug 2, 2020
1 parent 73691e4 commit fdacb31
Showing 1 changed file with 140 additions and 1 deletion.
141 changes: 140 additions & 1 deletion tests/unit/Service/SocialApiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public function socialProfileProvider() {
];
}

public function updateAddressbookProvider() {
return [
'not user enabled' => ['yes', 'no', Http::STATUS_FORBIDDEN],
'not admin allowed' => ['no', 'yes', Http::STATUS_FORBIDDEN],
'not allowed, not enabled' => ['no', 'no', Http::STATUS_FORBIDDEN],
'allowed and enabled' => ['yes', 'yes', Http::STATUS_OK],
];
}

public function setUp() {
parent::setUp();

Expand Down Expand Up @@ -98,7 +107,6 @@ public function testDeactivatedSocial() {
$this->assertEmpty($result);
}


/**
* @dataProvider socialProfileProvider
*/
Expand Down Expand Up @@ -144,4 +152,135 @@ public function testUpdateContact($social, $connector, $httpResult, $expected) {

$this->assertEquals($expected, $result->getStatus());
}

protected function setupAddressbooks() {
$validContact1 = [
'UID' => '11111111-1111-1111-1111-111111111111',
'FN' => 'Valid Contact One',
'VERSION' => '4.0',
'X-SOCIALPROFILE' => [['type' => 'someNetwork', 'value' => 'someId1']],
];
$validContact2 = [
'UID' => '22222222-2222-2222-2222-222222222222',
'FN' => 'Valid Contact Two',
'VERSION' => '4.0',
'PHOTO' => "data:someHeader;base64," . base64_encode('someBody'),
'X-SOCIALPROFILE' => [['type' => 'someNetwork', 'value' => 'someId2']],
];
$emptyContact = [
'UID' => '00000000-0000-0000-0000-000000000000',
'FN' => 'Empty Contact',
'VERSION' => '4.0',
'X-SOCIALPROFILE' => [],
];
$invalidContact = [
'UID' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'FN' => 'Invalid Contact',
'VERSION' => '4.0',
'X-SOCIALPROFILE' => [['type' => 'someNetwork', 'value' => 'invalidId']],
];

$addressbook1 = $this->createMock(IAddressBook::class);
$addressbook1->method('getUri')->willReturn('contacts1');
$addressbook2 = $this->createMock(IAddressBook::class);
$addressbook2->method('getUri')->willReturn('contacts2');

$searchMap1 = [
['', ['UID'], ['types' => true], [$validContact1, $invalidContact]],
[$validContact1['UID'], ['UID'], ['types' => true], [$validContact1]],
[$invalidContact['UID'], ['UID'], ['types' => true], [$invalidContact]],
];
$searchMap2 = [
['', ['UID'], ['types' => true], [$validContact2, $emptyContact]],
[$validContact2['UID'], ['UID'], ['types' => true], [$validContact2]],
[$emptyContact['UID'], ['UID'], ['types' => true], [$emptyContact]],
];
$addressbook1
->method('search')
->will($this->returnValueMap($searchMap1));
$addressbook2
->method('search')
->will($this->returnValueMap($searchMap2));

$this->manager
->method('getUserAddressBooks')
->willReturn([$addressbook1, $addressbook2]);

$socialConnectorMap = [
[$validContact1['X-SOCIALPROFILE'], 'any', 'validConnector'],
[$validContact2['X-SOCIALPROFILE'], 'any', 'validConnector'],
[$invalidContact['X-SOCIALPROFILE'], 'any', 'invalidConnector'],
[$emptyContact['X-SOCIALPROFILE'], 'any', 'emptyConnector'],
];
$this->socialProvider
->method('getSocialConnector')
->will($this->returnValueMap($socialConnectorMap));

$validResponse = $this->createMock(IResponse::class);
$validResponse
->method('getBody')
->willReturn('someBody');
$validResponse
->method('getHeader')
->willReturn('someHeader');
$invalidResponse = $this->createMock(IResponse::class);
$invalidResponse
->method('getBody')
->willReturn('');
$invalidResponse
->method('getHeader')
->willReturn('');
$response = $this->createMock(IResponse::class);
$response
->method('getBody')
->willReturn($httpResult);
$response
->method('getHeader')
->willReturn($httpResult);

$clientResponseMap = [
['validConnector', [], $validResponse],
['invalidConnector', [], $invalidResponse],
['emptyConnector', [], $invalidResponse],
];
$client = $this->createMock(IClient::class);
$client
->method('get')
->will($this->returnValueMap($clientResponseMap));
$this->clientService
->method('NewClient')
->willReturn($client);
}


/**
* @dataProvider updateAddressbookProvider
*/
public function testUpdateAddressbooks($syncAllowedByAdmin, $bgSyncEnabledByUser, $expected) {
$this->config
->method('getAppValue')
->willReturn($syncAllowedByAdmin);
$this->config
->method('getUserValue')
->willReturn($bgSyncEnabledByUser);

$this->setupAddressbooks();

$result = $this->service->updateAddressbooks('any', 'mrstest');

$this->assertEquals($expected, $result->getStatus());

if (($syncAllowedByAdmin === 'yes') && ($bgSyncEnabledByUser === 'yes')) {
$report = $result->getData();
$this->assertArrayHasKey('0', $report);
$this->assertArrayHasKey('updated', $report[0]);
$this->assertContains('Valid Contact One', $report[0]['updated']);
$this->assertArrayHasKey('checked', $report[0]);
$this->assertContains('Valid Contact Two', $report[0]['checked']);
$this->assertArrayHasKey('failed', $report[0]);
$this->assertArrayHasKey('404', $report[0]['failed']);
$this->assertContains('Invalid Contact', $report[0]['failed']['404']);
$this->assertContains('Empty Contact', $report[0]['failed']['404']);
}
}
}

0 comments on commit fdacb31

Please sign in to comment.