FOSSology  4.4.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 
18 use Mockery as M;
28 use Slim\Psr7\Request;
29 
34 class UserControllerTest extends \PHPUnit\Framework\TestCase
35 {
36 
42 
47  private $dbHelper;
48 
53  private $restHelper;
54 
59  protected function setUp() : void
60  {
61  global $container;
62  $container = M::mock('ContainerBuilder');
63  $this->dbHelper = M::mock(DbHelper::class);
64  $this->restHelper = M::mock(RestHelper::class);
65  $this->userDao = M::mock(UserDao::class);
66 
67  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
68  $this->restHelper->shouldReceive('getUserDao')
69  ->andReturn($this->userDao);
70 
71  $container->shouldReceive('get')->withArgs(array(
72  'helper.restHelper'))->andReturn($this->restHelper);
73  $this->userController = new UserController($container);
74  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
75  }
76 
81  protected function tearDown() : void
82  {
83  $this->addToAssertionCount(
84  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
85  M::close();
86  }
87 
94  private function getResponseJson($response)
95  {
96  $response->getBody()->seek(0);
97  return json_decode($response->getBody()->getContents(), true);
98  }
99 
105  private function getUsers($userIds)
106  {
107  $userArray = array();
108  foreach ($userIds as $userId) {
109  if ($userId == 2) {
110  $accessLevel = PLUGIN_DB_ADMIN;
111  } elseif ($userId > 2 && $userId <= 4) {
112  $accessLevel = PLUGIN_DB_WRITE;
113  } elseif ($userId == 5) {
114  $accessLevel = PLUGIN_DB_READ;
115  } else {
116  continue;
117  }
118  $user = new User($userId, "user$userId", "User $userId",
119  "user$userId@example.com", $accessLevel, 2, 4, "");
120  $userArray[] = $user;
121  }
122  return $userArray;
123  }
124 
130  public function testGetSpecificUserV1()
131  {
132  $this->testGetSpecificUser(ApiVersion::V1);
133  }
139  public function testGetSpecificUserV2()
140  {
141  $this->testGetSpecificUser();
142  }
147  private function testGetSpecificUser($version = ApiVersion::V2)
148  {
149  $userId = 2;
150  $userName = 'fossy';
151  $userArray = ['user_pk' => $userId];
152  $user = $this->getUsers([$userId]);
153  if ($version == ApiVersion::V2) {
154  $userArray = ['user_pk' => $userId];
155  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
156  ->withArgs([$userId])->andReturn($userArray);
157  }
158  $request = M::mock(Request::class);
159  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
160  ->withArgs([$userName])->andReturn($userArray);
161  $request->shouldReceive('getAttribute')->andReturn($version);
162  $this->dbHelper->shouldReceive('doesIdExist')
163  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
164  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
165  ->andReturn($user);
166  $expectedResponse = (new ResponseHelper())->withJson($user[0]->getArray($version), 200);
167  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(),
168  ['pathParam' => $userId]);
169  $this->assertEquals($expectedResponse->getStatusCode(),
170  $actualResponse->getStatusCode());
171  $this->assertEquals($this->getResponseJson($expectedResponse),
172  $this->getResponseJson($actualResponse));
173  }
174 
181  {
182  $this->testGetSpecificUserNotFound(ApiVersion::V1);
183  }
190  {
192  }
197  private function testGetSpecificUserNotFound($version = ApiVersion::V2)
198  {
199  $userId = 6;
200  $request = M::mock(Request::class);
201  if ($version == ApiVersion::V2) {
202  $userArray = ['user_pk' => $userId];
203  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
204  ->withArgs([$userId])->andReturn($userArray);
205  }
206  $request->shouldReceive('getAttribute')->andReturn($version);
207  $this->dbHelper->shouldReceive('doesIdExist')
208  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
209  $this->expectException(HttpNotFoundException::class);
210 
211  $this->userController->getUsers($request, new ResponseHelper(),
212  ['pathParam' => $userId]);
213  }
214 
220  public function testGetAllUsersV1()
221  {
222  $this->testGetAllUsers(ApiVersion::V1);
223  }
229  public function testGetAllUsersV2()
230  {
231  $this->testGetAllUsers();
232  }
237  private function testGetAllUsers($version = ApiVersion::V2)
238  {
239  $userId = 2;
240  $users = $this->getUsers([2, 3, 4]);
241  if ($version == ApiVersion::V2) {
242  $userArray = ['user_pk' => $userId];
243  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
244  ->withArgs([$userId])->andReturn($userArray);
245  }
246  $request = M::mock(Request::class);
247  $request->shouldReceive('getAttribute')->andReturn($version);
248  $this->dbHelper->shouldReceive('getUsers')->withArgs([null])
249  ->andReturn($users);
250 
251  $allUsers = array();
252  foreach ($users as $user) {
253  $allUsers[] = $user->getArray($version);
254  }
255 
256  $expectedResponse = (new ResponseHelper())->withJson($allUsers, 200);
257  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(), []);
258  $this->assertEquals($expectedResponse->getStatusCode(),
259  $actualResponse->getStatusCode());
260  $this->assertEquals($this->getResponseJson($expectedResponse),
261  $this->getResponseJson($actualResponse));
262  }
263 
269  public function testDeleteUserV1()
270  {
271  $this->testDeleteUser(ApiVersion::V1);
272  }
278  public function testDeleteUserV2()
279  {
280  $this->testDeleteUser();
281  }
286  private function testDeleteUser($version = ApiVersion::V2)
287  {
288  $userId = 4;
289  $userArray = ['user_pk' => $userId];
290  $request = M::mock(Request::class);
291  $request->shouldReceive('getAttribute')->andReturn($version);
292  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
293  ->withArgs([$userId])->andReturn($userArray);
294  $this->dbHelper->shouldReceive('doesIdExist')
295  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
296  $this->dbHelper->shouldReceive('deleteUser')->withArgs([$userId]);
297  $info = new Info(202, "User will be deleted", InfoType::INFO);
298  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
299  $info->getCode());
300  $actualResponse = $this->userController->deleteUser($request, new ResponseHelper(),
301  ['pathParam' => $userId]);
302  $this->assertEquals($expectedResponse->getStatusCode(),
303  $actualResponse->getStatusCode());
304  $this->assertEquals($this->getResponseJson($expectedResponse),
305  $this->getResponseJson($actualResponse));
306  }
307 
314  {
315  $this->testDeleteUserDoesNotExists(ApiVersion::V1);
316  }
323  {
325  }
330  private function testDeleteUserDoesNotExists($version = ApiVersion::V2)
331  {
332  $userId = 8;
333  $userArray = ['user_pk' => $userId];
334  $request = M::mock(Request::class);
335  $request->shouldReceive('getAttribute')->andReturn($version);
336  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
337  ->withArgs([$userId])->andReturn($userArray);
338  $this->dbHelper->shouldReceive('doesIdExist')
339  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
340  $this->expectException(HttpNotFoundException::class);
341 
342  $this->userController->deleteUser($request, new ResponseHelper(),
343  ['pathParam' => $userId]);
344  }
345 
351  public function testGetCurrentUserV1()
352  {
353  $this->testGetCurrentUser(ApiVersion::V1);
354  }
360  public function testGetCurrentUserV2()
361  {
362  $this->testGetCurrentUser();
363  }
368  private function testGetCurrentUser($version = ApiVersion::V2)
369  {
370  $userId = 2;
371  $user = $this->getUsers([$userId]);
372  $request = M::mock(Request::class);
373  $request->shouldReceive('getAttribute')->andReturn($version);
374  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
375  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
376  ->andReturn($user);
377  $this->userDao->shouldReceive('getUserAndDefaultGroupByUserName')->withArgs([$user[0]->getArray()["name"]])
378  ->andReturn(["group_name" => "fossy"]);
379 
380  $expectedUser = $user[0]->getArray($version);
381  if ($version == ApiVersion::V1) {
382  $expectedUser["default_group"] = "fossy";
383  }
384  $expectedResponse = (new ResponseHelper())->withJson($expectedUser, 200);
385 
386  $actualResponse = $this->userController->getCurrentUser($request,
387  new ResponseHelper(), []);
388  $this->assertEquals($expectedResponse->getStatusCode(),
389  $actualResponse->getStatusCode());
390  $this->assertEquals($this->getResponseJson($expectedResponse),
391  $this->getResponseJson($actualResponse));
392  }
393 }
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