-
Notifications
You must be signed in to change notification settings - Fork 1
Unit test for the PasswordGrantExtension #28
base: v2.2
Are you sure you want to change the base?
Changes from all commits
79c6d6b
fbd2dd5
cc0d41e
d7cd253
bf13f03
17d73aa
55a0842
d23c851
534700b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
| namespace Majora\Component\OAuth\Tests\GrantType; | ||
|
|
||
| use Majora\Component\OAuth\Entity\LoginAttempt; | ||
| use Majora\Component\OAuth\Exception\InvalidGrantException; | ||
| use Majora\Component\OAuth\GrantType\PasswordGrantExtension; | ||
| use Majora\Component\OAuth\Loader\AccountLoaderInterface; | ||
| use Majora\Component\OAuth\Model\AccountInterface; | ||
| use Majora\Component\OAuth\Model\ApplicationInterface; | ||
| use Prophecy\Argument; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | ||
| use Symfony\Component\Security\Core\User\UserInterface; | ||
|
|
||
| /** | ||
| * Unit test class for Majora\Component\OAuth\GrantType\PasswordGrantTypeExtension. | ||
| */ | ||
| class PasswordGrantExtensionTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| /** | ||
| * Test configureRequestParameters() method. | ||
| */ | ||
| public function testConfigureRequestParameters() | ||
| { | ||
| $optionsResolver = new OptionsResolver(); | ||
| $passwordGrantExtension = new PasswordGrantExtension( | ||
| $this->prophesize(AccountLoaderInterface::class)->reveal(), | ||
| $this->prophesize(UserPasswordEncoderInterface::class)->reveal() | ||
| ); | ||
| $passwordGrantExtension->configureRequestParameters($optionsResolver); | ||
|
|
||
| // Testing the required options | ||
| $actualRequiredOptions = $optionsResolver->getRequiredOptions(); | ||
| $expectedRequiredOptions = ['password', 'username']; | ||
| $this->assertEquals($expectedRequiredOptions, $actualRequiredOptions, '', $delta = 0.0, 10, true); // Not caring about keys | ||
|
|
||
| // Testing the optional options | ||
| $actualOptionalOptions = array_diff($optionsResolver->getDefinedOptions(), $actualRequiredOptions); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi faire la distinction ? Ce que tu veux tester ici, c'est que les options définies le sont toujours, une option obligatoire est requise donc tu peux la tester. |
||
| $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 AccountLoader | ||
| $accountLoader = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') | ||
| ->willReturn($account) | ||
| ->shouldBeCalled(); | ||
|
|
||
| // Mocking UserPasswordEncoderInterface | ||
| $userPasswordEncoder = $this->prophesize(UserPasswordEncoderInterface::class); | ||
| $userPasswordEncoder->isPasswordValid(Argument::type(UserInterface::class), 'password_test') | ||
| ->willReturn(true) | ||
| ->shouldBeCalled(); | ||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension( | ||
| $accountLoader->reveal(), | ||
| $userPasswordEncoder->reveal() | ||
| ); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttempt = $this->prophesize(LoginAttempt::class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tu n'es pas obligé de mock un value object. |
||
| $loginAttempt->getData('username') | ||
| ->willReturn('username_test') | ||
| ->shouldBeCalled(); | ||
| $loginAttempt->getData('password') | ||
| ->willReturn('password_test') | ||
| ->shouldBeCalled(); | ||
|
|
||
| $actualAccount = $passwordGrantExtension->grant( | ||
| $this->prophesize(ApplicationInterface::class)->reveal(), | ||
| $loginAttempt->reveal() | ||
| ); | ||
|
|
||
| $this->assertSame($account, $actualAccount); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Same" est overkill, envoyer une instance différente et néanmoins égale ne casse pas l'interface applicative de ta classe. |
||
| } | ||
|
|
||
| /** | ||
| * Test grant() when it fails loading an account. | ||
| */ | ||
| public function testGrantFailingAccountLoading() | ||
| { | ||
| // Mocking AccountLoader | ||
| $accountLoader = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') | ||
| ->willReturn(null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pas besoin, par défaut les méthodes mockées renvoient |
||
| ->shouldBeCalled(); | ||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension( | ||
| $accountLoader->reveal(), | ||
| $this->prophesize(UserPasswordEncoderInterface::class)->reveal() | ||
| ); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttempt = $this->prophesize(LoginAttempt::class); | ||
| $loginAttempt->getData('username') | ||
| ->willReturn('username_test') | ||
| ->shouldBeCalled(); | ||
|
|
||
| $this->expectException(InvalidGrantException::class); | ||
| $this->expectExceptionMessage('Username not found on loaded application.'); | ||
|
|
||
| $passwordGrantExtension->grant( | ||
| $this->prophesize(ApplicationInterface::class)->reveal(), | ||
| $loginAttempt->reveal() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test grant() when it fails to validate the password. | ||
| */ | ||
| public function testGrantFailingPasswordValidation() | ||
| { | ||
| // Mocking AccountLoader | ||
| $accountLoader = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') | ||
| ->willReturn($this->prophesize(AccountInterface::class)->reveal()) | ||
| ->shouldBeCalled(); | ||
|
|
||
| // Mocking UserPasswordEncoderInterface | ||
| $userPasswordEncoder = $this->prophesize(UserPasswordEncoderInterface::class); | ||
| $userPasswordEncoder->isPasswordValid(Argument::type(UserInterface::class), 'password_test') | ||
| ->willReturn(false) | ||
| ->shouldBeCalled(); | ||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension( | ||
| $accountLoader->reveal(), | ||
| $userPasswordEncoder->reveal() | ||
| ); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttempt = $this->prophesize(LoginAttempt::class); | ||
| $loginAttempt->getData('username') | ||
| ->willReturn('username_test') | ||
| ->shouldBeCalled(); | ||
| $loginAttempt->getData('password') | ||
| ->willReturn('password_test') | ||
| ->shouldBeCalled(); | ||
|
|
||
|
|
||
| $this->expectException(InvalidGrantException::class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Teste aussi le message de l'exception vu que la classe peut servir dans plusieurs cas. |
||
| $this->expectExceptionMessage('Invalid password for loaded account.'); | ||
|
|
||
| $passwordGrantExtension->grant( | ||
| $this->prophesize(ApplicationInterface::class)->reveal(), | ||
| $loginAttempt->reveal() | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc