FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
35 use Mockery as M;
36 use Slim\Psr7\Factory\StreamFactory;
37 use Slim\Psr7\Headers;
38 use Slim\Psr7\Request;
39 use Slim\Psr7\Uri;
40 use Symfony\Component\HttpFoundation\Response;
41 
46 class LicenseControllerTest extends \PHPUnit\Framework\TestCase
47 {
53 
58  private $userId;
59 
64  private $groupId;
65 
70  private $dbHelper;
71 
76  private $dbManager;
77 
82  private $restHelper;
83 
89 
94  private $licenseDao;
95 
100  private $userDao;
101 
107 
113 
119 
124  private $streamFactory;
125 
131 
132 
137  private $auth;
138 
143  protected function setUp() : void
144  {
145  global $container;
146  $this->userId = 2;
147  $this->groupId = 2;
148  $container = M::mock('ContainerBuilder');
149  $this->dbHelper = M::mock(DbHelper::class);
150  $this->auth = M::mock(Auth::class);
151  $this->dbManager = M::mock(DbManager::class);
152  $this->restHelper = M::mock(RestHelper::class);
153  $this->licenseDao = M::mock(LicenseDao::class);
154  $this->userDao = M::mock(UserDao::class);
155  $this->adminLicenseAckDao = M::mock(LicenseAcknowledgementDao::class);
156  $this->adminLicensePlugin = M::mock('admin_license_from_csv');
157  $this->licenseCandidatePlugin = M::mock('admin_license_candidate');
158  $this->licenseStdCommentDao = M::mock(LicenseStdCommentDao::class);
159 
160  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
161 
162  $this->restHelper->shouldReceive('getPlugin')->withArgs(["admin_license_candidate"])->andReturn($this->licenseCandidatePlugin);
163  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
164  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
165  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
166  $this->restHelper->shouldReceive('getUserDao')->andReturn($this->userDao);
167 
168  $this->restHelper->shouldReceive('getPlugin')
169  ->withArgs(array('admin_license_from_csv'))->andReturn($this->adminLicensePlugin);
170  $container->shouldReceive('get')->withArgs(array(
171  'dao.license.stdc'))->andReturn($this->licenseStdCommentDao);
172  $container->shouldReceive('get')->withArgs(array(
173  'helper.restHelper'))->andReturn($this->restHelper);
174  $container->shouldReceive('get')->withArgs(array(
175  'dao.license.acknowledgement'))->andReturn($this->adminLicenseAckDao);
176  $container->shouldReceive('get')->withArgs(array(
177  'dao.license'))->andReturn($this->licenseDao);
178  $container->shouldReceive('get')->withArgs(array(
179  'dao.license.acknowledgement'))->andReturn($this->adminLicenseAckDao);
180  $this->licenseController = new LicenseController($container);
181  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
182  $this->streamFactory = new StreamFactory();
183  }
184 
189  protected function tearDown() : void
190  {
191  $this->addToAssertionCount(
192  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
193  M::close();
194  }
195 
202  private function getResponseJson($response)
203  {
204  $response->getBody()->seek(0);
205  return json_decode($response->getBody()->getContents(), true);
206  }
207 
213  private function getObligation($id)
214  {
215  return new Obligation($id, 'My obligation', 'Obligation',
216  'This should represent some valid obligation text.', 'yellow');
217  }
218 
224  private function getDaoObligation($id)
225  {
226  return [
227  'ob_pk' => $id,
228  'ob_topic' => 'My obligation',
229  'ob_text' => 'This should represent some valid obligation text.',
230  'ob_active' => true,
231  'rf_fk' => 2,
232  'ob_type' => 'Obligation',
233  'ob_classification' => 'yellow',
234  'ob_comment' => "",
235  'rf_shortname' => null
236  ];
237  }
238 
246  private function getLicense($shortname, $obligations=false,
247  $emptyObligation=true)
248  {
249  $obligationList = [
250  $this->getObligation(123),
251  $this->getObligation(124)
252  ];
253  $license = null;
254  if ($shortname == "MIT") {
255  $license = new License(22, "MIT", "MIT License",
256  "MIT License Copyright (c) <year> <copyright holders> ...",
257  "https://opensource.org/licenses/MIT", null, 2, false);
258  } else {
259  $license = new License(25, $shortname, "Exotic License",
260  "Exotic license for magical codes", "", null, 0, true);
261  }
262  if ($obligations) {
263  if ($emptyObligation) {
264  $license->setObligations([]);
265  } else {
266  $license->setObligations($obligationList);
267  }
268  }
269  return $license;
270  }
271 
277  private function getDaoLicense($shortname)
278  {
279  $license = null;
280  if ($shortname == "MIT") {
281  $license = new \Fossology\Lib\Data\License(22, "MIT", "MIT License",
282  2, "MIT License Copyright (c) <year> <copyright holders> ...",
283  "https://opensource.org/licenses/MIT", 1, true);
284  } else {
285  $license = new \Fossology\Lib\Data\License(25, $shortname,
286  "Exotic License", 0, "Exotic license for magical codes", "", 1,
287  false);
288  }
289  return $license;
290  }
291 
297  private function translateLicenseToDb($licenses)
298  {
299  $licenseList = [];
300  foreach ($licenses as $license) {
301  $licenseList[] = [
302  'rf_pk' => $license->getId(),
303  'rf_shortname' => $license->getShortName(),
304  'rf_fullname' => $license->getFullName(),
305  'rf_text' => $license->getText(),
306  'rf_url' => $license->getUrl(),
307  'rf_risk' => $license->getRisk(),
308  'group_fk' => $license->getIsCandidate() ? $this->groupId : 0
309  ];
310  }
311  return $licenseList;
312  }
313 
319  public function testGetLicense()
320  {
321  $licenseShortName = "MIT";
322  $license = $this->getLicense($licenseShortName, true);
323 
324  $requestHeaders = new Headers();
325  $body = $this->streamFactory->createStream();
326  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
327  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
328  $this->licenseDao->shouldReceive('getLicenseByShortName')
329  ->withArgs([$licenseShortName, $this->groupId])
330  ->andReturn($this->getDaoLicense($licenseShortName));
331  $this->licenseDao->shouldReceive('getLicenseObligations')
332  ->withArgs([[22], false])->andReturn([]);
333  $this->licenseDao->shouldReceive('getLicenseObligations')
334  ->withArgs([[22], true])->andReturn([]);
335  $expectedResponse = (new ResponseHelper())->withJson($license->getArray(), 200);
336 
337  $actualResponse = $this->licenseController->getLicense($request,
338  new ResponseHelper(), ['shortname' => $licenseShortName]);
339  $this->assertEquals($expectedResponse->getStatusCode(),
340  $actualResponse->getStatusCode());
341  $this->assertEquals($this->getResponseJson($expectedResponse),
342  $this->getResponseJson($actualResponse));
343  }
344 
351  public function testGetLicenseObligations()
352  {
353  $licenseShortName = "MIT";
354  $license = $this->getLicense($licenseShortName, true, false);
355 
356  $requestHeaders = new Headers();
357  $body = $this->streamFactory->createStream();
358  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
359  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
360  $this->licenseDao->shouldReceive('getLicenseByShortName')
361  ->withArgs([$licenseShortName, $this->groupId])
362  ->andReturn($this->getDaoLicense($licenseShortName));
363  $this->licenseDao->shouldReceive('getLicenseObligations')
364  ->withArgs([[22], false])->andReturn([$this->getDaoObligation(123)]);
365  $this->licenseDao->shouldReceive('getLicenseObligations')
366  ->withArgs([[22], true])->andReturn([$this->getDaoObligation(124)]);
367  $expectedResponse = (new ResponseHelper())->withJson($license->getArray(), 200);
368 
369  $actualResponse = $this->licenseController->getLicense($request,
370  new ResponseHelper(), ['shortname' => $licenseShortName]);
371  $this->assertEquals($expectedResponse->getStatusCode(),
372  $actualResponse->getStatusCode());
373  $this->assertEquals($this->getResponseJson($expectedResponse),
374  $this->getResponseJson($actualResponse));
375  }
376 
382  public function testGetLicenseNotFound()
383  {
384  $licenseShortName = "Bogus";
385 
386  $requestHeaders = new Headers();
387  $body = $this->streamFactory->createStream();
388  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
389  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
390  $this->licenseDao->shouldReceive('getLicenseByShortName')
391  ->withArgs([$licenseShortName, $this->groupId])
392  ->andReturn(null);
393  $this->expectException(HttpNotFoundException::class);
394 
395  $this->licenseController->getLicense($request, new ResponseHelper(),
396  ['shortname' => $licenseShortName]);
397  }
398 
405  public function testGetAllLicenseV1()
406  {
407  $this->testGetAllLicense(ApiVersion::V1);
408  }
415  public function testGetAllLicenseV2()
416  {
417  $this->testGetAllLicense();
418  }
419  private function testGetAllLicense($version = ApiVersion::V2)
420  {
421  $licenses = [
422  $this->getLicense("MIT"),
423  $this->getLicense("Exotic"),
424  $this->getLicense("Exotic2"),
425  $this->getLicense("Exotic3")
426  ];
427 
428  $requestHeaders = new Headers();
429  $body = $this->streamFactory->createStream();
430  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
431  "/license"), $requestHeaders, [], [], $body);
432  if ($version == ApiVersion::V2) {
433  $request = $request->withQueryParams(["page"=>1, "limit"=>100]);
434  } else {
435  $request = $request->withHeader('limit', 100)
436  ->withHeader('page', 1);
437  }
438  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
439  $this->dbHelper->shouldReceive('getLicenseCount')
440  ->withArgs(["all", $this->groupId])->andReturn(4);
441  $this->dbHelper->shouldReceive('getLicensesPaginated')
442  ->withArgs([1, 100, "all", $this->groupId, false])
443  ->andReturn($this->translateLicenseToDb($licenses));
444 
445  $responseLicense = [];
446  foreach ($licenses as $license) {
447  $responseLicense[] = $license->getArray();
448  }
449  $expectedResponse = (new ResponseHelper())->withHeader("X-Total-Pages", 1)
450  ->withJson($responseLicense, 200);
451 
452  $actualResponse = $this->licenseController->getAllLicenses($request,
453  new ResponseHelper(), []);
454  $this->assertEquals($expectedResponse->getStatusCode(),
455  $actualResponse->getStatusCode());
456  $this->assertEquals($this->getResponseJson($expectedResponse),
457  $this->getResponseJson($actualResponse));
458  $this->assertEquals($expectedResponse->getHeaders(),
459  $actualResponse->getHeaders());
460  }
461 
469  public function testGetAllLicenseBoundsV1()
470  {
471  $this->testGetAllLicenseBounds(ApiVersion::V1);
472  }
480  public function testGetAllLicenseBoundsV2()
481  {
482  $this->testGetAllLicenseBounds();
483  }
488  private function testGetAllLicenseBounds($version = ApiVersion::V2)
489  {
490  $requestHeaders = new Headers();
491  $body = $this->streamFactory->createStream();
492  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
493  "/license"), $requestHeaders, [], [], $body);
494  if ($version == ApiVersion::V2) {
495  $request = $request->withQueryParams(["page"=>2, "limit"=>5]);
496  } else {
497  $request = $request->withHeader('limit', 5)
498  ->withHeader('page', 2);
499  }
500  $request = $request->withAttribute(Apiversion::ATTRIBUTE_NAME,$version);
501  $this->dbHelper->shouldReceive('getLicenseCount')
502  ->withArgs(["all", $this->groupId])->andReturn(4);
503  $this->expectException(HttpBadRequestException::class);
504 
505  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
506  }
507 
513  public function testGetAllLicenseFiltersV1()
514  {
515  $this->testGetAllLicenseFilters(ApiVersion::V1);
516  }
522  public function testGetAllLicenseFiltersV2()
523  {
524  $this->testGetAllLicenseFilters();
525  }
530  private function testGetAllLicenseFilters($version = ApiVersion::V2)
531  {
532  // All licenses
533  $requestHeaders = new Headers();
534  $body = $this->streamFactory->createStream();
535  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
536  "/license", "kind=all"), $requestHeaders, [], [], $body);
537  $this->dbHelper->shouldReceive('getLicenseCount')
538  ->withArgs(["all", $this->groupId])->andReturn(4)->once();
539  $this->dbHelper->shouldReceive('getLicensesPaginated')
540  ->withArgs([1, 100, "all", $this->groupId, false])
541  ->andReturn([])->once();
542 
543  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
544 
545  // Main licenses
546  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
547  "/license", "kind=main"), $requestHeaders, [], [], $body);
548  $this->dbHelper->shouldReceive('getLicenseCount')
549  ->withArgs(["main", $this->groupId])->andReturn(4)->once();
550  $this->dbHelper->shouldReceive('getLicensesPaginated')
551  ->withArgs([1, 100, "main", $this->groupId, false])
552  ->andReturn([])->once();
553 
554  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
555 
556  // Candidate licenses
557  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
558  "/license", "kind=candidate"), $requestHeaders, [], [], $body);
559  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
560  $this->dbHelper->shouldReceive('getLicenseCount')
561  ->withArgs(["candidate", $this->groupId])->andReturn(4)->once();
562  $this->dbHelper->shouldReceive('getLicensesPaginated')
563  ->withArgs([1, 100, "candidate", $this->groupId, false])
564  ->andReturn([])->once();
565 
566  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
567 
568  // wrong filter
569  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
570  "/license", "kind=bogus"), $requestHeaders, [], [], $body);
571  $this->dbHelper->shouldReceive('getLicenseCount')
572  ->withArgs(["all", $this->groupId])->andReturn(4)->once();
573  $this->dbHelper->shouldReceive('getLicensesPaginated')
574  ->withArgs([1, 100, "all", $this->groupId, false])
575  ->andReturn([])->once();
576 
577  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
578  }
579 
585  public function testCreateLicense()
586  {
587  $license = $this->getLicense("MIT");
588  $requestBody = $license->getArray();
589  $requestBody["isCandidate"] = true;
590  unset($requestBody['id']);
591 
592  $requestHeaders = new Headers();
593  $requestHeaders->setHeader('Content-Type', 'application/json');
594  $body = $this->streamFactory->createStream();
595  $body->write(json_encode($requestBody));
596  $body->seek(0);
597  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
598  "/license"), $requestHeaders, [], [], $body);
599 
600  $tableName = "license_candidate";
601  $assocData = [
602  "rf_shortname" => $license->getShortName(),
603  "rf_fullname" => $license->getFullName(),
604  "rf_text" => $license->getText(),
605  "rf_md5" => md5($license->getText()),
606  "rf_risk" => $license->getRisk(),
607  "rf_url" => $license->getUrl(),
608  "rf_detector_type" => 1,
609  "group_fk" => $this->groupId,
610  "rf_user_fk_created" => $this->userId,
611  "rf_user_fk_modified" => $this->userId,
612  "marydone" => false
613  ];
614 
615  $sql = "SELECT count(*) cnt FROM ".
616  "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
617 
618  $this->dbManager->shouldReceive('insertTableRow')
619  ->withArgs([$tableName, $assocData, M::any(), "rf_pk"])->andReturn(4);
620  $this->dbManager->shouldReceive('getSingleRow')
621  ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()])
622  ->andReturn(["cnt" => 0]);
623 
624  $info = new Info(201, '4', InfoType::INFO);
625  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
626  $info->getCode());
627 
628  $actualResponse = $this->licenseController->createLicense($request,
629  new ResponseHelper(), []);
630  $this->assertEquals($expectedResponse->getStatusCode(),
631  $actualResponse->getStatusCode());
632  $this->assertEquals($this->getResponseJson($expectedResponse),
633  $this->getResponseJson($actualResponse));
634  }
635 
642  public function testCreateLicenseNoShort()
643  {
644  $license = $this->getLicense("MIT");
645  $requestBody = $license->getArray();
646  $requestBody["isCandidate"] = true;
647  unset($requestBody['id']);
648  unset($requestBody['shortName']);
649 
650  $requestHeaders = new Headers();
651  $requestHeaders->setHeader('Content-Type', 'application/json');
652  $body = $this->streamFactory->createStream();
653  $body->write(json_encode($requestBody));
654  $body->seek(0);
655  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
656  "/license"), $requestHeaders, [], [], $body);
657  $this->expectException(HttpBadRequestException::class);
658 
659  $this->licenseController->createLicense($request, new ResponseHelper(), []);
660  }
661 
668  public function testCreateLicenseNoAdmin()
669  {
670  $license = $this->getLicense("MIT");
671  $requestBody = $license->getArray();
672  unset($requestBody['id']);
673 
674  $requestHeaders = new Headers();
675  $requestHeaders->setHeader('Content-Type', 'application/json');
676  $body = $this->streamFactory->createStream();
677  $body->write(json_encode($requestBody));
678  $body->seek(0);
679  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
680  "/license"), $requestHeaders, [], [], $body);
681  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
682  $this->expectException(HttpForbiddenException::class);
683 
684  $this->licenseController->createLicense($request, new ResponseHelper(), []);
685  }
686 
694  {
695  $this->testCreateDuplicateLicense(ApiVersion::V1);
696  }
704  {
706  }
711  private function testCreateDuplicateLicense($version = ApiVersion::V2)
712  {
713  $license = $this->getLicense("MIT");
714  $requestBody = $license->getArray();
715  $requestBody["isCandidate"] = true;
716  unset($requestBody['id']);
717 
718  $requestHeaders = new Headers();
719  $requestHeaders->setHeader('Content-Type', 'application/json');
720  $body = $this->streamFactory->createStream();
721  $body->write(json_encode($requestBody));
722  $body->seek(0);
723  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
724  "/license"), $requestHeaders, [], [], $body);
725  $request = $request->withAttribute(Apiversion::class, $version);
726  $tableName = "license_candidate";
727 
728  $sql = "SELECT count(*) cnt FROM ".
729  "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
730 
731  $this->dbManager->shouldReceive('getSingleRow')
732  ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()])
733  ->andReturn(["cnt" => 1]);
734  $this->expectException(HttpConflictException::class);
735 
736  $this->licenseController->createLicense($request, new ResponseHelper(), []);
737  }
738 
744  public function testUpdateLicenseV1()
745  {
746  $this->testUpdateLicense(ApiVersion::V1);
747  }
753  public function testUpdateLicenseV2()
754  {
755  $this->testUpdateLicense();
756  }
761  private function testUpdateLicense($version = ApiVersion::V2)
762  {
763  $license = $this->getDaoLicense("Exotic");
764  $requestBody = [
765  "fullName" => "Exotic License - style",
766  "risk" => 0
767  ];
768 
769  $requestHeaders = new Headers();
770  $requestHeaders->setHeader('Content-Type', 'application/json');
771  $body = $this->streamFactory->createStream();
772  $body->write(json_encode($requestBody));
773  $body->seek(0);
774  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
775  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
776  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
777 
778  $tableName = "license_candidate";
779  $assocData = [
780  "rf_fullname" => "Exotic License - style",
781  "rf_risk" => 0
782  ];
783 
784  $this->userDao->shouldReceive('isAdvisorOrAdmin')
785  ->withArgs([$this->userId, $this->groupId])->andReturn(true);
786  $this->licenseDao->shouldReceive('getLicenseByShortName')
787  ->withArgs([$license->getShortName(), $this->groupId])
788  ->andReturn($license);
789  $this->dbHelper->shouldReceive('doesIdExist')
790  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
791  ->andReturn(true);
792  $this->dbManager->shouldReceive('updateTableRow')
793  ->withArgs([$tableName, $assocData, "rf_pk", $license->getId(), M::any()]);
794 
795  $info = new Info(200, "License " . $license->getShortName() . " updated.",
796  InfoType::INFO);
797  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
798  $info->getCode());
799 
800  $actualResponse = $this->licenseController->updateLicense($request,
801  new ResponseHelper(), ["shortname" => $license->getShortName()]);
802  $this->assertEquals($expectedResponse->getStatusCode(),
803  $actualResponse->getStatusCode());
804  $this->assertEquals($this->getResponseJson($expectedResponse),
805  $this->getResponseJson($actualResponse));
806  }
807 
815  {
816  $this->testUpdateLicenseNonAdvisor(ApiVersion::V1);
817  }
825  {
827  }
832  private function testUpdateLicenseNonAdvisor($version = ApiVersion::V2)
833  {
834  $license = $this->getDaoLicense("Exotic");
835  $requestBody = [
836  "fullName" => "Exotic License - style",
837  "risk" => 0
838  ];
839 
840  $requestHeaders = new Headers();
841  $requestHeaders->setHeader('Content-Type', 'application/json');
842  $body = $this->streamFactory->createStream();
843  $body->write(json_encode($requestBody));
844  $body->seek(0);
845  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
846  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
847  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
848  $this->userDao->shouldReceive('isAdvisorOrAdmin')
849  ->withArgs([$this->userId, $this->groupId])->andReturn(false);
850  $this->licenseDao->shouldReceive('getLicenseByShortName')
851  ->withArgs([$license->getShortName(), $this->groupId])
852  ->andReturn($license);
853  $this->dbHelper->shouldReceive('doesIdExist')
854  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
855  ->andReturn(true);
856  $this->expectException(HttpForbiddenException::class);
857 
858  $this->licenseController->updateLicense($request, new ResponseHelper(),
859  ["shortname" => $license->getShortName()]);
860  }
861 
868  public function testUpdateLicenseNonAdminV1()
869  {
870  $this->testUpdateLicenseNonAdmin(ApiVersion::V1);
871  }
878  public function testUpdateLicenseNonAdminV2()
879  {
880  $this->testUpdateLicenseNonAdmin();
881  }
886  private function testUpdateLicenseNonAdmin($version = ApiVersion::V2)
887  {
888  $license = $this->getDaoLicense("MIT");
889  $requestBody = [
890  "fullName" => "MIT License - style",
891  "risk" => 0
892  ];
893 
894  $requestHeaders = new Headers();
895  $requestHeaders->setHeader('Content-Type', 'application/json');
896  $body = $this->streamFactory->createStream();
897  $body->write(json_encode($requestBody));
898  $body->seek(0);
899  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
900  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
901  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
902  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
903 
904  $this->userDao->shouldReceive('isAdvisorOrAdmin')
905  ->withArgs([$this->userId, $this->groupId])->andReturn(true);
906  $this->licenseDao->shouldReceive('getLicenseByShortName')
907  ->withArgs([$license->getShortName(), $this->groupId])
908  ->andReturn($license);
909  $this->dbHelper->shouldReceive('doesIdExist')
910  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
911  ->andReturn(false);
912  $this->expectException(HttpForbiddenException::class);
913 
914  $this->licenseController->updateLicense($request, new ResponseHelper(),
915  ["shortname" => $license->getShortName()]);
916  }
917 
918 
927  public function testImportLicenseV1()
928  {
929  $this->testImportLicense(ApiVersion::V1);
930  }
939  public function testImportLicenseV2()
940  {
941  $this->testImportLicense();
942  }
947  private function testImportLicense($version = ApiVersion::V2)
948  {
949 
950  $delimiter = ',';
951  $enclosure = '"';
952 
953  $requestHeaders = new Headers();
954  $requestHeaders->setHeader('Content-Type', 'application/json');
955 
956  $body = $this->streamFactory->createStream(json_encode([]));
957 
958  $request = new Request("POST", new Uri("HTTP", "localhost"),
959  $requestHeaders, [], [], $body);
960  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
961 
962  $FILE_INPUT_NAME = "file_input";
963 
964  $this->adminLicensePlugin->shouldReceive('getFileInputName')
965  ->andReturn($FILE_INPUT_NAME);
966 
967  $res = array(true,"random_message",200);
968 
969  $this->adminLicensePlugin->shouldReceive('handleFileUpload')-> withArgs([NULL,$delimiter,$enclosure])
970  ->andReturn($res);
971  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
972  $this->auth->shouldReceive('isAdmin')->andReturn(true);
973 
974  $info = new Info(200, "random_message", InfoType::INFO);
975  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
976  $info->getCode());
977  $actualResponse = $this->licenseController->handleImportLicense($request,
978  new ResponseHelper(), []);
979  $this->assertEquals($expectedResponse->getStatusCode(),
980  $actualResponse->getStatusCode());
981  $this->assertEquals($this->getResponseJson($expectedResponse),
982  $this->getResponseJson($actualResponse));
983  }
984 
985 
995  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
996  $id = 1;
997  $this->auth->shouldReceive('isAdmin')->andReturn(true);
998  $this->licenseCandidatePlugin->shouldReceive('getDataRow')->withArgs([$id])->andReturn(true);
999  $res = new Response('true',Response::HTTP_OK,array('Content-type'=>'text/plain'));
1000  $this->licenseCandidatePlugin->shouldReceive("doDeleteCandidate")->withArgs([$id,false])->andReturn($res);
1001  $expectedResponse = new Info(202,"License candidate will be deleted.", InfoType::INFO);
1002  $actualResponse = $this->licenseController->deleteAdminLicenseCandidate(null,
1003  new ResponseHelper(), ["id" => $id]);
1004  $this->assertEquals($expectedResponse->getCode(),
1005  $actualResponse->getStatusCode());
1006  $this->assertEquals($expectedResponse->getArray(),
1007  $this->getResponseJson($actualResponse));
1008  }
1009 
1019  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1020  $id = 1;
1021  $this->auth->shouldReceive('isAdmin')->andReturn(false);
1022  $this->expectException(HttpForbiddenException::class);
1023 
1024  $this->licenseController->deleteAdminLicenseCandidate(null,
1025  new ResponseHelper(), ["id" => $id]);
1026  }
1027 
1037  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1038  $id = 1;
1039  $this->auth->shouldReceive('isAdmin')->andReturn(true);
1040  $this->licenseCandidatePlugin->shouldReceive('getDataRow')->withArgs([$id])->andReturn(false);
1041  $res = new Response('true',Response::HTTP_OK,array('Content-type'=>'text/plain'));
1042  $this->licenseCandidatePlugin->shouldReceive("doDeleteCandidate")->withArgs([$id])->andReturn($res);
1043  $this->expectException(HttpNotFoundException::class);
1044 
1045  $this->licenseController->deleteAdminLicenseCandidate(null,
1046  new ResponseHelper(), ["id" => $id]);
1047  }
1048 
1058  {
1059  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1060  $dummyLicenseAcknowledgments = [
1061  array(
1062  "la_pk" => 1,
1063  "name" => "MIT license",
1064  "acknowledgement" => "Permission is hereby granted, free of charge",
1065  "updated" => "2024-06-16 15:02:22.245855+02",
1066  "user_fk" => 2,
1067  "is_enabled" => false,
1068  ),
1069  array(
1070  "la_pk" => 2,
1071  "name" => "GPL-2.0-or-later",
1072  "acknowledgement" => "Permission is hereby granted, free of charge",
1073  "updated" => "2024-08-16 15:02:22.245855+02",
1074  "user_fk" => 4,
1075  "is_enabled" => true,
1076  )
1077  ];
1078 
1079  $expectedInfo = [
1080  Array (
1081  'name' => $dummyLicenseAcknowledgments[0]["name"],
1082  'acknowledgement' => $dummyLicenseAcknowledgments[0]["acknowledgement"],
1083  'is_enabled' => false,
1084  'id' => 1,
1085  ),
1086  Array (
1087  'name' => $dummyLicenseAcknowledgments[1]["name"],
1088  'acknowledgement' => $dummyLicenseAcknowledgments[1]['acknowledgement'],
1089  'is_enabled' => true,
1090  'id' => 2
1091  )
1092  ];
1093 
1094  $this->adminLicenseAckDao->shouldReceive('getAllAcknowledgements')->andReturn($dummyLicenseAcknowledgments);
1095  $requestHeaders = new Headers();
1096  $requestHeaders->setHeader('Content-Type', 'application/json');
1097  $body = $this->streamFactory->createStream();
1098  $request = new Request("GET", new Uri("HTTP", "localhost"),
1099  $requestHeaders, [], [], $body);
1100  $response = new ResponseHelper();
1101  $expectedResponse = (new ResponseHelper())->withJson($expectedInfo, 200);
1102  $actualResponse = $this->licenseController->getAllAdminAcknowledgements($request,$response,[]);
1103 
1104  $this->assertEquals(200,$actualResponse->getStatusCode());
1105  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1106  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1107 
1108  }
1109 
1119  {
1120  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
1121 
1122  $requestHeaders = new Headers();
1123  $requestHeaders->setHeader('Content-Type', 'application/json');
1124  $body = $this->streamFactory->createStream();
1125  $request = new Request("GET", new Uri("HTTP", "localhost"),
1126  $requestHeaders, [], [], $body);
1127  $response = new ResponseHelper();
1128  $this->expectException(HttpForbiddenException::class);
1129  $this->licenseController->getAllAdminAcknowledgements($request,$response,[]);
1130  }
1131 
1140  {
1141  $requestHeaders = new Headers();
1142  $requestHeaders->setHeader('Content-Type', 'application/json');
1143  $body = $this->streamFactory->createStream();
1144  $request = new Request("GET", new Uri("HTTP", "localhost"),
1145  $requestHeaders, [], [], $body);
1146  $response = new ResponseHelper();
1147  $this->expectException(HttpBadRequestException::class);
1148 
1149  $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1150 
1151  }
1152 
1161  {
1162  $requestHeaders = new Headers();
1163  $requestHeaders->setHeader('Content-Type', 'application/json');
1164  $body = $this->streamFactory->createStream(json_encode([]));
1165  $request = new Request("GET", new Uri("HTTP", "localhost"),
1166  $requestHeaders, [], [], $body);
1167  $response = new ResponseHelper();
1168  $this->expectException(HttpBadRequestException::class);
1169 
1170  $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1171  }
1172 
1173 
1184  {
1185 
1186  $this->dbHelper->shouldReceive("doesIdExist")
1187  ->withArgs(["license_std_acknowledgement","name", $this->getDummyVars()["bodyContent"][0]["name"]])->andReturn(true);
1188  $this->dbManager->shouldReceive("getSingleRow")
1189  ->withAnyArgs()->andReturn($this->getDummyVars()["dummyExistingAck"]);
1190  $this->adminLicenseAckDao->shouldReceive('updateAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"], $this->getDummyVars()["bodyContent"][0]["name"], $this->getDummyVars()["bodyContent"][0]["ack"]]);
1191  $this->adminLicenseAckDao->shouldReceive('toggleAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"]]);
1192 
1193 
1194  $requestHeaders = new Headers();
1195  $requestHeaders->setHeader('Content-Type', 'application/json');
1196  $body = $this->streamFactory->createStream(json_encode($this->getDummyVars()["bodyContent"]));
1197  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1198  $requestHeaders, [], [], $body);
1199  $response = new ResponseHelper();
1200 
1201  $expectedInfo = new Info(200, "Successfully updated admin license acknowledgement with name '" . $this->getDummyVars()["dummyExistingAck"]["name"] . "'", InfoType::INFO);
1202  $success [] =$expectedInfo->getArray();
1203  $expectedResponse = (new ResponseHelper())->withJson(["success" => $success, "errors" => []], 200);
1204 
1205  $actualResponse = $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1206 
1207  $this->assertEquals(200,$actualResponse->getStatusCode());
1208  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1209  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1210 
1211  }
1212 
1222  {
1223 
1224  $bodyContent = $this->getDummyVars()["bodyContent"];
1225  $bodyContent[0]["update"] = false;
1226 
1227  $this->dbHelper->shouldReceive("doesIdExist")
1228  ->withArgs(["license_std_acknowledgement","name", $this->getDummyVars()["bodyContent"][0]["name"]])->andReturn(true);
1229  $this->dbManager->shouldReceive("getSingleRow")
1230  ->withAnyArgs()->andReturn($this->getDummyVars()["dummyExistingAck"]);
1231  $this->adminLicenseAckDao->shouldReceive('updateAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"], $this->getDummyVars()["bodyContent"][0]["name"], $this->getDummyVars()["bodyContent"][0]["ack"]]);
1232  $this->adminLicenseAckDao->shouldReceive('toggleAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"]]);
1233 
1234 
1235  $requestHeaders = new Headers();
1236  $requestHeaders->setHeader('Content-Type', 'application/json');
1237  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1238  $request = new Request("POST", new Uri("HTTP", "localhost"),
1239  $requestHeaders, [], [], $body);
1240  $response = new ResponseHelper();
1241 
1242 
1243  $actualResponse = $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1244  $this->assertEquals(400,$this->getResponseJson($actualResponse)["errors"][0]["code"]);
1245  $this->assertEquals([],$this->getResponseJson($actualResponse)["success"]);
1246  }
1247 
1257  {
1258 
1259  $bodyContent = $this->getDummyVars()["bodyContent"];
1260  $bodyContent[0]["update"] = false;
1261 
1262  $this->dbHelper->shouldReceive("doesIdExist")
1263  ->withArgs(["license_std_acknowledgement","name", $this->getDummyVars()["bodyContent"][0]["name"]])->andReturn(false);
1264  $this->dbManager->shouldReceive("getSingleRow")
1265  ->withAnyArgs()->andReturn($this->getDummyVars()["dummyExistingAck"]);
1266  $this->adminLicenseAckDao->shouldReceive('updateAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"], $this->getDummyVars()["bodyContent"][0]["name"], $this->getDummyVars()["bodyContent"][0]["ack"]]);
1267  $this->adminLicenseAckDao->shouldReceive('toggleAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"]]);
1268  $this->adminLicenseAckDao->shouldReceive("insertAcknowledgement")
1269  ->withArgs([$bodyContent[0]['name'], $bodyContent[0]['ack']])->andReturn(-1);
1270 
1271  $requestHeaders = new Headers();
1272  $requestHeaders->setHeader('Content-Type', 'application/json');
1273  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1274  $request = new Request("POST", new Uri("HTTP", "localhost"),
1275  $requestHeaders, [], [], $body);
1276  $response = new ResponseHelper();
1277 
1278  $info = new Info(201, "Acknowledgement added successfully.", InfoType::INFO);
1279  $success [] = $info->getArray();
1280 
1281  $expectedResponse = (new ResponseHelper())->withJson(["success" => $success, "errors" => []], 200);
1282  $actualResponse = $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1283  $this->assertEquals(201,$this->getResponseJson($actualResponse)["success"][0]["code"]);
1284  $this->assertEmpty($this->getResponseJson($actualResponse)["errors"]);
1285  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1286  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1287  }
1288 
1297  {
1298 
1299  $bodyContent = $this->getDummyVars()["bodyContent"];
1300  $bodyContent[0]["update"] = false;
1301 
1302  $this->dbHelper->shouldReceive("doesIdExist")
1303  ->withArgs(["license_std_acknowledgement","name", $this->getDummyVars()["bodyContent"][0]["name"]])->andReturn(false);
1304  $this->dbManager->shouldReceive("getSingleRow")
1305  ->withAnyArgs()->andReturn($this->getDummyVars()["dummyExistingAck"]);
1306  $this->adminLicenseAckDao->shouldReceive('updateAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"], $this->getDummyVars()["bodyContent"][0]["name"], $this->getDummyVars()["bodyContent"][0]["ack"]]);
1307  $this->adminLicenseAckDao->shouldReceive('toggleAcknowledgement')->withArgs([$this->getDummyVars()["bodyContent"][0]["id"]]);
1308  $this->adminLicenseAckDao->shouldReceive("insertAcknowledgement")
1309  ->withArgs([$bodyContent[0]['name'], $bodyContent[0]['ack']])->andReturn(-2);
1310 
1311  $requestHeaders = new Headers();
1312  $requestHeaders->setHeader('Content-Type', 'application/json');
1313  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1314  $request = new Request("POST", new Uri("HTTP", "localhost"),
1315  $requestHeaders, [], [], $body);
1316  $response = new ResponseHelper();
1317 
1318  $expectedError = new Info(500, "Error while inserting new acknowledgement.", InfoType::ERROR);
1319  $errors [] = $expectedError->getArray();
1320 
1321  $expectedResponse = (new ResponseHelper())->withJson(["success" => [], "errors" => $errors], 200);
1322  $actualResponse = $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1323  $this->assertEquals(500,$this->getResponseJson($actualResponse)["errors"][0]["code"]);
1324  $this->assertEmpty($this->getResponseJson($actualResponse)["success"]);
1325  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1326  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1327  }
1328 
1338  {
1339  $dbLicenseStdComments = [
1340  array(
1341  "lsc_pk" => 1,
1342  "name" => "Test License Standard",
1343  "comment" => "MIT License Standard",
1344  "updated" => "2024-06-16 15:04:08.07613+02",
1345  "user_fk" => 3,
1346  "is_enabled" => true
1347  ),
1348  ];
1349 
1350  $requestHeaders = new Headers();
1351  $requestHeaders->setHeader('Content-Type', 'application/json');
1352  $body = $this->streamFactory->createStream();
1353  $request = new Request("GET", new Uri("HTTP", "localhost"),
1354  $requestHeaders, [], [], $body);
1355  $response = new ResponseHelper();
1356 
1357  $this->licenseStdCommentDao->shouldReceive('getAllComments')
1358  ->andReturn($dbLicenseStdComments);
1359  $actualResponse = $this->licenseController->getAllLicenseStandardComments($request, $response,[]);
1360 
1361  $this->assertEquals(200,intval($actualResponse->getStatusCode()));
1362  $this->assertEquals($dbLicenseStdComments[0]['lsc_pk'], $this->getResponseJson($actualResponse)[0]['id']);
1363  $this->assertEquals($dbLicenseStdComments[0]['name'], $this->getResponseJson($actualResponse)[0]['name']);
1364  $this->assertEquals($dbLicenseStdComments[0]['comment'], $this->getResponseJson($actualResponse)[0]['comment']);
1365  $this->assertEquals($dbLicenseStdComments[0]['is_enabled'], $this->getResponseJson($actualResponse)[0]['is_enabled']);
1366 
1367  }
1368 
1377  {
1378  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1379 
1380  $requestHeaders = new Headers();
1381  $requestHeaders->setHeader('Content-Type', 'application/json');
1382  $body = $this->streamFactory->createStream();
1383  $request = new Request("POST", new Uri("HTTP", "localhost"),
1384  $requestHeaders, [], [], $body);
1385  $response = new ResponseHelper();
1386 
1387  $this->expectException(HttpForbiddenException::class);
1388  $this->licenseController->handleLicenseStandardComment($request, $response,[]);
1389 
1390  }
1391 
1400  {
1401  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1402  $requestHeaders = new Headers();
1403  $requestHeaders->setHeader('Content-Type', 'application/json');
1404  $body = $this->streamFactory->createStream(json_encode([]));
1405  $request = new Request("POST", new Uri("HTTP", "localhost"),
1406  $requestHeaders, [], [], $body);
1407  $response = new ResponseHelper();
1408  $this->expectException(HttpBadRequestException::class);
1409 
1410  $this->licenseController->handleLicenseStandardComment($request,$response,[]);
1411  }
1412 
1417  public function getDummyVars()
1418  {
1419  $bodyContent = [
1420  array(
1421  "update" => true,
1422  "ack" => "acknowledgement",
1423  "name" => "MIT license",
1424  "toggle" => "toggle license",
1425  "id" => 3
1426  )
1427  ];
1428  $dummyExistingAck = [
1429  "la_pk" => 1,
1430  "name" => "MIT license",
1431  "acknowledgement" => "Permission is hereby granted, free of charge",
1432  "updated" => "2024-06-16 15:02:22.245855+02",
1433  "user_fk" => 2,
1434  "is_enabled" => false
1435  ];
1436  $licenseStdComments = [
1437  array(
1438  "id" =>5,
1439  "name" => "Test License Standard",
1440  "comment" => "MIT License Standard",
1441  "update" => true,
1442  "toggle" => true
1443  ),
1444  ];
1445  $existingLicenseStdComment = [
1446  "name" => "Test License Standard",
1447  "comment" => "MIT License Standard",
1448  "update" => true,
1449  ];
1450 
1451  $tableName = "license_std_acknowledgement";
1452 
1453  return [
1454  "bodyContent" => $bodyContent,
1455  "dummyExistingAck" => $dummyExistingAck,
1456  "tableName" => $tableName,
1457  "licenseStdComments" => $licenseStdComments,
1458  "existingLicenseStdComment" => $existingLicenseStdComment
1459  ];
1460 
1461  }
1462 
1471  {
1472  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1473  $bodyContent = $this->getDummyVars()['licenseStdComments'];
1474  $existingLicenseStdComments = [
1475  "name" => "Test License Standard",
1476  "comment" => "MIT License Standard",
1477  "update" => true,
1478  ];
1479 
1480  $this->dbManager->shouldReceive("getSingleRow")
1481  ->withAnyArgs()->andReturn($existingLicenseStdComments);
1482  $this->licenseStdCommentDao->shouldReceive('updateComment')->withArgs([$bodyContent[0]["id"], $bodyContent[0]["name"], $bodyContent[0]["comment"]]);
1483  $this->licenseStdCommentDao->shouldReceive('toggleComment')->withArgs([$bodyContent[0]["id"]]);
1484 
1485 
1486  $requestHeaders = new Headers();
1487  $requestHeaders->setHeader('Content-Type', 'application/json');
1488  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1489  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1490  $requestHeaders, [], [], $body);
1491  $response = new ResponseHelper();
1492 
1493  $expectedInfo = new Info(200, "Successfully updated standard comment", InfoType::INFO);
1494  $success [] =$expectedInfo->getArray();
1495  $expectedResponse = (new ResponseHelper())->withJson(["success" => $success, "errors" => []], 200);
1496 
1497  $actualResponse = $this->licenseController->handleLicenseStandardComment($request,$response,[]);
1498 
1499  $this->assertEquals(200,$actualResponse->getStatusCode());
1500  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1501  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1502 
1503  }
1504 
1514  {
1515  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1516  $bodyContent = $this->getDummyVars()['licenseStdComments'];
1517  $bodyContent[0]["update"] = false;
1518 
1519  $this->dbHelper->shouldReceive("doesIdExist")
1520  ->withArgs(["license_std_comment","name", $bodyContent[0]['name']])->andReturn(true);
1521 
1522  $requestHeaders = new Headers();
1523  $requestHeaders->setHeader('Content-Type', 'application/json');
1524  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1525  $request = new Request("POST", new Uri("HTTP", "localhost"),
1526  $requestHeaders, [], [], $body);
1527  $response = new ResponseHelper();
1528 
1529  $actualResponse = $this->licenseController->handleAdminLicenseAcknowledgement($request,$response,[]);
1530  $this->assertEquals(400,$this->getResponseJson($actualResponse)["errors"][0]["code"]);
1531  $this->assertEquals([],$this->getResponseJson($actualResponse)["success"]);
1532  }
1533 
1534 
1544  {
1545  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1546  $bodyContent = $this->getDummyVars()['licenseStdComments'];
1547  $bodyContent[0]["update"] = false;
1548 
1549  $this->dbHelper->shouldReceive("doesIdExist")
1550  ->withArgs(["license_std_comment","name", $bodyContent[0]["name"]])->andReturn(false);
1551  $this->licenseStdCommentDao->shouldReceive("insertComment")
1552  ->withArgs([$bodyContent[0]['name'], $bodyContent[0]['comment']])->andReturn(-1);
1553 
1554  $requestHeaders = new Headers();
1555  $requestHeaders->setHeader('Content-Type', 'application/json');
1556  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1557  $request = new Request("POST", new Uri("HTTP", "localhost"),
1558  $requestHeaders, [], [], $body);
1559  $response = new ResponseHelper();
1560 
1561  $info = new Info(201, "Comment with name '". $bodyContent[0]['name'] ."' added successfully.", InfoType::INFO);
1562  $success [] = $info->getArray();
1563 
1564  $expectedResponse = (new ResponseHelper())->withJson(["success" => $success, "errors" => []], 200);
1565  $actualResponse = $this->licenseController->handleLicenseStandardComment($request,$response,[]);
1566 
1567  $this->assertEquals(201,$this->getResponseJson($actualResponse)["success"][0]["code"]);
1568  $this->assertEmpty($this->getResponseJson($actualResponse)["errors"]);
1569  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1570  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1571  }
1572 
1582  {
1583 
1584  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1585  $bodyContent = $this->getDummyVars()['licenseStdComments'];
1586  $bodyContent[0]["update"] = false;
1587 
1588  $this->dbHelper->shouldReceive("doesIdExist")
1589  ->withArgs(["license_std_comment","name", $bodyContent[0]["name"]])->andReturn(false);
1590  $this->licenseStdCommentDao->shouldReceive("insertComment")
1591  ->withArgs([$bodyContent[0]['name'], $bodyContent[0]['comment']])->andReturn(-2);
1592 
1593  $requestHeaders = new Headers();
1594  $requestHeaders->setHeader('Content-Type', 'application/json');
1595  $body = $this->streamFactory->createStream(json_encode($bodyContent));
1596  $request = new Request("POST", new Uri("HTTP", "localhost"),
1597  $requestHeaders, [], [], $body);
1598  $response = new ResponseHelper();
1599 
1600  $expectedError = new Info(500, "Error while inserting new comment.", InfoType::ERROR);
1601  $errors [] = $expectedError->getArray();
1602 
1603  $expectedResponse = (new ResponseHelper())->withJson(["success" => [], "errors" => $errors], 200);
1604  $actualResponse = $this->licenseController->handleLicenseStandardComment($request,$response,[]);
1605  $this->assertEquals(500,$this->getResponseJson($actualResponse)["errors"][0]["code"]);
1606  $this->assertEmpty($this->getResponseJson($actualResponse)["success"]);
1607  $this->assertEquals($this->getResponseJson($expectedResponse),$this->getResponseJson($actualResponse));
1608  $this->assertEquals($expectedResponse->getBody()->getContents(),$actualResponse->getBody()->getContents());
1609  }
1610 
1611 
1612 
1620  {
1621  $id = 1;
1622  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("license_ref","rf_pk",$id))->andReturn(true);
1623  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("license_candidate","rf_pk",$id))->andReturn(true);
1624  $this->dbManager->shouldReceive('prepare');
1625  $this->dbManager->shouldReceive('fetchAll')->andReturn([]);;
1626  $this->dbManager->shouldReceive('freeResult')->andReturn([]);
1627  $this->dbManager->shouldReceive('execute');
1628  $this->dbManager->shouldReceive("getSingleRow")->withAnyArgs()->andReturn([]);
1629 
1630  $requestHeaders = new Headers();
1631  $body = $this->streamFactory->createStream(json_encode(["referenceText" =>"rftext"]));
1632  $request = new Request("GET", new Uri("HTTP", "localhost"),
1633  $requestHeaders, [], [], $body);
1634 
1635  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1636  $actualResponse = $this->licenseController->exportAdminLicenseToCSV($request,new ResponseHelper(), []);
1637  $this->assertEquals(200,$actualResponse->getStatusCode());
1638 
1639  }
1640 
1641 
1649  {
1650  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1651  $request = $this->getEmptyRequest("GET");
1652  $this->expectException(HttpForbiddenException::class);
1653  $actualResponse = $this->licenseController->exportAdminLicenseToCSV($request,new ResponseHelper(), []);
1654  $this->assertEquals(200,$actualResponse->getStatusCode());
1655 
1656  }
1657 
1662  public function getEmptyRequest($method)
1663  {
1664  $requestHeaders = new Headers();
1665  $body = $this->streamFactory->createStream();
1666  $request = new Request($method, new Uri("HTTP", "localhost"),
1667  $requestHeaders, [], [], $body);
1668  return $request;
1669  }
1670 
1674  public function getRequestWithBody($method, $body)
1675  {
1676  $requestHeaders = new Headers();
1677  $requestHeaders->setHeader('Content-Type', 'application/json');
1678 
1679  $request = new Request($method, new Uri("HTTP", "localhost"),
1680  $requestHeaders, [], [], $body);
1681  return $request;
1682  }
1683 
1691  public function testVerifyLicenseNotAdmin()
1692  {
1693  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1694  $request = $this->getEmptyRequest("POST");
1695 
1696  $this->expectException(HttpForbiddenException::class);
1697  $this->licenseController->verifyLicense($request, new ResponseHelper(),[]);
1698 
1699  }
1700 
1701 
1710  {
1711  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1712 
1713  $license = $this->getLicenseArgs()["license"];
1714  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1715  $body = $this->streamFactory->createStream(json_encode([
1716  "parentShortname" => $parentLicense->getShortName(),
1717  ]));
1718  $request = $this->getRequestWithBody("POST", $body);
1719  $this->expectException(HttpBadRequestException::class);
1720  $this->licenseController->verifyLicense($request, new ResponseHelper(), [
1721  "shortname" => ""
1722  ]);
1723  }
1724 
1725 
1726 
1736  {
1737  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1738  $license = $this->getLicenseArgs()["license"];
1739  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1740 
1741  $body = $this->streamFactory->createStream(json_encode([
1742  "parentShortname" => $parentLicense->getShortName(),
1743  ]));
1744 
1745  $this->licenseDao->shouldReceive("getLicenseByShortName")
1746  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1747  $this->licenseDao->shouldReceive("getLicenseByShortName")
1748  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1749 
1750  $this->licenseCandidatePlugin->shouldReceive("verifyCandidate")
1751  ->withArgs([$license->getId(),$license->getShortName(), $parentLicense->getId()])
1752  ->andReturn(false);
1753  $request = $this->getRequestWithBody("POST", $body);
1754 
1755  $this->expectException(HttpBadRequestException::class);
1756  $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1757 
1758  }
1767  public function testVerifyNotFoundLicense()
1768  {
1769  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1770  $license = $this->getLicenseArgs()["license"];
1771  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1772 
1773  $body = $this->streamFactory->createStream(json_encode([
1774  "parentShortname" => $parentLicense->getShortName(),
1775  ]));
1776 
1777  $this->licenseDao->shouldReceive("getLicenseByShortName")
1778  ->withArgs([$license->getShortName(),$this->groupId])->andReturn([]);
1779  $this->licenseDao->shouldReceive("getLicenseByShortName")
1780  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1781 
1782  $request = $this->getRequestWithBody("POST", $body);
1783 
1784  $this->expectException(HttpNotFoundException::class);
1785  $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1786 
1787  }
1788 
1797  public function testVerifyLicense()
1798  {
1799  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1800  $license = $this->getLicenseArgs()["license"];
1801  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1802 
1803  $body = $this->streamFactory->createStream(json_encode([
1804  "parentShortname" => $parentLicense->getShortName(),
1805  ]));
1806 
1807  $this->licenseDao->shouldReceive("getLicenseByShortName")
1808  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1809  $this->licenseDao->shouldReceive("getLicenseByShortName")
1810  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1811 
1812  $this->licenseCandidatePlugin->shouldReceive("verifyCandidate")
1813  ->withArgs([$license->getId(),$license->getShortName(), $parentLicense->getId()])
1814  ->andReturn(true);
1815  $request = $this->getRequestWithBody("POST", $body);
1816 
1817  $info = new Info(200, 'Successfully verified candidate ('.$license->getShortName().')'.' as variant of ('.$parentLicense->getShortName().').', InfoType::INFO);
1818  $expectedResponse = (new ResponseHelper())->withJson([
1819  "code" => $info->getCode(),
1820  "message" => $info->getMessage(),
1821  "type" => $info->getType()
1822  ]);
1823 
1824  $actualResponse = $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1825 
1826  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
1827  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
1828 
1829  }
1830 
1835  public function getLicenseArgs()
1836  {
1837  $license = new License(1,
1838  "MIT",
1839  "MIT License",
1840  "GNU GENERAL PUBLIC LICENSE Copyright (C) 1989 Free Software ",
1841  );
1842 
1843  $parentLicense = new License(3,
1844  "MPL-2.0",
1845  "MPL-2.0 License",
1846  "GNU GENERAL PUBLIC LICENSE Copyright (C) 1989 Free Software ",
1847  );
1848 
1849  return [
1850  "license" => $license,
1851  "parentLicense" => $parentLicense
1852  ];
1853  }
1854 
1862  public function testMergeLicenseNotAdmin()
1863  {
1864  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1865  $body = $this->streamFactory->createStream(json_encode([]));
1866  $request = $this->getRequestWithBody("POST", $body);
1867 
1868  $this->expectException(HttpForbiddenException::class);
1869  $this->licenseController->mergeLicense($request, new ResponseHelper(), []);
1870 
1871  }
1872 
1881  {
1882  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1883  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1884 
1885  $body = $this->streamFactory->createStream(json_encode([
1886  "parentShortname" => $parentLicense->getShortName(),
1887  ]));
1888  $request = $this->getRequestWithBody("POST", $body);
1889 
1890  $this->expectException(HttpBadRequestException::class);
1891  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => ""]);
1892 
1893  }
1894 
1903  {
1904  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1905  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1906 
1907  $body = $this->streamFactory->createStream(json_encode([
1908  "parentShortname" => $parentLicense->getShortName(),
1909  ]));
1910  $request = $this->getRequestWithBody("POST", $body);
1911 
1912  $this->expectException(HttpBadRequestException::class);
1913  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $parentLicense->getShortName()]);
1914 
1915  }
1916 
1924  public function testMergeNotFoundLicense()
1925  {
1926  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1927  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1928  $license = $this->getLicenseArgs()["license"];
1929 
1930  $body = $this->streamFactory->createStream(json_encode([
1931  "parentShortname" => $parentLicense->getShortName(),
1932  ]));
1933  $this->licenseDao->shouldReceive("getLicenseByShortName")
1934  ->withArgs([$license->getShortName(),$this->groupId])->andReturn(null);
1935  $this->licenseDao->shouldReceive("getLicenseByShortName")
1936  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1937 
1938  $request = $this->getRequestWithBody("POST", $body);
1939 
1940  $this->expectException(HttpNotFoundException::class);
1941  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1942 
1943  }
1944 
1954  {
1955  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1956  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1957  $license = $this->getLicenseArgs()["license"];
1958 
1959  $body = $this->streamFactory->createStream(json_encode([
1960  "parentShortname" => $parentLicense->getShortName(),
1961  ]));
1962  $this->licenseDao->shouldReceive("getLicenseByShortName")
1963  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1964  $this->licenseDao->shouldReceive("getLicenseByShortName")
1965  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1966  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
1967  ->withArgs([$license->getId()])->andReturn([]);
1968 
1969  $request = $this->getRequestWithBody("POST", $body);
1970 
1971  $this->expectException(HttpNotFoundException::class);
1972  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1973 
1974  }
1975 
1984  {
1985  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1986  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1987  $license = $this->getLicenseArgs()["license"];
1988 
1989  $vars = [
1990  "rf_shortname" => "AGPL-1.0-or-later",
1991  "rf_text" => "License by OJO.",
1992  "shortname" => "AGPL-1.0-or-later",
1993  ];
1994  $body = $this->streamFactory->createStream(json_encode([
1995  "parentShortname" => $parentLicense->getShortName(),
1996  ]));
1997  $this->licenseDao->shouldReceive("getLicenseByShortName")
1998  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1999  $this->licenseDao->shouldReceive("getLicenseByShortName")
2000  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2001  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2002  ->withArgs([$license->getId()])->andReturn($vars);
2003  $this->licenseCandidatePlugin->shouldReceive("mergeCandidate")
2004  ->withArgs([$license->getId(), $parentLicense->getId(), $vars ])->andReturn(false);
2005  $request = $this->getRequestWithBody("POST", $body);
2006 
2007  $this->expectException(HttpInternalServerErrorException::class);
2008  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2009 
2010  }
2011 
2020  public function testMergeLicense()
2021  {
2022  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2023  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2024  $license = $this->getLicenseArgs()["license"];
2025 
2026  $vars = [
2027  "rf_shortname" => "AGPL-1.0-or-later",
2028  "rf_text" => "License by OJO.",
2029  "shortname" => "AGPL-1.0-or-later",
2030  ];
2031  $body = $this->streamFactory->createStream(json_encode([
2032  "parentShortname" => $parentLicense->getShortName(),
2033  ]));
2034  $this->licenseDao->shouldReceive("getLicenseByShortName")
2035  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
2036  $this->licenseDao->shouldReceive("getLicenseByShortName")
2037  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2038  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2039  ->withArgs([$license->getId()])->andReturn($vars);
2040  $this->licenseCandidatePlugin->shouldReceive("mergeCandidate")
2041  ->withArgs([$license->getId(), $parentLicense->getId(), $vars ])->andReturn(true);
2042  $request = $this->getRequestWithBody("PUT", $body);
2043 
2044  $info = new Info(200, "Successfully merged candidate (". $parentLicense->getShortName() .") into (".$license->getShortName() .").", InfoType::INFO);
2045  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
2046 
2047  $actualResponse = $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2048  $this->assertEquals(200, $actualResponse->getStatusCode());
2049  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
2050 
2051  }
2052 
2061  {
2062  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
2063  $request = $this->getEmptyRequest("GET");
2064  $this->expectException(HttpForbiddenException::class);
2065  $this->licenseController->getSuggestedLicense($request, new ResponseHelper(), []);
2066  }
2067 
2077  {
2078  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2079  $body = $this->streamFactory->createStream(json_encode([
2080  "referenceText" => ""
2081  ]));
2082 
2083  $licenseCandidate = [
2084  "rf_pk" => 2,
2085  "rf_spdx_id"=> 4,
2086  "rf_fullname" => "GFDL-1.3-no-invariants-or-later",
2087  "rf_shortname" => "GFDL-1.1-no-invariants-only",
2088  "rf_text" => "License by OJO.",
2089  "rf_url" => "",
2090  "rf_notes" => "",
2091  "rf_risk" => "",
2092 
2093  ];
2094 
2095  $this->licenseCandidatePlugin->shouldReceive("suggestLicenseId")
2096  ->withArgs([$licenseCandidate['rf_text'],true])->andReturn([[2,3,5,4],[]]);
2097  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2098  ->withArgs([2,"ONLY license-ref"])->andReturn($licenseCandidate);
2099  $expectedResponse = (new ResponseHelper())->withJson([
2100  'id' => intval($licenseCandidate['rf_pk']),
2101  'spdxName' => $licenseCandidate['rf_spdx_id'],
2102  'shortName' => $licenseCandidate['rf_shortname'],
2103  'fullName' => $licenseCandidate['rf_fullname'],
2104  'text' => $licenseCandidate['rf_text'],
2105  'url' => $licenseCandidate['rf_url'],
2106  'notes' => $licenseCandidate['rf_notes'],
2107  'risk' => intval($licenseCandidate['rf_risk']),
2108  "highlights" => []
2109  ], 200);
2110 
2111  $request = $this->getRequestWithBody("GET", $body);
2112  $this->expectException(HttpBadRequestException::class);
2113  $actualResponse = $this->licenseController->getSuggestedLicense($request, new ResponseHelper(), []);
2114  $this->assertEquals(200,$actualResponse->getStatusCode());
2115  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
2116 
2117  }
2118 
2119 
2120 
2121 
2122 
2123 
2130  public function testGetCandidatesV1()
2131  {
2132  $this->testGetCandidates(ApiVersion::V1);
2133  }
2140  public function testGetCandidatesV2()
2141  {
2142  $this->testGetCandidates();
2143  }
2144  private function testGetCandidates($version = ApiVersion::V2)
2145  {
2146  $request = M::mock(Request::class);
2147  $request->shouldReceive('getAttribute')->andReturn($version);
2148  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2149  $this->licenseCandidatePlugin->shouldReceive('getCandidateArrayData')->andReturn([]);
2150 
2151  $expectedResponse = (new ResponseHelper())->withJson([], 200);
2152  $actualResponse = $this->licenseController->getCandidates($request,
2153  new ResponseHelper(), null);
2154  $this->assertEquals($expectedResponse->getStatusCode(),
2155  $actualResponse->getStatusCode());
2156  $this->assertEquals($this->getResponseJson($expectedResponse),
2157  $this->getResponseJson($actualResponse));
2158  }
2159 
2165  public function testGetCandidatesNoAdminV1()
2166  {
2167  $this->testGetCandidatesNoAdmin(ApiVersion::V1);
2168  }
2174  public function testGetCandidatesNoAdminV2()
2175  {
2176  $this->testGetCandidatesNoAdmin();
2177  }
2178 
2183  private function testGetCandidatesNoAdmin($version = ApiVersion::V2)
2184  {
2185  $request = M::mock(Request::class);
2186  $request->shouldReceive('getAttribute')->andReturn($version);
2187  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_READ;
2188 
2189  $this->expectException(HttpForbiddenException::class);
2190 
2191  $this->licenseController->getCandidates($request,
2192  new ResponseHelper(), null);
2193  }
2194 }
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
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
getLicense($shortname, $obligations=false, $emptyObligation=true)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16