FOSSology  4.4.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  );
56 
62 
67  private $uploadDao;
68 
73  private $userId;
74 
79  private $groupId;
80 
85  private $spdxPlugin;
86 
92 
97  private $clixmlPlugin;
98 
103  private $unifiedPlugin;
104 
110 
116 
121  private $dbManager;
122 
128 
133  private $streamFactory;
134 
139  protected function setUp() : void
140  {
141  global $container;
142  $this->userId = 2;
143  $this->groupId = 2;
144  $container = M::mock('Psr\Container\ContainerInterface');
145  $this->dbHelper = M::mock(DbHelper::class);
146  $this->dbManager = M::mock(DbManager::class);
147  $this->restHelper = M::mock(RestHelper::class);
148  $this->uploadDao = M::mock(UploadDao::class);
149  $this->spdxPlugin = M::mock('SpdxTwoGeneratorUi');
150  $this->readmeossPlugin = M::mock('ReadMeOssPlugin');
151  $this->clixmlPlugin = M::mock('CliXmlGeneratorUi');
152  $this->unifiedPlugin = M::mock('FoUnifiedReportGenerator');
153  $this->decisionExporterPlugin = M::mock('DecisionExporterAgentPlugin');
154  $this->downloadPlugin = M::mock('ui_download');
155 
156  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
157 
158  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
159  $this->restHelper->shouldReceive('getUploadDao')
160  ->andReturn($this->uploadDao);
161  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
162  $this->restHelper->shouldReceive('getPlugin')
163  ->withArgs(array('ui_spdx2'))->andReturn($this->spdxPlugin);
164  $this->restHelper->shouldReceive('getPlugin')
165  ->withArgs(array('ui_readmeoss'))->andReturn($this->readmeossPlugin);
166  $this->restHelper->shouldReceive('getPlugin')
167  ->withArgs(array('ui_clixml'))->andReturn($this->clixmlPlugin);
168  $this->restHelper->shouldReceive('getPlugin')
169  ->withArgs(array('download'))->andReturn($this->downloadPlugin);
170  $this->restHelper->shouldReceive('getPlugin')
171  ->withArgs(array('agent_founifiedreport'))
172  ->andReturn($this->unifiedPlugin);
173  $this->restHelper->shouldReceive('getPlugin')
174  ->withArgs(['agent_fodecisionexporter'])->andReturn($this->decisionExporterPlugin);
175 
176  $container->shouldReceive('get')->withArgs(array(
177  'helper.restHelper'))->andReturn($this->restHelper);
178  $this->reportController = new ReportController($container);
179  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
180  $this->streamFactory = new StreamFactory();
181  }
182 
187  protected function tearDown() : void
188  {
189  $this->addToAssertionCount(
190  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
191  M::close();
192  }
193 
200  private function getResponseJson($response)
201  {
202  $response->getBody()->seek(0);
203  return json_decode($response->getBody()->getContents(), true);
204  }
205 
211  private function getUpload($id)
212  {
213  $filename = "";
214  $description = "";
215  $treeTableName = "uploadtree_a";
216  $timestamp = "";
217  switch ($id) {
218  case 2:
219  $filename = "top$id";
220  $timestamp = "01-01-2020";
221  break;
222  case 3:
223  $filename = "child$id";
224  $timestamp = "02-01-2020";
225  break;
226  case 4:
227  $filename = "child$id";
228  $timestamp = "03-01-2020";
229  break;
230  default:
231  return null;
232  }
233  return new Upload($id, $filename, $description, $treeTableName, $timestamp);
234  }
235 
242  private function getResponseForReport($uploadId, $reportFormat)
243  {
244  $GLOBALS["apiBasePath"] = "/repo/api/v1";
245  $requestHeaders = new Headers();
246  $requestHeaders->setHeader('uploadId', $uploadId);
247  $requestHeaders->setHeader('reportFormat', $reportFormat);
248  $body = $this->streamFactory->createStream();
249  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
250  "/repo/api/v1/report"), $requestHeaders, [], [], $body);
251  $response = new ResponseHelper();
252  return $this->reportController->getReport($request, $response, []);
253  }
254 
260  public function testGetReportAllFormats()
261  {
262  $uploadId = 3;
263  $upload = $this->getUpload($uploadId);
264 
265  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
266  $this->groupId])->andReturn(true);
267  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
268  ->andReturn($upload);
269  $this->spdxPlugin->shouldReceive('scheduleAgent')
270  ->withArgs([$this->groupId, $upload, M::anyOf($this->reportsAllowed[0],
271  $this->reportsAllowed[1], $this->reportsAllowed[2])])
272  ->andReturn([32, 33, ""]);
273  $this->readmeossPlugin->shouldReceive('scheduleAgent')
274  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
275  $this->unifiedPlugin->shouldReceive('scheduleAgent')
276  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
277  $this->clixmlPlugin->shouldReceive('scheduleAgent')
278  ->withArgs([$this->groupId, $upload])->andReturn([32, 33, ""]);
279  $this->decisionExporterPlugin->shouldReceive('scheduleAgent')
280  ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
281 
282  $expectedResponse = new Info(201, "http://localhost/repo/api/v1/report/32",
283  InfoType::INFO);
284 
285  foreach ($this->reportsAllowed as $reportFormat) {
286  $actualResponse = $this->getResponseForReport($uploadId, $reportFormat);
287  $this->assertEquals($expectedResponse->getArray(),
288  $this->getResponseJson($actualResponse));
289  $this->assertEquals($expectedResponse->getCode(),
290  $actualResponse->getStatusCode());
291  }
292  }
293 
299  public function testGetReportInvalidFormat()
300  {
301  $uploadId = 3;
302  $reportFormat = 'report';
303 
304  $this->expectException(HttpBadRequestException::class);
305 
306  $this->getResponseForReport($uploadId, $reportFormat);
307  }
308 
315  {
316  $uploadId = 4;
317  $reportFormat = $this->reportsAllowed[1];
318 
319  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
320  $this->groupId])->andReturn(false);
321 
322  $this->expectException(HttpForbiddenException::class);
323 
324  $this->getResponseForReport($uploadId, $reportFormat);
325  }
326 
332  public function testGetReportInvalidUpload()
333  {
334  $uploadId = 10;
335  $reportFormat = $this->reportsAllowed[1];
336  $upload = $this->getUpload($uploadId);
337 
338  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
339  $this->groupId])->andReturn(true);
340  $this->uploadDao->shouldReceive('getUpload')->withArgs([$uploadId])
341  ->andReturn($upload);
342 
343  $this->expectException(HttpNotFoundException::class);
344 
345  $this->getResponseForReport($uploadId, $reportFormat);
346  }
347 
357  public function testDownloadReport()
358  {
359  $reportId = 43;
360  $uploadId = 3;
361 
362  $this->dbManager->shouldReceive('getSingleRow')
363  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
364  [$reportId], "reportValidity"])
365  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
366  $this->dbManager->shouldReceive('getSingleRow')
367  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
368  [$reportId], "reportFileUpload"])
369  ->andReturn(["job_upload_fk" => $uploadId]);
370  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
371  $this->groupId])->andReturn(true);
372  $this->dbManager->shouldReceive('getSingleRow')
373  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
374  [$reportId], "reportFileName"])
375  ->andReturn(["job_upload_fk" => $uploadId]);
376 
377  $tmpfile = tempnam(sys_get_temp_dir(), "FOO");
378 
379  $handle = fopen($tmpfile, "w");
380  fwrite($handle, "writing to tempfile");
381  fclose($handle);
382 
383  $fileResponse = new BinaryFileResponse($tmpfile);
384  $fileResponse->headers->set('Content-Type', 'text/plain');
385  $fileResponse->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
386  $fileContent = $fileResponse->getFile();
387  $this->downloadPlugin->shouldReceive('getReport')->andReturn($fileResponse);
388 
389  $expectedResponse = new ResponseHelper();
390  $expectedResponse = $expectedResponse->withHeader('Content-Description',
391  'File Transfer')
392  ->withHeader('Content-Type', $fileResponse->headers->get('Content-Type'))
393  ->withHeader('Content-Disposition',
394  $fileResponse->headers->get('Content-Disposition'))
395  ->withHeader('Cache-Control', 'must-revalidate')
396  ->withHeader('Pragma', 'private')
397  ->withHeader('Content-Length', filesize($fileContent));
398 
399  $actualResponse = $this->reportController->downloadReport(null,
400  new ResponseHelper(), ["id" => $reportId]);
401 
402  $expectedResponse->getBody()->seek(0);
403  $this->assertEquals(file_get_contents($tmpfile),
404  $actualResponse->getBody()->getContents());
405  $this->assertEquals($expectedResponse->getHeaders(),
406  $actualResponse->getHeaders());
407  unlink($tmpfile);
408  }
409 
416  {
417  $reportId = 43;
418  $uploadId = 3;
419 
420  $this->dbManager->shouldReceive('getSingleRow')
421  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
422  [$reportId], "reportValidity"])
423  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
424  $this->dbManager->shouldReceive('getSingleRow')
425  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
426  [$reportId], "reportFileUpload"])
427  ->andReturn(["job_upload_fk" => $uploadId]);
428  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
429  $this->groupId])->andReturn(false);
430 
431  $this->expectException(HttpForbiddenException::class);
432 
433  $this->reportController->downloadReport(null, new ResponseHelper(),
434  ["id" => $reportId]);
435  }
436 
443  {
444  $reportId = 43;
445 
446  $this->dbManager->shouldReceive('getSingleRow')
447  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
448  [$reportId], "reportValidity"])
449  ->andReturn(["jq_type" => ""]);
450 
451  $this->expectException(HttpNotFoundException::class);
452 
453  $this->reportController->downloadReport(null, new ResponseHelper(),
454  ["id" => $reportId]);
455  }
456 
462  public function testDownloadReportTryLater()
463  {
464  $reportId = 43;
465  $uploadId = 3;
466 
467  $this->dbManager->shouldReceive('getSingleRow')
468  ->withArgs(['SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
469  [$reportId], "reportValidity"])
470  ->andReturn(["jq_type" => $this->reportsAllowed[1]]);
471  $this->dbManager->shouldReceive('getSingleRow')
472  ->withArgs(['SELECT job_upload_fk FROM job WHERE job_pk = $1',
473  [$reportId], "reportFileUpload"])
474  ->andReturn(["job_upload_fk" => $uploadId]);
475  $this->uploadDao->shouldReceive('isAccessible')->withArgs([$uploadId,
476  $this->groupId])->andReturn(true);
477  $this->dbManager->shouldReceive('getSingleRow')
478  ->withArgs(['SELECT * FROM reportgen WHERE job_fk = $1',
479  [$reportId], "reportFileName"])
480  ->andReturn(false);
481 
482  $this->expectException(HttpServiceUnavailableException::class);
483 
484  $this->reportController->downloadReport(null, new ResponseHelper(),
485  ["id" => $reportId]);
486  }
487 }
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