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 
45 use Mockery as M;
46 use Slim\Psr7\Factory\StreamFactory;
47 use Slim\Psr7\Headers;
48 use Slim\Psr7\Request;
49 use Slim\Psr7\Uri;
50 
51 function TryToDelete($uploadpk, $user_pk, $group_pk, $uploadDao)
52 {
53  return UploadControllerTest::$functions->TryToDelete($uploadpk, $user_pk,
54  $group_pk, $uploadDao);
55 }
56 
61 class UploadControllerTest extends \PHPUnit\Framework\TestCase
62 {
67  public static $functions;
68 
74 
79  private $userId;
80 
85  private $groupId;
86 
91  private $dbHelper;
92 
97  private $dbManager;
98 
103  private $restHelper;
104 
110 
115  private $uploadDao;
116 
122 
127  private $folderDao;
128 
133  private $agentDao;
134 
139  private $clearingDao;
140 
145  private $licenseDao;
146 
152 
157  private $streamFactory;
158 
162  private $copyrightPlugin;
163 
167  private $downloadPlugin;
168 
173  protected function setUp() : void
174  {
175  global $container;
176  $this->userId = 2;
177  $this->groupId = 2;
178  $container = M::mock('ContainerBuilder');
179  self::$functions = M::mock();
180  $this->dbHelper = M::mock(DbHelper::class);
181  $this->dbManager = M::mock(DbManager::class);
182  $this->restHelper = M::mock(RestHelper::class);
183  $this->uploadDao = M::mock(UploadDao::class);
184  $this->folderDao = M::mock(FolderDao::class);
185  $this->agentDao = M::mock(AgentDao::class);
186  $this->userDao = M::mock(UserDao::class);
187  $this->clearingDao = M::mock(ClearingDao::class);
188  $this->licenseDao = M::mock(LicenseDao::class);
189  $this->reuseReportProcess = M::mock(ReuseReportProcessor::class);
190  $this->uploadPermissionDao = M::mock(UploadPermissionDao::class);
191  $this->downloadPlugin = M::mock("download");
192 
193  $this->dbManager->shouldReceive('getSingleRow')
194  ->withArgs([M::any(), [$this->groupId, UploadStatus::OPEN,
195  Auth::PERM_READ]]);
196  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
197 
198  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
199  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
200  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
201  $this->restHelper->shouldReceive('getPlugin')->withArgs(array("download"))->andReturn($this->downloadPlugin);
202  $this->restHelper->shouldReceive('getUploadDao')
203  ->andReturn($this->uploadDao);
204  $this->restHelper->shouldReceive('getFolderDao')
205  ->andReturn($this->folderDao);
206  $this->restHelper->shouldReceive('getUserDao')
207  ->andReturn($this->userDao);
208  $container->shouldReceive('get')->withArgs(['dao.license'])->andReturn(
209  $this->licenseDao);
210  $container->shouldReceive('get')->withArgs(array(
211  'dao.clearing'))->andReturn($this->clearingDao);
212  $container->shouldReceive('get')->withArgs(array(
213  'helper.restHelper'))->andReturn($this->restHelper);
214  $container->shouldReceive('get')->withArgs(array(
215  'dao.agent'))->andReturn($this->agentDao);
216  $container->shouldReceive('get')->withArgs(array(
217  'dao.upload'))->andReturn($this->uploadDao);
218  $container->shouldReceive('get')->withArgs(
219  ['db.manager'])->andReturn($this->dbManager);
220  $container->shouldReceive('get')->withArgs(array('dao.license'))->andReturn($this->licenseDao);
221  $container->shouldReceive('get')->withArgs(array(
222  'businessrules.reusereportprocessor'))->andReturn($this->reuseReportProcess);
223  $this->uploadController = new UploadController($container);
224  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
225  $this->streamFactory = new StreamFactory();
226  }
227 
232  protected function tearDown() : void
233  {
234  $this->addToAssertionCount(
235  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
236  M::close();
237  }
238 
245  private function getResponseJson($response)
246  {
247  $response->getBody()->seek(0);
248  return json_decode($response->getBody()->getContents(), true);
249  }
250 
256  private function getUploadBounds($id)
257  {
258  if ($id > 4) {
259  return false;
260  }
261  $itemId = ($id * 100) + 1;
262  $left = ($id * 100) + 2;
263  $right = ($id * 100) + 50;
264  return new ItemTreeBounds($itemId, 'uploadtree_a', $id, $left, $right);
265  }
266 
272  private function getUpload($id)
273  {
274  $uploadName = "";
275  $description = "";
276  $uploadDate = "";
277  $folderId = 2;
278  $folderName = "SR";
279  $fileSize = 0;
280  switch ($id) {
281  case 2:
282  $uploadName = "top$id";
283  $uploadDate = "01-01-2020";
284  $fileSize = 123;
285  break;
286  case 3:
287  $uploadName = "child$id";
288  $uploadDate = "02-01-2020";
289  $fileSize = 133;
290  break;
291  case 4:
292  $uploadName = "child$id";
293  $uploadDate = "03-01-2020";
294  $fileSize = 153;
295  break;
296  default:
297  return null;
298  }
299  $hash = new Hash('sha1checksum', 'md5checksum', 'sha256checksum', $fileSize);
300  return new Upload($folderId, $folderName, $id, $description,
301  $uploadName, $uploadDate, null, $hash);
302  }
303 
309  public function testGetSingleUploadV1()
310  {
311  $this->testGetSingleUpload(ApiVersion::V1);
312  }
318  public function testGetSingleUploadV2()
319  {
320  $this->testGetSingleUpload(ApiVersion::V2);
321  }
322 
328  private function testGetSingleUpload($version)
329  {
330  $uploadId = 3;
331  $options = [
332  "folderId" => null,
333  "name" => null,
334  "status" => null,
335  "assignee" => null,
336  "since" => null
337  ];
338  $upload = $this->getUpload($uploadId);
339  $requestHeaders = new Headers();
340  $body = $this->streamFactory->createStream();
341  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
342  "/uploads/$uploadId"), $requestHeaders, [], [], $body);
343  if ($version == ApiVersion::V2) {
344  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
345  ApiVersion::V2);
346  }
347  $this->dbHelper->shouldReceive('doesIdExist')
348  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
349  $this->uploadDao->shouldReceive('isAccessible')
350  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
351  $this->uploadDao->shouldReceive('getParentItemBounds')
352  ->withArgs([$uploadId])->andReturn($this->getUploadBounds($uploadId));
353  $this->dbHelper->shouldReceive('getUploads')
354  ->withArgs([$this->userId, $this->groupId, 100, 1, $uploadId, $options,
355  true, $version])->andReturn([1, [$upload->getArray()]]);
356  $expectedResponse = (new ResponseHelper())->withJson($upload->getArray(), 200);
357  $actualResponse = $this->uploadController->getUploads($request,
358  new ResponseHelper(), ['id' => $uploadId]);
359  $this->assertEquals($expectedResponse->getStatusCode(),
360  $actualResponse->getStatusCode());
361  $this->assertEquals($this->getResponseJson($expectedResponse),
362  $this->getResponseJson($actualResponse));
363  }
364 
371  {
372  $uploadId = 3;
373  $requestHeaders = new Headers();
374  $body = $this->streamFactory->createStream();
375  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
376  "/uploads/$uploadId"), $requestHeaders, [], [], $body);
377  $this->dbHelper->shouldReceive('doesIdExist')
378  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
379  $this->uploadDao->shouldReceive('isAccessible')
380  ->withArgs([$uploadId, $this->groupId])->andReturn(false);
381  $this->expectException(HttpForbiddenException::class);
382 
383  $this->uploadController->getUploads($request, new ResponseHelper(),
384  ['id' => $uploadId]);
385  }
386 
394  public function testGetUploadWithFiltersV1()
395  {
396  $this->testGetUploadWithFilters(ApiVersion::V1);
397  }
405  public function testGetUploadWithFiltersV2()
406  {
407  $this->testGetUploadWithFilters(ApiVersion::V2);
408  }
409 
410  private function testGetUploadWithFilters($version)
411  {
412  $options = [
413  "folderId" => null,
414  "name" => null,
415  "status" => null,
416  "assignee" => null,
417  "since" => null
418  ];
419 
420  // Test for folder filter
421  $folderId = 2;
422  $folderOptions = $options;
423  $folderOptions["folderId"] = $folderId;
424  $requestHeaders = new Headers();
425  $body = $this->streamFactory->createStream();
426  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
427  "/uploads", UploadController::FOLDER_PARAM . "=$folderId"),
428  $requestHeaders, [], [], $body);
429  if ($version == ApiVersion::V2) {
430  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
431  ApiVersion::V2);
432  }
433  $this->folderDao->shouldReceive('isFolderAccessible')
434  ->withArgs([$folderId, $this->userId])->andReturn(true)->once();
435  $this->dbHelper->shouldReceive('getUploads')
436  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $folderOptions,
437  true, $version])->andReturn([1, []])->once();
438  $this->uploadController->getUploads($request, new ResponseHelper(), []);
439 
440  // Test for name filter
441  $name = "foss";
442  $nameOptions = $options;
443  $nameOptions["name"] = $name;
444  $requestHeaders = new Headers();
445  $body = $this->streamFactory->createStream();
446  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
447  "/uploads", UploadController::FILTER_NAME . "=$name"), $requestHeaders,
448  [], [], $body);
449  if ($version == ApiVersion::V2) {
450  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
451  ApiVersion::V2);
452  }
453  $this->dbHelper->shouldReceive('getUploads')
454  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $nameOptions,
455  true, $version])->andReturn([1, []])->once();
456  $this->uploadController->getUploads($request, new ResponseHelper(), []);
457 
458  // Test for status filter
459  $statusString = "InProgress";
460  $status = UploadStatus::IN_PROGRESS;
461  $statusOptions = $options;
462  $statusOptions["status"] = $status;
463  $requestHeaders = new Headers();
464  $body = $this->streamFactory->createStream();
465  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
466  "/uploads", UploadController::FILTER_STATUS . "=$statusString"),
467  $requestHeaders, [], [], $body);
468  if ($version == ApiVersion::V2) {
469  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
470  ApiVersion::V2);
471  }
472  $this->dbHelper->shouldReceive('getUploads')
473  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $statusOptions,
474  true, $version])->andReturn([1, []])->once();
475  $this->uploadController->getUploads($request, new ResponseHelper(), []);
476 
477  // Test for assignee filter
478  $assignee = "-me-";
479  $assigneeOptions = $options;
480  $assigneeOptions["assignee"] = $this->userId;
481  $requestHeaders = new Headers();
482  $body = $this->streamFactory->createStream();
483  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
484  "/uploads", UploadController::FILTER_ASSIGNEE . "=$assignee"),
485  $requestHeaders, [], [], $body);
486  if ($version == ApiVersion::V2) {
487  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
488  ApiVersion::V2);
489  }
490  $this->dbHelper->shouldReceive('getUploads')
491  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $assigneeOptions,
492  true, $version])->andReturn([1, []])->once();
493  $this->uploadController->getUploads($request, new ResponseHelper(), []);
494 
495  // Test for since filter
496  $since = "2021-02-28";
497  $sinceOptions = $options;
498  $sinceOptions["since"] = strtotime($since);
499  $requestHeaders = new Headers();
500  $body = $this->streamFactory->createStream();
501  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
502  "/uploads", UploadController::FILTER_DATE . "=$since"),
503  $requestHeaders, [], [], $body);
504  if ($version == ApiVersion::V2) {
505  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
506  ApiVersion::V2);
507  }
508  $this->dbHelper->shouldReceive('getUploads')
509  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $sinceOptions,
510  true, $version])->andReturn([1, []])->once();
511  $this->uploadController->getUploads($request, new ResponseHelper(), []);
512 
513  // Test for status and since filter
514  $statusString = "Open";
515  $status = UploadStatus::OPEN;
516  $since = "2021-02-28";
517  $combOptions = $options;
518  $combOptions["since"] = strtotime($since);
519  $combOptions["status"] = $status;
520  $requestHeaders = new Headers();
521  $body = $this->streamFactory->createStream();
522  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
523  "/uploads", UploadController::FILTER_DATE . "=$since&" .
524  UploadController::FILTER_STATUS . "=$statusString"),
525  $requestHeaders, [], [], $body);
526  if ($version == ApiVersion::V2) {
527  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
528  ApiVersion::V2);
529  }
530  $this->dbHelper->shouldReceive('getUploads')
531  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $combOptions,
532  true, $version])->andReturn([1, []])->once();
533  $this->uploadController->getUploads($request, new ResponseHelper(), []);
534  }
535 
541  public function testGetUploadsV1(){
542  $this->testGetUploads(ApiVersion::V1);
543  }
549  public function testGetUploadsV2(){
550  $this->testGetUploads(ApiVersion::V2);
551  }
552  private function testGetUploads($version)
553  {
554  $uploads = [
555  $this->getUpload(2)->getArray(),
556  $this->getUpload(3)->getArray(),
557  $this->getUpload(4)->getArray()
558  ];
559  $options = [
560  "folderId" => null,
561  "name" => null,
562  "status" => null,
563  "assignee" => null,
564  "since" => null
565  ];
566  $requestHeaders = new Headers();
567  $body = $this->streamFactory->createStream();
568  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
569  "/uploads/"), $requestHeaders, [], [], $body);
570  if ($version == ApiVersion::V2) {
571  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
572  ApiVersion::V2);
573  }
574  $this->dbHelper->shouldReceive('getUploads')
575  ->withArgs([$this->userId, $this->groupId, 100, 1, null, $options, true, $version])
576  ->andReturn([1, $uploads]);
577  $expectedResponse = (new ResponseHelper())->withJson($uploads, 200);
578  $actualResponse = $this->uploadController->getUploads($request,
579  new ResponseHelper(), []);
580  $this->assertEquals($expectedResponse->getStatusCode(),
581  $actualResponse->getStatusCode());
582  $this->assertEquals($this->getResponseJson($expectedResponse),
583  $this->getResponseJson($actualResponse));
584  }
585 
593  {
594  $uploadId = 3;
595  $requestHeaders = new Headers();
596  $body = $this->streamFactory->createStream();
597  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
598  "/uploads/"), $requestHeaders, [], [], $body);
599  $this->dbHelper->shouldReceive('doesIdExist')
600  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
601  $this->uploadDao->shouldReceive('isAccessible')
602  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
603  $this->uploadDao->shouldReceive('getParentItemBounds')
604  ->withArgs([$uploadId])->andReturn(false);
605  $this->expectException(HttpServiceUnavailableException::class);
606 
607  $this->uploadController->getUploads($request, new ResponseHelper(),
608  ['id' => $uploadId]);
609  }
610 
616  public function testCopyUploadV1()
617  {
618  $this->testCopyUpload(ApiVersion::V1);
619  }
625  public function testCopyUploadV2()
626  {
627  $this->testCopyUpload(ApiVersion::V2);
628  }
634  private function testCopyUpload($version)
635  {
636  $uploadId = 3;
637  $folderId = 5;
638  $info = new Info(202, "Upload $uploadId will be copied to folder $folderId",
639  InfoType::INFO);
640 
641  $this->restHelper->shouldReceive('copyUpload')
642  ->withArgs([$uploadId, $folderId, true])->andReturn($info);
643  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
644  $info->getCode());
645 
646  $requestHeaders = new Headers();
647  $body = $this->streamFactory->createStream();
648 
649  if($version==ApiVersion::V1){
650  $requestHeaders->setHeader('folderId', $folderId);
651  $requestHeaders->setHeader('action', 'copy');
652  $request = new Request("PUT", new Uri("HTTP", "localhost"),
653  $requestHeaders, [], [], $body);
654  $actualResponse = $this->uploadController->moveUpload($request,
655  new ResponseHelper(), ['id' => $uploadId]);
656  }
657  else{
658  $request = new Request("PUT", new Uri("HTTP", "localhost"),
659  $requestHeaders, [], [], $body);
660  if ($version == ApiVersion::V2) {
661  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
662  ApiVersion::V2);
663  }
664  $actualResponse = $this->uploadController->moveUpload($request->withUri($request->getUri()->withQuery("folderId=$folderId&action=copy")),
665  new ResponseHelper(), ['id' => $uploadId]);
666  }
667  $this->assertEquals($expectedResponse->getStatusCode(),
668  $actualResponse->getStatusCode());
669  $this->assertEquals($this->getResponseJson($expectedResponse),
670  $this->getResponseJson($actualResponse));
671  }
682  {
683  $this->testMoveUploadInvalidFolder(ApiVersion::V1);
684  }
691  {
693  }
698  private function testMoveUploadInvalidFolder($version = ApiVersion::V2)
699  {
700  $uploadId = 3;
701 
702  $requestHeaders = new Headers();
703  $requestHeaders->setHeader('folderId', 'alpha');
704  $requestHeaders->setHeader('action', 'move');
705  $body = $this->streamFactory->createStream();
706  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
707  $requestHeaders, [], [], $body);
708  if ($version==ApiVersion::V2) {
709  $request = $request->withQueryParams(['folderId' => 'alpha', 'action' => 'move']);
710  } else {
711  $request = $request->withHeader("folderId", "alpha")
712  ->withHeader("action", "move");
713  }
714  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
715  $this->expectException(HttpBadRequestException::class);
716 
717  $this->uploadController->moveUpload($request, new ResponseHelper(),
718  ['id' => $uploadId]);
719  }
720 
728  public function testPostUploadV1()
729  {
730  $this->testPostUpload(ApiVersion::V1);
731  }
732 
740  public function testPostUploadV2()
741  {
742  $this->testPostUpload(ApiVersion::V2);
743  }
744 
749  private function testPostUpload(int $version)
750  {
751  $folderId = 2;
752  $uploadId = 20;
753  $uploadDescription = "Test Upload";
754 
755  $requestHeaders = new Headers();
756  $requestHeaders->setHeader('Content-Type', 'application/json');
757  if ($version == ApiVersion::V2) {
758  $reqBody = [
759  "location" => "data",
760  "folderId" => $folderId,
761  "uploadDescription" => $uploadDescription,
762  "ignoreScm" => "true",
763  "scanOptions" => "scanOptions",
764  "uploadType" => "vcs"
765  ];
766  } else {
767  $reqBody = [
768  "location" => "data",
769  "scanOptions" => "scanOptions"
770  ];
771  $requestHeaders->setHeader('folderId', $folderId);
772  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
773  $requestHeaders->setHeader('ignoreScm', 'true');
774  $requestHeaders->setHeader('Content-Type', 'application/json');
775  $requestHeaders->setHeader('uploadType', 'vcs');
776  }
777 
778 
779  $body = $this->streamFactory->createStream(json_encode(
780  $reqBody
781  ));
782  $request = new Request("POST", new Uri("HTTP", "localhost"),
783  $requestHeaders, [], [], $body);
784  if ($version == ApiVersion::V2) {
785  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
786  ApiVersion::V2);
787  }
788  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
789  $uploadHelper->shouldReceive('createNewUpload')
790  ->withArgs([$reqBody["location"], $folderId, $uploadDescription, 'protected', 'true',
791  'vcs', false])
792  ->andReturn([true, '', '', $uploadId]);
793 
794  $info = new Info(201, intval(20), InfoType::INFO);
795 
796  $uploadHelper->shouldReceive('handleScheduleAnalysis')->withArgs([$uploadId,$folderId,$reqBody["scanOptions"],false])
797  ->andReturn($info);
798 
799  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
800  $this->folderDao->shouldReceive('isFolderAccessible')
801  ->withArgs([$folderId])->andReturn(true);
802 
803  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
804  $info->getCode());
805  $actualResponse = $this->uploadController->postUpload($request,
806  new ResponseHelper(), []);
807  $this->assertEquals($expectedResponse->getStatusCode(),
808  $actualResponse->getStatusCode());
809  $this->assertEquals($this->getResponseJson($expectedResponse),
810  $this->getResponseJson($actualResponse));
811  }
812 
821  {
822  $this->testPostUploadFolderNotAccessible(ApiVersion::V1);
823  }
824 
833  {
834  $this->testPostUploadFolderNotAccessible(ApiVersion::V2);
835  }
836 
841  private function testPostUploadFolderNotAccessible(int $version)
842  {
843  $folderId = 2;
844  $uploadDescription = "Test Upload";
845 
846  $requestHeaders = new Headers();
847  $requestHeaders->setHeader('Content-type', 'application/json');
848  if ($version == ApiVersion::V2) {
849  $body = $this->streamFactory->createStream(json_encode([
850  "location" => "data",
851  "folderId" => $folderId,
852  "uploadDescription" => $uploadDescription,
853  "ignoreScm" => "true",
854  "scanOptions" => "scanOptions",
855  "uploadType" => "vcs"
856  ]));
857  } else {
858  $body = $this->streamFactory->createStream(json_encode([
859  "location" => "data",
860  "scanOptions" => "scanOptions"
861  ]));
862  $requestHeaders->setHeader('folderId', $folderId);
863  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
864  $requestHeaders->setHeader('ignoreScm', 'true');
865  $requestHeaders->setHeader('Content-Type', 'application/json');
866  $requestHeaders->setHeader('uploadType', 'vcs');
867  }
868  $request = new Request("POST", new Uri("HTTP", "localhost"),
869  $requestHeaders, [], [], $body);
870  if ($version == ApiVersion::V2) {
871  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
872  ApiVersion::V2);
873  }
874 
875  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
876 
877  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
878  $this->folderDao->shouldReceive('isFolderAccessible')
879  ->withArgs([$folderId])->andReturn(false);
880  $this->expectException(HttpForbiddenException::class);
881 
882  $this->uploadController->postUpload($request, new ResponseHelper(),
883  []);
884  }
885 
894  {
895  $this->testPostUploadFolderNotFound(ApiVersion::V1);
896  }
897 
906  {
907  $this->testPostUploadFolderNotFound(ApiVersion::V2);
908  }
909 
914  private function testPostUploadFolderNotFound(int $version)
915  {
916  $folderId = 8;
917  $uploadDescription = "Test Upload";
918 
919  $requestHeaders = new Headers();
920  $requestHeaders->setHeader('Content-type', 'application/json');
921  if ($version == ApiVersion::V2) {
922  $body = $this->streamFactory->createStream(json_encode([
923  "location" => "vcsData",
924  "folderId" => $folderId,
925  "uploadDescription" => $uploadDescription,
926  "ignoreScm" => "true",
927  "scanOptions" => "scanOptions",
928  "uploadType" => "vcs"
929  ]));
930  } else {
931  $body = $this->streamFactory->createStream(json_encode([
932  "location" => "vcsData",
933  "scanOptions" => "scanOptions"
934  ]));
935  $requestHeaders->setHeader('folderId', $folderId);
936  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
937  $requestHeaders->setHeader('ignoreScm', 'true');
938  $requestHeaders->setHeader('Content-Type', 'application/json');
939  $requestHeaders->setHeader('uploadType', 'vcs');
940  }
941  $request = new Request("POST", new Uri("HTTP", "localhost"),
942  $requestHeaders, [], [], $body);
943  if ($version == ApiVersion::V2) {
944  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
945  ApiVersion::V2);
946  }
947 
948  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
949 
950  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
951  $this->expectException(HttpNotFoundException::class);
952 
953  $this->uploadController->postUpload($request, new ResponseHelper(),
954  []);
955  }
956 
965  {
966  $this->testPostUploadInternalError(ApiVersion::V1);
967  }
968 
977  {
978  $this->testPostUploadInternalError(ApiVersion::V2);
979  }
980 
985  private function testPostUploadInternalError(int $version)
986  {
987  $folderId = 3;
988  $uploadDescription = "Test Upload";
989  $errorMessage = "Failed to insert upload record";
990  $errorDesc = "";
991 
992 
993  $requestHeaders = new Headers();
994  $requestHeaders->setHeader('Content-type', 'application/json');
995  if ($version == ApiVersion::V2) {
996  $body = $this->streamFactory->createStream(json_encode([
997  "location" => "vcsData",
998  "folderId" => $folderId,
999  "uploadDescription" => $uploadDescription,
1000  "ignoreScm" => "true",
1001  "scanOptions" => "scanOptions",
1002  "uploadType" => "vcs"
1003  ]));
1004  } else {
1005  $body = $this->streamFactory->createStream(json_encode([
1006  "location" => "vcsData",
1007  "scanOptions" => "scanOptions"
1008  ]));
1009  $requestHeaders->setHeader('folderId', $folderId);
1010  $requestHeaders->setHeader('uploadDescription', $uploadDescription);
1011  $requestHeaders->setHeader('ignoreScm', 'true');
1012  $requestHeaders->setHeader('Content-Type', 'application/json');
1013  $requestHeaders->setHeader('uploadType', 'vcs');
1014  }
1015 
1016  $request = new Request("POST", new Uri("HTTP", "localhost"),
1017  $requestHeaders, [], [], $body);
1018  if ($version == ApiVersion::V2) {
1019  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
1020  ApiVersion::V2);
1021  }
1022 
1023  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
1024  $uploadHelper->shouldReceive('createNewUpload')
1025  ->withArgs(['vcsData', $folderId, $uploadDescription, 'protected', 'true',
1026  'vcs', false])
1027  ->andReturn([false, $errorMessage, $errorDesc, [-1]]);
1028 
1029  $this->folderDao->shouldReceive('getAllFolderIds')->andReturn([2,3,4]);
1030  $this->folderDao->shouldReceive('isFolderAccessible')
1031  ->withArgs([$folderId])->andReturn(true);
1032  $this->expectException(HttpInternalServerErrorException::class);
1033 
1034  $this->uploadController->postUpload($request, new ResponseHelper(), []);
1035  }
1036 
1044  public function testGetUploadLicensesV1()
1045  {
1046  $this->testGetUploadLicenses(ApiVersion::V1);
1047  }
1055  public function testGetUploadLicensesV2()
1056  {
1057  $this->testGetUploadLicenses(ApiVersion::V2);
1058  }
1059  private function testGetUploadLicenses($version)
1060  {
1061  $uploadId = 3;
1062  $agentsRun = [
1063  ['agentName' => 'nomos', 'currentAgentId' => 2, 'isAgentRunning' => false],
1064  ['agentName' => 'monk', 'currentAgentId' => 3, 'isAgentRunning' => false]
1065  ];
1066  $licenseResponse = [
1067  ['filePath' => 'filea', 'agentFindings' => 'MIT', 'conclusions' => 'MIT'],
1068  ['filePath' => 'fileb', 'agentFindings' => 'MIT',
1069  'conclusions' => 'No_license_found']
1070  ];
1071 
1072  $requestHeaders = new Headers();
1073  $body = $this->streamFactory->createStream();
1074  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1075  "/uploads/$uploadId/licenses", UploadController::AGENT_PARAM .
1076  "=nomos,monk&containers=false"),
1077  $requestHeaders, [], [], $body);
1078  if ($version == ApiVersion::V2) {
1079  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
1080  ApiVersion::V2);
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 
1096  $uploadHelper = M::mock('overload:Fossology\UI\Api\Helper\UploadHelper');
1097  $uploadHelper->shouldReceive('getUploadLicenseList')
1098  ->withArgs([$uploadId, ['nomos', 'monk'], false, true, false, 0, 50, $version])
1099  ->andReturn(([[$licenseResponse], 1]));
1100 
1101  $expectedResponse = (new ResponseHelper())->withJson($licenseResponse, 200);
1102 
1103  $actualResponse = $this->uploadController->getUploadLicenses($request,
1104  new ResponseHelper(), ['id' => $uploadId]);
1105  $this->assertEquals($expectedResponse->getStatusCode(),
1106  $actualResponse->getStatusCode());
1107  $this->assertEquals($this->getResponseJson($expectedResponse),
1108  $this->getResponseJson($actualResponse)[0]);
1109  $this->assertEquals('1',
1110  $actualResponse->getHeaderLine('X-Total-Pages'));
1111  }
1112 
1122  {
1123  $this->testGetUploadLicensesPendingScan(ApiVersion::V1);
1124  }
1134  {
1136  }
1141  private function testGetUploadLicensesPendingScan($version = ApiVersion::V2)
1142  {
1143  $uploadId = 3;
1144  $agentsRun = [
1145  ['agentName' => 'nomos', 'currentAgentId' => 2, 'isAgentRunning' => true],
1146  ['agentName' => 'monk', 'currentAgentId' => 3, 'isAgentRunning' => false]
1147  ];
1148 
1149  $requestHeaders = new Headers();
1150  $body = $this->streamFactory->createStream();
1151  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1152  "/uploads/$uploadId/licenses", UploadController::AGENT_PARAM .
1153  "=nomos,monk&containers=false"),
1154  $requestHeaders, [], [], $body);
1155  if ($version == ApiVersion::V2) {
1156  $request = $request->withQueryParams(['page' => 1, 'limit' => 2, "agent" =>"nomos,monk" ]);
1157  } else {
1158  $request = $request->withHeader("limit",2)
1159  ->withHeader("page",1);
1160  }
1161  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1162 
1163  $this->agentDao->shouldReceive("arsTableExists")->withAnyArgs()->andReturn(true);
1164  $this->agentDao->shouldReceive("getRunningAgentIds")->withAnyArgs()->andReturn([$agentsRun]);
1165  $this->agentDao->shouldReceive("getSuccessfulAgentEntries")->withAnyArgs()->andReturn([]);
1166  $scanJobProxy = M::mock('overload:Fossology\Lib\Proxy\ScanJobProxy');
1167  $this->dbHelper->shouldReceive('doesIdExist')
1168  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1169  $this->uploadDao->shouldReceive('isAccessible')
1170  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1171  $this->uploadDao->shouldReceive('getParentItemBounds')
1172  ->withArgs([$uploadId])->andReturn($this->getUploadBounds($uploadId));
1173  $this->agentDao->shouldReceive('arsTableExists')
1174  ->withArgs([M::anyOf('nomos', 'monk')])->andReturn(true);
1175 
1176  $scanJobProxy->shouldReceive('createAgentStatus')
1177  ->withArgs([['nomos', 'monk']])
1178  ->andReturn($agentsRun);
1179  $this->expectException(HttpServiceUnavailableException::class);
1180 
1181  $this->uploadController->getUploadLicenses($request, new ResponseHelper(),
1182  ['id' => $uploadId]);
1183  }
1184 
1190  public function testUpdateUpload()
1191  {
1192  $upload = 2;
1193  $assignee = 4;
1194  $status = UploadStatus::REJECTED;
1195  $comment = "Not helpful";
1196 
1197  $resource = fopen('data://text/plain;base64,' .
1198  base64_encode($comment), 'r+');
1199  $body = $this->streamFactory->createStreamFromResource($resource);
1200  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1201  "/uploads/$upload", UploadController::FILTER_STATUS . "=Rejected&" .
1202  UploadController::FILTER_ASSIGNEE . "=$assignee"),
1203  new Headers(), [], [], $body);
1204 
1205  $this->userDao->shouldReceive('isAdvisorOrAdmin')
1206  ->withArgs([$this->userId, $this->groupId])
1207  ->andReturn(true);
1208  $this->userDao->shouldReceive('getUserChoices')
1209  ->withArgs([$this->groupId])
1210  ->andReturn([$this->userId => "fossy", $assignee => "friendly-fossy"]);
1211  $this->dbManager->shouldReceive('getSingleRow')
1212  ->withArgs([M::any(), [$assignee, $this->groupId, $upload], M::any()]);
1213  $this->dbManager->shouldReceive('getSingleRow')
1214  ->withArgs([M::any(), [$status, $comment, $this->groupId, $upload],
1215  M::any()]);
1216  $this->dbManager->shouldReceive('getSingleRow')
1217  ->withArgs([M::any(), [$upload], M::any()])
1218  ->andReturn(["exists" => ""]);
1219 
1220  $info = new Info(202, "Upload updated successfully.", InfoType::INFO);
1221  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
1222  $info->getCode());
1223  $actualResponse = $this->uploadController->updateUpload($request,
1224  new ResponseHelper(), ['id' => $upload]);
1225  $this->assertEquals($expectedResponse->getStatusCode(),
1226  $actualResponse->getStatusCode());
1227  $this->assertEquals($this->getResponseJson($expectedResponse),
1228  $this->getResponseJson($actualResponse));
1229  }
1230 
1236  public function testUpdateUploadNoPerm()
1237  {
1238  $upload = 2;
1239  $assignee = 4;
1240  $comment = "Not helpful";
1241 
1242  $resource = fopen('data://text/plain;base64,' .
1243  base64_encode($comment), 'r+');
1244  $body = $this->streamFactory->createStreamFromResource($resource);
1245  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
1246  "/uploads/$upload", UploadController::FILTER_STATUS . "=Rejected&" .
1247  UploadController::FILTER_ASSIGNEE . "=$assignee"),
1248  new Headers(), [], [], $body);
1249 
1250  $this->userDao->shouldReceive('isAdvisorOrAdmin')
1251  ->withArgs([$this->userId, $this->groupId])
1252  ->andReturn(false);
1253  $this->expectException(HttpForbiddenException::class);
1254 
1255  $this->uploadController->updateUpload($request, new ResponseHelper(),
1256  ['id' => $upload]);
1257  }
1258 
1264  public function testGetMainLicenses()
1265  {
1266  $uploadId = 1;
1267  $licenseIds = array();
1268  $licenseId = 123;
1269  $licenseIds[$licenseId] = $licenseId;
1270  $license = new License($licenseId, "MIT", "MIT License", "risk", "texts", [],
1271  'type', false);
1272 
1273  $this->uploadDao->shouldReceive('isAccessible')
1274  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1275  $this->dbHelper->shouldReceive('doesIdExist')
1276  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1277  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1278  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], false])->andReturn([]);
1279  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], true])->andReturn([]);
1280  $this->licenseDao->shouldReceive('getLicenseById')->withArgs([$licenseId])->andReturn($license);
1281 
1282  $licenses[] = $license->getArray();
1283  $expectedResponse = (new ResponseHelper())->withJson($licenses, 200);
1284  $actualResponse = $this->uploadController->getMainLicenses(null, new ResponseHelper(), ['id' => $uploadId]);
1285  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
1286  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
1287  }
1288 
1289 
1295  public function testSetMainLicense()
1296  {
1297  $uploadId = 2;
1298  $shortName = "MIT";
1299  $licenseId = 1;
1300  $rq = [
1301  "shortName" => $shortName,
1302  ];
1303  $license = new License(2, $shortName, "MIT License", "risk", "texts", [],
1304  'type', 1);
1305  $licenseIds[$licenseId] = $licenseId;
1306  $this->uploadDao->shouldReceive('isAccessible')
1307  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1308  $this->dbHelper->shouldReceive('doesIdExist')
1309  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1310  $this->licenseDao->shouldReceive('getLicenseByShortName')
1311  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1312  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1313  $this->clearingDao->shouldReceive('makeMainLicense')
1314  ->withArgs([$uploadId, $this->groupId, $license->getId()])->andReturn(null);
1315 
1316  $info = new Info(200, "Successfully added new main license", InfoType::INFO);
1317 
1318  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
1319  $reqBody = $this->streamFactory->createStream(json_encode(
1320  $rq
1321  ));
1322  $requestHeaders = new Headers();
1323  $requestHeaders->setHeader('Content-Type', 'application/json');
1324  $request = new Request("POST", new Uri("HTTP", "localhost"),
1325  $requestHeaders, [], [], $reqBody);
1326 
1327  $actualResponse = $this->uploadController->setMainLicense($request, new ResponseHelper(), ['id' => $uploadId]);
1328 
1329  $this->assertEquals($expectedResponse->getStatusCode(),
1330  $actualResponse->getStatusCode());
1331  $this->assertEquals($this->getResponseJson($expectedResponse),
1332  $this->getResponseJson($actualResponse));
1333  }
1334 
1340  public function testSetMainLicense_exists()
1341  {
1342  $uploadId = 2;
1343  $shortName = "MIT";
1344  $licenseId = 1;
1345  $rq = [
1346  "shortName" => $shortName,
1347  ];
1348  $license = new License($licenseId, $shortName, "MIT License", "risk", "texts", [],
1349  'type', 1);
1350  $licenseIds[$licenseId] = $licenseId;
1351  $this->uploadDao->shouldReceive('isAccessible')
1352  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1353  $this->dbHelper->shouldReceive('doesIdExist')
1354  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1355  $this->licenseDao->shouldReceive('getLicenseByShortName')
1356  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1357  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1358  $this->clearingDao->shouldReceive('makeMainLicense')
1359  ->withArgs([$uploadId, $this->groupId, $license->getId()])->andReturn(null);
1360 
1361  $reqBody = $this->streamFactory->createStream(json_encode(
1362  $rq
1363  ));
1364  $requestHeaders = new Headers();
1365  $requestHeaders->setHeader('Content-Type', 'application/json');
1366  $request = new Request("POST", new Uri("HTTP", "localhost"),
1367  $requestHeaders, [], [], $reqBody);
1368  $this->expectException(HttpBadRequestException::class);
1369 
1370  $this->uploadController->setMainLicense($request, new ResponseHelper(), ['id' => $uploadId]);
1371  }
1372 
1373 
1379  public function testRemoveMainLicense()
1380  {
1381  $uploadId = 3;
1382  $licenseId = 1;
1383  $shortName = "MIT";
1384  $license = new License($licenseId, $shortName, "MIT License", "risk", "texts", [],
1385  'type', 1);
1386  $licenseIds[$licenseId] = $licenseId;
1387 
1388  $this->uploadDao->shouldReceive('isAccessible')
1389  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1390  $this->dbHelper->shouldReceive('doesIdExist')
1391  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1392  $this->dbHelper->shouldReceive('doesIdExist')
1393  ->withArgs(["license_ref", "rf_pk", $licenseId])->andReturn(true);
1394  $this->clearingDao->shouldReceive('getMainLicenseIds')->withArgs([$uploadId, $this->groupId])->andReturn($licenseIds);
1395 
1396  $this->clearingDao->shouldReceive('removeMainLicense')->withArgs([$uploadId, $this->groupId, $licenseId])->andReturn(null);
1397  $this->licenseDao->shouldReceive('getLicenseByShortName')
1398  ->withArgs([$shortName, $this->groupId])->andReturn($license);
1399 
1400  $info = new Info(200, "Main license removed successfully.", InfoType::INFO);
1401  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
1402  $info->getCode());
1403  $actualResponse = $this->uploadController->removeMainLicense(null,
1404  new ResponseHelper(), ['id' => $uploadId, 'shortName' => $shortName]);
1405  $this->assertEquals($expectedResponse->getStatusCode(),
1406  $actualResponse->getStatusCode());
1407  $this->assertEquals($this->getResponseJson($expectedResponse),
1408  $this->getResponseJson($actualResponse));
1409  }
1410 
1417  {
1418  $uploadId = 3;
1419  $this->uploadDao->shouldReceive('isAccessible')
1420  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1421  $this->dbHelper->shouldReceive('doesIdExist')
1422  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1423  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
1424  $this->uploadDao->shouldReceive("getGlobalDecisionSettingsFromInfo")->andReturn(false);
1425  $this->agentDao->shouldReceive("arsTableExists")->andReturn(true);
1426  $this->agentDao->shouldReceive("getSuccessfulAgentEntries")->andReturn([['agent_id' => 1, 'agent_rev' => 1]]);
1427  $this->agentDao->shouldReceive("getCurrentAgentRef")->andReturn(new AgentRef(1, "agent", 1));
1428  $this->dbManager->shouldReceive("getSingleRow")
1429  ->withArgs([M::any(), [], 'no_license_uploadtree' . $uploadId])
1430  ->andReturn(['count' => 1]);
1431  $this->dbManager->shouldReceive("getSingleRow")
1432  ->withArgs([M::any(), [], 'already_cleared_uploadtree' . $uploadId])
1433  ->andReturn(['count' => 0]);
1434  $this->licenseDao->shouldReceive('getLicenseByShortName')
1435  ->withArgs(['No_license_found'])->andReturn(null);
1436  $this->licenseDao->shouldReceive('getLicenseByShortName')
1437  ->withArgs(['Void'])->andReturn(null);
1438  $res = [
1439  "totalFilesOfInterest" => 1,
1440  "totalFilesCleared" => 1,
1441  ];
1442  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
1443  $actualResponse = $this->uploadController->getClearingProgressInfo(null,
1444  new ResponseHelper(), ['id' => $uploadId]);
1445  $this->assertEquals($expectedResponse->getStatusCode(),
1446  $actualResponse->getStatusCode());
1447  $this->assertEquals($this->getResponseJson($expectedResponse),
1448  $this->getResponseJson($actualResponse));
1449  }
1455  public function testGetReuseReportSummary()
1456  {
1457  $uploadId = 2;
1458  $reuseReportSummary = [
1459  'declearedLicense' => "",
1460  'clearedLicense' => "MIT, BSD-3-Clause",
1461  'usedLicense' => "",
1462  'unusedLicense' => "",
1463  'missingLicense' => "MIT, BSD-3-Clause",
1464  ];
1465  $this->uploadDao->shouldReceive('isAccessible')
1466  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1467  $this->dbHelper->shouldReceive('doesIdExist')
1468  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1469  $this->reuseReportProcess->shouldReceive('getReuseSummary')
1470  ->withArgs([$uploadId])->andReturn($reuseReportSummary);
1471 
1472  $expectedResponse = (new ResponseHelper())->withJson($reuseReportSummary,
1473  200);
1474  $actualResponse = $this->uploadController->getReuseReportSummary(
1475  null, new ResponseHelper(), ['id' => $uploadId]);
1476  $this->assertEquals($expectedResponse->getStatusCode(),
1477  $actualResponse->getStatusCode());
1478  $this->assertEquals($this->getResponseJson($expectedResponse),
1479  $this->getResponseJson($actualResponse));
1480  }
1481 
1482 
1491  {
1492  $publicPerm = 0;
1493  $uploadId = 2;
1494 
1495  $this->uploadDao->shouldReceive('isAccessible')
1496  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1497  $this->dbHelper->shouldReceive('doesIdExist')
1498  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1499 
1500  $this->restHelper->shouldReceive('getUploadPermissionDao')->andReturn($this->uploadPermissionDao);
1501  $this->uploadPermissionDao->shouldReceive("getPublicPermission")->withAnyArgs()->andReturn($publicPerm);
1502  $this->uploadPermissionDao->shouldReceive("getPermissionGroups")->withAnyArgs()->andReturn([]);
1503  $this->restHelper->shouldReceive("getGroupId")->andReturn($this->groupId);
1504  $this->restHelper->shouldReceive("getUserId")->andReturn($this->userId);
1505  $this->restHelper->shouldReceive("getUploadDao")->andReturn($this->uploadDao);
1506 
1507  $body = $this->streamFactory->createStream();
1508  $requestHeaders = new Headers();
1509  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
1510  $requestHeaders, [], [], $body);
1511  $response = new ResponseHelper();
1512  $actualResponse = $this->uploadController->getGroupsWithPermissions($request,$response,["id"=>$this->groupId]);
1513  $this->assertEquals(200,$actualResponse->getStatusCode());
1514 
1515  }
1516 
1524  {
1525  $uploadId = 2;
1526  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(false);
1527 
1528  $this->dbHelper->shouldReceive('doesIdExist')
1529  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(false);
1530  $body = $this->streamFactory->createStream();
1531  $requestHeaders = new Headers();
1532  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
1533  $requestHeaders, [], [], $body);
1534  $response = new ResponseHelper();
1535  $this->expectException(HttpNotFoundException::class);
1536  $this->uploadController->getGroupsWithPermissions($request,$response,["id"=>$this->groupId]);
1537 
1538  }
1539 
1547  {
1548  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(true);
1549  $this->uploadDao->shouldReceive('isAccessible')->withAnyArgs()->andReturn(false);
1550 
1551  $body = $this->streamFactory->createStream();
1552  $requestHeaders = new Headers();
1553  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
1554  $requestHeaders, [], [], $body);
1555  $response = new ResponseHelper();
1556  $this->expectException(HttpForbiddenException::class);
1557  $this->uploadController->getGroupsWithPermissions($request,$response,["id"=>$this->groupId]);
1558 
1559  }
1560 
1565  public function testGetAllAgents()
1566  {
1567  $groupId = 2;
1568  $uploadId = 3;
1569  $agentsRun = [
1570  ["uploadId" => $uploadId, 'agentName' => 'nomos', 'successfulAgents'=> [], 'currentAgentId' => 2, 'isAgentRunning' => false],
1571  ["uploadId" => $uploadId,'agentName' => 'monk', "successfulAgents" => [], 'currentAgentId' => 3, 'isAgentRunning' => false]
1572  ];
1573 
1574  $this->restHelper->shouldReceive("getGroupId")->andReturn($groupId);
1575  $this->agentDao->shouldReceive("getCurrentAgentRef")->withAnyArgs()->andReturn(new AgentRef($uploadId,"momoa",45));
1576 
1577  $this->uploadDao->shouldReceive("isAccessible")->withAnyArgs()->andReturn(true);
1578  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(true);
1579  $scanJobProxy = M::mock('overload:Fossology\Lib\Proxy\ScanJobProxy');
1580  $scanJobProxy->shouldReceive('createAgentStatus')
1581  ->withAnyArgs()
1582  ->andReturn($agentsRun);
1583  $this->agentDao->shouldReceive("arsTableExists")->withAnyArgs()->andReturn(true);
1584  $this->agentDao->shouldReceive("getRunningAgentIds")->withAnyArgs()->andReturn([$agentsRun]);
1585  $this->agentDao->shouldReceive("getSuccessfulAgentEntries")->withAnyArgs()->andReturn([]);
1586 
1587  $body = $this->streamFactory->createStream();
1588  $requestHeaders = new Headers();
1589  $request = new Request("POST", new Uri("HTTP", "localhost"),
1590  $requestHeaders, [], [], $body);
1591 
1592  $actualResponse = $this->uploadController->getAllAgents($request, new ResponseHelper(),["id"=>$uploadId]);
1593  $this->assertEquals(200,$actualResponse->getStatusCode());
1594 
1595  }
1603  {
1604  $uploadId = 3;
1605  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(false);
1606  $this->dbHelper->shouldReceive('doesIdExist')
1607  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(false);
1608 
1609  $this->expectException(HttpNotFoundException::class);
1610  $this->uploadController->getAllAgents(null, new ResponseHelper(),["id"=>$uploadId]);
1611 
1612  }
1620  {
1621  $uploadId = 3;
1622 
1623  $this->uploadDao->shouldReceive('isAccessible')
1624  ->withArgs([$uploadId, $this->groupId])->andReturn(false);
1625  $this->dbHelper->shouldReceive('doesIdExist')
1626  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1627 
1628  $this->expectException(HttpForbiddenException::class);
1629  $this->uploadController->getAllAgents(null, new ResponseHelper(),["id"=>$uploadId]);
1630 
1631  }
1632 
1639  public function testGetEditedLicenses()
1640  {
1641  $groupId = 2;
1642  $uploadId = 3;
1643  $uploadName = "Testing name";
1644  $this->uploadDao->shouldReceive('getParentItemBounds')
1645  ->withAnyArgs()->andReturn($this->getUploadBounds($uploadId));
1646  $this->uploadDao->shouldReceive("isAccessible")->withAnyArgs()->andReturn(true);
1647  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(true);
1648  $this->restHelper->shouldReceive('getUploadDao')->andReturn($this->uploadDao);
1649  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withAnyArgs()->andReturn($uploadName);
1650  $this->clearingDao->shouldReceive("getClearedLicenseIdAndMultiplicities")->withAnyArgs()->andReturn([]);
1651 
1652  $actualResponse = $this->uploadController->getEditedLicenses(null,new ResponseHelper(),["id"=>$groupId]);
1653  $this->assertEquals(200,$actualResponse->getStatusCode());
1654  }
1655 
1663  {
1664  $groupId = 2;
1665  $uploadId = 3;
1666  $this->uploadDao->shouldReceive('getParentItemBounds')
1667  ->withAnyArgs()->andReturn($this->getUploadBounds($uploadId));
1668  $this->uploadDao->shouldReceive("isAccessible")->withAnyArgs()->andReturn(true);
1669  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(false);
1670 
1671  $this->expectException(HttpNotFoundException::class);
1672  $this->uploadController->getEditedLicenses(null,new ResponseHelper(),["id"=>$groupId]);
1673  }
1674 
1682  {
1683  $groupId = 2;
1684  $uploadId = 3;
1685  $this->uploadDao->shouldReceive('getParentItemBounds')
1686  ->withAnyArgs()->andReturn($this->getUploadBounds($uploadId));
1687  $this->uploadDao->shouldReceive("isAccessible")->withAnyArgs()->andReturn(false);
1688  $this->dbHelper->shouldReceive('doesIdExist')->withAnyArgs()->andReturn(true);
1689 
1690  $this->expectException(HttpForbiddenException::class);
1691  $this->uploadController->getEditedLicenses(null,new ResponseHelper(),["id"=>$groupId]);
1692  }
1693 
1701  {
1702  $uploadId = 3;
1703  $this->uploadDao->shouldReceive('isAccessible')
1704  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1705  $this->dbHelper->shouldReceive('doesIdExist')
1706  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(false);
1707 
1708  $body = $this->streamFactory->createStream();
1709  $requestHeaders = new Headers();
1710  $request = new Request("GET", new Uri("HTTP", "localhost"),
1711  $requestHeaders, [], [], $body);
1712 
1713  $this->expectException(HttpNotFoundException::class);
1714  $this->uploadController->getScannedLicenses($request,new ResponseHelper(),["id"=>$uploadId]);
1715  }
1716 
1724  public function testAgentsRevision()
1725  {
1726  $uploadId = 3;
1727  $agentsRun = [
1728  ["uploadId" => $uploadId, 'agentName' => 'nomos', 'successfulAgents'=> [], 'currentAgentId' => 2, 'isAgentRunning' => false],
1729  ["uploadId" => $uploadId,'agentName' => 'monk', "successfulAgents" => [], 'currentAgentId' => 3, 'isAgentRunning' => false]
1730  ];
1731  $agent = new Agent([],$uploadId,"MOMO agent",45,"4.4.0.37.072417",false,"");
1732 
1733  $this->uploadDao->shouldReceive('isAccessible')
1734  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1735  $this->dbHelper->shouldReceive('doesIdExist')
1736  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1737  $scanJobProxy = M::mock('overload:Fossology\Lib\Proxy\ScanJobProxy');
1738 
1739  $scanJobProxy->shouldReceive('createAgentStatus')
1740  ->withAnyArgs()
1741  ->andReturn($agentsRun);
1742  $scanJobProxy->shouldReceive("getSuccessfulAgents")->andReturn($agent);
1743  $this->agentDao->shouldReceive("arsTableExists")->withAnyArgs()->andReturn(true);
1744  $this->agentDao->shouldReceive("getSuccessfulAgentEntries")->withAnyArgs()->andReturn([]);
1745  $this->agentDao->shouldReceive("getRunningAgentIds")->withAnyArgs()->andReturn([$agent]);
1746 
1747  $actualResponse = $this->uploadController->getAgentsRevision(null,new ResponseHelper(),["id"=>$uploadId]);
1748  $this->assertEquals(200,$actualResponse->getStatusCode());
1749  }
1750 
1757  public function testAgentsRevisionNotFound()
1758  {
1759  $uploadId = 3;
1760 
1761  $this->dbHelper->shouldReceive('doesIdExist')
1762  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(false);
1763 
1764  $this->expectException(HttpNotFoundException::class);
1765  $this->uploadController->getAgentsRevision(null,new ResponseHelper(),["id"=>$uploadId]);
1766  }
1767 
1775  {
1776  $uploadId = 3;
1777 
1778  $this->uploadDao->shouldReceive('isAccessible')
1779  ->withArgs([$uploadId, $this->groupId])->andReturn(false);
1780  $this->dbHelper->shouldReceive('doesIdExist')
1781  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1782 
1783  $this->expectException(HttpForbiddenException::class);
1784  $this->uploadController->getAgentsRevision(null,new ResponseHelper(),["id"=>$uploadId]);
1785  }
1786 
1793  public function testGetTopItem()
1794  {
1795  $uploadId = 2;
1796 
1797  $this->dbHelper->shouldReceive('doesIdExist')
1798  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1799  $this->uploadDao->shouldReceive('getParentItemBounds')
1800  ->withAnyArgs()->andReturn($this->getUploadBounds($uploadId));
1801  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
1802 
1803  $actualResponse = $this->uploadController->getTopItem(null,new ResponseHelper(),["id"=>$uploadId]);
1804  $itemTreeBounds = $this->getUploadBounds($uploadId);
1805  $info = new Info(200, $itemTreeBounds->getItemId(), InfoType::INFO);
1806  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
1807 
1808  $this->assertEquals($expectedResponse->getStatusCode(),$actualResponse->getStatusCode());
1809 
1810  }
1811 
1819  {
1820  $uploadId = 2;
1821 
1822  $this->dbHelper->shouldReceive('doesIdExist')
1823  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(false);
1824 
1825  $actualResponse = $this->uploadController->getTopItem(null,new ResponseHelper(),["id"=>$uploadId]);
1826  $info = new Info(404, "Upload does not exist", InfoType::ERROR);
1827  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
1828  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
1829 
1830  }
1831 
1832 
1841  {
1842  $uploadId = 12;
1843 
1844  $this->dbHelper->shouldReceive('doesIdExist')
1845  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1846  $this->uploadDao->shouldReceive('getParentItemBounds')
1847  ->withAnyArgs()->andReturn($this->getUploadBounds($uploadId));
1848  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
1849 
1850  $actualResponse = $this->uploadController->getTopItem(null,new ResponseHelper(),["id"=>$uploadId]);
1851 
1852 
1853  $this->assertEquals(500,$actualResponse->getStatusCode());
1854 
1855  }
1856 }
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