Commit 0338ca5a authored by adiouf's avatar adiouf
Browse files

dev #56299: disctinct actions for reservation device multimedia and...

dev #56299: disctinct actions for reservation device multimedia and authentification user multimedia on bokeh
parent a1811483
Pipeline #583 passed with stage
in 21 minutes and 54 seconds
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
*
......@@ -624,32 +625,20 @@ class AbonneController extends ZendAfi_Controller_Action {
public function authenticateAction() {
$this->getHelper('ViewRenderer')->setNoRender();
$response = new StdClass();
$response->auth = 0;
$request = Class_Multimedia_AuthenticateRequest::newWithRequest($this->_request);
if ($user = $request->getUser()) {
foreach (['id', 'login', 'password', 'nom',
'prenom', 'date_debut', 'date_fin'] as $attribute)
$response->$attribute = $user->$attribute;
$response->groupes = $user->getUserGroupsLabels();
$response->date_naissance = $user->getDateNaissanceIso8601();
}
if (!$request->isValid()) {
$response->error = $request->getError();
$this->_response->setBody(json_encode($response));
return;
}
$response->auth = 1;
$response->holding = $request->canHoldingForToDay();
$response->holded = $request->isSuccessHolding();
$response->until = '';
if($response->holded) $response->until = date('c', $request->getCurrentHoldEnd());
$mode = $this->_getParam('mode', '');
if ($mode === 'holdable-day')
return $this->_forward('is-holdable-day', 'multimedia', $this->_request->getModuleName(),
$this->_request->getParams());
if ($mode === 'hold')
return $this->_forward('hold-device', 'multimedia', $this->_request->getModuleName(),
$this->_request->getParams());
$this->_response->setBody(json_encode($response));
if ($mode === 'modified-hold')
return $this->_forward('close-holding', 'multimedia', $this->_request->getModuleName(),
$this->_request->getParams());
return $this->_forward('authenticate', 'multimedia', $this->_request->getModuleName(),
$this->_request->getParams());
}
......
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class MultimediaController extends ZendAfi_Controller_Action {
private function _initInfosUser($user, $response) {
foreach (['id', 'login', 'password', 'nom',
'prenom', 'date_debut', 'date_fin'] as $attribute)
$response->$attribute = $user->$attribute;
$response->groupes = $user->getUserGroupsLabels();
$response->date_naissance = $user->getDateNaissanceIso8601();
return $response;
}
public function authenticateAction() {
$this->getHelper('ViewRenderer')->setNoRender();
$response = new StdClass();
$response->auth = 0;
$request = Class_Multimedia_AuthenticateRequest::getInstance()->authenticate($this->_request);
if (!$request->isValid()) {
$response->error = $request->getError();
$this->_response->setBody(json_encode($response));
return;
}
if ($user = $request->getUser())
$response = $this->_initInfosUser($user, $response);
$response->auth = 1;
$this->_response->setBody(json_encode($response));
}
public function holdDeviceAction() {
$this->getHelper('ViewRenderer')->setNoRender();
$response = new StdClass();
$response->auth = 0;
$request = Class_Multimedia_AuthenticateRequest::getInstance()->holdDevice($this->_request);
if ($user = $request->getUser())
$response = $this->_initInfosUser($user, $response);
if (!$request->isValid()) {
$response->error = $request->getError();
$this->_response->setBody(json_encode($response));
return;
}
$response->auth = 1;
$response->holded = $request->isSuccessHolding();
$response->until = ($response->holded)
? $response->until = date('c', $request->getCurrentHoldEnd())
: '';
$this->_response->setBody(json_encode($response));
}
public function closeHoldingAction() {
$this->getHelper('ViewRenderer')->setNoRender();
$response = new StdClass();
$response->auth = 0;
$request = Class_Multimedia_AuthenticateRequest::getInstance()->closeHoldingDevice($this->_request);
if (!$request->isValid()) {
$response->error = $request->getError();
$this->_response->setBody(json_encode($response));
return;
}
$response->auth = 1;
$this->_response->setBody(json_encode($response));
}
public function isHoldableDayAction() {
$this->getHelper('ViewRenderer')->setNoRender();
$response = new StdClass();
$response->auth = 0;
$request = Class_Multimedia_AuthenticateRequest::getInstance()->isHoldableDay($this->_request);
if (!$request->isValid()) {
$response->error = $request->getError();
$this->_response->setBody(json_encode($response));
return;
}
$response->auth = 1;
$response->holding = $request->canHoldingForToDay();
$this->_response->setBody(json_encode($response));
}
}
\ No newline at end of file
......@@ -20,6 +20,8 @@
*/
class Class_Multimedia_AuthenticateRequest {
/** @var boolean */
protected $_valid = false;
......@@ -38,25 +40,21 @@ class Class_Multimedia_AuthenticateRequest {
/** @var boolean */
protected $_successHolding = false;
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public static function newWithRequest($request) {
$instance = new self();
return $instance->validate($request);
public static function getInstance() {
return new self();
}
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public function validate($request) {
private function _controleAuthRequest($request) {
if (!($login = $request->getParam('login'))
|| !($password = $request->getParam('password'))
|| !($poste = $request->getParam('poste'))
|| !($site = $request->getParam('site')))
|| !($password = $request->getParam('password')))
return $this->_error('MissingParameter');
$auth = ZendAfi_Auth::getInstance();
......@@ -75,41 +73,94 @@ class Class_Multimedia_AuthenticateRequest {
if (!$user->isAbonneInviteInGroupMultimedia() && !$user->isAbonnementValid())
return $this->_error('SubscriptionExpired');
$this->_user = $user;
}
private function _controleHoldRequest($request) {
if($this->_controleAuthRequest($request))
return $this;
if (!($site = $request->getParam('site'))
|| !($poste = $request->getParam('poste')))
return $this->_error('MissingParameter');
if ($location = Class_Multimedia_Location::findByIdOrigine($site))
$this->_device = Class_Multimedia_Device::findByIdOrigineAndLocation($poste, $location);
if (!$this->_device)
return $this->_error('DeviceNotFound');
}
$mode = $request->getParam('mode');
if(isset($mode) && $mode === 'holdable-day'){
$this->_holdableDay = $this->_device->isHoldableDayToDay();
}
if (isset($mode) && $mode === 'hold'){
$temps = $request->getParam('temps');
if(!$this->getCurrentHold($temps))
return $this->_error('DeviceNotHeldByUser');
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public function authenticate($request) {
if($this->_controleAuthRequest($request))
return $this;
$this->_successHolding = true;
}
return $this->beValid();
}
if(isset($mode) && $mode === 'modified-hold'){
if(!$hold = $this->getCurrentHold())
return $this->_error('DeviceNotHeldByUser');
$start = $hold->getStart();
$duration = floor(($location->getTimeSource()->time() - $start) / 60);
$hold->delete();
$this->_device->createHoldWithStartTimeAndDuration($user, $start, $duration);
}
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public function closeHoldingDevice($request) {
if($this->_controleHoldRequest($request))
return $this;
if(!$hold = $this->getCurrentHold())
return $this->_error('DeviceNotHeldByUser');
$start = $hold->getStart();
$duration = floor(($location->getTimeSource()->time() - $start) / 60);
$hold->delete();
$this->_device->createHoldWithStartTimeAndDuration($user, $start, $duration);
return $this->beValid();
}
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public function isHoldableDay($request) {
if($this->_controleHoldRequest($request))
return $this;
$this->_holdableDay = $this->_device->isHoldableDayToDay();
return $this->beValid();
}
/**
* @param Zend_Controller_Request_Abstract
* @return Class_Multimedia_AuthenticateRequest
*/
public function holdDevice($request) {
if($this->_controleHoldRequest($request))
return $this;
$temps = $request->getParam('temps');
if(!$this->getCurrentHold($temps))
return $this->_error('DeviceNotHeldByUser');
$this->_successHolding = true;
return $this->beValid();
}
public function canHoldingForToDay(){
return $this->_holdableDay;
}
......
......@@ -138,382 +138,48 @@ abstract class AbonneControllerMultimediaAuthenticateTestCase extends AbstractCo
}
class AbonneControllerMultimediaAuthenticateValidationTest extends AbonneControllerMultimediaAuthenticateTestCase {
class AbonneControllerMultimediaAuthenticateTest extends AbonneControllerMultimediaAuthenticateTestCase {
public function setUp() {
parent::setUp();
$this->_expectUserToLoad(AbonneControllerMultimediaUsersFixtures::getLaurent());
}
/** @test */
public function responseShouldNotBeARedirect() {
$json = $this->getJson('/abonne/authenticate/login/any/password/any');
$this->assertNotRedirect();
}
/** @test */
public function controllerActionShouldBeAbonneAuthenticate() {
/** @test */
public function authenticateActionShouldForwardMultimediaAuthenticateAction() {
$this->getJson('/abonne/authenticate/login/any/password/any');
$this->assertController('abonne');
$this->assertController('multimedia');
$this->assertAction('authenticate');
}
/** @test */
public function withoutPosteShouldReturnErrorMissingParameter() {
$json = $this->getJson('/abonne/authenticate/login/any');
$this->assertEquals('MissingParameter', $json->error);
}
/** @test */
public function withoutSiteShouldReturnErrorMissingParameter() {
$json = $this->getJson('/abonne/authenticate/login/any/password/any/poste/1');
$this->assertEquals('MissingParameter', $json->error);
}
/** @test */
public function getAbonneZorkShouldReturnErrorUserNotFound() {
$json = $this->getJson('/abonne/authenticate/login/any/password/toto/poste/1/site/1');
$this->assertEquals("UserNotFound", $json->error);
}
/** @test */
public function authenticateAbonneLaurentPasswordXXXShouldReturnWrongPassword() {
$json = $this->getJson('/abonne/authenticate/login/laurent/password/xxx/poste/1/site/1');
$this->assertEquals("PasswordIsWrong", $json->error);
}
}
class AbonneControllerMultimediaAuthenticateMireilleTest extends AbonneControllerMultimediaAuthenticateTestCase {
public function setUp() {
parent::setUp();
$user = AbonneControllerMultimediaUsersFixtures::getMireille();
$this->_expectUserToLoad($user);
$this->_json = $this->getJson('/abonne/authenticate/login/mireille/password/afi/poste/1/site/1');
}
/** @test */
public function shouldReturnSubscriptionExpired() {
$this->assertEquals('SubscriptionExpired', $this->_json->error);
}
/** @test */
public function dateFinShouldNotBePresent() {
$this->assertFalse(isset($this->_json->date_fin));
}
}
class AbonneControllerMultimediaAuthenticateInviteNonAfiMultimediaTest extends AbonneControllerMultimediaAuthenticateTestCase {
public function setUp() {
parent::setUp();
$user = AbonneControllerMultimediaUsersFixtures::getInvite();
$this->_expectUserToLoad($user);
$this->_json = $this->getJson('/abonne/authenticate/login/invite/password/invite/poste/1/site/1');
}
/** @test */
public function shouldReturnSubscriptionExpired() {
$this->assertEquals('SubscriptionExpired', $this->_json->error);
}
}
class AbonneControllerMultimediaAuthenticateInviteAfiMultimediaTest extends AbonneControllerMultimediaAuthenticateTestCase {
public function setUp() {
parent::setUp();
$user = AbonneControllerMultimediaUsersFixtures::getInvite();
$this->_expectUserToLoad($user);
$this->_expectGroupForUser($user, 'Abonne multimedia');
$this->_json = $this->getJson('/abonne/authenticate/login/invite/password/invite/poste/1/site/1');
}
/** @test */
public function shouldReturnDeviceNotFound() {
$this->assertEquals('DeviceNotFound', $this->_json->error);
}
}
abstract class AbonneControllerMultimediaAuthenticateValidTestCase extends AbonneControllerMultimediaAuthenticateTestCase {
protected $_user;
protected $_group;
public function setUp() {
Class_Multimedia_Location::setTimeSource(new TimeSourceForTest('2012-09-12 09:00:00'));
parent::setUp();
$this->_initUser();
$this->_expectUserToLoad($this->_user);
$this->_expectGroupForUser($this->_user, $this->_group);
Class_Users::clearCache();
$this->_launch();
}
protected function _initUser() {}
}
abstract class AbonneControllerMultimediaCheckedHoldableDayTestCase extends AbonneControllerMultimediaAuthenticateValidTestCase {
protected function _launch() {
$this->_json = $this->getJson(sprintf('/abonne/authenticate/login/%s/password/%s/poste/1/site/1/mode/holdable-day',
$this->_user->getLogin(),
$this->_user->getPassword()));
}
}
class AbonneControllerMultimediaHoldableDay extends AbonneControllerMultimediaCheckedHoldableDayTestCase {
use
TAbonneControllerMultimediaFixtureHoldSuccessOnSept12,
TAbonneControllerMultimediaFixtureWithUserLaurentInDevsAgiles;
/** @test */
public function shouldHaveAuth() {
$this->assertEquals(1, $this->_json->auth);
}
/** @test */
public function shouldHaveHoldDay() {
$this->assertEquals(1, $this->_json->holding);
}
}
abstract class AbonneControllerMultimediaHoldedTestCase extends AbonneControllerMultimediaAuthenticateValidTestCase {
protected function _launch() {
$this->_json = $this->getJson(sprintf('/abonne/authenticate/login/%s/password/%s/poste/1/site/1/mode/hold',
$this->_user->getLogin(),
$this->_user->getPassword()));
}
}
class AbonneControllerMultimediaAuthenticateLaurentTest extends AbonneControllerMultimediaHoldedTestCase {
use
TAbonneControllerMultimediaFixtureHoldSuccessOnSept12,
TAbonneControllerMultimediaFixtureWithUserLaurentInDevsAgiles;
/** @test */
public function shouldNotReturnError() {
$this->assertFalse(property_exists($this->_json, 'error'));
}
/** @test */
public function idShoudBe8() {
$this->assertEquals('8', $this->_json->id);
}
/** @test */
public function loginShoudBelaurent() {
$this->assertEquals('laurent', $this->_json->login);
}
/** @test */
public function passwordShoudBeAfi() {
$this->assertEquals('afi', $this->_json->password);
}
/** @test */
public function nomShoudBelaffont() {
$this->assertEquals('laffont', $this->_json->nom);
}
/** @test */
public function prenomShoudBelaurent() {
$this->assertEquals('laurent', $this->_json->prenom);
}
/** @test */
public function dateNaissanceShoudBe1978_02_17() {
$this->assertEquals('1978/02/17', $this->_json->date_naissance);
}
/** @test */
public function groupShoudBeAdulteAbonneAdminAndAgile() {
$this->assertEquals(['adulte', 'abonne', 'abonne_sigb', 'Devs agiles'],
$this->_json->groupes);
}
/** @test */
public function shouldHaveAuth() {
$this->assertEquals(1, $this->_json->auth);
}
/** @test */
public function shouldHaveHold() {
$this->assertEquals(1, $this->_json->holded);
}
/** @test */
public function holdShouldLastUntil16h40() {
$this->assertEquals('2012-09-12T16:40:00+02:00', $this->_json->until);
}
}
class AbonneControllerMultimediaAuthenticateLaurentDeviceNotHeldByUserTest extends AbonneControllerMultimediaHoldedTestCase {
use TAbonneControllerMultimediaFixtureWithUserLaurentInDevsAgiles;