Skip to content

Commit

Permalink
linting, unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: leith abdulla <online-nextcloud@eleith.com>
  • Loading branch information
eleith committed Nov 2, 2020
1 parent 296ebf7 commit c452f09
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/Service/SocialApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function registerAddressbooks($userId, IManager $manager) {
*
* @returns {JSONResponse} an empty JSONResponse with respective http status code
*/
public function updateContact(string $addressbookId, string $contactId, string $network) : JSONResponse {
public function updateContact(string $addressbookId, string $contactId, ?string $network) : JSONResponse {
$socialdata = null;
$imageType = null;
$urls = array();
Expand Down
186 changes: 142 additions & 44 deletions tests/unit/Service/SocialApiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCA\Contacts\Service;

use OCA\Contacts\Service\Social\CompositeSocialProvider;
use OCA\Contacts\Service\Social\ISocialProvider;

use OCP\AppFramework\Http;
use OCP\Http\Client\IClient;
Expand All @@ -45,31 +46,65 @@
class SocialApiServiceTest extends TestCase {
private $service;

/** @var CompositeSocialProvider */
/** @var CompositeSocialProvider|MockObject */
private $socialProvider;
/** @var IManager|MockObject */
private $manager;
/** @var IConfig|MockObject */
private $config;
/** @var IClientService|MockObject */
private $clientService;
/** @var IL10N|MockObject */
/** @var IL10N|MockObject */
private $l10n;
/** @var IURLGenerator|MockObject */
/** @var IURLGenerator|MockObject */
private $urlGen;
/** @var CardDavBackend|MockObject */
private $davBackend;
/** @var ITimeFactory|MockObject */
private $timeFactory;

public function socialProfileProvider() {
return [
'no social profiles set' => [null, 'someConnector', 'someResult', Http::STATUS_PRECONDITION_FAILED],
'valid social profile' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', 'someResult', Http::STATUS_OK],
'bad formatted profile id' => [[['type' => 'someNetwork', 'value' => 'someId']], null, 'someResult', Http::STATUS_BAD_REQUEST],
'not existing profile id' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', '', Http::STATUS_NOT_FOUND],
'unchanged data' => [[['type' => 'someNetwork', 'value' => 'someId']], 'someConnector', 'thePhoto', Http::STATUS_NOT_MODIFIED],
public function allSocialProfileProviders() {
$body = "the body";
$imageType = "jpg";
$contact = [
'URI' => '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0',
'VERSION' => '4.0'
];
$connector = $this->createMock(ISocialProvider::class);
$connector->method('supportsContact')->willReturn(true);
$connector->method('getImageUrls')->willReturn(["url1"]);

$connectorNoSupport = $this->createMock(ISocialProvider::class);
$connectorNoSupport->method('supportsContact')->willReturn(false);

$connectorNoUrl = $this->createMock(ISocialProvider::class);
$connectorNoUrl->method('supportsContact')->willReturn(true);
$connectorNoUrl->method('getImageUrls')->willReturn([]);

$addressbookEmpty = $this->createMock(IAddressBook::class);
$addressbookEmpty
->method('getUri')
->willReturn('contacts');
$addressbookEmpty
->method('search')
->willReturn(null);

$addressbook = $this->createMock(IAddressBook::class);
$addressbook
->method('getUri')
->willReturn('contacts');
$addressbook
->method('search')
->willReturn([$contact]);

return array(
'no address book found' => [null, [], "", "", Http::STATUS_BAD_REQUEST],
'no contact found' => [[$addressbookEmpty], [], "", "", Http::STATUS_PRECONDITION_FAILED],
'no supporting contacts found' => [[$addressbook], [$connectorNoSupport], "", "", Http::STATUS_PRECONDITION_FAILED],
'no url found' => [[$addressbook], [$connectorNoUrl], "", "", Http::STATUS_BAD_REQUEST],
'no image found' => [[$addressbook], [$connector], "", "", Http::STATUS_NOT_FOUND],
'image found' => [[$addressbook], [$connector], $body, $imageType, Http::STATUS_OK]
);
}

public function updateAddressbookProvider() {
Expand Down Expand Up @@ -114,15 +149,54 @@ public function testDeactivatedSocial() {
}

/**
* @dataProvider socialProfileProvider
* @dataProvider allSocialProfileProviders
*/
public function testUpdateContact($social, $connector, $httpResult, $expected) {
public function testUpdateContactWithoutNetwork($addressbooks, $providers, $body, $imageType, $status) {
$this->manager
->method('getUserAddressBooks')
->willReturn($addressbooks);

$this->socialProvider
->method('getSocialConnectors')
->willReturn($providers);

$response = $this->createMock(IResponse::class);
$response
->method('getBody')
->willReturn($body);
$response
->method('getHeader')
->willReturn($imageType);
$client = $this->createMock(IClient::class);
$client
->method('get')
->willReturn($response);
$this->clientService
->method('NewClient')
->willReturn($client);

$result = $this->service
->updateContact(
'contacts',
'3225c0d5-1bd2-43e5-a08c-4e65eaa406b0',
null);
$this->assertEquals($status, $result->getStatus());
}

public function testUpdateContactWithNetwork() {
$network = "mastodon";
$body = "the body";
$imageType = "jpg";
$addressBookId = "contacts";
$contactId = "3225c0d5-1bd2-43e5-a08c-4e65eaa406b0";
$contact = [
'URI' => '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0',
'VERSION' => '4.0',
'PHOTO' => "data:" . $httpResult . ";base64," . base64_encode('thePhoto'),
'X-SOCIALPROFILE' => $social,
'URI' => $contactId,
'VERSION' => '4.0'
];
$provider = $this->createMock(ISocialProvider::class);
$provider->method('supportsContact')->willReturn(true);
$provider->method('getImageUrls')->willReturn(["url1"]);

$addressbook = $this->createMock(IAddressBook::class);
$addressbook
->method('getUri')
Expand All @@ -135,17 +209,21 @@ public function testUpdateContact($social, $connector, $httpResult, $expected) {
->method('getUserAddressBooks')
->willReturn([$addressbook]);

$this->socialProvider
->method('getSocialConnectors')
->willReturn([$provider]);

$this->socialProvider
->method('getSocialConnector')
->willReturn($connector);
->willReturn($provider);

$response = $this->createMock(IResponse::class);
$response
->method('getBody')
->willReturn($httpResult);
->willReturn($body);
$response
->method('getHeader')
->willReturn($httpResult);
->willReturn($imageType);
$client = $this->createMock(IClient::class);
$client
->method('get')
Expand All @@ -154,9 +232,24 @@ public function testUpdateContact($social, $connector, $httpResult, $expected) {
->method('NewClient')
->willReturn($client);

$result = $this->service->updateContact('contacts', '3225c0d5-1bd2-43e5-a08c-4e65eaa406b0', 'theSocialNetwork');
$changes = array(
'URI' => $contact['URI'],
'VERSION' => $contact['VERSION'],
'PHOTO' => "data:".$imageType.";base64," . base64_encode($body)
);

$this->socialProvider
->expects($this->once())->method("getSocialConnector")->with($network);
$provider->expects($this->once())->method("supportsContact")->with($contact);
$addressbook->expects($this->once())->method("createOrUpdate")->with($changes, $addressBookId);

$this->assertEquals($expected, $result->getStatus());
$result = $this->service
->updateContact(
$addressBookId,
$contactId,
$network);

$this->assertEquals(Http::STATUS_OK, $result->getStatus());
}

protected function setupAddressbooks() {
Expand Down Expand Up @@ -219,15 +312,33 @@ protected function setupAddressbooks() {
->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'],
$providerSupportsMap = [
[$validContact1, true],
[$emptyContact, false],
[$invalidContact, false],
[$validContact2, true]
];

$providerUrlMap = [
[$validContact1, ["url1"]],
[$emptyContact, []],
[$invalidContact, []],
[$validContact2, ["url1"]]
];

$provider = $this->createMock(ISocialProvider::class);
$provider->method('getImageUrls')
->will($this->returnValueMap($providerUrlMap));
$provider->method('supportsContact')
->will($this->returnValueMap($providerSupportsMap));

$this->socialProvider
->method('getSocialConnectors')
->willReturn([$provider]);

$this->socialProvider
->method('getSocialConnector')
->will($this->returnValueMap($socialConnectorMap));
->willReturn($provider);

$validResponse = $this->createMock(IResponse::class);
$validResponse
Expand All @@ -236,29 +347,16 @@ protected function setupAddressbooks() {
$validResponse
->method('getHeader')
->willReturn('someHeader');
$invalidResponse = $this->createMock(IResponse::class);
$invalidResponse
->method('getBody')
->willReturn('');
$invalidResponse
->method('getHeader')
->willReturn('');

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


/**
* @dataProvider updateAddressbookProvider
*/
Expand Down Expand Up @@ -287,9 +385,9 @@ public function testUpdateAddressbooks($syncAllowedByAdmin, $bgSyncEnabledByUser
$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->assertNotContains('Empty Contact', $report[0]['failed']['404']);
$this->assertArrayHasKey('412', $report[0]['failed']);
$this->assertContains('Invalid Contact', $report[0]['failed']['412']);
$this->assertContains('Empty Contact', $report[0]['failed']['412']);
}
}

Expand Down

0 comments on commit c452f09

Please sign in to comment.