Commit 4a134629 authored by Karamel's avatar Karamel
Browse files

Role route with tests.

parent 05d11634
<?php
use \Pasteque\Server\Model\Role;
use \Pasteque\Server\System\API\APICaller;
/**
......@@ -25,3 +26,10 @@ $app->GET('/api/role/{id}', function($request, $response, $args) {
return $response->withApiResult(APICaller::run($ptApp, 'role', 'get', $args['id']));
});
/** Low level call. If an id is set, it's an update. If not, it's a create. */
$app->POST('/api/role', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$role = \Pasteque\Server\Model\Role::fromStruct($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'role', 'write', $role));
});
<?php
// Pasteque server testing
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pasteque.
//
// Pasteque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pasteque 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\Model\Role;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
require_once(dirname(dirname(__FILE__)) . "/common_load.php");
class HttpRoleTest extends TestCase
{
private $curl;
private static $token;
private $dao;
public static function setUpBeforeClass() {
static::$token = obtainToken();
}
public static function tearDownAfterClass() {
}
protected function setUp() {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token]);
global $dbInfo;
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$this->role = new Role();
$this->role->setName('Test Role');
$this->role->setPermissions(['perm1', 'perm2']);
$this->dao->write($this->role);
$this->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
$all = $this->dao->search(Role::class);
foreach($all as $record) {
$this->dao->delete($record);
}
$this->dao->commit();
$this->dao->close();
}
public function testGetAll() {
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/role/getAll'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertEquals(1, count($data));
$this->assertEquals($this->role->getName(), $data[0]['name']);
$this->assertEquals($this->role->getId(), $data[0]['id']);
}
public function testPostNew() {
$newRole = new Role();
$newRole->setName('New Role');
$newRole->addPermission('Perm1');
$postData = $newRole->toStruct();
unset($postData['id']);
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/role'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbUser = $this->dao->search(Role::class,
new DAOCondition('name', '=', 'New Role'));
$this->assertEquals(1, count($dbUser));
}
public function testPostUpdate() {
$this->role->setName('Edited Role');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/role'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->role->toStruct()));
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbUser = $this->dao->search(Role::class);
$this->assertEquals(1, count($dbUser));
$this->assertEquals('Edited Role', $dbUser[0]->getName());
}
}
......@@ -45,6 +45,7 @@
<file>http/HttpPlaceTest.php</file>
<file>http/HttpPaymentModeTest.php</file>
<file>http/HttpUserTest.php</file>
<file>http/HttpRoleTest.php</file>
</testsuite>
</testsuites>
</phpunit>
Supports Markdown
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