FOSSology  4.6.0
Open Source License Compliance by Open Source Software
UserControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
15 require_once dirname(__DIR__, 4) . '/lib/php/Plugin/FO_Plugin.php';
16 
19 use Mockery as M;
29 use Slim\Psr7\Request;
30 
35 class UserControllerTest extends \PHPUnit\Framework\TestCase
36 {
37 
43 
48  private $dbHelper;
49 
54  private $restHelper;
55 
60  protected function setUp() : void
61  {
62  global $container;
63  $container = M::mock('ContainerBuilder');
64  $this->dbHelper = M::mock(DbHelper::class);
65  $this->restHelper = M::mock(RestHelper::class);
66  $this->userDao = M::mock(UserDao::class);
67 
68  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
69  $this->restHelper->shouldReceive('getUserDao')
70  ->andReturn($this->userDao);
71 
72  $container->shouldReceive('get')->withArgs(array(
73  'helper.restHelper'))->andReturn($this->restHelper);
74  $this->userController = new UserController($container);
75  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
77  }
78 
83  protected function tearDown() : void
84  {
85  $this->addToAssertionCount(
86  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
87  M::close();
88  unset($_SESSION[Auth::USER_LEVEL]);
89  }
90 
97  private function getResponseJson($response)
98  {
99  $response->getBody()->seek(0);
100  return json_decode($response->getBody()->getContents(), true);
101  }
102 
108  private function getUsers($userIds)
109  {
110  $userArray = array();
111  foreach ($userIds as $userId) {
112  if ($userId == 2) {
113  $accessLevel = PLUGIN_DB_ADMIN;
114  } elseif ($userId > 2 && $userId <= 4) {
115  $accessLevel = PLUGIN_DB_WRITE;
116  } elseif ($userId == 5) {
117  $accessLevel = PLUGIN_DB_READ;
118  } else {
119  continue;
120  }
121  $user = new User($userId, "user$userId", "User $userId",
122  "user$userId@example.com", $accessLevel, 2, 4, "");
123  $userArray[] = $user;
124  }
125  return $userArray;
126  }
127 
133  public function testGetSpecificUserV1()
134  {
135  $this->testGetSpecificUser(ApiVersion::V1);
136  }
142  public function testGetSpecificUserV2()
143  {
144  $this->testGetSpecificUser();
145  }
150  private function testGetSpecificUser($version = ApiVersion::V2)
151  {
152  $userId = 2;
153  $userName = 'fossy';
154  $userArray = ['user_pk' => $userId];
155  $user = $this->getUsers([$userId]);
156  if ($version == ApiVersion::V2) {
157  $userArray = ['user_pk' => $userId];
158  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
159  ->withArgs([$userId])->andReturn($userArray);
160  }
161  $request = M::mock(Request::class);
162  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
163  ->withArgs([$userName])->andReturn($userArray);
164  $request->shouldReceive('getAttribute')->andReturn($version);
165  $this->dbHelper->shouldReceive('doesIdExist')
166  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
167  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
168  ->andReturn($user);
169  $expectedResponse = (new ResponseHelper())->withJson($user[0]->getArray($version), 200);
170  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(),
171  ['pathParam' => $userId]);
172  $this->assertEquals($expectedResponse->getStatusCode(),
173  $actualResponse->getStatusCode());
174  $this->assertEquals($this->getResponseJson($expectedResponse),
175  $this->getResponseJson($actualResponse));
176  }
177 
184  {
185  $this->testGetSpecificUserNotFound(ApiVersion::V1);
186  }
193  {
195  }
200  private function testGetSpecificUserNotFound($version = ApiVersion::V2)
201  {
202  $userId = 6;
203  $request = M::mock(Request::class);
204  if ($version == ApiVersion::V2) {
205  $userArray = ['user_pk' => $userId];
206  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
207  ->withArgs([$userId])->andReturn($userArray);
208  }
209  $request->shouldReceive('getAttribute')->andReturn($version);
210  $this->dbHelper->shouldReceive('doesIdExist')
211  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
212  $this->expectException(HttpNotFoundException::class);
213 
214  $this->userController->getUsers($request, new ResponseHelper(),
215  ['pathParam' => $userId]);
216  }
217 
223  public function testGetAllUsersV1()
224  {
225  $this->testGetAllUsers(ApiVersion::V1);
226  }
232  public function testGetAllUsersV2()
233  {
234  $this->testGetAllUsers();
235  }
240  private function testGetAllUsers($version = ApiVersion::V2)
241  {
242  $userId = 2;
243  $users = $this->getUsers([2, 3, 4]);
244  if ($version == ApiVersion::V2) {
245  $userArray = ['user_pk' => $userId];
246  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
247  ->withArgs([$userId])->andReturn($userArray);
248  }
249  $request = M::mock(Request::class);
250  $request->shouldReceive('getAttribute')->andReturn($version);
251  $this->dbHelper->shouldReceive('getUsers')->withArgs([null])
252  ->andReturn($users);
253 
254  $allUsers = array();
255  foreach ($users as $user) {
256  $allUsers[] = $user->getArray($version);
257  }
258 
259  $expectedResponse = (new ResponseHelper())->withJson($allUsers, 200);
260  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(), []);
261  $this->assertEquals($expectedResponse->getStatusCode(),
262  $actualResponse->getStatusCode());
263  $this->assertEquals($this->getResponseJson($expectedResponse),
264  $this->getResponseJson($actualResponse));
265  }
266 
272  public function testDeleteUserV1()
273  {
274  $this->testDeleteUser(ApiVersion::V1);
275  }
281  public function testDeleteUserV2()
282  {
283  $this->testDeleteUser();
284  }
289  private function testDeleteUser($version = ApiVersion::V2)
290  {
291  $userId = 4;
292  $userArray = ['user_pk' => $userId];
293  $request = M::mock(Request::class);
294  $request->shouldReceive('getAttribute')->andReturn($version);
295  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
296  ->withArgs([$userId])->andReturn($userArray);
297  $this->dbHelper->shouldReceive('doesIdExist')
298  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
299  $this->dbHelper->shouldReceive('deleteUser')->withArgs([$userId]);
300  $info = new Info(202, "User will be deleted", InfoType::INFO);
301  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
302  $info->getCode());
303  $actualResponse = $this->userController->deleteUser($request, new ResponseHelper(),
304  ['pathParam' => $userId]);
305  $this->assertEquals($expectedResponse->getStatusCode(),
306  $actualResponse->getStatusCode());
307  $this->assertEquals($this->getResponseJson($expectedResponse),
308  $this->getResponseJson($actualResponse));
309  }
310 
317  {
318  $this->testDeleteUserDoesNotExists(ApiVersion::V1);
319  }
326  {
328  }
333  private function testDeleteUserDoesNotExists($version = ApiVersion::V2)
334  {
335  $userId = 8;
336  $userArray = ['user_pk' => $userId];
337  $request = M::mock(Request::class);
338  $request->shouldReceive('getAttribute')->andReturn($version);
339  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
340  ->withArgs([$userId])->andReturn($userArray);
341  $this->dbHelper->shouldReceive('doesIdExist')
342  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
343  $this->expectException(HttpNotFoundException::class);
344 
345  $this->userController->deleteUser($request, new ResponseHelper(),
346  ['pathParam' => $userId]);
347  }
348 
354  public function testGetCurrentUserV1()
355  {
356  $this->testGetCurrentUser(ApiVersion::V1);
357  }
363  public function testGetCurrentUserV2()
364  {
365  $this->testGetCurrentUser();
366  }
371  private function testGetCurrentUser($version = ApiVersion::V2)
372  {
373  $userId = 2;
374  $user = $this->getUsers([$userId]);
375  $request = M::mock(Request::class);
376  $request->shouldReceive('getAttribute')->andReturn($version);
377  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
378  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
379  ->andReturn($user);
380  $this->userDao->shouldReceive('getUserAndDefaultGroupByUserName')->withArgs([$user[0]->getArray()["name"]])
381  ->andReturn(["group_name" => "fossy"]);
382 
383  $expectedUser = $user[0]->getArray($version);
384  if ($version == ApiVersion::V1) {
385  $expectedUser["default_group"] = "fossy";
386  }
387  $expectedResponse = (new ResponseHelper())->withJson($expectedUser, 200);
388 
389  $actualResponse = $this->userController->getCurrentUser($request,
390  new ResponseHelper(), []);
391  $this->assertEquals($expectedResponse->getStatusCode(),
392  $actualResponse->getStatusCode());
393  $this->assertEquals($this->getResponseJson($expectedResponse),
394  $this->getResponseJson($actualResponse));
395  }
396 }
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
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39