FOSSology  4.6.0
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 
47  private $reportsAllowed = array(
48  'dep5',
49  'spdx2',
50  'spdx2tv',
51  'readmeoss',
52  'unifiedreport',
53  'clixml',
54  'decisionexporter',
55  'cyclonedx',
56  'spdx3json',
57  'spdx3rdf',
58  'spdx3jsonld'
59  );
60 
66 
71  private $uploadDao;
72 
77  private $userId;
78 
83  private $groupId;
84 
89  private $spdxPlugin;
90 
96 
101  private $clixmlPlugin;
102 
107  private $unifiedPlugin;
108 
114 
120 
126 
131  private $spdx3Plugin;
132 
137  private $dbManager;
138 
144 
149  private $streamFactory;
150 
155  protected function setUp() : void
156  {
157  global $container;
158  $this->userId = 2;
159  $this->groupId = 2;
160  $container = M::mock('Psr\Container\ContainerInterface');
161  $this->dbHelper = M::mock(DbHelper::class);
162  $this->dbManager = M::mock(DbManager::class);
163  $this->restHelper = M::mock(RestHelper::class);
164  $this->uploadDao = M::mock(UploadDao::class);
165  $this->spdxPlugin = M::mock('SpdxTwoGeneratorUi');
166  $this->readmeossPlugin = M::mock('ReadMeOssPlugin');
167  $this->clixmlPlugin = M::mock('CliXmlGeneratorUi');
168  $this->unifiedPlugin = M::mock('FoUnifiedReportGenerator');
169  $this->decisionExporterPlugin = M::mock('DecisionExporterAgentPlugin');
170  $this->cyclonedxPlugin = M::mock('CycloneDXGeneratorUi');
171  $this->spdx3Plugin = M::mock('SpdxThreeGeneratorUi');
172  $this->downloadPlugin = M::mock('ui_download');
173 
174  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
175 
176  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
177  $this->restHelper->shouldReceive('getUploadDao')
178  ->andReturn($this->uploadDao);
179  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
180  $this->restHelper->shouldReceive('getPlugin')
181  ->withArgs(array('ui_spdx2'))->andReturn($this->spdxPlugin);
182  $this->restHelper->shouldReceive('getPlugin')
183  ->withArgs(array('ui_readmeoss'))->andReturn($this->readmeossPlugin);
184  $this->restHelper->shouldReceive('getPlugin')
185  ->withArgs(array('ui_clixml'))->andReturn($this->clixmlPlugin);
186  $this->restHelper->shouldReceive('getPlugin')
187  ->withArgs(array('download'))->andReturn($this->downloadPlugin);
188  $this->restHelper->shouldReceive('getPlugin')
189  ->withArgs(array('agent_founifiedreport'))
190  ->andReturn($this->unifiedPlugin);
191  $this->restHelper->shouldReceive('getPlugin')
192  ->withArgs(['agent_fodecisionexporter'])->andReturn($this->decisionExporterPlugin);
193  $this->restHelper->shouldReceive('getPlugin')
194  ->withArgs(array('ui_cyclonedx'))->andReturn($this->cyclonedxPlugin);
195  $this->restHelper->shouldReceive('getPlugin')
196  ->withArgs(array('ui_spdx3'))->andReturn($this->spdx3Plugin);
197 
198  $container->shouldReceive('get')->withArgs(array(
199  'helper.restHelper'))->andReturn($this->restHelper);
200  $this->reportController = new ReportController($container);
201  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
202  $this->streamFactory = new StreamFactory();
203  }
204 
209  protected function tearDown() : void
210  {
211  $this->addToAssertionCount(
212  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
213  M::close();
214  }
215 
222  private function getResponseJson($response)
223  {
224  $response->getBody()->seek(0);
225  return json_decode($response->getBody()->getContents(), true);
226  }
227 
233  private function getUpload($id)
234  {
235  $filename = "";
236  $description = "";
237  $treeTableName = "uploadtree_a";
238  $timestamp = "";
239  switch ($id) {
240  case 2:
241  $filename = "top$id";
242  $timestamp = "01-01-2020";
243  break;
244  case 3:
245  $filename = "child$id";
246  $timestamp = "02-01-2020";
247  break;
248  case 4:
249  $filename = "child$id";
250  $timestamp = "03-01-2020";
251  break;
252  default:
253  return null;
254  }
255  return new Upload($id, $filename, $description, $treeTableName, $timestamp);
256  }
257 
264  private function getResponseForReport($uploadId, $reportFormat)
265  {
266  $GLOBALS["apiBasePath"] = "/repo/api/v1";
267  $requestHeaders = new Headers();
268  $requestHeaders->setHeader('uploadId', $uploadId);
269  $requestHeaders->setHeader('reportFormat', $reportFormat);
270  $body = $this->streamFactory->createStream();
271  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
272  "/repo/api/v1/report"), $requestHeaders, [], [], $body);
273  $response = new ResponseHelper();
274  return $this->reportController->getReport($request, $response, []);
275  }
276 
282  public function testGetReportAllFormats()
283  {
284  $uploadId = 3;
285  $upload = $this->getUpload($uploadId);
286 
287  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
288  $this->groupId])->andReturn(true);
289  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
290  ->andReturn($upload);
291  $this->spdxPlugin->shouldReceive('scheduleAgent')
292  ->withArgs([$this->groupId, $upload, M::anyOf($this->reportsAllowed[0],
293  $this->reportsAllowed[1], $this->reportsAllowed[2])])
294  ->andReturn([32, 33, ""]);
295  $this->readmeossPlugin->shouldReceive('scheduleAgent')
296  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
297  $this->unifiedPlugin->shouldReceive('scheduleAgent')
298  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
299  $this->clixmlPlugin->shouldReceive('scheduleAgent')
300  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
301  $this->decisionExporterPlugin->shouldReceive('scheduleAgent')
302  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
303  $this->cyclonedxPlugin->shouldReceive('scheduleAgent')
304  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
305  $this->spdx3Plugin->shouldReceive('scheduleAgent')
306  ->withArgs([$this->groupId, $upload, M::anyOf($this->reportsAllowed[8],
307  $this->reportsAllowed[9], $this->reportsAllowed[10])])
308  ->andReturn([32, 33, ""]);
309 
310  $expectedResponse = new Info(201, "http://localhost/repo/api/v1/report/32",
311  InfoType::INFO);
312 
313  foreach ($this->reportsAllowed as $reportFormat) {
314  $actualResponse = $this->getResponseForReport($uploadId, $reportFormat);
315  $this->assertEquals($expectedResponse->getArray(),
316  $this->getResponseJson($actualResponse));
317  $this->assertEquals($expectedResponse->getCode(),
318  $actualResponse->getStatusCode());
319  }
320  }
321 
327  public function testGetReportInvalidFormat()
328  {
329  $uploadId = 3;
330  $reportFormat = 'report';
331 
332  $this->expectException(HttpBadRequestException::class);
333 
334  $this->getResponseForReport($uploadId, $reportFormat);
335  }
336 
343  {
344  $uploadId = 4;
345  $reportFormat = $this->reportsAllowed[1];
346 
347  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
348  $this->groupId])->andReturn(false);
349 
350  $this->expectException(HttpForbiddenException::class);
351 
352  $this->getResponseForReport($uploadId, $reportFormat);
353  }
354 
360  public function testGetReportInvalidUpload()
361  {
362  $uploadId = 10;
363  $reportFormat = $this->reportsAllowed[1];
364  $upload = $this->getUpload($uploadId);
365 
366  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
367  $this->groupId])->andReturn(true);
368  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
369  ->andReturn($upload);
370 
371  $this->expectException(HttpNotFoundException::class);
372 
373  $this->getResponseForReport($uploadId, $reportFormat);
374  }
375 
385  public function testDownloadReport()
386  {
387  $reportId = 43;
388  $uploadId = 3;
389 
390  $this->dbManager->shouldReceive('getSingleRow')
391  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
392  [$reportId], "reportValidity"])
393  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
394  $this->dbManager->shouldReceive('getSingleRow')
395  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
396  [$reportId], "reportFileUpload"])
397  ->andReturn(["job_upload_fk" => $uploadId]);
398  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
399  $this->groupId])->andReturn(true);
400  $this->dbManager->shouldReceive('getSingleRow')
401  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
402  [$reportId], "reportFileName"])
403  ->andReturn(["job_upload_fk" => $uploadId]);
404 
405  $tmpfile = tempnam(sys_get_temp_dir(), "FOO");
406 
407  $handle = fopen($tmpfile, "w");
408  fwrite($handle, "writing to tempfile");
409  fclose($handle);
410 
411  $fileResponse = new BinaryFileResponse($tmpfile);
412  $fileResponse->headers->set('Content-Type', 'text/plain');
413  $fileResponse->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
414  $fileContent = $fileResponse->getFile();
415  $this->downloadPlugin->shouldReceive('getReport')->andReturn($fileResponse);
416 
417  $expectedResponse = new ResponseHelper();
418  $expectedResponse = $expectedResponse->withHeader('Content-Description',
419  'File Transfer')
420  ->withHeader('Content-Type', $fileResponse->headers->get('Content-Type'))
421  ->withHeader('Content-Disposition',
422  $fileResponse->headers->get('Content-Disposition'))
423  ->withHeader('Cache-Control', 'must-revalidate')
424  ->withHeader('Pragma', 'private')
425  ->withHeader('Content-Length', filesize($fileContent));
426 
427  $actualResponse = $this->reportController->downloadReport(null,
428  new ResponseHelper(), ["id" => $reportId]);
429 
430  $expectedResponse->getBody()->seek(0);
431  $this->assertEquals(file_get_contents($tmpfile),
432  $actualResponse->getBody()->getContents());
433  $this->assertEquals($expectedResponse->getHeaders(),
434  $actualResponse->getHeaders());
435  unlink($tmpfile);
436  }
437 
444  {
445  $reportId = 43;
446  $uploadId = 3;
447 
448  $this->dbManager->shouldReceive('getSingleRow')
449  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
450  [$reportId], "reportValidity"])
451  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
452  $this->dbManager->shouldReceive('getSingleRow')
453  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
454  [$reportId], "reportFileUpload"])
455  ->andReturn(["job_upload_fk" => $uploadId]);
456  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
457  $this->groupId])->andReturn(false);
458 
459  $this->expectException(HttpForbiddenException::class);
460 
461  $this->reportController->downloadReport(null, new ResponseHelper(),
462  ["id" => $reportId]);
463  }
464 
471  {
472  $reportId = 43;
473 
474  $this->dbManager->shouldReceive('getSingleRow')
475  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
476  [$reportId], "reportValidity"])
477  ->andReturn(["jq_type" => ""]);
478 
479  $this->expectException(HttpNotFoundException::class);
480 
481  $this->reportController->downloadReport(null, new ResponseHelper(),
482  ["id" => $reportId]);
483  }
484 
490  public function testDownloadReportTryLater()
491  {
492  $reportId = 43;
493  $uploadId = 3;
494 
495  $this->dbManager->shouldReceive('getSingleRow')
496  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
497  [$reportId], "reportValidity"])
498  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
499  $this->dbManager->shouldReceive('getSingleRow')
500  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
501  [$reportId], "reportFileUpload"])
502  ->andReturn(["job_upload_fk" => $uploadId]);
503  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
504  $this->groupId])->andReturn(true);
505  $this->dbManager->shouldReceive('getSingleRow')
506  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
507  [$reportId], "reportFileName"])
508  ->andReturn(false);
509 
510  $this->expectException(HttpServiceUnavailableException::class);
511 
512  $this->reportController->downloadReport(null, new ResponseHelper(),
513  ["id" => $reportId]);
514  }
515 }
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