Skip to content
Snippets Groups Projects
Commit 43f8bbed authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

Merge branch 'dev#73651_api_account' into 'hotline'

Dev#73651 api account

See merge request !2575
parents c26e7498 24be5e54
Branches
Tags
4 merge requests!2660Master,!2586Master,!2585Hotline,!2575Dev#73651 api account
Pipeline #3805 passed with stage
in 34 minutes
<?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 Api_ErrorController extends Zend_Controller_Action {
public function errorAction() {
$errors = $this->_getParam('error_handler');
$this->_helper->json(['error' => 'invalid_request',
'message' => $errors->exception->getMessage()]);
$this->_response->setHttpResponseCode($errors->exception->getCode());
}
}
...@@ -21,31 +21,53 @@ ...@@ -21,31 +21,53 @@
class Api_UserController extends ZendAfi_Controller_Action { class Api_UserController extends ZendAfi_Controller_Action {
public function preDispatch() {
parent::preDispatch();
$this->_authenticate();
}
public function accountAction() {
$user = Class_Users::getIdentity();
$this->_helper
->json(['account' => ['label' => $user->getNomAff(),
'card' => ['id'=> $user->getIdabon(),
'expire_at' => $user->getDateFin()]
]]);
}
public function loansAction() { public function loansAction() {
$this->view->loans = (new Class_User_Cards(Class_Users::getIdentity()))->getLoans();
}
protected function _authenticate() {
if (Class_AdminVar_OAuthAcceptHTTP::shouldRejectRequest($this->_request)) if (Class_AdminVar_OAuthAcceptHTTP::shouldRejectRequest($this->_request))
return $this->_error($this->_('Protocole HTTPS obligatoire')); return $this->_error($this->_('Protocole HTTPS obligatoire'), 403);
if (!$authorization = $this->_request->getHeader('authorization')) if (!$authorization = $this->_request->getHeader('authorization'))
return $this->_error($this->_('Autorisation non spécifiée')); return $this->_error($this->_('Autorisation non spécifiée'), 401);
$parts = explode(' ', $authorization); $parts = explode(' ', $authorization);
if ($parts[0] !== 'Bearer') if ($parts[0] !== 'Bearer')
return $this->_error($this->_('Jeton d\'autorisation non fourni')); return $this->_error($this->_('Jeton d\'autorisation non fourni'), 401);
if (!$token = Class_User_ApiToken::findFirstBy(['token' => $parts[1]])) if (!$token = Class_User_ApiToken::findFirstBy(['token' => $parts[1]]))
return $this->_error($this->_('Jeton d\'autorisation invalide')); return $this->_error($this->_('Jeton d\'autorisation invalide'), 403);
if (!$user = $token->getUser()) if (!$user = $token->getUser())
return $this->_error($this->_('Utilisateur non trouvé')); return $this->_error($this->_('Utilisateur non trouvé'), 403);
ZendAfi_Auth::getInstance()->logUser($user); ZendAfi_Auth::getInstance()->logUser($user);
$this->view->loans = (new Class_User_Cards($user))->getLoans();
} }
protected function _error($message) { protected function _error($message, $code) {
$this->view->message = $message; Zend_Controller_Front::getInstance()
return $this->renderScript('invalid_request.pjson'); ->getPlugin('Zend_Controller_Plugin_ErrorHandler')
->setErrorHandlerModule('api');
throw new Zend_Controller_Action_Exception($message, $code);
} }
} }
?>
\ No newline at end of file
{
"error":"invalid_request",
"message":"<?php echo $this->message ?>"
}
\ No newline at end of file
...@@ -29,6 +29,8 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo ...@@ -29,6 +29,8 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo
$puppy = $this->fixture('Class_Users', $puppy = $this->fixture('Class_Users',
['id' => 345, ['id' => 345,
'pseudo' => 'Puppy',
'date_fin' => '2018-02-12',
'login' => 'puppy', 'login' => 'puppy',
'password' => 'opied', 'password' => 'opied',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB, 'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
...@@ -73,7 +75,7 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo ...@@ -73,7 +75,7 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo
class Scenario_MobileApplication_UserAccountWithTokenTest extends Scenario_MobileApplication_UserAccountTestCase { class Scenario_MobileApplication_UserAccountLoansWithTokenTest extends Scenario_MobileApplication_UserAccountTestCase {
protected protected
$_json; $_json;
...@@ -118,11 +120,11 @@ class Scenario_MobileApplication_UserAccountWithTokenTest extends Scenario_Mobil ...@@ -118,11 +120,11 @@ class Scenario_MobileApplication_UserAccountWithTokenTest extends Scenario_Mobil
class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_MobileApplication_UserAccountTestCase { class Scenario_MobileApplication_UserAccountLoansWithoutTokenTest extends Scenario_MobileApplication_UserAccountTestCase {
/** @test */ /** @test */
public function withoutAuthorizationShouldAnswerInvalidRequest() { public function withoutAuthorizationShouldAnswerInvalidRequest() {
$this->dispatch('/api/user/loans', $this->dispatch('/api/user/loans',
true, false,
["Content-Type" => "application/json"]); ["Content-Type" => "application/json"]);
$this->assertEquals(['error' => 'invalid_request', $this->assertEquals(['error' => 'invalid_request',
...@@ -134,7 +136,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo ...@@ -134,7 +136,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo
/** @test */ /** @test */
public function withWrongAuthorizationTypeShouldAnswerInvalidRequest() { public function withWrongAuthorizationTypeShouldAnswerInvalidRequest() {
$this->dispatch('/api/user/loans', $this->dispatch('/api/user/loans',
true, false,
["Authorization" => 'Catch nonos', ["Authorization" => 'Catch nonos',
"Content-Type" => "application/json"]); "Content-Type" => "application/json"]);
...@@ -147,7 +149,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo ...@@ -147,7 +149,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo
/** @test */ /** @test */
public function withWrongAuthorizationTokenShouldAnswerInvalidRequest() { public function withWrongAuthorizationTokenShouldAnswerInvalidRequest() {
$this->dispatch('/api/user/loans', $this->dispatch('/api/user/loans',
true, false,
["Authorization" => 'Bearer veget@ble', ["Authorization" => 'Bearer veget@ble',
"Content-Type" => "application/json"]); "Content-Type" => "application/json"]);
...@@ -165,7 +167,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo ...@@ -165,7 +167,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo
'user_id' => 987]); 'user_id' => 987]);
$this->dispatch('/api/user/loans', $this->dispatch('/api/user/loans',
true, false,
["Authorization" => 'Bearer veget@ble', ["Authorization" => 'Bearer veget@ble',
"Content-Type" => "application/json"]); "Content-Type" => "application/json"]);
...@@ -180,7 +182,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo ...@@ -180,7 +182,7 @@ class Scenario_MobileApplication_UserAccountWithoutTokenTest extends Scenario_Mo
unset($_SERVER['HTTPS']); unset($_SERVER['HTTPS']);
$this->dispatch('/api/user/loans', $this->dispatch('/api/user/loans',
true, false,
["Authorization" => "Bearer nonos" , ["Authorization" => "Bearer nonos" ,
"Content-Type" => "application/json"]); "Content-Type" => "application/json"]);
...@@ -365,4 +367,33 @@ class Scenario_MobileApplication_UserAccountOAuthPostLoginSuccessTest extends Sc ...@@ -365,4 +367,33 @@ class Scenario_MobileApplication_UserAccountOAuthPostLoginSuccessTest extends Sc
$this->assertEquals('My mobile bokeh', $token->getClientId()); $this->assertEquals('My mobile bokeh', $token->getClientId());
} }
} }
class Scenario_MobileApplication_UserAccountWithTokenTest extends Scenario_MobileApplication_UserAccountTestCase {
protected
$_json;
public function setUp() {
parent::setUp();
$this->dispatch('/api/user/account',
true,
["Authorization" => "Bearer nonos" ,
"Content-Type" => "application/json"]);
$this->_json = json_decode($this->_response->getBody(), true);
}
/** @test */
public function responseShouldContainsCardValidityAndLabel() {
$this->assertEquals(['label' => 'Puppy',
'card' => [
'id' => '234',
'expire_at' => '2018-02-12']
],
$this->_json['account']);
}
}
?> ?>
\ No newline at end of file
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