From a687a657e82a3a9e4a0a14357f2d07a9669bd085 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Tue, 22 Jan 2019 18:12:03 +0100 Subject: [PATCH] Add test for destroy with invalid user Some code cleanup - willReturn instead of will(returnValue) - Annotation for mocked objects Signed-off-by: Daniel Kesselberg --- .../Controller/AuthSettingsControllerTest.php | 123 +++++++++++++----- 1 file changed, 89 insertions(+), 34 deletions(-) diff --git a/tests/Settings/Controller/AuthSettingsControllerTest.php b/tests/Settings/Controller/AuthSettingsControllerTest.php index 19344a2305..73741bea57 100644 --- a/tests/Settings/Controller/AuthSettingsControllerTest.php +++ b/tests/Settings/Controller/AuthSettingsControllerTest.php @@ -34,7 +34,6 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\ILogger; use OCP\IRequest; use OCP\ISession; -use OCP\IUser; use OCP\Security\ISecureRandom; use OCP\Session\Exceptions\SessionNotAvailableException; use Test\TestCase; @@ -43,12 +42,17 @@ class AuthSettingsControllerTest extends TestCase { /** @var AuthSettingsController */ private $controller; + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ private $request; /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */ private $tokenProvider; + /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */ private $session; + /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ private $secureRandom; - private $uid; + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + private $activityManager; + private $uid = 'jane'; protected function setUp() { parent::setUp(); @@ -57,14 +61,20 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider = $this->createMock(IProvider::class); $this->session = $this->createMock(ISession::class); $this->secureRandom = $this->createMock(ISecureRandom::class); - $this->uid = 'jane'; - - $activityManager = $this->createMock(IManager::class); - $activityManager->method('generateEvent') - ->willReturn($this->createMock(IEvent::class)); + $this->activityManager = $this->createMock(IManager::class); + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $logger */ $logger = $this->createMock(ILogger::class); - $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->session, $this->secureRandom, $this->uid, $activityManager, $logger); + $this->controller = new AuthSettingsController( + 'core', + $this->request, + $this->tokenProvider, + $this->session, + $this->secureRandom, + $this->uid, + $this->activityManager, + $logger + ); } public function testIndex() { @@ -82,14 +92,14 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('getTokenByUser') ->with($this->uid) - ->will($this->returnValue($tokens)); + ->willReturn($tokens); $this->session->expects($this->once()) ->method('getId') - ->will($this->returnValue('session123')); + ->willReturn('session123'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('session123') - ->will($this->returnValue($sessionToken)); + ->willReturn($sessionToken); $this->assertEquals([ [ @@ -120,39 +130,42 @@ class AuthSettingsControllerTest extends TestCase { $this->session->expects($this->once()) ->method('getId') - ->will($this->returnValue('sessionid')); + ->willReturn('sessionid'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sessionid') - ->will($this->returnValue($sessionToken)); + ->willReturn($sessionToken); $this->tokenProvider->expects($this->once()) ->method('getPassword') ->with($sessionToken, 'sessionid') - ->will($this->returnValue($password)); + ->willReturn($password); $sessionToken->expects($this->once()) ->method('getLoginName') - ->will($this->returnValue('User13')); + ->willReturn('User13'); $this->secureRandom->expects($this->exactly(5)) ->method('generate') ->with(5, ISecureRandom::CHAR_HUMAN_READABLE) - ->will($this->returnValue('XXXXX')); + ->willReturn('XXXXX'); $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX'; $this->tokenProvider->expects($this->once()) ->method('generateToken') ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN) - ->will($this->returnValue($deviceToken)); + ->willReturn($deviceToken); $deviceToken->expects($this->once()) ->method('jsonSerialize') - ->will($this->returnValue(['dummy' => 'dummy', 'canDelete' => true])); + ->willReturn(['dummy' => 'dummy', 'canDelete' => true]); + + $this->mockActivityManager(); $expected = [ 'token' => $newToken, 'deviceToken' => ['dummy' => 'dummy', 'canDelete' => true], 'loginName' => 'User13', ]; + $response = $this->controller->create($name); $this->assertInstanceOf(JSONResponse::class, $response); $this->assertEquals($expected, $response->getData()); @@ -176,7 +189,7 @@ class AuthSettingsControllerTest extends TestCase { $this->session->expects($this->once()) ->method('getId') - ->will($this->returnValue('sessionid')); + ->willReturn('sessionid'); $this->tokenProvider->expects($this->once()) ->method('getToken') ->with('sessionid') @@ -189,23 +202,49 @@ class AuthSettingsControllerTest extends TestCase { } public function testDestroy() { - $id = 123; - $user = $this->createMock(IUser::class); + $tokenId = 124; + $token = $this->createMock(DefaultToken::class); + + $this->mockGetTokenById($tokenId, $token); + $this->mockActivityManager(); + + $token->expects($this->exactly(2)) + ->method('getId') + ->willReturn($tokenId); + + $token->expects($this->once()) + ->method('getUID') + ->willReturn('jane'); $this->tokenProvider->expects($this->once()) ->method('invalidateTokenById') - ->with($this->uid, $id); + ->with($this->uid, $tokenId); - $this->assertEquals([], $this->controller->destroy($id)); + $this->assertEquals([], $this->controller->destroy($tokenId)); } - public function testUpdateToken() { + public function testDestroyWrongUser() { + $tokenId = 124; $token = $this->createMock(DefaultToken::class); - $this->tokenProvider->expects($this->once()) - ->method('getTokenById') - ->with($this->equalTo(42)) - ->willReturn($token); + $this->mockGetTokenById($tokenId, $token); + + $token->expects($this->once()) + ->method('getUID') + ->willReturn('foobar'); + + $response = $this->controller->destroy($tokenId); + $this->assertSame([], $response->getData()); + $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testUpdateToken() { + $tokenId = 42; + $token = $this->createMock(DefaultToken::class); + + $this->mockGetTokenById($tokenId, $token); + $this->mockActivityManager(); $token->expects($this->once()) ->method('getUID') @@ -221,16 +260,14 @@ class AuthSettingsControllerTest extends TestCase { ->method('updateToken') ->with($this->equalTo($token)); - $this->assertSame([], $this->controller->update(42, ['filesystem' => true])); + $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true])); } public function testUpdateTokenWrongUser() { + $tokenId = 42; $token = $this->createMock(DefaultToken::class); - $this->tokenProvider->expects($this->once()) - ->method('getTokenById') - ->with($this->equalTo(42)) - ->willReturn($token); + $this->mockGetTokenById($tokenId, $token); $token->expects($this->once()) ->method('getUID') @@ -241,7 +278,7 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->never()) ->method('updateToken'); - $response = $this->controller->update(42, ['filesystem' => true]); + $response = $this->controller->update($tokenId, ['filesystem' => true]); $this->assertSame([], $response->getData()); $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -260,4 +297,22 @@ class AuthSettingsControllerTest extends TestCase { $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus()); } + private function mockActivityManager(): void { + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($this->createMock(IEvent::class)); + $this->activityManager->expects($this->once()) + ->method('publish'); + } + + /** + * @param int $tokenId + * @param $token + */ + private function mockGetTokenById(int $tokenId, $token): void { + $this->tokenProvider->expects($this->once()) + ->method('getTokenById') + ->with($this->equalTo($tokenId)) + ->willReturn($token); + } }