[Security] some tests

This commit is contained in:
Johannes Schmitt 2011-01-29 14:47:30 +01:00 committed by Fabien Potencier
parent faf871990e
commit 36e30e21cd
5 changed files with 89 additions and 21 deletions

View File

@ -42,18 +42,15 @@ abstract class AbstractFactory implements SecurityFactoryInterface
$config = array(); $config = array();
} }
// merge set options with default options
$options = $this->getOptionsFromConfig($config);
// authentication provider // authentication provider
$authProviderId = $this->createAuthProvider($container, $id, $options, $userProviderId); $authProviderId = $this->createAuthProvider($container, $id, $config, $userProviderId);
$container $container
->getDefinition($authProviderId) ->getDefinition($authProviderId)
->addTag('security.authentication_provider') ->addTag('security.authentication_provider')
; ;
// authentication listener // authentication listener
$listenerId = $this->createListener($container, $id, $options, $userProviderId); $listenerId = $this->createListener($container, $id, $config, $userProviderId);
// add remember-me aware tag if requested // add remember-me aware tag if requested
if ($this->isRememberMeAware($config)) { if ($this->isRememberMeAware($config)) {
@ -64,7 +61,7 @@ abstract class AbstractFactory implements SecurityFactoryInterface
} }
// create entry point if applicable (optional) // create entry point if applicable (optional)
$entryPointId = $this->createEntryPoint($container, $id, $options, $defaultEntryPointId); $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPointId);
return array($authProviderId, $listenerId, $entryPointId); return array($authProviderId, $listenerId, $entryPointId);
} }
@ -85,7 +82,7 @@ abstract class AbstractFactory implements SecurityFactoryInterface
* *
* @return string never null, the id of the authentication provider * @return string never null, the id of the authentication provider
*/ */
abstract protected function createAuthProvider(ContainerBuilder $container, $id, $options, $userProviderId); abstract protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId);
/** /**
* Subclasses must return the id of the abstract listener template. * Subclasses must return the id of the abstract listener template.
@ -110,12 +107,12 @@ abstract class AbstractFactory implements SecurityFactoryInterface
* *
* @param ContainerBuilder $container * @param ContainerBuilder $container
* @param string $id * @param string $id
* @param array $options * @param array $config
* @param string $defaultEntryPointId * @param string $defaultEntryPointId
* *
* @return string the entry point id * @return string the entry point id
*/ */
protected function createEntryPoint($container, $id, $options, $defaultEntryPointId) protected function createEntryPoint($container, $id, $config, $defaultEntryPointId)
{ {
return $defaultEntryPointId; return $defaultEntryPointId;
} }
@ -133,8 +130,11 @@ abstract class AbstractFactory implements SecurityFactoryInterface
return !isset($config['remember_me']) || (Boolean) $config['remember_me']; return !isset($config['remember_me']) || (Boolean) $config['remember_me'];
} }
protected function createListener($container, $id, $options, $userProvider) protected function createListener($container, $id, $config, $userProvider)
{ {
// merge set options with default options
$options = $this->getOptionsFromConfig($config);
$listenerId = $this->getListenerId(); $listenerId = $this->getListenerId();
$listener = new DefinitionDecorator($listenerId); $listener = new DefinitionDecorator($listenerId);
$listener->setArgument(3, $id); $listener->setArgument(3, $id);

View File

@ -45,7 +45,7 @@ class FormLoginFactory extends AbstractFactory
return 'security.authentication.listener.form'; return 'security.authentication.listener.form';
} }
protected function createAuthProvider(ContainerBuilder $container, $id, $options, $userProviderId) protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
{ {
$provider = 'security.authentication.provider.dao.'.$id; $provider = 'security.authentication.provider.dao.'.$id;
$container $container
@ -57,8 +57,11 @@ class FormLoginFactory extends AbstractFactory
return $provider; return $provider;
} }
protected function createEntryPoint($container, $id, $options, $defaultEntryPoint) protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
{ {
// merge set options with default options
$options = $this->getOptionsFromConfig($config);
$entryPointId = 'security.authentication.form_entry_point.'.$id; $entryPointId = 'security.authentication.form_entry_point.'.$id;
$container $container
->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.form_entry_point')) ->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.form_entry_point'))

View File

@ -10,9 +10,9 @@
<user name="foo" password="foo" roles="ROLE_USER" /> <user name="foo" password="foo" roles="ROLE_USER" />
</provider> </provider>
<firewall pattern="/login" security="false" /> <firewall name="simple" pattern="/login" security="false" />
<firewall stateless="true"> <firewall name="secure" stateless="true">
<http-basic /> <http-basic />
<http-digest /> <http-digest />
<form-login /> <form-login />

View File

@ -0,0 +1,65 @@
<?php
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Security\Factory;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$factory = $this->getFactory();
$factory
->expects($this->once())
->method('createAuthProvider')
->will($this->returnValue('auth_provider'))
;
$factory
->expects($this->atLeastOnce())
->method('getListenerId')
->will($this->returnValue('abstract_listener'))
;
$container = new ContainerBuilder();
$container->register('auth_provider');
list($authProviderId,
$listenerId,
$entryPointId
) = $factory->create($container, 'foo', array('use_forward' => true, 'failure_path' => '/foo', 'success_handler' => 'foo'), 'user_provider', 'entry_point');
// auth provider
$this->assertEquals('auth_provider', $authProviderId);
$this->assertEquals(array('security.authentication_provider' => array(array())), $container->getDefinition('auth_provider')->getTags());
// listener
$this->assertEquals('abstract_listener.foo', $listenerId);
$this->assertTrue($container->hasDefinition('abstract_listener.foo'));
$definition = $container->getDefinition('abstract_listener.foo');
$this->assertEquals(array(
'index_3' => 'foo',
'index_4' => array(
'check_path' => '/login_check',
'login_path' => '/login',
'use_forward' => true,
'always_use_default_target_path' => false,
'default_target_path' => '/',
'target_path_parameter' => '_target_path',
'use_referer' => false,
'failure_path' => '/foo',
'failure_forward' => false,
),
'index_5' => new Reference('foo'),
), $definition->getArguments());
// entry point
$this->assertEquals('entry_point', $entryPointId, '->create() does not change the default entry point.');
}
protected function getFactory()
{
return $this->getMockForAbstractClass('Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory', array());
}
}

View File

@ -61,21 +61,21 @@ abstract class SecurityExtensionTest extends \PHPUnit_Framework_TestCase
foreach (array_keys($arguments[1]) as $contextId) { foreach (array_keys($arguments[1]) as $contextId) {
$contextDef = $container->getDefinition($contextId); $contextDef = $container->getDefinition($contextId);
$arguments = $contextDef->getArguments(); $arguments = $contextDef->getArguments();
$listeners[] = array_map(function ($ref) { return preg_replace('/\.[a-f0-9]+$/', '', (string) $ref); }, $arguments['index_0']); $listeners[] = array_map(function ($ref) { return (string) $ref; }, $arguments['index_0']);
} }
$this->assertEquals(array( $this->assertEquals(array(
array(), array(),
array( array(
'security.channel_listener', 'security.channel_listener',
'security.logout_listener', 'security.logout_listener.secure',
'security.authentication.listener.x509', 'security.authentication.listener.x509.secure',
'security.authentication.listener.form', 'security.authentication.listener.form.secure',
'security.authentication.listener.basic', 'security.authentication.listener.basic.secure',
'security.authentication.listener.digest', 'security.authentication.listener.digest.secure',
'security.authentication.listener.anonymous', 'security.authentication.listener.anonymous',
'security.access_listener', 'security.access_listener',
'security.authentication.switchuser_listener', 'security.authentication.switchuser_listener.secure',
), ),
), $listeners); ), $listeners);
} }