FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DbHelperTest.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 if (session_status() == PHP_SESSION_NONE) {
16  session_start();
17 }
18 
19 require_once dirname(dirname(dirname(dirname(__DIR__)))) .
20  '/lib/php/Plugin/FO_Plugin.php';
21 
23 use Mockery as M;
30 
35 class DbHelperTest extends \PHPUnit\Framework\TestCase
36 {
42 
47  private $dbManager;
48 
53  private $dbHelper;
54 
59  private $folderDao;
60 
65  private $uploadDao;
66 
71  protected function setUp() : void
72  {
73  global $container;
74  $restHelper = M::mock(RestHelper::class);
75  $container = M::mock('ContainerBuilder');
76  $this->dbManager = M::mock(ModernDbManager::class);
77  $this->folderDao = M::mock(FolderDao::class);
78  $this->uploadDao = M::mock(UploadDao::class);
79 
80  $this->dbHelper = new DbHelper($this->dbManager, $this->folderDao,
81  $this->uploadDao);
82  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
83  $container->shouldReceive('get')->withArgs(array(
84  'helper.restHelper'))->andReturn($restHelper);
85  }
86 
91  protected function tearDown() : void
92  {
93  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = -1;
94  $_SESSION[Auth::USER_LEVEL] = -1;
95  $this->addToAssertionCount(
96  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
97  M::close();
98  }
99 
104  private function generateUserRow()
105  {
106  $agent_list = [];
107  $agent_list[2] = 'nomos,monk';
108  $agent_list[3] = 'nomos,ojo';
109  $agent_list[4] = 'copyright,ecc';
110 
111  $perm_list = [];
112  $perm_list[2] = PLUGIN_DB_ADMIN;
113  $perm_list[3] = PLUGIN_DB_WRITE;
114  $perm_list[4] = PLUGIN_DB_READ;
115 
116  $userRows = [];
117  for ($i = 2; $i <= 4; $i++) {
118  $row = [
119  'user_pk' => $i, 'user_name' => "user$i", 'user_desc' => "user $i",
120  'user_email' => "user$i@local", 'email_notify' => 'y',
121  'root_folder_fk' => 2, 'group_fk' => 2, 'user_perm' => $perm_list[$i],
122  'user_agent_list' => $agent_list[$i], 'default_bucketpool_fk' => 2
123  ];
124  $userRows[$i] = $row;
125  }
126  return $userRows;
127  }
128 
136  private function generateUserFromRow($rows, $currentUser = null)
137  {
138  $users = [];
139  foreach ($rows as $row) {
140  if ($currentUser === null || $row['user_pk'] == $currentUser) {
141  $user = new User($row["user_pk"], $row["user_name"], $row["user_desc"],
142  $row["user_email"], $row["user_perm"], $row["root_folder_fk"],
143  $row["email_notify"], $row["user_agent_list"], $row['group_fk'], $row["default_bucketpool_fk"]);
144  } else {
145  $user = new User($row["user_pk"], $row["user_name"], $row["user_desc"],
146  null, null, null, null, null, null);
147  }
148  $users[$row["user_pk"]] = $user->getArray();
149  }
150  return $users;
151  }
152 
158  public function testGetUsersAll()
159  {
160  $userId = 2;
161 
162  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = $userId;
163  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
164 
165  $sql = 'SELECT user_pk, user_name, user_desc, user_email,
166  email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, ' .
167  'default_bucketpool_fk FROM users;';
168  $statement = DbHelper::class . "::getUsers.getAllUsers";
169  $userRows = $this->generateUserRow();
170 
171  $this->dbManager->shouldReceive('getRows')
172  ->withArgs([$sql, [], $statement])
173  ->once()
174  ->andReturn($userRows);
175 
176  $expectedUsers = array_values($this->generateUserFromRow($userRows));
177  $actualUsers = $this->dbHelper->getUsers();
178 
179  $allUsers = array();
180  foreach ($actualUsers as $user) {
181  $allUsers[] = $user->getArray();
182  }
183 
184  $this->assertEquals($expectedUsers, $allUsers);
185  }
186 
192  public function testGetUsersAllNonAdmin()
193  {
194  $userId = 3;
195 
196  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = $userId;
197  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
198 
199  $sql = 'SELECT user_pk, user_name, user_desc, user_email,
200  email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, ' .
201  'default_bucketpool_fk FROM users;';
202  $statement = DbHelper::class . "::getUsers.getAllUsers";
203  $userRows = $this->generateUserRow();
204 
205  $this->dbManager->shouldReceive('getRows')
206  ->withArgs([$sql, [], $statement])
207  ->once()
208  ->andReturn($userRows);
209 
210  $expectedUsers = array_values($this->generateUserFromRow($userRows,
211  $userId));
212  $actualUsers = $this->dbHelper->getUsers();
213  $allUsers = array();
214  foreach ($actualUsers as $user) {
215  $allUsers[] = $user->getArray();
216  }
217  $this->assertEquals($expectedUsers, $allUsers);
218  }
219 
225  public function testGetUsersSingleUserAdmin()
226  {
227  $userId = 2;
228 
229  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = $userId;
230  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
231 
232  $sql = "SELECT user_pk, user_name, user_desc, user_email,
233  email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, default_bucketpool_fk FROM users
234  WHERE user_pk = $1;";
235  $statement = DbHelper::class . "::getUsers.getSpecificUser";
236  $userRows = $this->generateUserRow();
237 
238  $this->dbManager->shouldReceive('getRows')
239  ->withArgs([$sql, [$userId], $statement])
240  ->once()
241  ->andReturn([$userRows[$userId]]);
242 
243  $expectedUsers = $this->generateUserFromRow($userRows, $userId);
244  $actualUsers = $this->dbHelper->getUsers($userId);
245 
246  $allUsers = array();
247  foreach ($actualUsers as $user) {
248  $allUsers[] = $user->getArray();
249  }
250  $this->assertEquals([$expectedUsers[$userId]], $allUsers);
251  }
252 
259  {
260  $userId = 3;
261  $fetchId = 4;
262 
263  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = $userId;
264  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
265 
266  $sql = "SELECT user_pk, user_name, user_desc, user_email,
267  email_notify, root_folder_fk, group_fk, user_perm, user_agent_list, default_bucketpool_fk FROM users
268  WHERE user_pk = $1;";
269  $statement = DbHelper::class . "::getUsers.getSpecificUser";
270  $userRows = $this->generateUserRow();
271 
272  $this->dbManager->shouldReceive('getRows')
273  ->withArgs([$sql, [$fetchId], $statement])
274  ->once()
275  ->andReturn([$userRows[$fetchId]]);
276 
277  $expectedUsers = $this->generateUserFromRow($userRows, $userId);
278  $actualUsers = $this->dbHelper->getUsers($fetchId);
279 
280  $allUsers = array();
281  foreach ($actualUsers as $user) {
282  $allUsers[] = $user->getArray();
283  }
284  $this->assertEquals([$expectedUsers[$fetchId]], $allUsers);
285  }
286 }
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
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Model to hold user information.
Definition: User.php:21
generateUserFromRow($rows, $currentUser=null)
#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
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16