Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc

{
$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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas besoin, par défaut les méthodes mockées renvoient null.

->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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
);
}
}