Commit 0f3ff9e4 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Service d'enregistrement dans la table des utilisateurs de l'application de...

Service d'enregistrement dans la table des utilisateurs de l'application de l'utilisateur authentifié avec succès : test unitaires et améliorations légères.
parent db787c2d
...@@ -3,17 +3,22 @@ namespace UnicaenAuth\Service; ...@@ -3,17 +3,22 @@ namespace UnicaenAuth\Service;
use PDOException; use PDOException;
use UnicaenApp\Exception; use UnicaenApp\Exception;
use UnicaenApp\Service\Ldap\People as LdapPeopleService; use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface; use Zend\ServiceManager\ServiceManagerAwareInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent; use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface; use ZfcUser\Options\AuthenticationOptionsInterface;
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
/** /**
* Service d'enregistrement dans la table des utilisateurs de l'application * Service d'enregistrement dans la table des utilisateurs de l'application
* de l'utilisateur authentifié avec succès. * de l'utilisateur authentifié avec succès.
*
* Est notifié via la méthode 'userAuthenticated()' lorsque l'authentification
* est terminée avec succès.
* *
* @see \UnicaenAuth\Authentication\Adapter\AbstractFactory
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/ */
class User implements ServiceManagerAwareInterface class User implements ServiceManagerAwareInterface
...@@ -34,9 +39,9 @@ class User implements ServiceManagerAwareInterface ...@@ -34,9 +39,9 @@ class User implements ServiceManagerAwareInterface
protected $zfcUserOptions; protected $zfcUserOptions;
/** /**
* @var LdapPeopleService * @var LdapPeopleMapper
*/ */
protected $ldapPeopleService; protected $ldapPeopleMapper;
/** /**
* Save authenticated user in database from LDAP data. * Save authenticated user in database from LDAP data.
...@@ -53,7 +58,7 @@ class User implements ServiceManagerAwareInterface ...@@ -53,7 +58,7 @@ class User implements ServiceManagerAwareInterface
} }
if (is_int($username)) { if (is_int($username)) {
// c'est un id: l'utilisateur existe déjà dans la bdd (et pas dans le LDAP), rien à faire // c'est un id : cela signifie que l'utilisateur existe déjà dans la bdd (et pas dans le LDAP), rien à faire
return true; return true;
} }
...@@ -62,7 +67,7 @@ class User implements ServiceManagerAwareInterface ...@@ -62,7 +67,7 @@ class User implements ServiceManagerAwareInterface
} }
// recherche de l'individu dans l'annuaire LDAP // recherche de l'individu dans l'annuaire LDAP
$ldapPeople = $this->getLdapPeopleService()->getMapper()->findOneByUsername($username); $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($username);
if (!$ldapPeople) { if (!$ldapPeople) {
return false; return false;
} }
...@@ -77,7 +82,7 @@ class User implements ServiceManagerAwareInterface ...@@ -77,7 +82,7 @@ class User implements ServiceManagerAwareInterface
"Erreur lors de la recherche de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(), "Erreur lors de la recherche de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(),
null, null,
$pdoe); $pdoe);
return true; // return true;
} }
if (!$entity) { if (!$entity) {
$entityClass = $this->getZfcUserOptions()->getUserEntityClass(); $entityClass = $this->getZfcUserOptions()->getUserEntityClass();
...@@ -100,7 +105,7 @@ class User implements ServiceManagerAwareInterface ...@@ -100,7 +105,7 @@ class User implements ServiceManagerAwareInterface
"Erreur lors de l'enregistrement de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(), "Erreur lors de l'enregistrement de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(),
null, null,
$pdoe); $pdoe);
return true; // return true;
} }
return true; return true;
...@@ -124,30 +129,31 @@ class User implements ServiceManagerAwareInterface ...@@ -124,30 +129,31 @@ class User implements ServiceManagerAwareInterface
public function setServiceManager(ServiceManager $serviceManager) public function setServiceManager(ServiceManager $serviceManager)
{ {
$this->serviceManager = $serviceManager; $this->serviceManager = $serviceManager;
return $this;
} }
/** /**
* get ldap people service * get ldap people mapper
* *
* @return LdapPeopleService * @return LdapPeopleMapper
*/ */
public function getLdapPeopleService() public function getLdapPeopleMapper()
{ {
if (null === $this->ldapPeopleService) { if (null === $this->ldapPeopleMapper) {
$this->ldapPeopleService = $this->getServiceManager()->get('ldap_people_service'); $this->ldapPeopleMapper = $this->getServiceManager()->get('ldap_people_mapper');
} }
return $this->ldapPeopleService; return $this->ldapPeopleMapper;
} }
/** /**
* set ldap people service * set ldap people mapper
* *
* @param LdapPeopleService $service * @param LdapPeopleMapper $mapper
* @return User * @return User
*/ */
public function setLdapPeopleService(LdapPeopleService $service) public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
{ {
$this->ldapPeopleService = $service; $this->ldapPeopleMapper = $mapper;
return $this; return $this;
} }
...@@ -157,6 +163,7 @@ class User implements ServiceManagerAwareInterface ...@@ -157,6 +163,7 @@ class User implements ServiceManagerAwareInterface
public function setOptions(ModuleOptions $options) public function setOptions(ModuleOptions $options)
{ {
$this->options = $options; $this->options = $options;
return $this;
} }
/** /**
...@@ -171,21 +178,22 @@ class User implements ServiceManagerAwareInterface ...@@ -171,21 +178,22 @@ class User implements ServiceManagerAwareInterface
} }
/** /**
* @param AuthenticationOptionsInterface $options * @param ZfcUserModuleOptions $options
*/ */
public function setZfcUserOptions(AuthenticationOptionsInterface $options) public function setZfcUserOptions(ZfcUserModuleOptions $options)
{ {
$this->zfcUserOptions = $options; $this->zfcUserOptions = $options;
return $this;
} }
/** /**
* @return AuthenticationOptionsInterface * @return ZfcUserModuleOptions
*/ */
public function getZfcUserOptions() public function getZfcUserOptions()
{ {
if (!$this->zfcUserOptions instanceof AuthenticationOptionsInterface) { if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
$this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options')); $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
} }
return $this->zfcUserOptions; return $this->zfcUserOptions;
} }
} }
\ No newline at end of file
<?php
namespace UnicaenAuthTest\Service;
use PHPUnit_Framework_TestCase;
use UnicaenApp\Entity\Ldap\People as LdapPeopleEntity;
use UnicaenAppTest\Entity\Ldap\TestAsset\People as LdapPeopleTestAsset;
use UnicaenAuth\Service\User;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
/**
* Description of UserTest
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class UserTest extends PHPUnit_Framework_TestCase
{
protected $service;
protected $authModuleOptions;
protected $zfcMapper;
protected $mapper;
protected $event;
protected function setUp()
{
$this->authModuleOptions = $authModuleOptions = new \UnicaenAuth\Options\ModuleOptions(array(
'save_ldap_user_in_database' => true,
// 'usurpation_allowed_usernames' => array('usurpateur'),
));
$this->zfcMapper = $zfcMapper = $this->getMock('ZfcUser\Mapper\User', array('findByUsername', 'insert', 'update'));
$this->mapper = $mapper = $this->getMock('UnicaenApp\Mapper\Ldap\People', array('findOneByUsername'));
$serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager', array('get'));
$serviceManager->expects($this->any())
->method('get')
->will($this->returnCallback(function($serviceName) use ($authModuleOptions, $zfcMapper, $mapper) {
if ('zfcuser_module_options' === $serviceName) {
return new \ZfcUser\Options\ModuleOptions();
}
if ('unicaen-auth_module_options' === $serviceName) {
return $authModuleOptions;
}
if ('ldap_people_mapper' === $serviceName) {
return $mapper;
}
if ('zfcuser_user_mapper' === $serviceName) {
return $zfcMapper;
}
return null;
}));
$this->event = new AdapterChainEvent();
$this->event->setIdentity('username');
$this->service = new User();
$this->service->setServiceManager($serviceManager);
}
public function testCanRetrieveDefaultLdapPeopleMapperFromServiceManager()
{
$this->assertInstanceOf('UnicaenApp\Mapper\Ldap\People', $this->service->getLdapPeopleMapper());
}
public function testCanRetrieveModuleOptionsFromServiceManager()
{
$this->assertInstanceOf('UnicaenAuth\Options\ModuleOptions', $this->service->getOptions());
}
public function testCanRetrieveZfcModuleOptionsFromServiceManager()
{
$this->assertInstanceOf('ZfcUser\Options\ModuleOptions', $this->service->getZfcUserOptions());
}
public function testEntryPointReturnsFalseIfOptionFlagIsFalse()
{
$this->service->getOptions()->setSaveLdapUserInDatabase(false);
$this->assertFalse($this->service->userAuthenticated($this->event));
}
public function testEntryPointReturnsFalseIfNoIdentitySpecifiedInEvent()
{
$this->event->setIdentity(null);
$this->assertFalse($this->service->userAuthenticated($this->event));
}
public function testEntryPointReturnsTrueIfIntegerIdentitySpecifiedInEvent()
{
$this->event->setIdentity(12);
$this->assertTrue($this->service->userAuthenticated($this->event));
}
/**
* @expectedException \UnicaenApp\Exception
*/
public function testEntryPointThrowsExceptionIfUnexpectedNonEmptyIdentitySpecifiedInEvent()
{
$this->event->setIdentity(array('content'));
$this->service->userAuthenticated($this->event);
}
public function testEntryPointReturnsFalseIfUsernameNotFoundInLdap()
{
$this->mapper->expects($this->once())
->method('findOneByUsername')
->will($this->returnValue(null));
$this->assertFalse($this->service->userAuthenticated($this->event));
}
/**
* @expectedException \UnicaenApp\Exception
*/
public function testEntryPointThrowsExceptionIfPDOExceptionThrownDuringFetch()
{
$entity = new LdapPeopleEntity(LdapPeopleTestAsset::$data1);
$this->mapper->expects($this->once())
->method('findOneByUsername')
->will($this->returnValue($entity));
$this->zfcMapper->expects($this->once())
->method('findByUsername')
->will($this->throwException(new \PDOException()));
$this->service->userAuthenticated($this->event);
}
public function testEntryPointPerformsInsertAndReturnsTrueWhenUserDoesNotExistInDb()
{
$entity = new LdapPeopleEntity(LdapPeopleTestAsset::$data1);
$this->mapper->expects($this->once())
->method('findOneByUsername')
->will($this->returnValue($entity));
$this->zfcMapper->expects($this->once())
->method('findByUsername')
->will($this->returnValue(null));
$this->zfcMapper->expects($this->once())
->method('insert')
->with($this->isInstanceOf('ZfcUser\Entity\User'));
$this->assertTrue($this->service->userAuthenticated($this->event));
}
public function testEntryPointPerformsUpdateAndReturnsTrueWhenUserExistsFoundInDb()
{
$people = new LdapPeopleEntity(LdapPeopleTestAsset::$data1);
$this->mapper->expects($this->once())
->method('findOneByUsername')
->will($this->returnValue($people));
$user = new \ZfcUser\Entity\User();
$this->zfcMapper->expects($this->once())
->method('findByUsername')
->will($this->returnValue($user));
$this->zfcMapper->expects($this->once())
->method('update')
->with($this->isInstanceOf('ZfcUser\Entity\User'));
$this->assertTrue($this->service->userAuthenticated($this->event));
}
/**
* @expectedException \UnicaenApp\Exception
*/
public function testEntryPointThrowsExceptionIfPDOExceptionThrownDuringSave()
{
$entity = new LdapPeopleEntity(LdapPeopleTestAsset::$data1);
$this->mapper->expects($this->once())
->method('findOneByUsername')
->will($this->returnValue($entity));
$user = new \ZfcUser\Entity\User();
$this->zfcMapper->expects($this->once())
->method('findByUsername')
->will($this->returnValue($user));
$this->zfcMapper->expects($this->once())
->method('update')
->will($this->throwException(new \PDOException()));
$this->service->userAuthenticated($this->event);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment