Skip to content

Commit

Permalink
[Security] added unit tests for the Authentication sub-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 31, 2010
1 parent eb4d51f commit 3d5054f
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 10 deletions.
Expand Up @@ -37,7 +37,7 @@ class AuthenticationProviderManager implements AuthenticationManagerInterface
public function __construct(array $providers = array(), $eraseCredentials = true)
{
$this->setProviders($providers);
$this->eraseCredentials = $eraseCredentials;
$this->eraseCredentials = (Boolean) $eraseCredentials;
}

/**
Expand All @@ -60,7 +60,7 @@ public function authenticate(TokenInterface $token)
try {
$result = $provider->authenticate($token);
} catch (AccountStatusException $e) {
$e->setToken($token);
$e->setExtraInformation($token);

throw $e;
} catch (AuthenticationException $e) {
Expand All @@ -69,7 +69,7 @@ public function authenticate(TokenInterface $token)
}

if (null !== $result) {
if ($this->eraseCredentials) {
if (true === $this->eraseCredentials) {
$result->eraseCredentials();
}

Expand All @@ -80,7 +80,7 @@ public function authenticate(TokenInterface $token)
$lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', get_class($token)));
}

$lastException->setToken($token);
$lastException->setExtraInformation($token);

throw $lastException;
}
Expand Down
Expand Up @@ -39,9 +39,9 @@ class DaoAuthenticationProvider extends UserAuthenticationProvider
* @param AccountCheckerInterface $accountChecker An AccountCheckerInterface instance
* @param PasswordEncoderInterface $passwordEncoder A PasswordEncoderInterface instance
*/
public function __construct(UserProviderInterface $userProvider, AccountCheckerInterface $accountChecker, PasswordEncoderInterface $passwordEncoder = null)
public function __construct(UserProviderInterface $userProvider, AccountCheckerInterface $accountChecker, PasswordEncoderInterface $passwordEncoder = null, $hideUserNotFoundExceptions = true)
{
parent::__construct($accountChecker);
parent::__construct($accountChecker, $hideUserNotFoundExceptions);

if (null === $passwordEncoder) {
$passwordEncoder = new PlaintextPasswordEncoder();
Expand Down
Expand Up @@ -53,15 +53,15 @@ public function authenticate(TokenInterface $token)
return null;
}

if (null === $token->getUser()) {
if (!$user = $token->getUser()) {
throw new BadCredentialsException('No pre-authenticated principal found in request.');
}
/*
if (null === $token->getCredentials()) {
throw new BadCredentialsException('No pre-authenticated credentials found in request.');
}
*/
$user = $this->userProvider->loadUserByUsername($token->getUser());
$user = $this->userProvider->loadUserByUsername($user);

$this->accountChecker->checkPostAuth($user);

Expand Down
Expand Up @@ -23,8 +23,8 @@ class PreAuthenticatedToken extends Token
*/
public function __construct($user, $credentials, array $roles = null)
{
parent::__construct(null === $roles ? array() : $roles);
if (null !== $roles) {
parent::__construct($roles);
$this->setAuthenticated(true);
}

Expand Down
Expand Up @@ -42,6 +42,8 @@ public function __construct(array $roles = array())
}
$this->addRole($role);
}
$this->authenticated = false;
$this->immutable = false;
}

/**
Expand Down Expand Up @@ -107,7 +109,7 @@ public function getUser()
}

/**
* Removes sensitive information from the token.
* {@inheritdoc}
*/
public function eraseCredentials()
{
Expand Down
Expand Up @@ -66,4 +66,9 @@ function isAuthenticated();
* @param Boolean $isAuthenticated The authenticated flag
*/
function setAuthenticated($isAuthenticated);

/**
* Removes sensitive information from the token.
*/
function eraseCredentials();
}
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Security\Authentication;

use Symfony\Component\Security\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Exception\ProviderNotFoundException;
use Symfony\Component\Security\Exception\AuthenticationException;
use Symfony\Component\Security\Exception\AccountStatusException;
use Symfony\Component\Security\Authentication\Token\UsernamePasswordToken;

class AuthenticationProviderManagerTest extends \PHPUnit_Framework_TestCase
{
public function testProviderAccessors()
{
$manager = new AuthenticationProviderManager();
$manager->addProvider($provider = $this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface'));
$this->assertSame(array($provider), $manager->getProviders());

$manager->setProviders($providers = array($this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface')));
$this->assertSame($providers, $manager->getProviders());
}

/**
* @expectedException LogicException
*/
public function testAuthenticateWithoutProviders()
{
$manager = new AuthenticationProviderManager();
$manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
}

public function testAuthenticateWhenNoProviderSupportsToken()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(false),
));

try {
$manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->fail();
} catch (ProviderNotFoundException $e) {
$this->assertSame($token, $e->getExtraInformation());
}
}

public function testAuthenticateWhenProviderReturnsAccountStatusException()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AccountStatusException'),
));

try {
$manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->fail();
} catch (AccountStatusException $e) {
$this->assertSame($token, $e->getExtraInformation());
}
}

public function testAuthenticateWhenProviderReturnsAuthenticationException()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AuthenticationException'),
));

try {
$manager->authenticate($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->fail();
} catch (AuthenticationException $e) {
$this->assertSame($token, $e->getExtraInformation());
}
}

public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Exception\AuthenticationException'),
$this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
));

$token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertSame($expected, $token);
}

public function testAuthenticateReturnsTokenForTheLastMatch()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
$this->getAuthenticationProvider(true, $expected = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')),
));

$token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertSame($expected, $token);
}

public function testEraseCredentialFlag()
{
$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar')),
));

$token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertEquals('', $token->getCredentials());

$manager = new AuthenticationProviderManager(array(
$this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar')),
), false);

$token = $manager->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertEquals('bar', $token->getCredentials());
}

protected function getAuthenticationProvider($supports, $token = null, $exception = null)
{
$provider = $this->getMock('Symfony\Component\Security\Authentication\Provider\AuthenticationProviderInterface');
$provider->expects($this->once())
->method('supports')
->will($this->returnValue($supports))
;

if (null !== $token) {
$provider->expects($this->once())
->method('authenticate')
->will($this->returnValue($token))
;
} elseif (null !== $exception) {
$provider->expects($this->once())
->method('authenticate')
->will($this->throwException($this->getMock($exception, null, array(), '', false)))
;
}

return $provider;
}
}
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Security\Authentication\Provider;

use Symfony\Component\Security\Authentication\Provider\AnonymousAuthenticationProvider;

class AnonymousAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
{
public function testSupports()
{
$provider = $this->getProvider('foo');

$this->assertTrue($provider->supports($this->getSupportedToken('foo')));
$this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
}

public function testAuthenticateWhenTokenIsNotSupported()
{
$provider = $this->getProvider('foo');

$this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
}

/**
* @expectedException Symfony\Component\Security\Exception\BadCredentialsException
*/
public function testAuthenticateWhenKeyIsNotValid()
{
$provider = $this->getProvider('foo');

$this->assertNull($provider->authenticate($this->getSupportedToken('bar')));
}

public function testAuthenticate()
{
$provider = $this->getProvider('foo');
$token = $this->getSupportedToken('foo');

$this->assertSame($token, $provider->authenticate($token));
}

protected function getSupportedToken($key)
{
$token = $this->getMock('Symfony\Component\Security\Authentication\Token\AnonymousToken', array('getKey'), array(), '', false);
$token->expects($this->any())
->method('getKey')
->will($this->returnValue($key))
;

return $token;
}

protected function getProvider($key)
{
return new AnonymousAuthenticationProvider($key);
}
}

0 comments on commit 3d5054f

Please sign in to comment.