API implementation and tests

This commit is contained in:
kiswa 2016-05-13 20:03:20 +00:00
parent 278adffcd6
commit 5e18051188
8 changed files with 153 additions and 31 deletions

View File

@ -10,8 +10,10 @@ class AutoActions extends BaseController {
$this->apiJson->setSuccess();
foreach($actionBeans as $bean) {
$this->apiJson->addData(
AutoAction::fromBean($this->container, $bean));
$action = new AutoAction($this->container);
$action->loadFromBean($bean);
$this->apiJson->addData($action);
}
} else {
$this->logger->addInfo('No automatic actions in database.');

View File

@ -23,26 +23,15 @@ class AutoAction extends BaseModel {
public $type;
public $change_to = ''; // Whatever the target of the action changes to
public function __construct($container, $id = 0, $internal = false) {
public function __construct($container, $id = 0) {
parent::__construct('auto_action', $id, $container);
$this->trigger = new ActionTrigger(ActionTrigger::MoveToColumn);
$this->type = new ActionType(ActionType::SetColor);
if ($internal) {
return;
}
$this->loadFromBean($this->bean);
}
public static function fromBean($container, $bean) {
$instance = new self($container, 0, true);
$instance->loadFromBean($bean);
return $instance;
}
public function updateBean() {
$bean = $this->bean;

View File

@ -9,7 +9,7 @@ class SecurityLevel extends Enum {
class User extends BaseModel {
public $id = 0;
public $security_level = SecurityLevel::User;
public $security_level;
public $username = '';
public $salt = '';
public $password_hash = '';
@ -20,6 +20,8 @@ class User extends BaseModel {
public function __construct($container, $id = 0) {
parent::__construct('user', $id, $container);
$this->security_level = new SecurityLevel(SecurityLevel::User);
$this->loadFromBean($this->bean);
}
@ -27,7 +29,7 @@ class User extends BaseModel {
$bean = $this->bean;
$bean->id = $this->id;
$bean->security_level = $this->security_level;
$bean->security_level = $this->security_level->getValue();
$bean->username = $this->username;
$bean->salt = $this->salt;
$bean->password_hash = $this->password_hash;

View File

@ -62,7 +62,7 @@ class DataMock {
$user->password_hash = 'hashpass1234';
$user->email = 'user@example.com';
$user->default_board_id = 1;
$user->options[] = DataMock::getUserOptions();
$user->user_option_id = 1;
return $user;
}

View File

@ -109,6 +109,18 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
}
private function createBoard() {
$users = new Users(new ContainerMock());
$request = new RequestMock();
$user = DataMock::getUser();
$user->id = 0;
$request->payload = $user;
$response = $users->addUser($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'success');
$response = $this->boards->addBoard(new RequestMock(),
new ResponseMock(), null);
$this->assertTrue($response->status === 'success');

View File

@ -0,0 +1,124 @@
<?php
require_once __DIR__ . '/../Mocks.php';
class UsersTest extends PHPUnit_Framework_TestCase {
private $users;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
public function setUp() {
RedBeanPHP\R::nuke();
$this->users = new Users(new ContainerMock());
}
public function testGetAllUsers() {
$expected = new ApiJson();
$expected->addAlert('info', 'No users in database.');
$actual = $this->users->getAllUsers(null, new ResponseMock(), null);
$this->assertEquals($expected, $actual);
$this->createUser();
$users = $this->users->getAllUsers(null, new ResponseMock(), null);
$this->assertTrue(count($users->data) === 1);
$this->assertTrue($users->status === 'success');
}
public function testGetUser() {
$expected = new ApiJson();
$expected->addAlert('error', 'No user found for ID 1.');
$args = [];
$args['id'] = 1;
$actual = $this->users->getUser(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
$this->createUser();
$actual = $this->users->getUser(null, new ResponseMock(), $args);
$this->assertTrue($actual->status === 'success');
$this->assertTrue(count($actual->data) === 1);
}
public function testAddRemoveUser() {
$expected = new ApiJson();
$actual = $this->createUser();
$expected->setSuccess();
$expected->addAlert('success', 'User tester added.');
$this->assertEquals($expected, $actual);
$expected->addAlert('success', 'User tester removed.');
$args = [];
$args['id'] = 1;
$actual = $this->users->removeUser(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
}
public function testAddBadUser() {
$request = new RequestMock();
$request->invalidPayload = true;
$response = $this->users->addUser($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'failure');
$this->assertTrue($response->alerts[0]['type'] === 'error');
}
public function testRemoveBadUser() {
$args = [];
$args['id'] = 5; // No such user
$response = $this->users->removeUser(null, new ResponseMock(), $args);
$this->assertTrue($response->status === 'failure');
}
public function testUpdateUser() {
$this->createUser();
$user = DataMock::getUser();
$user->username = 'newname';
$args = [];
$args['id'] = $user->id;
$request = new RequestMock();
$request->payload = $user;
$response = $this->users->updateUser($request,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'success');
$request->payload = new stdClass();
$response = $this->users->updateUser($request,
new ResponseMock(), $args);
$this->assertTrue($response->alerts[2]['type'] === 'error');
}
private function createUser() {
$request = new RequestMock();
$user = DataMock::getUser();
$user->id = 0;
$request->payload = $user;
$response = $this->users->addUser($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'success');
return $response;
}
}

View File

@ -28,16 +28,6 @@ class AutoActionTest extends PHPUnit_Framework_TestCase {
$this->assertDefaultProperties($action);
}
public function testCreateFromBean() {
$action = AutoAction::fromBean(new ContainerMock, null);
$this->assertDefaultProperties($action);
$action = AutoAction::fromBean(new ContainerMock(), $this->bean);
$this->assertTrue($action->id === 1);
}
public function testLoadFromJson() {
$action = new AutoAction(new ContainerMock());
@ -54,6 +44,9 @@ class AutoActionTest extends PHPUnit_Framework_TestCase {
public function testLoadFromBean() {
$action = new AutoAction(new ContainerMock());
$action->loadFromBean(null);
$this->assertDefaultProperties($action);
$action->loadFromBean($this->bean);
$this->assertMockProperties($action);
}

View File

@ -21,8 +21,6 @@ class UserTest extends PHPUnit_Framework_TestCase {
$user = DataMock::getUser();
$this->json = json_encode($user);
$this->bean = $user;
// Convert to bean format
$this->bean->xownOptionList = $user->options;
}
public function testCreateUser() {
@ -61,7 +59,8 @@ class UserTest extends PHPUnit_Framework_TestCase {
$bean = $user->getBean();
$this->assertTrue($bean->id === $user->id);
$this->assertTrue($bean->security_level === $user->security_level);
$this->assertTrue($bean->security_level ===
$user->security_level->getValue());
$this->assertTrue($bean->username === $user->username);
$this->assertTrue($bean->salt === $user->salt);
$this->assertTrue($bean->password_hash === $user->password_hash);
@ -71,7 +70,8 @@ class UserTest extends PHPUnit_Framework_TestCase {
private function assertDefaultProperties($user) {
$this->assertTrue($user->id === 0);
$this->assertTrue($user->security_level == SecurityLevel::User);
$this->assertTrue($user->security_level->getValue() ===
SecurityLevel::User);
$this->assertTrue($user->username === '');
$this->assertTrue($user->salt === '');
$this->assertTrue($user->password_hash === '');