FOSSology  4.7.1
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  'spdx2csv',
52  'readmeoss',
53  'unifiedreport',
54  'clixml',
55  'decisionexporter',
56  'cyclonedx',
57  'spdx3json',
58  'spdx3rdf',
59  'spdx3jsonld',
60  'spdx3tv'
61  );
62 
68 
73  private $uploadDao;
74 
79  private $userId;
80 
85  private $groupId;
86 
91  private $spdxPlugin;
92 
98 
103  private $clixmlPlugin;
104 
109  private $unifiedPlugin;
110 
116 
122 
128 
133  private $spdx3Plugin;
134 
139  private $dbManager;
140 
146 
151  private $streamFactory;
152 
157  protected function setUp() : void
158  {
159  global $container;
160  $this->userId = 2;
161  $this->groupId = 2;
162  $container = M::mock('Psr\Container\ContainerInterface');
163  $this->dbHelper = M::mock(DbHelper::class);
164  $this->dbManager = M::mock(DbManager::class);
165  $this->restHelper = M::mock(RestHelper::class);
166  $this->uploadDao = M::mock(UploadDao::class);
167  $this->spdxPlugin = M::mock('SpdxTwoGeneratorUi');
168  $this->readmeossPlugin = M::mock('ReadMeOssPlugin');
169  $this->clixmlPlugin = M::mock('CliXmlGeneratorUi');
170  $this->unifiedPlugin = M::mock('FoUnifiedReportGenerator');
171  $this->decisionExporterPlugin = M::mock('DecisionExporterAgentPlugin');
172  $this->cyclonedxPlugin = M::mock('CycloneDXGeneratorUi');
173  $this->spdx3Plugin = M::mock('SpdxThreeGeneratorUi');
174  $this->downloadPlugin = M::mock('ui_download');
175 
176  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
177 
178  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
179  $this->restHelper->shouldReceive('getUploadDao')
180  ->andReturn($this->uploadDao);
181  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
182  $this->restHelper->shouldReceive('getPlugin')
183  ->withArgs(array('ui_spdx2'))->andReturn($this->spdxPlugin);
184  $this->restHelper->shouldReceive('getPlugin')
185  ->withArgs(array('ui_readmeoss'))->andReturn($this->readmeossPlugin);
186  $this->restHelper->shouldReceive('getPlugin')
187  ->withArgs(array('ui_clixml'))->andReturn($this->clixmlPlugin);
188  $this->restHelper->shouldReceive('getPlugin')
189  ->withArgs(array('download'))->andReturn($this->downloadPlugin);
190  $this->restHelper->shouldReceive('getPlugin')
191  ->withArgs(array('agent_founifiedreport'))
192  ->andReturn($this->unifiedPlugin);
193  $this->restHelper->shouldReceive('getPlugin')
194  ->withArgs(['agent_fodecisionexporter'])->andReturn($this->decisionExporterPlugin);
195  $this->restHelper->shouldReceive('getPlugin')
196  ->withArgs(array('ui_cyclonedx'))->andReturn($this->cyclonedxPlugin);
197  $this->restHelper->shouldReceive('getPlugin')
198  ->withArgs(array('ui_spdx3'))->andReturn($this->spdx3Plugin);
199 
200  $container->shouldReceive('get')->withArgs(array(
201  'helper.restHelper'))->andReturn($this->restHelper);
202  $this->reportController = new ReportController($container);
203  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
204  $this->streamFactory = new StreamFactory();
205  }
206 
211  protected function tearDown() : void
212  {
213  $this->addToAssertionCount(
214  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
215  M::close();
216  }
217 
224  private function getResponseJson($response)
225  {
226  $response->getBody()->seek(0);
227  return json_decode($response->getBody()->getContents(), true);
228  }
229 
235  private function getUpload($id)
236  {
237  $filename = "";
238  $description = "";
239  $treeTableName = "uploadtree_a";
240  $timestamp = "";
241  switch ($id) {
242  case 2:
243  $filename = "top$id";
244  $timestamp = "01-01-2020";
245  break;
246  case 3:
247  $filename = "child$id";
248  $timestamp = "02-01-2020";
249  break;
250  case 4:
251  $filename = "child$id";
252  $timestamp = "03-01-2020";
253  break;
254  default:
255  return null;
256  }
257  return new Upload($id, $filename, $description, $treeTableName, $timestamp);
258  }
259 
266  private function getResponseForReport($uploadId, $reportFormat)
267  {
268  $GLOBALS["apiBasePath"] = "/repo/api/v1";
269  $requestHeaders = new Headers();
270  $requestHeaders->setHeader('uploadId', $uploadId);
271  $requestHeaders->setHeader('reportFormat', $reportFormat);
272  $body = $this->streamFactory->createStream();
273  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
274  "/repo/api/v1/report"), $requestHeaders, [], [], $body);
275  $response = new ResponseHelper();
276  return $this->reportController->getReport($request, $response, []);
277  }
278 
284  public function testGetReportAllFormats()
285  {
286  $uploadId = 3;
287  $upload = $this->getUpload($uploadId);
288 
289  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
290  $this->groupId])->andReturn(true);
291  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
292  ->andReturn($upload);
293  $this->spdxPlugin->shouldReceive('scheduleAgent')
294  ->withArgs([$this->groupId, $upload, M::anyOf('dep5', 'spdx2', 'spdx2tv', 'spdx2csv')])
295  ->andReturn([32, 33, ""]);
296  $this->readmeossPlugin->shouldReceive('scheduleAgent')
297  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
298  $this->unifiedPlugin->shouldReceive('scheduleAgent')
299  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
300  $this->clixmlPlugin->shouldReceive('scheduleAgent')
301  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
302  $this->decisionExporterPlugin->shouldReceive('scheduleAgent')
303  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
304  $this->cyclonedxPlugin->shouldReceive('scheduleAgent')
305  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
306  $this->spdx3Plugin->shouldReceive('scheduleAgent')
307  ->withArgs([$this->groupId, $upload, M::anyOf('spdx3json', 'spdx3rdf', 'spdx3jsonld', 'spdx3tv')])
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