Add tests to guest access

This commit is contained in:
Romain Neutron
2012-10-04 17:49:55 +02:00
parent c31ac156a0
commit 08ef9ebad0
3 changed files with 68 additions and 5 deletions

View File

@@ -803,7 +803,7 @@ class Login implements ControllerProviderInterface
$is_guest = false;
if (null !== $request->request->get('nolog') && \phrasea::guest_allowed($app)) {
if (null !== $request->get('nolog') && \phrasea::guest_allowed($app)) {
$is_guest = true;
}

View File

@@ -10,6 +10,10 @@
*/
use Alchemy\Phrasea\Application;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
/**
* Session Authentication Object for guest access
@@ -90,11 +94,20 @@ class Session_Authentication_Guest implements Session_Authentication_Interface
*/
public function postlog()
{
/**
* TODO NEUTRON FIX THIS
*/
\Session_Handler::set_cookie('invite-usr_id', $this->user->get_id(), 0, true);
$this->app['dispatcher']->addListener(KernelEvents::RESPONSE, array($this, 'addInviteCookie'), -128);
return $this;
}
public function addInviteCookie(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$response = $event->getResponse();
$response->headers->setCookie(new Cookie('invite-usr-id', $this->user->get_id()));
$event->setResponse($response);
}
}

View File

@@ -3,6 +3,7 @@
require_once __DIR__ . '/../../../../PhraseanetWebTestCaseAuthenticatedAbstract.class.inc';
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
{
@@ -657,9 +658,12 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
public function testAuthenticate()
{
$password = \random::generatePassword();
$login = self::$DI['app']['phraseanet.user']->get_login();
self::$DI['app']['phraseanet.user']->set_password($password);
self::$DI['app']->closeAccount();
self::$DI['client'] = new Client(self::$DI['app'], array());
$this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']);
self::$DI['client']->request('POST', '/login/authenticate/', array(
@@ -671,6 +675,52 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
$this->assertRegExp('/^\/prod\/$/', self::$DI['client']->getResponse()->headers->get('Location'));
}
/**
* @covers \Alchemy\Phrasea\Controller\Root\Login::authenticate
*/
public function testGuestAuthenticate()
{
$usr_id = \User_Adapter::get_usr_id_from_login(self::$DI['app'], 'invite');
$user = \User_Adapter::getInstance($usr_id, self::$DI['app']);
$user->ACL()->give_access_to_base(array(self::$DI['collection']->get_base_id()));
self::$DI['app']->closeAccount();
self::$DI['client'] = new Client(self::$DI['app'], array());
$this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']);
self::$DI['client']->request('POST', '/login/authenticate/?nolog');
$this->assertTrue(self::$DI['client']->getResponse()->isRedirect());
$this->assertRegExp('/^\/prod\/$/', self::$DI['client']->getResponse()->headers->get('Location'));
$cookies = self::$DI['client']->getResponse()->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertArrayHasKey('invite-usr-id', $cookies['']['/']);
$this->assertInternalType('integer', $cookies['']['/']['invite-usr-id']->getValue());
}
/**
* @covers \Alchemy\Phrasea\Controller\Root\Login::authenticate
*/
public function testGuestAuthenticateWithPostParam()
{
self::$DI['app']->closeAccount();
self::$DI['client'] = new Client(self::$DI['app'], array());
$this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']);
self::$DI['client']->request('POST', '/login/authenticate/', array('nolog'=>''));
$this->assertTrue(self::$DI['client']->getResponse()->isRedirect());
$this->assertRegExp('/^\/prod\/$/', self::$DI['client']->getResponse()->headers->get('Location'));
$cookies = self::$DI['client']->getResponse()->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertArrayHasKey('invite-usr-id', $cookies['']['/']);
$this->assertInternalType('integer', $cookies['']['/']['invite-usr-id']->getValue());
}
/**
* @covers \Alchemy\Phrasea\Controller\Root\Login::authenticate
*/