From 5e18051188f9cf1ec19d60be89e6dc31c392c358 Mon Sep 17 00:00:00 2001 From: kiswa Date: Fri, 13 May 2016 20:03:20 +0000 Subject: [PATCH] API implementation and tests --- src/api/controllers/AutoActions.php | 6 +- src/api/models/AutoAction.php | 13 +-- src/api/models/User.php | 6 +- test/api/Mocks.php | 2 +- test/api/controllers/BoardsTest.php | 12 +++ test/api/controllers/UsersTest.php | 124 ++++++++++++++++++++++++++++ test/api/models/AutoActionTest.php | 13 +-- test/api/models/UserTest.php | 8 +- 8 files changed, 153 insertions(+), 31 deletions(-) create mode 100644 test/api/controllers/UsersTest.php diff --git a/src/api/controllers/AutoActions.php b/src/api/controllers/AutoActions.php index 63f5ac7..ef3088e 100644 --- a/src/api/controllers/AutoActions.php +++ b/src/api/controllers/AutoActions.php @@ -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.'); diff --git a/src/api/models/AutoAction.php b/src/api/models/AutoAction.php index a55ca4b..bd0f1ce 100644 --- a/src/api/models/AutoAction.php +++ b/src/api/models/AutoAction.php @@ -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; diff --git a/src/api/models/User.php b/src/api/models/User.php index ee33d36..f504391 100644 --- a/src/api/models/User.php +++ b/src/api/models/User.php @@ -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; diff --git a/test/api/Mocks.php b/test/api/Mocks.php index 108196c..ae01ef6 100644 --- a/test/api/Mocks.php +++ b/test/api/Mocks.php @@ -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; } diff --git a/test/api/controllers/BoardsTest.php b/test/api/controllers/BoardsTest.php index fae87c1..05923d3 100644 --- a/test/api/controllers/BoardsTest.php +++ b/test/api/controllers/BoardsTest.php @@ -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'); diff --git a/test/api/controllers/UsersTest.php b/test/api/controllers/UsersTest.php new file mode 100644 index 0000000..03f95ec --- /dev/null +++ b/test/api/controllers/UsersTest.php @@ -0,0 +1,124 @@ +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; + } +} + diff --git a/test/api/models/AutoActionTest.php b/test/api/models/AutoActionTest.php index 9df5ffa..ce1f992 100644 --- a/test/api/models/AutoActionTest.php +++ b/test/api/models/AutoActionTest.php @@ -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); } diff --git a/test/api/models/UserTest.php b/test/api/models/UserTest.php index e92f0b1..f178c76 100644 --- a/test/api/models/UserTest.php +++ b/test/api/models/UserTest.php @@ -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 === '');