From 40363ae4ab285b12d1d8655b163e99c044b232ad Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 12:56:30 +0100 Subject: [PATCH] =?UTF-8?q?[test]=20add=20test=20for=20the=20RefreshTokenG?= =?UTF-8?q?rantExtension=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RefreshTokenGrantExtensionTest.php | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/Majora/Component/OAuth/Tests/GrantType/RefreshTokenGrantExtensionTest.php diff --git a/src/Majora/Component/OAuth/Tests/GrantType/RefreshTokenGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/RefreshTokenGrantExtensionTest.php new file mode 100644 index 0000000..00630d5 --- /dev/null +++ b/src/Majora/Component/OAuth/Tests/GrantType/RefreshTokenGrantExtensionTest.php @@ -0,0 +1,158 @@ +prophesize(AccountLoaderInterface::class)->reveal(), + $this->prophesize(RefreshTokenLoaderInterface::class)->reveal() + ); + $refreshTokenGrantExtension->configureRequestParameters($optionsResolver); + + // Testing the required options + $actualRequiredOptions = $optionsResolver->getRequiredOptions(); + $expectedRequiredOptions = ['refresh_token']; + $this->assertEquals($expectedRequiredOptions, $actualRequiredOptions, '', $delta = 0.0, 10, true); // Not caring about keys + + // Testing the optional options + $actualOptionalOptions = array_diff($optionsResolver->getDefinedOptions(), $actualRequiredOptions); + $expectedOptionalOptions = []; + $this->assertCount(0, array_diff($expectedOptionalOptions, $actualOptionalOptions)); + } + + /** + * Test grant() method on success. + */ + public function testSuccessGrant() + { + // Mocking AccountInterface + $account = $this->prophesize(AccountInterface::class)->reveal(); + + // Mocking ApplicationInterface + $application = $this->prophesize(ApplicationInterface::class)->reveal(); + + // Mocking RefreshToken + $refreshToken = $this->prophesize(RefreshTokenInterface::class); + $refreshToken->getAccount() + ->willReturn($account) + ->shouldBeCalled(); + $refreshToken->getApplication() + ->willReturn($application) + ->shouldBeCalled(); + + // Mocking RefreshTokenLoader + $refreshTokenLoader = $this->prophesize(RefreshTokenLoaderInterface::class); + $refreshTokenLoader->retrieveByHash('hash_test') + ->willReturn($refreshToken->reveal()) + ->shouldBeCalled(); + + $refreshTokenGrantExtension = new RefreshTokenGrantExtension( + $this->prophesize(AccountLoaderInterface::class)->reveal(), + $refreshTokenLoader->reveal() + ); + + // Mocking LoginAttempt + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('refresh_token') + ->willReturn('hash_test') + ->shouldBeCalled(); + + $actualAccount = $refreshTokenGrantExtension->grant( + $application, + $loginAttempt->reveal() + ); + + $this->assertSame($account, $actualAccount); + } + + /** + * Test grant() when it fails loading a refresh token. + */ + public function testGrantFailingTokenLoading() + { + // Mocking ApplicationInterface + $application = $this->prophesize(ApplicationInterface::class)->reveal(); + + // Mocking RefreshTokenLoader + $refreshTokenLoader = $this->prophesize(RefreshTokenLoaderInterface::class); + $refreshTokenLoader->retrieveByHash('hash_test') + ->willReturn(null) + ->shouldBeCalled(); + + $refreshTokenGrantExtension = new RefreshTokenGrantExtension( + $this->prophesize(AccountLoaderInterface::class)->reveal(), + $refreshTokenLoader->reveal() + ); + + // Mocking LoginAttempt + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('refresh_token') + ->willReturn('hash_test') + ->shouldBeCalled(); + + $this->expectException(InvalidGrantException::class); + $this->expectExceptionMessage('RefreshToken not found.'); + + $refreshTokenGrantExtension->grant( + $application, + $loginAttempt->reveal() + ); + } + + /** + * Test grant() when it fails the application comparison. + */ + public function testGrantFailingApplicationValidation() + { + // Mocking RefreshToken + $refreshToken = $this->prophesize(RefreshTokenInterface::class); + $refreshToken->getApplication() + ->willReturn(null) + ->shouldBeCalled(); + + // Mocking RefreshTokenLoader + $refreshTokenLoader = $this->prophesize(RefreshTokenLoaderInterface::class); + $refreshTokenLoader->retrieveByHash('hash_test') + ->willReturn($refreshToken->reveal()) + ->shouldBeCalled(); + + $refreshTokenGrantExtension = new RefreshTokenGrantExtension( + $this->prophesize(AccountLoaderInterface::class)->reveal(), + $refreshTokenLoader->reveal() + ); + + // Mocking LoginAttempt + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('refresh_token') + ->willReturn('hash_test') + ->shouldBeCalled(); + + $this->expectException(InvalidGrantException::class); + $this->expectExceptionMessage('Invalid RefreshToken for loaded account.'); + + $refreshTokenGrantExtension->grant( + $this->prophesize(ApplicationInterface::class)->reveal(), + $loginAttempt->reveal() + ); + } +}