-
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 1 commit
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,139 @@ | ||
| <?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 | ||
| { | ||
| public function testConfigureRequestParameters() | ||
| { | ||
| $optionsResolver = new OptionsResolver(); | ||
| $passwordGrantExtension = new PasswordGrantExtension( | ||
| $this->prophesize(AccountLoaderInterface::class)->reveal(), | ||
| $this->prophesize(UserPasswordEncoderInterface::class)->reveal() | ||
| ); | ||
| $passwordGrantExtension->configureRequestParameters($optionsResolver); | ||
|
|
||
| $actualDefinedOptions = $optionsResolver->getDefinedOptions(); | ||
| $expectedDefinedOptions = ['password', 'username']; | ||
| $this->assertEquals($expectedDefinedOptions, $actualDefinedOptions, '', $delta = 0.0, 10, true); // Not caring about keys | ||
|
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. Là un assert equals va planter tes tests si tu rajoutes une option, or une option supplémentaire ne casse pas la compatibilité.
Author
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 faux, j'avais pas pensé à ça
Author
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. Fixé
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. Mais il faut aussi tester que les options optionnelles existent toujours. |
||
| } | ||
|
|
||
| public function testSuccessGrant() | ||
| { | ||
| // Mocking AccountInterface | ||
| /** @var AccountInterface $account */ | ||
| $account = $this->prophesize(AccountInterface::class)->reveal(); | ||
|
|
||
| // Mocking AccountLoaderInterface | ||
| $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); | ||
|
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. indent plz ✨ |
||
| /** @var AccountLoaderInterface $accountLoader */ | ||
| $accountLoader = $accountLoaderMock->reveal(); | ||
|
|
||
| // Mocking UserPasswordEncoderInterface | ||
| $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); | ||
| $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(true)->shouldBeCalled(); | ||
| /** @var UserPasswordEncoderInterface $userPasswordEncoder */ | ||
| $userPasswordEncoder = $userPasswordEncoderMock->reveal(); | ||
|
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. fait le dans le constructeur |
||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); | ||
|
|
||
| // Mocking ApplicationInterface | ||
| $applicationMock = $this->prophesize(ApplicationInterface::class); | ||
| /** @var ApplicationInterface $application */ | ||
| $application = $applicationMock->reveal(); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttemptMock = $this->prophesize(LoginAttempt::class); | ||
| $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); | ||
| $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); | ||
| /** @var LoginAttempt $loginAttempt */ | ||
| $loginAttempt = $loginAttemptMock->reveal(); | ||
|
|
||
| $actualAccount = $passwordGrantExtension->grant($application, $loginAttempt); | ||
|
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. $actualAccount = $passwordGrantExtension->grant(
$application->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. |
||
| } | ||
|
|
||
| public function testGrantFailingAccountLoading() | ||
| { | ||
| // Mocking AccountInterface | ||
| /** @var AccountInterface $account */ | ||
| $account = $this->prophesize(AccountInterface::class)->reveal(); | ||
|
|
||
| // Mocking AccountLoaderInterface | ||
| $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn(null)->shouldBeCalled(); | ||
| /** @var AccountLoaderInterface $accountLoader */ | ||
| $accountLoader = $accountLoaderMock->reveal(); | ||
|
|
||
| // Mocking UserPasswordEncoderInterface | ||
| $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); | ||
| /** @var UserPasswordEncoderInterface $userPasswordEncoder */ | ||
| $userPasswordEncoder = $userPasswordEncoderMock->reveal(); | ||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); | ||
|
|
||
| // Mocking ApplicationInterface | ||
| $applicationMock = $this->prophesize(ApplicationInterface::class); | ||
| /** @var ApplicationInterface $application */ | ||
| $application = $applicationMock->reveal(); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttemptMock = $this->prophesize(LoginAttempt::class); | ||
| $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); | ||
| /** @var LoginAttempt $loginAttempt */ | ||
| $loginAttempt = $loginAttemptMock->reveal(); | ||
|
|
||
| $this->expectException(InvalidGrantException::class); | ||
| $passwordGrantExtension->grant($application, $loginAttempt); | ||
| } | ||
|
|
||
| public function testGrantFailingPasswordValidation() | ||
| { | ||
| // Mocking AccountInterface | ||
| /** @var AccountInterface $account */ | ||
| $account = $this->prophesize(AccountInterface::class)->reveal(); | ||
|
|
||
| // Mocking AccountLoaderInterface | ||
| $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); | ||
| /** @var AccountLoaderInterface $accountLoader */ | ||
| $accountLoader = $accountLoaderMock->reveal(); | ||
|
|
||
| // Mocking UserPasswordEncoderInterface | ||
| $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); | ||
| $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(false)->shouldBeCalled(); | ||
| /** @var UserPasswordEncoderInterface $userPasswordEncoder */ | ||
| $userPasswordEncoder = $userPasswordEncoderMock->reveal(); | ||
|
|
||
| $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); | ||
|
|
||
| // Mocking ApplicationInterface | ||
| $applicationMock = $this->prophesize(ApplicationInterface::class); | ||
| /** @var ApplicationInterface $application */ | ||
| $application = $applicationMock->reveal(); | ||
|
|
||
| // Mocking LoginAttempt | ||
| $loginAttemptMock = $this->prophesize(LoginAttempt::class); | ||
| $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); | ||
| $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); | ||
| /** @var LoginAttempt $loginAttempt */ | ||
| $loginAttempt = $loginAttemptMock->reveal(); | ||
| $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. |
||
|
|
||
| $passwordGrantExtension->grant($application, $loginAttempt); | ||
| } | ||
| } | ||
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