Files
Phraseanet/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php
Romain Neutron a74ad06ad1 Fix CS
2013-12-18 18:15:16 +01:00

306 lines
10 KiB
PHP

<?php
namespace Alchemy\Tests\Phrasea\Authentication;
use Alchemy\Phrasea\Authentication\Authenticator;
use Alchemy\Phrasea\Exception\RuntimeException;
use Alchemy\Phrasea\Model\Entities\Session;
class AuthenticatorTest extends \PhraseanetTestCase
{
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::getUser
*/
public function testGetUser()
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$this->assertNull($authenticator->getUser());
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::getUser
*/
public function testGetUserWhenAuthenticated()
{
$app = $this->loadApp();
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$sessionEntity = new Session();
$sessionEntity->setUsrId($user->get_id());
$sessionEntity->setUserAgent('');
$app['EM']->persist($sessionEntity);
$app['EM']->flush();
$session->set('usr_id', $user->get_id());
$session->set('session_id', $sessionEntity->getId());
$authenticator = new Authenticator($app, $browser, $session, $app['EM']);
$this->assertEquals($user, $authenticator->getUser());
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::setUser
*/
public function testSetUser()
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$user = $this->getMockBuilder('\User_Adapter')
->disableOriginalConstructor()
->getMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$authenticator->setUser($user);
$this->assertEquals($user, $authenticator->getUser());
$authenticator->setUser(null);
$this->assertNull($authenticator->getUser());
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::openAccount
*/
public function testOpenAccount()
{
$app = $this->loadApp();
$capturedSession = null;
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$user = $this->getMockBuilder('\User_Adapter')
->disableOriginalConstructor()
->getMock();
$user->expects($this->any())
->method('get_id')
->will($this->returnvalue(self::$DI['user']->get_id()));
$acl = $this->getMockBuilder('ACL')
->disableOriginalConstructor()
->getMock();
$acl->expects($this->once())
->method('get_granted_sbas')
->will($this->returnValue([]));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($acl));
$app['acl'] = $aclProvider;
$em->expects($this->at(0))
->method('persist')
->with($this->isInstanceOf('Alchemy\Phrasea\Model\Entities\Session'))
->will($this->returnCallback(function ($session) use (&$capturedSession) {
$capturedSession = $session;
}));
$em->expects($this->at(1))
->method('flush');
$authenticator = new Authenticator($app, $browser, $session, $em);
$phsession = $authenticator->openAccount($user);
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\Session', $phsession);
$this->assertEquals($capturedSession, $phsession);
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::refreshAccount
*/
public function testRefreshAccount()
{
$app = $this->loadApp();
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $SFsession = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$usrId = $user->get_id();
$sessionId = 4224242;
$session = new Session();
$session->setUsrId($usrId);
$ref = new \ReflectionObject($session);
$prop = $ref->getProperty('id');
$prop->setAccessible(true);
$prop->setValue($session, $sessionId);
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
$repo->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['id' => $session->getId()]))
->will($this->returnValue($session));
$em->expects($this->once())
->method('getRepository')
->with($this->equalTo('Alchemy\Phrasea\Model\Entities\Session'))
->will($this->returnValue($repo));
$authenticator = new Authenticator($app, $browser, $SFsession, $em);
$this->assertEquals($session, $authenticator->refreshAccount($session));
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::refreshAccount
*/
public function testRefreshAccountWithWrongSessionShouldThrowException()
{
$app = $this->loadApp();
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $SFsession = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$usrId = $user->get_id();
$sessionId = 4224242;
$session = new Session();
$session->setUsrId($usrId);
$ref = new \ReflectionObject($session);
$prop = $ref->getProperty('id');
$prop->setAccessible(true);
$prop->setValue($session, $sessionId);
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
$repo->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['id' => $session->getId()]))
->will($this->returnValue(null));
$em->expects($this->once())
->method('getRepository')
->with($this->equalTo('Alchemy\Phrasea\Model\Entities\Session'))
->will($this->returnValue($repo));
$authenticator = new Authenticator($app, $browser, $SFsession, $em);
try {
$authenticator->refreshAccount($session);
$this->fail('Should have raised an exception');
} catch (RuntimeException $e) {
$this->assertEquals('Unable to refresh the session, it does not exist anymore', $e->getMessage());
}
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::closeAccount
*/
public function testCloseAccount()
{
$app = self::$DI['app'];
$user = self::$DI['user'];
$authenticator = new Authenticator($app, $app['browser'], $app['session'], $app['EM']);
$authenticator->openAccount($user);
$this->assertNotNull($authenticator->getUser());
$authenticator->closeAccount();
$this->assertNull($authenticator->getUser());
}
public function testCloseAccountWhenNoSessionThrowsAnException()
{
$app = self::$DI['app'];
$authenticator = new Authenticator($app, $app['browser'], $app['session'], $app['EM']);
$this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'No session to close.');
$authenticator->closeAccount();
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::isAuthenticated
*/
public function testIsAuthenticated()
{
$app = $this->loadApp();
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$sessionEntity = new Session();
$sessionEntity->setUsrId($user->get_id());
$sessionEntity->setUserAgent('');
$app['EM']->persist($sessionEntity);
$app['EM']->flush();
$session->set('usr_id', $user->get_id());
$session->set('session_id', $sessionEntity->getId());
$authenticator = new Authenticator($app, $browser, $session, $app['EM']);
$this->assertTrue($authenticator->isAuthenticated());
}
/**
* @covers Alchemy\Phrasea\Authentication\Authenticator::isAuthenticated
*/
public function testIsNotAuthenticated()
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$this->assertFalse($authenticator->isAuthenticated());
}
private function getEntityManagerMock()
{
return $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
}
private function getRegistryMock()
{
return $this->getMockBuilder('registryInterface')
->disableOriginalConstructor()
->getMock();
}
private function getSessionMock()
{
return new \Symfony\Component\HttpFoundation\Session\Session(new \Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage());
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')
->disableOriginalConstructor()
->getMock();
}
private function getBrowserMock()
{
return $this->getMockBuilder('Browser')
->disableOriginalConstructor()
->getMock();
}
}