FOSSology  4.8.0-rc1
Open Source License Compliance by Open Source Software
ReportControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020-2021 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
28 use Mockery as M;
29 use Slim\Psr7\Factory\StreamFactory;
30 use Slim\Psr7\Headers;
31 use Slim\Psr7\Request;
32 use Slim\Psr7\Uri;
33 use Symfony\Component\HttpFoundation\BinaryFileResponse;
34 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
35 
40 class ReportControllerTest extends \PHPUnit\Framework\TestCase
41 {
42  public $dbHelper;
43  public $restHelper;
44 
45 
50  private $reportsAllowed = array(
51  'dep5',
52  'spdx2',
53  'spdx2tv',
54  'spdx2csv',
55  'readmeoss',
56  'unifiedreport',
57  'clixml',
58  'decisionexporter',
59  'cyclonedx',
60  'spdx3json',
61  'spdx3rdf',
62  'spdx3jsonld',
63  'spdx3tv'
64  );
65 
71 
76  private $uploadDao;
77 
82  private $userId;
83 
88  private $groupId;
89 
94  private $spdxPlugin;
95 
101 
106  private $clixmlPlugin;
107 
112  private $unifiedPlugin;
113 
119 
125 
131 
136  private $spdx3Plugin;
137 
142  private $dbManager;
143 
149 
154  private $streamFactory;
155 
160  protected function setUp() : void
161  {
162  global $container;
163  $this->userId = 2;
164  $this->groupId = 2;
165  $container = M::mock('Psr\Container\ContainerInterface');
166  $this->dbHelper = M::mock(DbHelper::class);
167  $this->dbManager = M::mock(DbManager::class);
168  $this->restHelper = M::mock(RestHelper::class);
169  $this->uploadDao = M::mock(UploadDao::class);
170  $this->spdxPlugin = M::mock('SpdxTwoGeneratorUi');
171  $this->readmeossPlugin = M::mock('ReadMeOssPlugin');
172  $this->clixmlPlugin = M::mock('CliXmlGeneratorUi');
173  $this->unifiedPlugin = M::mock('FoUnifiedReportGenerator');
174  $this->decisionExporterPlugin = M::mock('DecisionExporterAgentPlugin');
175  $this->cyclonedxPlugin = M::mock('CycloneDXGeneratorUi');
176  $this->spdx3Plugin = M::mock('SpdxThreeGeneratorUi');
177  $this->downloadPlugin = M::mock('ui_download');
178 
179  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
180 
181  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
182  $this->restHelper->shouldReceive('getUploadDao')
183  ->andReturn($this->uploadDao);
184  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
185  $this->restHelper->shouldReceive('getPlugin')
186  ->withArgs(array('ui_spdx2'))->andReturn($this->spdxPlugin);
187  $this->restHelper->shouldReceive('getPlugin')
188  ->withArgs(array('ui_readmeoss'))->andReturn($this->readmeossPlugin);
189  $this->restHelper->shouldReceive('getPlugin')
190  ->withArgs(array('ui_clixml'))->andReturn($this->clixmlPlugin);
191  $this->restHelper->shouldReceive('getPlugin')
192  ->withArgs(array('download'))->andReturn($this->downloadPlugin);
193  $this->restHelper->shouldReceive('getPlugin')
194  ->withArgs(array('agent_founifiedreport'))
195  ->andReturn($this->unifiedPlugin);
196  $this->restHelper->shouldReceive('getPlugin')
197  ->withArgs(['agent_fodecisionexporter'])->andReturn($this->decisionExporterPlugin);
198  $this->restHelper->shouldReceive('getPlugin')
199  ->withArgs(array('ui_cyclonedx'))->andReturn($this->cyclonedxPlugin);
200  $this->restHelper->shouldReceive('getPlugin')
201  ->withArgs(array('ui_spdx3'))->andReturn($this->spdx3Plugin);
202 
203  $container->shouldReceive('get')->withArgs(array(
204  'helper.restHelper'))->andReturn($this->restHelper);
205  $this->reportController = new ReportController($container);
206  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
207  $this->streamFactory = new StreamFactory();
208  }
209 
214  protected function tearDown() : void
215  {
216  $this->addToAssertionCount(
217  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
218  M::close();
219  }
220 
227  private function getResponseJson($response)
228  {
229  $response->getBody()->seek(0);
230  return json_decode($response->getBody()->getContents(), true);
231  }
232 
238  private function getUpload($id)
239  {
240  $filename = "";
241  $description = "";
242  $treeTableName = "uploadtree_a";
243  $timestamp = "";
244  switch ($id) {
245  case 2:
246  $filename = "top$id";
247  $timestamp = "01-01-2020";
248  break;
249  case 3:
250  $filename = "child$id";
251  $timestamp = "02-01-2020";
252  break;
253  case 4:
254  $filename = "child$id";
255  $timestamp = "03-01-2020";
256  break;
257  default:
258  return null;
259  }
260  return new Upload($id, $filename, $description, $treeTableName, $timestamp);
261  }
262 
269  private function getResponseForReport($uploadId, $reportFormat)
270  {
271  $GLOBALS["apiBasePath"] = "/repo/api/v1";
272  $requestHeaders = new Headers();
273  $requestHeaders->setHeader('uploadId', $uploadId);
274  $requestHeaders->setHeader('reportFormat', $reportFormat);
275  $body = $this->streamFactory->createStream();
276  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
277  "/repo/api/v1/report"), $requestHeaders, [], [], $body);
278  $response = new ResponseHelper();
279  return $this->reportController->getReport($request, $response, []);
280  }
281 
287  public function testGetReportAllFormats()
288  {
289  $uploadId = 3;
290  $upload = $this->getUpload($uploadId);
291 
292  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
293  $this->groupId])->andReturn(true);
294  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
295  ->andReturn($upload);
296  $this->spdxPlugin->shouldReceive('scheduleAgent')
297  ->withArgs([$this->groupId, $upload, M::anyOf('dep5', 'spdx2', 'spdx2tv', 'spdx2csv')])
298  ->andReturn([32, 33, ""]);
299  $this->readmeossPlugin->shouldReceive('scheduleAgent')
300  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
301  $this->unifiedPlugin->shouldReceive('scheduleAgent')
302  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
303  $this->clixmlPlugin->shouldReceive('scheduleAgent')
304  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
305  $this->decisionExporterPlugin->shouldReceive('scheduleAgent')
306  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
307  $this->cyclonedxPlugin->shouldReceive('scheduleAgent')
308  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
309  $this->spdx3Plugin->shouldReceive('scheduleAgent')
310  ->withArgs([$this->groupId, $upload, M::anyOf('spdx3json', 'spdx3rdf', 'spdx3jsonld', 'spdx3tv')])
311  ->andReturn([32, 33, ""]);
312 
313  $expectedResponse = new Info(201, "http://localhost/repo/api/v1/report/32",
314  InfoType::INFO);
315 
316  foreach ($this->reportsAllowed as $reportFormat) {
317  $actualResponse = $this->getResponseForReport($uploadId, $reportFormat);
318  $this->assertEquals($expectedResponse->getArray(),
319  $this->getResponseJson($actualResponse));
320  $this->assertEquals($expectedResponse->getCode(),
321  $actualResponse->getStatusCode());
322  }
323  }
324 
330  public function testGetReportInvalidFormat()
331  {
332  $uploadId = 3;
333  $reportFormat = 'report';
334 
335  $this->expectException(HttpBadRequestException::class);
336 
337  $this->getResponseForReport($uploadId, $reportFormat);
338  }
339 
346  {
347  $uploadId = 4;
348  $reportFormat = $this->reportsAllowed[1];
349 
350  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
351  $this->groupId])->andReturn(false);
352 
353  $this->expectException(HttpForbiddenException::class);
354 
355  $this->getResponseForReport($uploadId, $reportFormat);
356  }
357 
363  public function testGetReportInvalidUpload()
364  {
365  $uploadId = 10;
366  $reportFormat = $this->reportsAllowed[1];
367  $upload = $this->getUpload($uploadId);
368 
369  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
370  $this->groupId])->andReturn(true);
371  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
372  ->andReturn($upload);
373 
374  $this->expectException(HttpNotFoundException::class);
375 
376  $this->getResponseForReport($uploadId, $reportFormat);
377  }
378 
388  public function testDownloadReport()
389  {
390  $reportId = 43;
391  $uploadId = 3;
392 
393  $this->dbManager->shouldReceive('getSingleRow')
394  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
395  [$reportId], "reportValidity"])
396  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
397  $this->dbManager->shouldReceive('getSingleRow')
398  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
399  [$reportId], "reportFileUpload"])
400  ->andReturn(["job_upload_fk" => $uploadId]);
401  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
402  $this->groupId])->andReturn(true);
403  $this->dbManager->shouldReceive('getSingleRow')
404  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
405  [$reportId], "reportFileName"])
406  ->andReturn(["job_upload_fk" => $uploadId]);
407 
408  $tmpfile = tempnam(sys_get_temp_dir(), "FOO");
409 
410  $handle = fopen($tmpfile, "w");
411  fwrite($handle, "writing to tempfile");
412  fclose($handle);
413 
414  $fileResponse = new BinaryFileResponse($tmpfile);
415  $fileResponse->headers->set('Content-Type', 'text/plain');
416  $fileResponse->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
417  $fileContent = $fileResponse->getFile();
418  $this->downloadPlugin->shouldReceive('getReport')->andReturn($fileResponse);
419 
420  $expectedResponse = new ResponseHelper();
421  $expectedResponse = $expectedResponse->withHeader('Content-Description',
422  'File Transfer')
423  ->withHeader('Content-Type', $fileResponse->headers->get('Content-Type'))
424  ->withHeader('Content-Disposition',
425  $fileResponse->headers->get('Content-Disposition'))
426  ->withHeader('Cache-Control', 'must-revalidate')
427  ->withHeader('Pragma', 'private')
428  ->withHeader('Content-Length', filesize($fileContent));
429 
430  $actualResponse = $this->reportController->downloadReport(null,
431  new ResponseHelper(), ["id" => $reportId]);
432 
433  $expectedResponse->getBody()->seek(0);
434  $this->assertEquals(file_get_contents($tmpfile),
435  $actualResponse->getBody()->getContents());
436  $this->assertEquals($expectedResponse->getHeaders(),
437  $actualResponse->getHeaders());
438  unlink($tmpfile);
439  }
440 
447  {
448  $reportId = 43;
449  $uploadId = 3;
450 
451  $this->dbManager->shouldReceive('getSingleRow')
452  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
453  [$reportId], "reportValidity"])
454  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
455  $this->dbManager->shouldReceive('getSingleRow')
456  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
457  [$reportId], "reportFileUpload"])
458  ->andReturn(["job_upload_fk" => $uploadId]);
459  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
460  $this->groupId])->andReturn(false);
461 
462  $this->expectException(HttpForbiddenException::class);
463 
464  $this->reportController->downloadReport(null, new ResponseHelper(),
465  ["id" => $reportId]);
466  }
467 
474  {
475  $reportId = 43;
476 
477  $this->dbManager->shouldReceive('getSingleRow')
478  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
479  [$reportId], "reportValidity"])
480  ->andReturn(["jq_type" => ""]);
481 
482  $this->expectException(HttpNotFoundException::class);
483 
484  $this->reportController->downloadReport(null, new ResponseHelper(),
485  ["id" => $reportId]);
486  }
487 
493  public function testDownloadReportTryLater()
494  {
495  $reportId = 43;
496  $uploadId = 3;
497 
498  $this->dbManager->shouldReceive('getSingleRow')
499  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
500  [$reportId], "reportValidity"])
501  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
502  $this->dbManager->shouldReceive('getSingleRow')
503  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
504  [$reportId], "reportFileUpload"])
505  ->andReturn(["job_upload_fk" => $uploadId]);
506  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
507  $this->groupId])->andReturn(true);
508  $this->dbManager->shouldReceive('getSingleRow')
509  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
510  [$reportId], "reportFileName"])
511  ->andReturn(false);
512 
513  $this->expectException(HttpServiceUnavailableException::class);
514 
515  $this->reportController->downloadReport(null, new ResponseHelper(),
516  ["id" => $reportId]);
517  }
518 }
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
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16