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 testGetSpecificUser()
131  {
132  $userId = 2;
133  $userName = 'fossy';
134  $userArray = ['user_pk' => $userId];
135  $user = $this->getUsers([$userId]);
136  $request = M::mock(Request::class);
137  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
138  ->withArgs([$userName])->andReturn($userArray);
139  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
140  $this->dbHelper->shouldReceive('doesIdExist')
141  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
142  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
143  ->andReturn($user);
144  $expectedResponse = (new ResponseHelper())->withJson($user[0]->getArray(), 200);
145  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(),
146  ['pathParam' => $userId]);
147  $this->assertEquals($expectedResponse->getStatusCode(),
148  $actualResponse->getStatusCode());
149  $this->assertEquals($this->getResponseJson($expectedResponse),
150  $this->getResponseJson($actualResponse));
151  }
152 
158  public function testGetSpecificUserNotFound()
159  {
160  $userId = 6;
161  $request = M::mock(Request::class);
162  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
163  $this->dbHelper->shouldReceive('doesIdExist')
164  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
165  $this->expectException(HttpNotFoundException::class);
166 
167  $this->userController->getUsers($request, new ResponseHelper(),
168  ['pathParam' => $userId]);
169  }
170 
176  public function testGetAllUsers()
177  {
178  $users = $this->getUsers([2, 3, 4]);
179  $request = M::mock(Request::class);
180  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
181  $this->dbHelper->shouldReceive('getUsers')->withArgs([null])
182  ->andReturn($users);
183 
184  $allUsers = array();
185  foreach ($users as $user) {
186  $allUsers[] = $user->getArray();
187  }
188 
189  $expectedResponse = (new ResponseHelper())->withJson($allUsers, 200);
190  $actualResponse = $this->userController->getUsers($request, new ResponseHelper(), []);
191  $this->assertEquals($expectedResponse->getStatusCode(),
192  $actualResponse->getStatusCode());
193  $this->assertEquals($this->getResponseJson($expectedResponse),
194  $this->getResponseJson($actualResponse));
195  }
196 
202  public function testDeleteUser()
203  {
204  $userId = 4;
205  $userName = 'fossy';
206  $userArray = ['user_pk' => $userId];
207  $request = M::mock(Request::class);
208  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
209  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
210  ->withArgs([$userName])->andReturn($userArray);
211  $this->dbHelper->shouldReceive('doesIdExist')
212  ->withArgs(["users", "user_pk", $userId])->andReturn(true);
213  $this->dbHelper->shouldReceive('deleteUser')->withArgs([$userId]);
214  $info = new Info(202, "User will be deleted", InfoType::INFO);
215  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
216  $info->getCode());
217  $actualResponse = $this->userController->deleteUser($request, new ResponseHelper(),
218  ['pathParam' => $userId]);
219  $this->assertEquals($expectedResponse->getStatusCode(),
220  $actualResponse->getStatusCode());
221  $this->assertEquals($this->getResponseJson($expectedResponse),
222  $this->getResponseJson($actualResponse));
223  }
224 
230  public function testDeleteUserDoesNotExists()
231  {
232  $userId = 8;
233  $userName = 'fossy';
234  $userArray = ['user_pk' => $userId];
235  $request = M::mock(Request::class);
236  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
237  $this->restHelper->getUserDao()->shouldReceive('getUserByName')
238  ->withArgs([$userName])->andReturn($userArray);
239  $this->dbHelper->shouldReceive('doesIdExist')
240  ->withArgs(["users", "user_pk", $userId])->andReturn(false);
241  $this->expectException(HttpNotFoundException::class);
242 
243  $this->userController->deleteUser($request, new ResponseHelper(),
244  ['pathParam' => $userId]);
245  }
246 
252  public function testGetCurrentUser()
253  {
254  $userId = 2;
255  $user = $this->getUsers([$userId]);
256  $request = M::mock(Request::class);
257  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
258  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
259  $this->dbHelper->shouldReceive('getUsers')->withArgs([$userId])
260  ->andReturn($user);
261  $this->userDao->shouldReceive('getUserAndDefaultGroupByUserName')->withArgs([$user[0]->getArray()["name"]])
262  ->andReturn(["group_name" => "fossy"]);
263 
264  $expectedUser = $user[0]->getArray();
265  $expectedUser["default_group"] = "fossy";
266 
267  $expectedResponse = (new ResponseHelper())->withJson($expectedUser, 200);
268 
269  $actualResponse = $this->userController->getCurrentUser($request,
270  new ResponseHelper(), []);
271 
272  $this->assertEquals($expectedResponse->getStatusCode(),
273  $actualResponse->getStatusCode());
274  $this->assertEquals($this->getResponseJson($expectedResponse),
275  $this->getResponseJson($actualResponse));
276  }
277 }
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