14 require_once dirname(__DIR__, 4) .
'/lib/php/Plugin/FO_Plugin.php';
32 use Slim\Psr7\Factory\StreamFactory;
33 use Slim\Psr7\Headers;
34 use Slim\Psr7\Request;
49 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
79 protected function setUp() : void
82 $container = M::mock(
'ContainerBuilder');
83 $this->dbHelper = M::mock(DbHelper::class);
84 $this->restHelper = M::mock(RestHelper::class);
85 $this->userDao = M::mock(UserDao::class);
86 $this->adminPlugin = M::mock(
'AdminGroupUsers');
88 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
89 $this->restHelper->shouldReceive(
'getUserDao')
90 ->andReturn($this->userDao);
92 $this->restHelper->shouldReceive(
'getPlugin')
93 ->withArgs(array(
'group_manage_users'))->andReturn($this->adminPlugin);
95 $container->shouldReceive(
'get')->withArgs(array(
96 'helper.restHelper'))->andReturn($this->restHelper);
98 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
99 $this->
dbManager = M::mock(DbManager::class);
100 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
101 $this->streamFactory =
new StreamFactory();
111 $this->addToAssertionCount(
112 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
124 $response->getBody()->seek(0);
125 return json_decode($response->getBody()->getContents(),
true);
135 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
136 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
138 $memberList = array();
139 foreach ($userIds as $userId) {
140 $key = array_rand($groupPermissions);
142 null,
null,
null,
null,
null),$groupPermissions[$key]) ;
143 $memberList[] = $userGroupMember->getArray();
155 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
156 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
158 $usersWithGroup = array();
159 foreach ($userIds as $userId) {
160 $perm = array_rand($groupPermissions);
162 "user_pk" => $userId,
163 "group_perm"=> $perm,
164 "user_name" => $userId.
"username",
165 "user_desc" => $userId.
"desc",
166 "user_status"=>
'active'
168 $usersWithGroup[] = $user;
170 return $usersWithGroup;
200 $request = M::mock(Request::class);
201 $userArray = [
'user_pk' => $newUser];
204 if ($version == ApiVersion::V2) {
205 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupId])->andReturn($groupId);
206 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userPk])->andReturn($userArray);
208 $request->shouldReceive(
'getAttribute')->andReturn($version);
209 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
210 $this->dbHelper->shouldReceive(
'doesIdExist')
211 ->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
213 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,$_SESSION[
Auth::USER_LEVEL]]);
214 $this->userDao->shouldReceive(
'deleteGroup')->withArgs([$groupId]);
216 $info =
new Info(202,
"User Group will be deleted", InfoType::INFO);
217 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
219 $actualResponse = $this->groupController->deleteGroup($request,
new ResponseHelper(),
220 [
'pathParam' => $groupId]);
222 $this->assertEquals($expectedResponse->getStatusCode(),
223 $actualResponse->getStatusCode());
252 $groupList = array();
253 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
255 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,
257 $expectedResponse = (
new ResponseHelper())->withJson($groupList, 200);
258 $actualResponse = $this->groupController->getDeletableGroups(
null,
new ResponseHelper(), []);
259 $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
287 $groupName =
'fossy';
292 $request = M::mock(Request::class);
293 $groupIds = [1,2,3,4,5,6];
294 $userArray = [
'user_pk' => $newuser];
297 if ($version == ApiVersion::V2) {
298 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupIds[0]);
299 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userPk])->andReturn($userArray);
301 $request->shouldReceive(
'getAttribute')->andReturn($version);
302 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
303 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userIds[0]);
304 $this->userDao->shouldReceive(
'getAdminGroupMap')->withArgs([$userIds[0],$_SESSION[
Auth::USER_LEVEL]])->andReturn([1]);
306 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
307 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId)])->andReturn(1);
309 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
313 $users[] =
new User($user[
"user_pk"], $user[
"user_name"], $user[
"user_desc"],
314 null,
null,
null,
null,
null);
315 $this->dbHelper->shouldReceive(
"getUsers")->withArgs([$user[
'user_pk']])->andReturn($users);
317 $expectedResponse = (
new ResponseHelper())->withJson($memberList, 200);
319 $actualResponse = $this->groupController->getGroupMembers($request,
new ResponseHelper(), [
'pathParam' => $groupId]);
320 $this->assertEquals($expectedResponse->getStatusCode(),$actualResponse->getStatusCode());
356 $groupIds = [1,2,3,4,5,6];
357 $userArray = [
'user_pk' => $newuser];
361 if ($version == ApiVersion::V2) {
362 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupId);
363 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userId])->andReturn($userArray);
365 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
366 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
367 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
368 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
369 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
371 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
372 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
373 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
375 $body = $this->streamFactory->createStream(json_encode([
378 $requestHeaders =
new Headers();
379 $requestHeaders->setHeader(
'Content-Type',
'application/json');
380 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
381 $requestHeaders, [], [], $body);
382 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
383 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
385 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
386 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
387 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
418 $groupIds = [1,2,3,4,5,6];
419 $userArray = [
'user_pk' => $newuser];
423 if ($version == ApiVersion::V2) {
424 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupId);
425 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userId])->andReturn($userArray);
427 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
428 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
429 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
430 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
false);
432 $body = $this->streamFactory->createStream(json_encode([
435 $requestHeaders =
new Headers();
436 $requestHeaders->setHeader(
'Content-Type',
'application/json');
437 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
438 $requestHeaders, [], [], $body);
439 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
440 $this->expectException(HttpForbiddenException::class);
443 [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
454 $this->testAddMemberUserGroupAdmin(ApiVersion::V1);
464 $this->testAddMemberUserGroupAdmin();
466 private function testAddMemberUserGroupAdmin($version = ApiVersion::V2)
472 $groupIds = [1,2,3,4,5,6];
473 $userArray = [
'user_pk' => $newuser];
477 if ($version == ApiVersion::V2) {
478 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupId);
479 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userId])->andReturn($userArray);
481 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
482 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
483 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
484 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
485 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
487 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
488 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
489 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
491 $body = $this->streamFactory->createStream(json_encode([
494 $requestHeaders =
new Headers();
495 $requestHeaders->setHeader(
'Content-Type',
'application/json');
496 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
497 $requestHeaders, [], [], $body);
498 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
499 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
501 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
502 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
503 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
540 $groupIds = [1,2,3,4,5,6];
541 $userArray = [
'user_pk' => $newuser];
545 if ($version == ApiVersion::V2) {
546 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupId);
547 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userId])->andReturn($userArray);
549 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
550 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
551 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn(
true);
552 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
553 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
555 $body = $this->streamFactory->createStream(json_encode([
558 $requestHeaders =
new Headers();
559 $requestHeaders->setHeader(
'Content-Type',
'application/json');
560 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
561 $requestHeaders, [], [], $body);
562 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
563 $this->expectException(HttpBadRequestException::class);
566 [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
593 $group_user_member_pk = 1;
597 $groupIds = [1,2,3,4,5,6];
598 $userArray = [
'user_pk' => $userPk];
602 if ($version == ApiVersion::V2) {
603 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupIds[0]])->andReturn($groupId);
604 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userId])->andReturn($userArray);
606 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupIds[0]])->andReturn(
true);
607 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$userPk])->andReturn(
true);
608 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn([
'group_pk'=>$groupIds[0],
'group_user_member_pk'=>$group_user_member_pk,
'permission'=>$newPerm]);
609 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
610 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userPk, $groupIds[0]])->andReturn(
true);
611 $this->userDao->shouldReceive(
'getUserByName')->withArgs([M::any(),M::any()]);
613 $this->adminPlugin->shouldReceive(
'updateGUMPermission')->withArgs([$group_user_member_pk,$newPerm, $this->
dbManager ]);
615 $body = $this->streamFactory->createStream(json_encode([
618 $requestHeaders =
new Headers();
619 $requestHeaders->setHeader(
'Content-Type',
'application/json');
620 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
621 $requestHeaders, [], [], $body);
622 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
623 $expectedResponse =
new Info(202,
"Permission updated successfully.", InfoType::INFO);
625 $actualResponse = $this->groupController->changeUserPermission($request,
new ResponseHelper(), [
'pathParam' => $groupIds[0],
'userPathParam' => $userId]);
626 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
627 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
Contains the constants and helpers for authentication of user.
Controller for Group model.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Different type of infos provided by REST.
Info model to contain general error and return values.
Model to hold user information.
Tests for GroupController.
setUp()
Setup test objects.
testAddMemberUserNotAdminV1()
testChangeUserPermissionV2()
tearDown()
Remove test objects.
testGetDeletableGroupsV1()
testChangeUserPermissionV1()
getResponseJson($response)
testChangeUserPermission($version=ApiVersion::V2)
testAddMemberUserAlreadyMember($version=ApiVersion::V2)
testAddMemberUserAlreadyMemberV1()
testAddMemberUserNotMember($version=ApiVersion::V2)
testGetGroupMembers($version=ApiVersion::V2)
testDeleteGroup($version=ApiVersion::V2)
testGetDeletableGroups($version=ApiVersion::V2)
testAddMemberUserNotMemberV1()
getUsersWithGroup($userIds)
getGroupMembers($userIds)
testAddMemberUserNotAdmin($version=ApiVersion::V2)
testAddMemberUserNotMemberV2()
testAddMemberUserNotAdminV2()
testAddMemberUserGroupAdminV1()
testGetDeletableGroupsV2()
testAddMemberUserAlreadyMemberV2()
testAddMemberUserGroupAdminV2()
fo_dbManager * dbManager
fo_dbManager object