-
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 6 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,177 @@ | ||
| <?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); | ||
|
|
||
| $actualRequiredOptions = $optionsResolver->getRequiredOptions(); | ||
| $expectedRequiredOptions = ['password', 'username']; | ||
| $this->assertEquals($expectedRequiredOptions, $actualRequiredOptions, '', $delta = 0.0, 10, true); // Not caring about keys | ||
| } | ||
|
|
||
| /** | ||
| * Test grant() method on success | ||
| */ | ||
| 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(); | ||
| /** @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. |
||
| } | ||
|
|
||
| /** | ||
| * Test grant() when it fails loading an account. | ||
| */ | ||
| public function testGrantFailingAccountLoading() | ||
| { | ||
| // Mocking AccountLoaderInterface | ||
| $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); | ||
| $accountLoaderMock | ||
| ->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(); | ||
| /** @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); | ||
| } | ||
|
|
||
| /** | ||
| * Test grant() when it fails to validate the password. | ||
| */ | ||
| 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