FOSSology  4.4.0
Open Source License Compliance by Open Source Software
GroupControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  * SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
4  *
5  * SPDX-License-Identifier: GPL-2.0-only
6 */
13 
14 require_once dirname(__DIR__, 4) . '/lib/php/Plugin/FO_Plugin.php';
15 
16 
30 use Mockery as M;
31 use Slim\Psr7\Factory\StreamFactory;
32 use Slim\Psr7\Headers;
33 use Slim\Psr7\Request;
34 use Slim\Psr7\Uri;
35 
36 
41 class GroupControllerTest extends \PHPUnit\Framework\TestCase
42 {
43 
48  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
49 
55 
60  private $dbHelper;
61 
66  private $restHelper;
67 
72  private $adminPlugin;
73 
78  protected function setUp() : void
79  {
80  global $container;
81  $container = M::mock('ContainerBuilder');
82  $this->dbHelper = M::mock(DbHelper::class);
83  $this->restHelper = M::mock(RestHelper::class);
84  $this->userDao = M::mock(UserDao::class);
85  $this->adminPlugin = M::mock('AdminGroupUsers');
86 
87  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
88  $this->restHelper->shouldReceive('getUserDao')
89  ->andReturn($this->userDao);
90 
91  $this->restHelper->shouldReceive('getPlugin')
92  ->withArgs(array('group_manage_users'))->andReturn($this->adminPlugin);
93 
94  $container->shouldReceive('get')->withArgs(array(
95  'helper.restHelper'))->andReturn($this->restHelper);
96  $this->groupController = new GroupController($container);
97  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
98  $this->dbManager = M::mock(DbManager::class);
99  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
100  $this->streamFactory = new StreamFactory();
101  }
102 
103 
108  protected function tearDown() : void
109  {
110  $this->addToAssertionCount(
111  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
112  M::close();
113  }
114 
121  private function getResponseJson($response)
122  {
123  $response->getBody()->seek(0);
124  return json_decode($response->getBody()->getContents(), true);
125  }
126 
132  private function getGroupMembers($userIds)
133  {
134  $groupPermissions = array("NONE" => -1, UserDao::USER => 0,
135  UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
136 
137  $memberList = array();
138  foreach ($userIds as $userId) {
139  $key = array_rand($groupPermissions);
140  $userGroupMember = new UserGroupMember(new User($userId, "user$userId", "User $userId",
141  null, null, null, null, null),$groupPermissions[$key]) ;
142  $memberList[] = $userGroupMember->getArray();
143  }
144  return $memberList;
145  }
146 
152  private function getUsersWithGroup($userIds)
153  {
154  $groupPermissions = array("NONE" => -1, UserDao::USER => 0,
155  UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
156 
157  $usersWithGroup = array();
158  foreach ($userIds as $userId) {
159  $perm = array_rand($groupPermissions);
160  $user = [
161  "user_pk" => $userId,
162  "group_perm"=> $perm,
163  "user_name" => $userId."username",
164  "user_desc" => $userId."desc",
165  "user_status"=> 'active'
166  ];
167  $usersWithGroup[] = $user;
168  }
169  return $usersWithGroup;
170  }
171 
172 
173 
179  public function testDeleteGroup()
180  {
181  $groupId = 4;
182  $userId = 1;
183  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
184  $this->dbHelper->shouldReceive('doesIdExist')
185  ->withArgs(["groups", "group_pk", $groupId])->andReturn(true);
186  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
187  $this->userDao->shouldReceive('getDeletableAdminGroupMap')->withArgs([$userId,$_SESSION[Auth::USER_LEVEL]]);
188  $this->userDao->shouldReceive('deleteGroup')->withArgs([$groupId]);
189 
190  $info = new Info(202, "User Group will be deleted", InfoType::INFO);
191  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
192  $info->getCode());
193  $actualResponse = $this->groupController->deleteGroup(null, new ResponseHelper(),
194  ['id' => $groupId]);
195 
196  $this->assertEquals($expectedResponse->getStatusCode(),
197  $actualResponse->getStatusCode());
198  $this->assertEquals($this->getResponseJson($expectedResponse),
199  $this->getResponseJson($actualResponse));
200  }
201 
207  public function testGetDeletableGroups()
208  {
209  $userId = 2;
210  $groupList = array();
211  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
212  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
213  $this->userDao->shouldReceive('getDeletableAdminGroupMap')->withArgs([$userId,
214  $_SESSION[Auth::USER_LEVEL]])->andReturn([]);
215  $expectedResponse = (new ResponseHelper())->withJson($groupList, 200);
216  $actualResponse = $this->groupController->getDeletableGroups(null, new ResponseHelper(), []);
217  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
218  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
219  }
225  public function testGetGroupMembers()
226  {
227  $userIds = [2];
228  $groupId = 1;
229  $memberList = $this->getGroupMembers($userIds);
230  $this->restHelper->shouldReceive('getUserId')->andReturn($userIds[0]);
231  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
232  $this->userDao->shouldReceive('getAdminGroupMap')->withArgs([$userIds[0],$_SESSION[Auth::USER_LEVEL]])->andReturn([1]);
233 
234  $this->dbManager->shouldReceive('prepare')->withArgs([M::any(),M::any()]);
235  $this->dbManager->shouldReceive('execute')->withArgs([M::any(),array($groupId)])->andReturn(1);
236  $this->dbManager->shouldReceive('fetchAll')->withArgs([1])->andReturn($this->getUsersWithGroup($userIds));
237  $this->dbManager->shouldReceive('freeResult')->withArgs([1]);
238 
239  $user = $this->getUsersWithGroup($userIds)[0];
240  $users = [];
241  $users[] = new User($user["user_pk"], $user["user_name"], $user["user_desc"],
242  null, null, null, null, null);
243  $this->dbHelper->shouldReceive("getUsers")->withArgs([$user['user_pk']])->andReturn($users);
244 
245  $expectedResponse = (new ResponseHelper())->withJson($memberList, 200);
246 
247  $actualResponse = $this->groupController->getGroupMembers(null, new ResponseHelper(), ['id' => $groupId]);
248  $this->assertEquals($expectedResponse->getStatusCode(),$actualResponse->getStatusCode());
249  }
250 
251 
260  public function testAddMemberUserNotMember()
261  {
262  $groupId = 1;
263  $newuser = 1;
264  $newPerm = 2;
265  $emptyArr=[];
266  $userId = 1;
267 
268  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
269  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["groups", "group_pk", $groupId])->andReturn(true);
270  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["users","user_pk",$newuser])->andReturn(true);
271  $this->dbManager->shouldReceive('getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
272  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
273  $this->userDao->shouldReceive('isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(true);
274 
275  $this->dbManager->shouldReceive('prepare')->withArgs([M::any(),M::any()]);
276  $this->dbManager->shouldReceive('execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
277  $this->dbManager->shouldReceive('freeResult')->withArgs([1]);
278 
279 
280  $body = $this->streamFactory->createStream(json_encode([
281  "perm" => $newPerm
282  ]));
283  $requestHeaders = new Headers();
284  $requestHeaders->setHeader('Content-Type', 'application/json');
285  $request = new Request("POST", new Uri("HTTP", "localhost"),
286  $requestHeaders, [], [], $body);
287 
288  $expectedResponse = new Info(200, "User will be added to group.", InfoType::INFO);
289 
290  $actualResponse = $this->groupController->addMember($request, new ResponseHelper(), ['id' => $groupId,'userId' => $newuser]);
291  $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
292  $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
293  }
294 
301  public function testAddMemberUserNotAdmin()
302  {
303  $groupId = 1;
304  $newuser = 1;
305  $newPerm = 2;
306  $userId = 1;
307 
308  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
309  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["groups", "group_pk", $groupId])->andReturn(true);
310  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["users","user_pk",$newuser])->andReturn(true);
311  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
312  $this->userDao->shouldReceive('isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(false);
313 
314  $body = $this->streamFactory->createStream(json_encode([
315  "perm" => $newPerm
316  ]));
317  $requestHeaders = new Headers();
318  $requestHeaders->setHeader('Content-Type', 'application/json');
319  $request = new Request("POST", new Uri("HTTP", "localhost"),
320  $requestHeaders, [], [], $body);
321 
322  $this->expectException(HttpForbiddenException::class);
323 
324  $this->groupController->addMember($request, new ResponseHelper(),
325  ['id' => $groupId,'userId' => $newuser]);
326  }
327 
334  public function testAddMemberUserGroupAdmin()
335  {
336  $groupId = 1;
337  $newuser = 1;
338  $newPerm = 2;
339  $emptyArr=[];
340  $userId = 1;
341 
342  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
343  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["groups", "group_pk", $groupId])->andReturn(true);
344  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["users","user_pk",$newuser])->andReturn(true);
345  $this->dbManager->shouldReceive('getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
346  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
347  $this->userDao->shouldReceive('isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(true);
348 
349  $this->dbManager->shouldReceive('prepare')->withArgs([M::any(),M::any()]);
350  $this->dbManager->shouldReceive('execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
351  $this->dbManager->shouldReceive('freeResult')->withArgs([1]);
352 
353  $body = $this->streamFactory->createStream(json_encode([
354  "perm" => $newPerm
355  ]));
356  $requestHeaders = new Headers();
357  $requestHeaders->setHeader('Content-Type', 'application/json');
358  $request = new Request("POST", new Uri("HTTP", "localhost"),
359  $requestHeaders, [], [], $body);
360 
361  $expectedResponse = new Info(200, "User will be added to group.", InfoType::INFO);
362 
363  $actualResponse = $this->groupController->addMember($request, new ResponseHelper(), ['id' => $groupId,'userId' => $newuser]);
364  $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
365  $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
366  }
367 
368 
378  {
379  $groupId = 1;
380  $newuser = 1;
381  $newPerm = 2;
382  $userId = 1;
383 
384  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
385  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["groups", "group_pk", $groupId])->andReturn(true);
386  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["users","user_pk",$newuser])->andReturn(true);
387  $this->dbManager->shouldReceive('getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn(true);
388  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
389  $this->userDao->shouldReceive('isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(true);
390 
391  $body = $this->streamFactory->createStream(json_encode([
392  "perm" => $newPerm
393  ]));
394  $requestHeaders = new Headers();
395  $requestHeaders->setHeader('Content-Type', 'application/json');
396  $request = new Request("POST", new Uri("HTTP", "localhost"),
397  $requestHeaders, [], [], $body);
398 
399  $this->expectException(HttpBadRequestException::class);
400 
401  $this->groupController->addMember($request, new ResponseHelper(),
402  ['id' => $groupId,'userId' => $newuser]);
403  }
409  public function testChangeUserPermission()
410  {
411  $groupIds = [1,2,3,4,5,6];
412  $userId = 1;
413  $group_user_member_pk = 1;
414  $newPerm = 2;
415  $userPk = 1;
416 
417  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
418  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["groups", "group_pk", $groupIds[0]])->andReturn(true);
419  $this->dbHelper->shouldReceive('doesIdExist')->withArgs(["users","user_pk",$userId])->andReturn(true);
420  $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]);
421  $this->restHelper->shouldReceive('getUserId')->andReturn($userPk);
422  $this->userDao->shouldReceive('isAdvisorOrAdmin')->withArgs([$userPk, $groupIds[0]])->andReturn(true);
423 
424  $this->adminPlugin->shouldReceive('updateGUMPermission')->withArgs([$group_user_member_pk,$newPerm, $this->dbManager ]);
425 
426  $body = $this->streamFactory->createStream(json_encode([
427  "perm" => $newPerm
428  ]));
429  $requestHeaders = new Headers();
430  $requestHeaders->setHeader('Content-Type', 'application/json');
431  $request = new Request("POST", new Uri("HTTP", "localhost"),
432  $requestHeaders, [], [], $body);
433 
434  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
435  $expectedResponse = new Info(202, "Permission updated successfully.", InfoType::INFO);
436 
437  $actualResponse = $this->groupController->changeUserPermission($request, new ResponseHelper(), ['id' => $groupIds[0],'userId' => $userId]);
438  $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
439  $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
440  }
441 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
Model to hold user information.
Definition: User.php:21
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16