Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security] added unit tests for the Authentication sub-namespace
- Loading branch information
Showing
13 changed files
with
553 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/Symfony/Tests/Component/Security/Authentication/AuthenticationProviderManagerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../Tests/Component/Security/Authentication/Provider/AnonymousAuthenticationProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.