From b6182489be8cc0820b3138e25afda912b9a98d9f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 13 Jun 2018 21:24:06 +0200 Subject: Fix tests Signed-off-by: Roeland Jago Douma --- .../tests/Controller/SettingsControllerTest.php | 82 +++++++++++++++++----- 1 file changed, 65 insertions(+), 17 deletions(-) (limited to 'apps/oauth2/tests') diff --git a/apps/oauth2/tests/Controller/SettingsControllerTest.php b/apps/oauth2/tests/Controller/SettingsControllerTest.php index a6c036949ed..b0c6035cddb 100644 --- a/apps/oauth2/tests/Controller/SettingsControllerTest.php +++ b/apps/oauth2/tests/Controller/SettingsControllerTest.php @@ -26,6 +26,7 @@ use OCA\OAuth2\Controller\SettingsController; use OCA\OAuth2\Db\AccessTokenMapper; use OCA\OAuth2\Db\Client; use OCA\OAuth2\Db\ClientMapper; +use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\RedirectResponse; use OCP\IRequest; use OCP\IURLGenerator; @@ -90,27 +91,39 @@ class SettingsControllerTest extends TestCase { $this->clientMapper ->expects($this->once()) ->method('insert') - ->with($client); + ->with($this->callback(function (Client $c) { + return $c->getName() === 'My Client Name' && + $c->getRedirectUri() === 'https://example.com/' && + $c->getSecret() === 'MySecret' && + $c->getClientIdentifier() === 'MyClientIdentifier'; + }))->will($this->returnCallback(function (Client $c) { + $c->setId(42); + return $c; + })); - $this->urlGenerator - ->expects($this->once()) - ->method('getAbsoluteURL') - ->with('/index.php/settings/admin/security') - ->willReturn('https://example.com/index.php/settings/admin/security'); + $result = $this->settingsController->addClient('My Client Name', 'https://example.com/'); + $this->assertInstanceOf(JSONResponse::class, $result); + + $data = $result->getData(); - $expected = new RedirectResponse('https://example.com/index.php/settings/admin/security'); - $this->assertEquals($expected, $this->settingsController->addClient('My Client Name', 'https://example.com/')); + $this->assertEquals([ + 'id' => 42, + 'name' => 'My Client Name', + 'redirectUri' => 'https://example.com/', + 'clientId' => 'MyClientIdentifier', + 'clientSecret' => 'MySecret', + ], $data); } public function testDeleteClient() { $client = new Client(); + $client->setId(123); $client->setName('My Client Name'); $client->setRedirectUri('https://example.com/'); $client->setSecret('MySecret'); $client->setClientIdentifier('MyClientIdentifier'); $this->clientMapper - ->expects($this->at(0)) ->method('getByUid') ->with(123) ->willReturn($client); @@ -123,17 +136,52 @@ class SettingsControllerTest extends TestCase { ->method('deleteByName') ->with('My Client Name'); $this->clientMapper - ->expects($this->at(1)) ->method('delete') ->with($client); - $this->urlGenerator - ->expects($this->once()) - ->method('getAbsoluteURL') - ->with('/index.php/settings/admin/security') - ->willReturn('https://example.com/index.php/settings/admin/security'); + $result = $this->settingsController->deleteClient(123); + $this->assertInstanceOf(JSONResponse::class, $result); + $this->assertEquals([], $result->getData()); + } + + public function testGetClients() { + $client1 = new Client(); + $client1->setId(123); + $client1->setName('My Client Name'); + $client1->setRedirectUri('https://example.com/'); + $client1->setSecret('MySecret'); + $client1->setClientIdentifier('MyClientIdentifier'); + + $client2 = new Client(); + $client2->setId(42); + $client2->setName('My Client Name2'); + $client2->setRedirectUri('https://example.com/2'); + $client2->setSecret('MySecret2'); + $client2->setClientIdentifier('MyClientIdentifier2'); + + $this->clientMapper->method('getClients') + ->willReturn([$client1, $client2]); + + $result = $this->settingsController->getClients(); + $this->assertInstanceOf(JSONResponse::class, $result); + + $data = $result->getData(); - $expected = new RedirectResponse('https://example.com/index.php/settings/admin/security'); - $this->assertEquals($expected, $this->settingsController->deleteClient(123)); + $this->assertSame([ + [ + 'id' => 123, + 'name' => 'My Client Name', + 'redirectUri' => 'https://example.com/', + 'clientId' => 'MyClientIdentifier', + 'clientSecret' => 'MySecret', + ], + [ + 'id' => 42, + 'name' => 'My Client Name2', + 'redirectUri' => 'https://example.com/2', + 'clientId' => 'MyClientIdentifier2', + 'clientSecret' => 'MySecret2', + ], + ], $data); } } -- cgit v1.2.3