FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadControllerTest.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  SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
15 
43 use Mockery as M;
44 use Slim\Psr7\Factory\StreamFactory;
45 use Slim\Psr7\Headers;
46 use Slim\Psr7\Request;
47 use Slim\Psr7\Uri;
48 
49 function TryToDelete($uploadpk, $user_pk, $group_pk, $uploadDao)
50 {
51  return UploadControllerTest::$functions->TryToDelete($uploadpk, $user_pk,
52  $group_pk, $uploadDao);
53 }
54 
59 class UploadControllerTest extends \PHPUnit\Framework\TestCase
60 {
65  public static $functions;
66 
72 
77  private $userId;
78 
83  private $groupId;
84 
89  private $dbHelper;
90 
95  private $dbManager;
96 
101  private $restHelper;
102 
108 
113  private $uploadDao;
114 
119  private $folderDao;
120 
125  private $agentDao;
126 
131  private $clearingDao;
132 
137  private $licenseDao;
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('ContainerBuilder');
161  self::$functions = M::mock();
162  $this->dbHelper = M::mock(DbHelper::class);
163  $this->dbManager = M::mock(DbManager::class);
164  $this->restHelper = M::mock(RestHelper::class);
165  $this->uploadDao = M::mock(UploadDao::class);
166  $this->folderDao = M::mock(FolderDao::class);
167  $this->agentDao = M::mock(AgentDao::class);
168  $this->userDao = M::mock(UserDao::class);
169  $this->clearingDao = M::mock(ClearingDao::class);
170  $this->licenseDao = M::mock(LicenseDao::class);
171  $this->reuseReportProcess = M::mock(ReuseReportProcessor::class);
172 
173  $this->dbManager->shouldReceive('getSingleRow')
174  ->withArgs([M::any(), [$this->groupId, UploadStatus::OPEN,
175  Auth::PERM_READ]]);
176  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
177 
178  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
179  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
180  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
181  $this->restHelper->shouldReceive('getUploadDao')
182  ->andReturn($this->uploadDao);
183  $this->restHelper->shouldReceive('getFolderDao')
184  ->andReturn($this->folderDao);
185  $this->restHelper->shouldReceive('getUserDao')
186  ->andReturn($this->userDao);
187  $container->shouldReceive('get')->withArgs(['dao.license'])->andReturn(
188  $this->licenseDao);
189  $container->shouldReceive('get')->withArgs(array(
190  'dao.clearing'))->andReturn($this->clearingDao);
191  $container->shouldReceive('get')->withArgs(array(
192  'helper.restHelper'))->andReturn($this->restHelper);
193  $container->shouldReceive('get')->withArgs(array(
194  'dao.agent'))->andReturn($this->agentDao);
195  $container->shouldReceive('get')->withArgs(array(
196  'dao.upload'))->andReturn($this->uploadDao);
197  $container->shouldReceive('get')->withArgs(
198  ['db.manager'])->andReturn($this->dbManager);
199  $container->shouldReceive('get')->withArgs(array('dao.license'))->andReturn($this->licenseDao);
200  $container->shouldReceive('get')->withArgs(array(
201  'businessrules.reusereportprocessor'))->andReturn($this->reuseReportProcess);
202  $this->uploadController = new UploadController($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 getUploadBounds($id)
236  {
237  if ($id > 4) {
238  return false;
239  }
240  $itemId = ($id * 100) + 1;
241  $left = ($id * 100) + 2;
242  $right = ($id * 100) + 50;
243  return new ItemTreeBounds($itemId, 'uploadtree_a', $id, $left, $right);
244  }
245 
251  private function getUpload($id)
252  {
253  $uploadName = "";
254  $description = "";
255  $uploadDate = "";
256  $folderId = 2;
257  $folderName = "SR";
258  $fileSize = 0;
259  switch ($id) {
260  case 2:
261  $uploadName = "top$id";
262  $uploadDate = "01-01-2020";
263  $fileSize = 123;
264  break;
265  case 3:
266  $uploadName = "child$id";
267  $uploadDate = "02-01-2020";
268  $fileSize = 133;
269  break;
270  case 4:
271  $uploadName = "child$id";
272  $uploadDate = "03-01-2020";
273  $fileSize = 153;
274  break;
275  default:
276  return null;
277  }
278  $hash = new Hash('sha1checksum', 'md5checksum', 'sha256checksum', $fileSize);
279  return new Upload($folderId, $folderName, $id, $description,
280  $uploadName, $uploadDate, null, $hash);
281  }
282 
288  public function testGetSingleUploadV1()
289  {
290  $this->testGetSingleUpload(ApiVersion::V1);
291  }
297  public function testGetSingleUploadV2()
298  {
299  $this->testGetSingleUpload(ApiVersion::V2);
300  }
301  private function testGetSingleUpload($version)
302  {
303  $uploadId = 3;
304  $options = [
305  "folderId" => null,
306  "name" => null,
307  "status" => null,
308  "assignee" => null,
309  "since" => null
310  ];
311  $upload = $this->getUpload($uploadId);
312  $requestHeaders = new Headers();
313  $body = $this->streamFactory->createStream();
314  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
315  "/uploads/$uploadId"), $requestHeaders, [], [], $body);
316  if ($version == ApiVersion::V2) {
317  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
318  ApiVersion::V2);
319  }
320  $this->dbHelper->shouldReceive('doesIdExist')
321  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
322  $this->uploadDao->shouldReceive('isAccessible')
323  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
324  $this->uploadDao->shouldReceive('getParentItemBounds')
325  ->withArgs([$uploadId])->andReturn($this->getUploadBounds($uploadId));
326  $this->dbHelper->shouldReceive('getUploads')
327  ->withArgs([$this->userId, $this->groupId, 100, 1, $uploadId, $options,
328  true, $version])->andReturn([1, [$upload->getArray()]]);
329  $expectedResponse = (new ResponseHelper())->withJson($upload->getArray(), 200);
330  $actualResponse = $this->uploadController->getUploads($request,
331  new ResponseHelper(), ['id' => $uploadId]);
332  $this->assertEquals($expectedResponse->getStatusCode(),
333  $actualResponse->getStatusCode());
334  $this->assertEquals($this->getResponseJson($expectedResponse),
335  $this->getResponseJson($actualResponse));
336  }
337 
344  {
345  $uploadId = 3;
346  $requestHeaders = new Headers();
347  $body = $this->streamFactory->createStream();
348  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
349  "/uploads/$uploadId"), $requestHeaders, [], [], $body);
350  $this->dbHelper->shouldReceive('doesIdExist')
351  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
352  $this->uploadDao->shouldReceive('isAccessible')
353  ->withArgs([$uploadId, $this->groupId])->andReturn(false);
354  $this->expectException(HttpForbiddenException::class);
355 
356  $this->uploadController->getUploads($request, new ResponseHelper(),
357  ['id' => $uploadId]);
358  }
359 
367  public function testGetUploadWithFiltersV1()
368  {
369  $this->testGetUploadWithFilters(ApiVersion::V1);
370  }
378  public function testGetUploadWithFiltersV2()
379  {
380  $this->testGetUploadWithFilters(ApiVersion::V2);
381  }
382 
383  private function testGetUploadWithFilters($version)
384  {
385  $options = [
386  "folderId" => null,
387  "name" => null,
388  "status" => null,
389  "assignee" => null,
390  "since" => null
391  ];
392 
393  // Test for folder filter
394  $folderId = 2;
395  $folderOptions = $options;
396  $folderOptions["folderId"] = $folderId;
397  $requestHeaders = new Headers();
398  $body = $this->streamFactory->createStream();
399  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
400  "/uploads", UploadController::FOLDER_PARAM . "=$folderId"),
401  $requestHeaders, [], [], $body);
402  if ($version == ApiVersion::V2) {
403  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
404  ApiVersion::V2);
405  }
406  $this->folderDao->shouldReceive('isFolderAccessible')
407  ->withArgs([$folderId, $this->userId])->andReturn(true)->once();
408  $this->dbHelper->shouldReceive('getUploads')
409  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $folderOptions,
410  true, $version])->andReturn([1, []])->once();
411  $this->uploadController->getUploads($request, new ResponseHelper(), []);
412 
413  // Test for name filter
414  $name = "foss";
415  $nameOptions = $options;
416  $nameOptions["name"] = $name;
417  $requestHeaders = new Headers();
418  $body = $this->streamFactory->createStream();
419  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
420  "/uploads", UploadController::FILTER_NAME . "=$name"), $requestHeaders,
421  [], [], $body);
422  if ($version == ApiVersion::V2) {
423  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
424  ApiVersion::V2);
425  }
426  $this->dbHelper->shouldReceive('getUploads')
427  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $nameOptions,
428  true, $version])->andReturn([1, []])->once();
429  $this->uploadController->getUploads($request, new ResponseHelper(), []);
430 
431  // Test for status filter
432  $statusString = "InProgress";
433  $status = UploadStatus::IN_PROGRESS;
434  $statusOptions = $options;
435  $statusOptions["status"] = $status;
436  $requestHeaders = new Headers();
437  $body = $this->streamFactory->createStream();
438  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
439  "/uploads", UploadController::FILTER_STATUS . "=$statusString"),
440  $requestHeaders, [], [], $body);
441  if ($version == ApiVersion::V2) {
442  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
443  ApiVersion::V2);
444  }
445  $this->dbHelper->shouldReceive('getUploads')
446  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $statusOptions,
447  true, $version])->andReturn([1, []])->once();
448  $this->uploadController->getUploads($request, new ResponseHelper(), []);
449 
450  // Test for assignee filter
451  $assignee = "-me-";
452  $assigneeOptions = $options;
453  $assigneeOptions["assignee"] = $this->userId;
454  $requestHeaders = new Headers();
455  $body = $this->streamFactory->createStream();
456  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
457  "/uploads", UploadController::FILTER_ASSIGNEE . "=$assignee"),
458  $requestHeaders, [], [], $body);
459  if ($version == ApiVersion::V2) {
460  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
461  ApiVersion::V2);
462  }
463  $this->dbHelper->shouldReceive('getUploads')
464  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $assigneeOptions,
465  true, $version])->andReturn([1, []])->once();
466  $this->uploadController->getUploads($request, new ResponseHelper(), []);
467 
468  // Test for since filter
469  $since = "2021-02-28";
470  $sinceOptions = $options;
471  $sinceOptions["since"] = strtotime($since);
472  $requestHeaders = new Headers();
473  $body = $this->streamFactory->createStream();
474  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
475  "/uploads", UploadController::FILTER_DATE . "=$since"),
476  $requestHeaders, [], [], $body);
477  if ($version == ApiVersion::V2) {
478  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
479  ApiVersion::V2);
480  }
481  $this->dbHelper->shouldReceive('getUploads')
482  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $sinceOptions,
483  true, $version])->andReturn([1, []])->once();
484  $this->uploadController->getUploads($request, new ResponseHelper(), []);
485 
486  // Test for status and since filter
487  $statusString = "Open";
488  $status = UploadStatus::OPEN;
489  $since = "2021-02-28";
490  $combOptions = $options;
491  $combOptions["since"] = strtotime($since);
492  $combOptions["status"] = $status;
493  $requestHeaders = new Headers();
494  $body = $this->streamFactory->createStream();
495  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
496  "/uploads", UploadController::FILTER_DATE . "=$since&" .
497  UploadController::FILTER_STATUS . "=$statusString"),
498  $requestHeaders, [], [], $body);
499  if ($version == ApiVersion::V2) {
500  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
501  ApiVersion::V2);
502  }
503  $this->dbHelper->shouldReceive('getUploads')
504  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $combOptions,
505  true, $version])->andReturn([1, []])->once();
506  $this->uploadController->getUploads($request, new ResponseHelper(), []);
507  }
508 
514  public function testGetUploadsV1(){
515  $this->testGetUploads(ApiVersion::V1);
516  }
522  public function testGetUploadsV2(){
523  $this->testGetUploads(ApiVersion::V2);
524  }
525  private function testGetUploads($version)
526  {
527  $uploads = [
528  $this->getUpload(2)->getArray(),
529  $this->getUpload(3)->getArray(),
530  $this->getUpload(4)->getArray()
531  ];
532  $options = [
533  "folderId" => null,
534  "name" => null,
535  "status" => null,
536  "assignee" => null,
537  "since" => null
538  ];
539  $requestHeaders = new Headers();
540  $body = $this->streamFactory->createStream();
541  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
542  "/uploads/"), $requestHeaders, [], [], $body);
543  if ($version == ApiVersion::V2) {
544  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
545  ApiVersion::V2);
546  }
547  $this->dbHelper->shouldReceive('getUploads')
548  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $options, true, $version])
549  ->andReturn([1, $uploads]);
550  $expectedResponse = (new ResponseHelper())->withJson($uploads, 200);
551  $actualResponse = $this->uploadController->getUploads($request,
552  new ResponseHelper(), []);
553  $this->assertEquals($expectedResponse->getStatusCode(),
554  $actualResponse->getStatusCode());
555  $this->assertEquals($this->getResponseJson($expectedResponse),
556  $this->getResponseJson($actualResponse));
557  }
558 
566  {
567  $uploadId = 3;
568  $requestHeaders = new Headers();
569  $body = $this->streamFactory->createStream();
570  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
571  "/uploads/"), $requestHeaders, [], [], $body);
572  $this->dbHelper->shouldReceive('doesIdExist')
573  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
574  $this->uploadDao->shouldReceive('isAccessible')
575  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
576  $this->uploadDao->shouldReceive('getParentItemBounds')
577  ->withArgs([$uploadId])->andReturn(false);
578  $this->expectException(HttpServiceUnavailableException::class);
579 
580  $this->uploadController->getUploads($request, new ResponseHelper(),
581  ['id' => $uploadId]);
582  }
583 
589  public function testCopyUploadV1()
590  {
591  $this->testCopyUpload(ApiVersion::V1);
592  }
598  public function testCopyUploadV2()
599  {
600  $this->testCopyUpload(ApiVersion::V2);
601  }
607  private function testCopyUpload($version)
608  {
609  $uploadId = 3;
610  $folderId = 5;
611  $info = new Info(202, "Upload $uploadId will be copied to folder $folderId",
612  InfoType::INFO);
613 
614  $this->restHelper->shouldReceive('copyUpload')
615  ->withArgs([$uploadId, $folderId, true])->andReturn($info);
616  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
617  $info->getCode());
618 
619  $requestHeaders = new Headers();
620  $body = $this->streamFactory->createStream();
621 
622  if($version==ApiVersion::V1){
623  $requestHeaders->setHeader('folderId', $folderId);
624  $requestHeaders->setHeader('action', 'copy');
625  $request = new Request("PUT", new Uri("HTTP", "localhost"),
626  $requestHeaders, [], [], $body);
627  $actualResponse = $this->uploadController->moveUpload($request,
628  new ResponseHelper(), ['id' => $uploadId]);
629  }
630  else{
631  $request = new Request("PUT", new Uri("HTTP", "localhost"),
632  $requestHeaders, [], [], $body);
633  if ($version == ApiVersion::V2) {
634  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
635  ApiVersion::V2);
636  }
637  $actualResponse = $this->uploadController->moveUpload($request->withUri($request->getUri()->withQuery("folderId=$folderId&action=copy")),
638  new ResponseHelper(), ['id' => $uploadId]);
639  }
640  $this->assertEquals($expectedResponse->getStatusCode(),
641  $actualResponse->getStatusCode());
642  $this->assertEquals($this->getResponseJson($expectedResponse),
643  $this->getResponseJson($actualResponse));
644  }
645 
651  public function testMoveUploadInvalidFolder()
652  {
653  $uploadId = 3;
654 
655  $requestHeaders = new Headers();
656  $requestHeaders->setHeader('folderId', 'alpha');
657  $requestHeaders->setHeader('action', 'move');
658  $body = $this->streamFactory->createStream();
659  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
660  $requestHeaders, [], [], $body);
661  $this->expectException(HttpBadRequestException::class);
662 
663  $this->uploadController->moveUpload($request, new ResponseHelper(),
664  ['id' => $uploadId]);
665  }
666 
674  public function testPostUploadV1()
675  {
676  $this->testPostUpload(ApiVersion::V1);
677  }
678 
686  public function testPostUploadV2()
687  {
688  $this->testPostUpload(ApiVersion::V2);
689  }
690 
695  private function testPostUpload(int $version)
696  {
697  $folderId = 2;
698  $uploadId = 20;
699  $uploadDescription = "Test Upload";
700 
701  $requestHeaders = new Headers();
702  $requestHeaders->setHeader('Content-Type', 'application/json');
703  if ($version == ApiVersion::V2) {
704  $reqBody = [
705  "location" => "data",
706  "folderId" => $folderId,
707  "uploadDescription" => $uploadDescription,
708  "ignoreScm" => "true",
709  "scanOptions" => "scanOptions",
710  "uploadType" => "vcs"
711  ];
712  } else {
713  $reqBody = [
714  "location" => "data",
715  "scanOptions" => "scanOptions"
716  ];
717  $requestHeaders->setHeader('folderId', $folderId);
718  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
719  $requestHeaders->setHeader('ignoreScm', 'true');
720  $requestHeaders->setHeader('Content-Type', 'application/json');
721  $requestHeaders->setHeader('uploadType', 'vcs');
722  }
723 
724 
725  $body = $this->streamFactory->createStream(json_encode(
726  $reqBody
727  ));
728  $request = new Request("POST", new Uri("HTTP", "localhost"),
729  $requestHeaders, [], [], $body);
730  if ($version == ApiVersion::V2) {
731  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
732  ApiVersion::V2);
733  }
734  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
735  $uploadHelper->shouldReceive('createNewUpload')
736  ->withArgs([$reqBody["location"], $folderId, $uploadDescription, 'protected', 'true',
737  'vcs', false])
738  ->andReturn([true, '', '', $uploadId]);
739 
740  $info = new Info(201, intval(20), InfoType::INFO);
741 
742  $uploadHelper->shouldReceive('handleScheduleAnalysis')->withArgs([$uploadId,$folderId,$reqBody["scanOptions"],false])
743  ->andReturn($info);
744 
745  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
746  $this->folderDao->shouldReceive('isFolderAccessible')
747  ->withArgs([$folderId])->andReturn(true);
748 
749  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
750  $info->getCode());
751  $actualResponse = $this->uploadController->postUpload($request,
752  new ResponseHelper(), []);
753  $this->assertEquals($expectedResponse->getStatusCode(),
754  $actualResponse->getStatusCode());
755  $this->assertEquals($this->getResponseJson($expectedResponse),
756  $this->getResponseJson($actualResponse));
757  }
758 
767  {
768  $this->testPostUploadFolderNotAccessible(ApiVersion::V1);
769  }
770 
779  {
780  $this->testPostUploadFolderNotAccessible(ApiVersion::V2);
781  }
782 
787  private function testPostUploadFolderNotAccessible(int $version)
788  {
789  $folderId = 2;
790  $uploadDescription = "Test Upload";
791 
792  $requestHeaders = new Headers();
793  $requestHeaders->setHeader('Content-type', 'application/json');
794  if ($version == ApiVersion::V2) {
795  $body = $this->streamFactory->createStream(json_encode([
796  "location" => "data",
797  "folderId" => $folderId,
798  "uploadDescription" => $uploadDescription,
799  "ignoreScm" => "true",
800  "scanOptions" => "scanOptions",
801  "uploadType" => "vcs"
802  ]));
803  } else {
804  $body = $this->streamFactory->createStream(json_encode([
805  "location" => "data",
806  "scanOptions" => "scanOptions"
807  ]));
808  $requestHeaders->setHeader('folderId', $folderId);
809  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
810  $requestHeaders->setHeader('ignoreScm', 'true');
811  $requestHeaders->setHeader('Content-Type', 'application/json');
812  $requestHeaders->setHeader('uploadType', 'vcs');
813  }
814  $request = new Request("POST", new Uri("HTTP", "localhost"),
815  $requestHeaders, [], [], $body);
816  if ($version == ApiVersion::V2) {
817  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
818  ApiVersion::V2);
819  }
820 
821  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
822 
823  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
824  $this->folderDao->shouldReceive('isFolderAccessible')
825  ->withArgs([$folderId])->andReturn(false);
826  $this->expectException(HttpForbiddenException::class);
827 
828  $this->uploadController->postUpload($request, new ResponseHelper(),
829  []);
830  }
831 
840  {
841  $this->testPostUploadFolderNotFound(ApiVersion::V1);
842  }
843 
852  {
853  $this->testPostUploadFolderNotFound(ApiVersion::V2);
854  }
855 
860  private function testPostUploadFolderNotFound(int $version)
861  {
862  $folderId = 8;
863  $uploadDescription = "Test Upload";
864 
865  $requestHeaders = new Headers();
866  $requestHeaders->setHeader('Content-type', 'application/json');
867  if ($version == ApiVersion::V2) {
868  $body = $this->streamFactory->createStream(json_encode([
869  "location" => "vcsData",
870  "folderId" => $folderId,
871  "uploadDescription" => $uploadDescription,
872  "ignoreScm" => "true",
873  "scanOptions" => "scanOptions",
874  "uploadType" => "vcs"
875  ]));
876  } else {
877  $body = $this->streamFactory->createStream(json_encode([
878  "location" => "vcsData",
879  "scanOptions" => "scanOptions"
880  ]));
881  $requestHeaders->setHeader('folderId', $folderId);
882  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
883  $requestHeaders->setHeader('ignoreScm', 'true');
884  $requestHeaders->setHeader('Content-Type', 'application/json');
885  $requestHeaders->setHeader('uploadType', 'vcs');
886  }
887  $request = new Request("POST", new Uri("HTTP", "localhost"),
888  $requestHeaders, [], [], $body);
889  if ($version == ApiVersion::V2) {
890  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
891  ApiVersion::V2);
892  }
893 
894  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
895 
896  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
897  $this->expectException(HttpNotFoundException::class);
898 
899  $this->uploadController->postUpload($request, new ResponseHelper(),
900  []);
901  }
902 
911  {
912  $this->testPostUploadInternalError(ApiVersion::V1);
913  }
914 
923  {
924  $this->testPostUploadInternalError(ApiVersion::V2);
925  }
926 
931  private function testPostUploadInternalError(int $version)
932  {
933  $folderId = 3;
934  $uploadDescription = "Test Upload";
935  $errorMessage = "Failed to insert upload record";
936  $errorDesc = "";
937 
938 
939  $requestHeaders = new Headers();
940  $requestHeaders->setHeader('Content-type', 'application/json');
941  if ($version == ApiVersion::V2) {
942  $body = $this->streamFactory->createStream(json_encode([
943  "location" => "vcsData",
944  "folderId" => $folderId,
945  "uploadDescription" => $uploadDescription,
946  "ignoreScm" => "true",
947  "scanOptions" => "scanOptions",
948  "uploadType" => "vcs"
949  ]));
950  } else {
951  $body = $this->streamFactory->createStream(json_encode([
952  "location" => "vcsData",
953  "scanOptions" => "scanOptions"
954  ]));
955  $requestHeaders->setHeader('folderId', $folderId);
956  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
957  $requestHeaders->setHeader('ignoreScm', 'true');
958  $requestHeaders->setHeader('Content-Type', 'application/json');
959  $requestHeaders->setHeader('uploadType', 'vcs');
960  }
961 
962  $request = new Request("POST", new Uri("HTTP", "localhost"),
963  $requestHeaders, [], [], $body);
964  if ($version == ApiVersion::V2) {
965  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
966  ApiVersion::V2);
967  }
968 
969  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
970  $uploadHelper->shouldReceive('createNewUpload')
971  ->withArgs(['vcsData', $folderId, $uploadDescription, 'protected', 'true',
972  'vcs', false])
973  ->andReturn([false, $errorMessage, $errorDesc, [-1]]);
974 
975  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
976  $this->folderDao->shouldReceive('isFolderAccessible')
977  ->withArgs([$folderId])->andReturn(true);
978  $this->expectException(HttpInternalServerErrorException::class);
979 
980  $this->uploadController->postUpload($request, new ResponseHelper(), []);
981  }
982 
990  public function testGetUploadLicensesV1()
991  {
992  $this->testGetUploadLicenses(ApiVersion::V1);
993  }
1001  public function testGetUploadLicensesV2()
1002  {
1003  $this->testGetUploadLicenses(ApiVersion::V2);
1004  }
1005  private function testGetUploadLicenses($version)
1006  {
1007  $uploadId = 3;
1008  $agentsRun = [
1009  ['agentName' => 'nomos', 'currentAgentId' => 2, 'isAgentRunning' => false],
1010  ['agentName' => 'monk', 'currentAgentId' => 3, 'isAgentRunning' => false]
1011  ];
1012  $licenseResponse = [
1013  ['filePath' => 'filea', 'agentFindings' => 'MIT', 'conclusions' => 'MIT'],
1014  ['filePath' => 'fileb', 'agentFindings' => 'MIT',
1015  'conclusions' => 'No_license_found']
1016  ];
1017 
1018  $requestHeaders = new Headers();
1019  $body = $this->streamFactory->createStream();
1020  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1021  "/uploads/$uploadId/licenses", UploadController::AGENT_PARAM .
1022  "=nomos,monk&containers=false"),
1023  $requestHeaders, [], [], $body);
1024  if ($version == ApiVersion::V2) {
1025  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
1026  ApiVersion::V2);
1027  }
1028  $this->dbHelper->shouldReceive('doesIdExist')
1029  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1030  $this->uploadDao->shouldReceive('isAccessible')
1031  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1032  $this->uploadDao->shouldReceive('getParentItemBounds')
1033  ->withArgs([$uploadId])->andReturn($this->getUploadBounds($uploadId));
1034  $this->agentDao->shouldReceive('arsTableExists')
1035  ->withArgs([M::anyOf('nomos', 'monk')])->andReturn(true);
1036 
1037  $scanJobProxy = M::mock('overload:Fossology\Lib\Proxy\ScanJobProxy');
1038  $scanJobProxy->shouldReceive('createAgentStatus')
1039  ->withArgs([['nomos', 'monk']])
1040  ->andReturn($agentsRun);
1041 
1042  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
1043  $uploadHelper->shouldReceive('getUploadLicenseList')
1044  ->withArgs([$uploadId, ['nomos', 'monk'], false, true, false, 0, 50, $version])
1045  ->andReturn(([[$licenseResponse], 1]));
1046 
1047  $expectedResponse = (new ResponseHelper())->withJson($licenseResponse, 200);
1048 
1049  $actualResponse = $this->uploadController->getUploadLicenses($request,
1050  new ResponseHelper(), ['id' => $uploadId]);
1051  $this->assertEquals($expectedResponse->getStatusCode(),
1052  $actualResponse->getStatusCode());
1053  $this->assertEquals($this->getResponseJson($expectedResponse),
1054  $this->getResponseJson($actualResponse)[0]);
1055  $this->assertEquals('1',
1056  $actualResponse->getHeaderLine('X-Total-Pages'));
1057  }
1058 
1068  {
1069  $uploadId = 3;
1070  $agentsRun = [
1071  ['agentName' => 'nomos', 'currentAgentId' => 2, 'isAgentRunning' => true],
1072  ['agentName' => 'monk', 'currentAgentId' => 3, 'isAgentRunning' => false]
1073  ];
1074 
1075  $requestHeaders = new Headers();
1076  $body = $this->streamFactory->createStream();
1077  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1078  "/uploads/$uploadId/licenses", UploadController::AGENT_PARAM .
1079  "=nomos,monk&containers=false"),
1080  $requestHeaders, [], [], $body);
1081 
1082  $this->dbHelper->shouldReceive('doesIdExist')
1083  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1084  $this->uploadDao->shouldReceive('isAccessible')
1085  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1086  $this->uploadDao->shouldReceive('getParentItemBounds')
1087  ->withArgs([$uploadId])->andReturn($this->getUploadBounds($uploadId));
1088  $this->agentDao->shouldReceive('arsTableExists')
1089  ->withArgs([M::anyOf('nomos', 'monk')])->andReturn(true);
1090 
1091  $scanJobProxy = M::mock('overload:Fossology\Lib\Proxy\ScanJobProxy');
1092  $scanJobProxy->shouldReceive('createAgentStatus')
1093  ->withArgs([['nomos', 'monk']])
1094  ->andReturn($agentsRun);
1095  $this->expectException(HttpServiceUnavailableException::class);
1096 
1097  $this->uploadController->getUploadLicenses($request, new ResponseHelper(),
1098  ['id' => $uploadId]);
1099  }
1100 
1106  public function testUpdateUpload()
1107  {
1108  $upload = 2;
1109  $assignee = 4;
1110  $status = UploadStatus::REJECTED;
1111  $comment = "Not helpful";
1112 
1113  $resource = fopen('data://text/plain;base64,' .
1114  base64_encode($comment), 'r+');
1115  $body = $this->streamFactory->createStreamFromResource($resource);
1116  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1117  "/uploads/$upload", UploadController::FILTER_STATUS . "=Rejected&" .
1118  UploadController::FILTER_ASSIGNEE . "=$assignee"),
1119  new Headers(), [], [], $body);
1120 
1121  $this->userDao->shouldReceive('isAdvisorOrAdmin')
1122  ->withArgs([$this->userId, $this->groupId])
1123  ->andReturn(true);
1124  $this->userDao->shouldReceive('getUserChoices')
1125  ->withArgs([$this->groupId])
1126  ->andReturn([$this->userId => "fossy", $assignee => "friendly-fossy"]);
1127  $this->dbManager->shouldReceive('getSingleRow')
1128  ->withArgs([M::any(), [$assignee, $this->groupId, $upload], M::any()]);
1129  $this->dbManager->shouldReceive('getSingleRow')
1130  ->withArgs([M::any(), [$status, $comment, $this->groupId, $upload],
1131  M::any()]);
1132  $this->dbManager->shouldReceive('getSingleRow')
1133  ->withArgs([M::any(), [$upload], M::any()])
1134  ->andReturn(["exists" => ""]);
1135 
1136  $info = new Info(202, "Upload updated successfully.", InfoType::INFO);
1137  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
1138  $info->getCode());
1139  $actualResponse = $this->uploadController->updateUpload($request,
1140  new ResponseHelper(), ['id' => $upload]);
1141  $this->assertEquals($expectedResponse->getStatusCode(),
1142  $actualResponse->getStatusCode());
1143  $this->assertEquals($this->getResponseJson($expectedResponse),
1144  $this->getResponseJson($actualResponse));
1145  }
1146 
1152  public function testUpdateUploadNoPerm()
1153  {
1154  $upload = 2;
1155  $assignee = 4;
1156  $comment = "Not helpful";
1157 
1158  $resource = fopen('data://text/plain;base64,' .
1159  base64_encode($comment), 'r+');
1160  $body = $this->streamFactory->createStreamFromResource($resource);
1161  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1162  "/uploads/$upload", UploadController::FILTER_STATUS . "=Rejected&" .
1163  UploadController::FILTER_ASSIGNEE . "=$assignee"),
1164  new Headers(), [], [], $body);
1165 
1166  $this->userDao->shouldReceive('isAdvisorOrAdmin')
1167  ->withArgs([$this->userId, $this->groupId])
1168  ->andReturn(false);
1169  $this->expectException(HttpForbiddenException::class);
1170 
1171  $this->uploadController->updateUpload($request, new ResponseHelper(),
1172  ['id' => $upload]);
1173  }
1174 
1180  public function testGetMainLicenses()
1181  {
1182  $uploadId = 1;
1183  $licenseIds = array();
1184  $licenseId = 123;
1185  $licenseIds[$licenseId] = $licenseId;
1186  $license = new License($licenseId, "MIT", "MIT License", "risk", "texts", [],
1187  'type', false);
1188 
1189  $this->uploadDao->shouldReceive('isAccessible')
1190  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1191  $this->dbHelper->shouldReceive('doesIdExist')
1192  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1193  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1194  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], false])->andReturn([]);
1195  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], true])->andReturn([]);
1196  $this->licenseDao->shouldReceive('getLicenseById')->withArgs([$licenseId])->andReturn($license);
1197 
1198  $licenses[] = $license->getArray();
1199  $expectedResponse = (new ResponseHelper())->withJson($licenses, 200);
1200  $actualResponse = $this->uploadController->getMainLicenses(null, new ResponseHelper(), ['id' => $uploadId]);
1201  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
1202  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
1203  }
1204 
1205 
1211  public function testSetMainLicense()
1212  {
1213  $uploadId = 2;
1214  $shortName = "MIT";
1215  $licenseId = 1;
1216  $rq = [
1217  "shortName" => $shortName,
1218  ];
1219  $license = new License(2, $shortName, "MIT License", "risk", "texts", [],
1220  'type', 1);
1221  $licenseIds[$licenseId] = $licenseId;
1222  $this->uploadDao->shouldReceive('isAccessible')
1223  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1224  $this->dbHelper->shouldReceive('doesIdExist')
1225  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1226  $this->licenseDao->shouldReceive('getLicenseByShortName')
1227  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1228  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1229  $this->clearingDao->shouldReceive('makeMainLicense')
1230  ->withArgs([$uploadId, $this->groupId, $license->getId()])->andReturn(null);
1231 
1232  $info = new Info(200, "Successfully added new main license", InfoType::INFO);
1233 
1234  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
1235  $reqBody = $this->streamFactory->createStream(json_encode(
1236  $rq
1237  ));
1238  $requestHeaders = new Headers();
1239  $requestHeaders->setHeader('Content-Type', 'application/json');
1240  $request = new Request("POST", new Uri("HTTP", "localhost"),
1241  $requestHeaders, [], [], $reqBody);
1242 
1243  $actualResponse = $this->uploadController->setMainLicense($request, new ResponseHelper(), ['id' => $uploadId]);
1244 
1245  $this->assertEquals($expectedResponse->getStatusCode(),
1246  $actualResponse->getStatusCode());
1247  $this->assertEquals($this->getResponseJson($expectedResponse),
1248  $this->getResponseJson($actualResponse));
1249  }
1250 
1256  public function testSetMainLicense_exists()
1257  {
1258  $uploadId = 2;
1259  $shortName = "MIT";
1260  $licenseId = 1;
1261  $rq = [
1262  "shortName" => $shortName,
1263  ];
1264  $license = new License($licenseId, $shortName, "MIT License", "risk", "texts", [],
1265  'type', 1);
1266  $licenseIds[$licenseId] = $licenseId;
1267  $this->uploadDao->shouldReceive('isAccessible')
1268  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1269  $this->dbHelper->shouldReceive('doesIdExist')
1270  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1271  $this->licenseDao->shouldReceive('getLicenseByShortName')
1272  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1273  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1274  $this->clearingDao->shouldReceive('makeMainLicense')
1275  ->withArgs([$uploadId, $this->groupId, $license->getId()])->andReturn(null);
1276 
1277  $reqBody = $this->streamFactory->createStream(json_encode(
1278  $rq
1279  ));
1280  $requestHeaders = new Headers();
1281  $requestHeaders->setHeader('Content-Type', 'application/json');
1282  $request = new Request("POST", new Uri("HTTP", "localhost"),
1283  $requestHeaders, [], [], $reqBody);
1284  $this->expectException(HttpBadRequestException::class);
1285 
1286  $this->uploadController->setMainLicense($request, new ResponseHelper(), ['id' => $uploadId]);
1287  }
1288 
1289 
1295  public function testRemoveMainLicense()
1296  {
1297  $uploadId = 3;
1298  $licenseId = 1;
1299  $shortName = "MIT";
1300  $license = new License($licenseId, $shortName, "MIT License", "risk", "texts", [],
1301  'type', 1);
1302  $licenseIds[$licenseId] = $licenseId;
1303 
1304  $this->uploadDao->shouldReceive('isAccessible')
1305  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1306  $this->dbHelper->shouldReceive('doesIdExist')
1307  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1308  $this->dbHelper->shouldReceive('doesIdExist')
1309  ->withArgs(["license_ref", "rf_pk", $licenseId])->andReturn(true);
1310  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1311 
1312  $this->clearingDao->shouldReceive('removeMainLicense')->withArgs([$uploadId, $this->groupId, $licenseId])->andReturn(null);
1313  $this->licenseDao->shouldReceive('getLicenseByShortName')
1314  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1315 
1316  $info = new Info(200, "Main license removed successfully.", InfoType::INFO);
1317  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
1318  $info->getCode());
1319  $actualResponse = $this->uploadController->removeMainLicense(null,
1320  new ResponseHelper(), ['id' => $uploadId, 'shortName' => $shortName]);
1321  $this->assertEquals($expectedResponse->getStatusCode(),
1322  $actualResponse->getStatusCode());
1323  $this->assertEquals($this->getResponseJson($expectedResponse),
1324  $this->getResponseJson($actualResponse));
1325  }
1326 
1333  {
1334  $uploadId = 3;
1335  $this->uploadDao->shouldReceive('isAccessible')
1336  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1337  $this->dbHelper->shouldReceive('doesIdExist')
1338  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1339  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
1340  $this->uploadDao->shouldReceive("getGlobalDecisionSettingsFromInfo")->andReturn(false);
1341  $this->agentDao->shouldReceive("arsTableExists")->andReturn(true);
1342  $this->agentDao->shouldReceive("getSuccessfulAgentEntries")->andReturn([['agent_id' => 1, 'agent_rev' => 1]]);
1343  $this->agentDao->shouldReceive("getCurrentAgentRef")->andReturn(new AgentRef(1, "agent", 1));
1344  $this->dbManager->shouldReceive("getSingleRow")
1345  ->withArgs([M::any(), [], 'no_license_uploadtree' . $uploadId])
1346  ->andReturn(['count' => 1]);
1347  $this->dbManager->shouldReceive("getSingleRow")
1348  ->withArgs([M::any(), [], 'already_cleared_uploadtree' . $uploadId])
1349  ->andReturn(['count' => 0]);
1350  $this->licenseDao->shouldReceive('getLicenseByShortName')
1351  ->withArgs(['No_license_found'])->andReturn(null);
1352  $this->licenseDao->shouldReceive('getLicenseByShortName')
1353  ->withArgs(['Void'])->andReturn(null);
1354  $res = [
1355  "totalFilesOfInterest" => 1,
1356  "totalFilesCleared" => 1,
1357  ];
1358  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
1359  $actualResponse = $this->uploadController->getClearingProgressInfo(null,
1360  new ResponseHelper(), ['id' => $uploadId]);
1361  $this->assertEquals($expectedResponse->getStatusCode(),
1362  $actualResponse->getStatusCode());
1363  $this->assertEquals($this->getResponseJson($expectedResponse),
1364  $this->getResponseJson($actualResponse));
1365  }
1371  public function testGetReuseReportSummary()
1372  {
1373  $uploadId = 2;
1374  $reuseReportSummary = [
1375  'declearedLicense' => "",
1376  'clearedLicense' => "MIT, BSD-3-Clause",
1377  'usedLicense' => "",
1378  'unusedLicense' => "",
1379  'missingLicense' => "MIT, BSD-3-Clause",
1380  ];
1381  $this->uploadDao->shouldReceive('isAccessible')
1382  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1383  $this->dbHelper->shouldReceive('doesIdExist')
1384  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1385  $this->reuseReportProcess->shouldReceive('getReuseSummary')
1386  ->withArgs([$uploadId])->andReturn($reuseReportSummary);
1387 
1388  $expectedResponse = (new ResponseHelper())->withJson($reuseReportSummary,
1389  200);
1390  $actualResponse = $this->uploadController->getReuseReportSummary(
1391  null, new ResponseHelper(), ['id' => $uploadId]);
1392  $this->assertEquals($expectedResponse->getStatusCode(),
1393  $actualResponse->getStatusCode());
1394  $this->assertEquals($this->getResponseJson($expectedResponse),
1395  $this->getResponseJson($actualResponse));
1396  }
1397 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
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
Hash model holding information about file like checksums and size.
Definition: Hash.php:18
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
Model class to hold Upload info.
Definition: Upload.php:17
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16