FOSSology  4.7.1
Open Source License Compliance by Open Source Software
JobControllerTest.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 
26 use Mockery as M;
27 use Slim\Psr7\Factory\StreamFactory;
28 use Slim\Psr7\Headers;
29 use Slim\Psr7\Request;
30 use Slim\Psr7\Uri;
31 
32 require_once dirname(__DIR__, 4) . "/lib/php/Plugin/FO_Plugin.php";
33 
38 class JobControllerTest extends \PHPUnit\Framework\TestCase
39 {
44  private $dbHelper;
45 
50  private $restHelper;
51 
56  private $jobDao;
57 
62  private $showJobsDao;
63 
68  private $uploadDao;
69 
74  private $groupId;
75 
80  private $jobController;
81 
87 
92  private $streamFactory;
93 
98  protected function setUp() : void
99  {
100  global $container;
101  $container = M::mock('ContainerBuilder');
102  $this->dbHelper = M::mock(DbHelper::class);
103  $this->restHelper = M::mock(RestHelper::class);
104  $this->jobDao = M::mock(JobDao::class);
105  $this->showJobsDao = M::mock(ShowJobsDao::class);
106  $this->uploadDao = M::mock(UploadDao::class);
107  $this->groupId = 2;
108 
109  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
110  $this->restHelper->shouldReceive('getJobDao')->andReturn($this->jobDao);
111  $this->restHelper->shouldReceive('getShowJobDao')->andReturn($this->showJobsDao);
112  $this->restHelper->shouldReceive('getUploadDao')->andReturn($this->uploadDao);
113  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
114 
115  $container->shouldReceive('get')->withArgs(array(
116  'helper.restHelper'))->andReturn($this->restHelper);
117  $this->jobController = new JobController($container);
118  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
119  $this->streamFactory = new StreamFactory();
120  }
121 
126  protected function tearDown() : void
127  {
128  $this->addToAssertionCount(
129  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
130  M::close();
131  }
132 
133 
140  private function getResponseJson($response)
141  {
142  $response->getBody()->seek(0);
143  return json_decode($response->getBody()->getContents(), true);
144  }
145 
151  private function getUsers($userIds)
152  {
153  $userArray = array();
154  foreach ($userIds as $userId) {
155  if ($userId == 2) {
156  $accessLevel = PLUGIN_DB_ADMIN;
157  } elseif ($userId > 2 && $userId <= 4) {
158  $accessLevel = PLUGIN_DB_WRITE;
159  } elseif ($userId == 5) {
160  $accessLevel = PLUGIN_DB_READ;
161  } else {
162  continue;
163  }
164  $user = new User($userId, "user$userId", "User $userId",
165  "user$userId@example.com", $accessLevel, 2, 4, "");
166  $userArray[] = $user->getArray();
167  }
168  return $userArray;
169  }
170 
176  public function testGetJobs()
177  {
178  $job = new Job(11, "job_name", "01-01-2020", 4, 2, 2, 0, "Completed");
179  $jobQueue = new JobQueue(44, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
180  'Completed', 0, null, [], 0, true, false, true,
181  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
182  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(11))
183  ->andReturn(['44' => 0]);
184  $this->showJobsDao->shouldReceive('getEstimatedTime')
185  ->withArgs(array(11, '', 0, 4))->andReturn("0");
186  $this->showJobsDao->shouldReceive('getDataForASingleJob')
187  ->withArgs(array(44))->andReturn(["jq_endtext"=>'Completed']);
188 
189  $requestHeaders = new Headers();
190  $body = $this->streamFactory->createStream();
191  $request = new Request("GET", new Uri("HTTP", "localhost"),
192  $requestHeaders, [], [], $body);
193  $response = new ResponseHelper();
194  $userId = 2;
195  $user = $this->getUsers([$userId]);
196  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
197  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(2, NULL, 'ASC', 0, 1))
198  ->andReturn([[$job], 1]);
199  $actualResponse = $this->jobController->getJobs($request, $response, []);
200  $expectedResponse = $job->getArray(ApiVersion::V1);
201  $this->assertEquals(200, $actualResponse->getStatusCode());
202  $this->assertEquals($expectedResponse,
203  $this->getResponseJson($actualResponse)[0]);
204  $this->assertEquals('1',
205  $actualResponse->getHeaderLine('X-Total-Pages'));
206  }
207 
213  public function testGetJobsLimitPage()
214  {
215  $jobTwo = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
216  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
217  'Completed', 0, null, [], 0, true, false, true,
218  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
219  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(11))
220  ->andReturn(['44' => 0]);
221  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
222  ->andReturn(['45' => 0]);
223  $this->showJobsDao->shouldReceive('getEstimatedTime')
224  ->withArgs(array(M::anyOf(11, 12), '', 0, M::anyOf(4, 5)))->andReturn("0");
225  $this->showJobsDao->shouldReceive('getDataForASingleJob')
226  ->withArgs([M::anyOf(44, 45)])->andReturn(["jq_endtext"=>'Completed']);
227 
228  $requestHeaders = new Headers();
229  $requestHeaders->setHeader('limit', '1');
230  $requestHeaders->setHeader('page', '2');
231  $body = $this->streamFactory->createStream();
232  $request = new Request("GET", new Uri("HTTP", "localhost"),
233  $requestHeaders, [], [], $body);
234  $response = new ResponseHelper();
235  $userId = 2;
236  $user = $this->getUsers([$userId]);
237  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
238  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(2, null, "ASC", 1, 2))
239  ->andReturn([[$jobTwo], 2]);
240  $actualResponse = $this->jobController->getJobs($request, $response, []);
241  $expectedResponse = $jobTwo->getArray(ApiVersion::V1);
242  $this->assertEquals(200, $actualResponse->getStatusCode());
243  $this->assertEquals($expectedResponse,
244  $this->getResponseJson($actualResponse)[0]);
245  $this->assertEquals('2',
246  $actualResponse->getHeaderLine('X-Total-Pages'));
247  }
248 
254  public function testGetInvalidJob()
255  {
256  $this->dbHelper->shouldReceive('doesIdExist')
257  ->withArgs(["job", "job_pk", 2])->andReturn(false);
258 
259  $requestHeaders = new Headers();
260  $requestHeaders->setHeader('limit', '1');
261  $requestHeaders->setHeader('page', '2');
262  $body = $this->streamFactory->createStream();
263  $request = new Request("GET", new Uri("HTTP", "localhost"),
264  $requestHeaders, [], [], $body);
265  $response = new ResponseHelper();
266  $userId = 2;
267  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
268  $this->expectException(HttpNotFoundException::class);
269 
270  $this->jobController->getJobs($request, $response, ["id" => 2]);
271  }
272 
278  public function testGetJobFromId()
279  {
280  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
281  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
282  'Completed', 0, null, [], 0, true, false, true,
283  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
284  $this->dbHelper->shouldReceive('doesIdExist')
285  ->withArgs(["job", "job_pk", 12])->andReturn(true);
286  $this->dbHelper->shouldReceive('doesIdExist')
287  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
288  $this->uploadDao->shouldReceive('isAccessible')
289  ->withArgs([5, $this->groupId])->andReturn(true);
290  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(12, NULL, 'ASC', 0, 1, NULL))
291  ->andReturn([[$job], 1]);
292  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
293  ->andReturn(['45' => 0]);
294  $this->showJobsDao->shouldReceive('getEstimatedTime')
295  ->withArgs(array(12, '', 0, 5))->andReturn("0");
296  $this->showJobsDao->shouldReceive('getDataForASingleJob')
297  ->withArgs([45])->andReturn(["jq_endtext"=>'Completed']);
298 
299  $requestHeaders = new Headers();
300  $body = $this->streamFactory->createStream();
301  $request = new Request("GET", new Uri("HTTP", "localhost"),
302  $requestHeaders, [], [], $body);
303  $response = new ResponseHelper();
304  $userId = 2;
305  $user = $this->getUsers([$userId]);
306  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
307  $actualResponse = $this->jobController->getJobs($request, $response, [
308  "id" => 12]);
309  $expectedResponse = $job->getArray(ApiVersion::V1);
310  $this->assertEquals(200, $actualResponse->getStatusCode());
311  $this->assertEquals($expectedResponse,
312  $this->getResponseJson($actualResponse));
313  $this->assertEquals('1',
314  $actualResponse->getHeaderLine('X-Total-Pages'));
315  }
316 
325  {
326  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
327  $this->dbHelper->shouldReceive('doesIdExist')
328  ->withArgs(["job", "job_pk", 12])->andReturn(true);
329  $this->dbHelper->shouldReceive('doesIdExist')
330  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
331  $this->uploadDao->shouldReceive('isAccessible')
332  ->withArgs([5, $this->groupId])->andReturn(false);
333  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(12, NULL, 'ASC', 0, 1, NULL))
334  ->andReturn([[$job], 1]);
335 
336  $requestHeaders = new Headers();
337  $body = $this->streamFactory->createStream();
338  $request = new Request("GET", new Uri("HTTP", "localhost"),
339  $requestHeaders, [], [], $body);
340  $response = new ResponseHelper();
341  $userId = 3;
342  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
343  $this->expectException(HttpForbiddenException::class);
344 
345  /* showJobsDao and the ajaxShowJobs plugin are deliberately left
346  * unmocked here: if getAllResults() ever proceeds past the upload
347  * accessibility check, building the job queue details would hit an
348  * unexpected Mockery call and fail the test for the wrong reason. */
349  $this->jobController->getJobs($request, $response, ["id" => 12]);
350  }
351 
357  public function testGetJobsFromUpload()
358  {
359  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
360  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
361  'Completed', 0, null, [], 0, true, false, true,
362  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
363  $this->dbHelper->shouldReceive('doesIdExist')
364  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
365  $this->uploadDao->shouldReceive('isAccessible')
366  ->withArgs([5, $this->groupId])->andReturn(true);
367  $this->dbHelper->shouldReceive('doesIdExist')
368  ->withArgs(['job', 'job_pk', 12])->andReturn(true);
369  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(null, null, "ASC", 0, 1, 5))
370  ->andReturn([[$job], 1]);
371  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
372  ->andReturn(['45' => 0]);
373  $this->showJobsDao->shouldReceive('getEstimatedTime')
374  ->withArgs(array(12, '', 0, 5))->andReturn("0");
375  $this->showJobsDao->shouldReceive('getDataForASingleJob')
376  ->withArgs([45])->andReturn(["jq_endtext"=>'Completed']);
377 
378  $requestHeaders = new Headers();
379  $body = $this->streamFactory->createStream();
380  $request = new Request("GET", new Uri("HTTP", "localhost"),
381  $requestHeaders, [], [], $body);
382  $request = $request->withQueryParams([JobController::UPLOAD_PARAM => 5]);
383  $response = new ResponseHelper();
384  $userId = 2;
385  $user = $this->getUsers([$userId]);
386  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
387  $actualResponse = $this->jobController->getJobs($request, $response, []);
388  $expectedResponse = $job->getArray(ApiVersion::V1);
389  $this->assertEquals(200, $actualResponse->getStatusCode());
390  $this->assertEquals($expectedResponse,
391  $this->getResponseJson($actualResponse)[0]);
392  $this->assertEquals('1',
393  $actualResponse->getHeaderLine('X-Total-Pages'));
394  }
395 
404  {
405  $this->dbHelper->shouldReceive('doesIdExist')
406  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
407  $this->uploadDao->shouldReceive('isAccessible')
408  ->withArgs([5, $this->groupId])->andReturn(false);
409  $this->dbHelper->shouldNotReceive('getJobs');
410 
411  $requestHeaders = new Headers();
412  $body = $this->streamFactory->createStream();
413  $request = new Request("GET", new Uri("HTTP", "localhost"),
414  $requestHeaders, [], [], $body);
415  $request = $request->withQueryParams([JobController::UPLOAD_PARAM => 5]);
416  $response = new ResponseHelper();
417  $userId = 3;
418  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
419  $this->expectException(HttpForbiddenException::class);
420 
421  $this->jobController->getJobs($request, $response, []);
422  }
423 
432  {
433  $this->dbHelper->shouldReceive('doesIdExist')
434  ->withArgs(["upload", "upload_pk", 99])->andReturn(false);
435  $this->dbHelper->shouldNotReceive('getJobs');
436 
437  $requestHeaders = new Headers();
438  $body = $this->streamFactory->createStream();
439  $request = new Request("GET", new Uri("HTTP", "localhost"),
440  $requestHeaders, [], [], $body);
441  $request = $request->withQueryParams([JobController::UPLOAD_PARAM => 99]);
442  $response = new ResponseHelper();
443  $userId = 3;
444  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
445  $this->expectException(HttpNotFoundException::class);
446 
447  $this->jobController->getJobs($request, $response, []);
448  }
449 
456  public function testGetUploadEtaInSeconds()
457  {
458  $jobId = 11;
459  $uploadId = 5;
460  $completedJob = 5;
461  $completedUpload = 3;
462  $this->showJobsDao->shouldReceive('getEstimatedTime')
463  ->withArgs([$jobId, '', 0, $uploadId])
464  ->andReturn("3:10:23");
465  $this->showJobsDao->shouldReceive('getEstimatedTime')
466  ->withArgs([$completedJob, '', 0, $completedUpload])
467  ->andReturn("0");
468  $reflection = new \ReflectionClass(get_class($this->jobController));
469  $method = $reflection->getMethod('getUploadEtaInSeconds');
470  $method->setAccessible(true);
471 
472  $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
473  $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
474 
475  $result = $method->invokeArgs($this->jobController,
476  [$completedJob, $completedUpload]);
477  $this->assertEquals(0, $result);
478  }
479 
480 }
Override Slim response for withJson function.
Model class to hold JobQueue info.
Definition: JobQueue.php:18
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