FOSSology  4.7.0-rc1
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 
1666  {
1667  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1668  $this->dbManager->shouldReceive('getRows')->withAnyArgs()->andReturn([]);
1669 
1670  $request = $this->getRequestWithQueryParams('GET', []);
1671  $actualResponse = $this->licenseController->exportBulkText($request,
1672  new ResponseHelper(), []);
1673 
1674  $this->assertEquals(200, $actualResponse->getStatusCode());
1675  $this->assertStringContainsString('application/json',
1676  $actualResponse->getHeaderLine('Content-type'));
1677  $this->assertStringContainsString('.json',
1678  $actualResponse->getHeaderLine('Content-Disposition'));
1679  }
1680 
1689  {
1690  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1691  $this->dbManager->shouldReceive('getRows')->withAnyArgs()->andReturn([]);
1692 
1693  $request = $this->getRequestWithQueryParams('GET', [
1694  'format' => 'csv',
1695  'filter' => 'user',
1696  'userId' => 4,
1697  'delimiter' => ';',
1698  'enclosure' => '"'
1699  ]);
1700  $actualResponse = $this->licenseController->exportBulkText($request,
1701  new ResponseHelper(), []);
1702 
1703  $this->assertEquals(200, $actualResponse->getStatusCode());
1704  $this->assertStringContainsString('text/csv',
1705  $actualResponse->getHeaderLine('Content-type'));
1706  $this->assertStringContainsString('.csv',
1707  $actualResponse->getHeaderLine('Content-Disposition'));
1708  }
1709 
1718  {
1719  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1720  $request = $this->getRequestWithQueryParams('GET', ['format' => 'xml']);
1721 
1722  $this->expectException(HttpBadRequestException::class);
1723  $this->licenseController->exportBulkText($request, new ResponseHelper(), []);
1724  }
1725 
1734  {
1735  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1736  $request = $this->getRequestWithQueryParams('GET', ['filter' => 'user']);
1737 
1738  $this->expectException(HttpBadRequestException::class);
1739  $this->licenseController->exportBulkText($request, new ResponseHelper(), []);
1740  }
1741 
1750  {
1751  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1752  $request = $this->getRequestWithQueryParams('GET', [
1753  'filter' => 'group',
1754  'groupId' => 0
1755  ]);
1756 
1757  $this->expectException(HttpBadRequestException::class);
1758  $this->licenseController->exportBulkText($request, new ResponseHelper(), []);
1759  }
1760 
1768  public function testExportBulkTextNotAdmin()
1769  {
1770  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1771  $request = $this->getRequestWithQueryParams('GET', []);
1772 
1773  $this->expectException(HttpForbiddenException::class);
1774  $this->licenseController->exportBulkText($request, new ResponseHelper(), []);
1775  }
1776 
1781  public function getEmptyRequest($method)
1782  {
1783  $requestHeaders = new Headers();
1784  $body = $this->streamFactory->createStream();
1785  $request = new Request($method, new Uri("HTTP", "localhost"),
1786  $requestHeaders, [], [], $body);
1787  return $request;
1788  }
1789 
1793  public function getRequestWithBody($method, $body)
1794  {
1795  $requestHeaders = new Headers();
1796  $requestHeaders->setHeader('Content-Type', 'application/json');
1797 
1798  $request = new Request($method, new Uri("HTTP", "localhost"),
1799  $requestHeaders, [], [], $body);
1800  return $request;
1801  }
1802 
1808  private function getRequestWithQueryParams($method, $queryParams)
1809  {
1810  $request = M::mock('Psr\\Http\\Message\\ServerRequestInterface');
1811  $request->shouldReceive('getQueryParams')->andReturn($queryParams);
1812  $request->shouldReceive('getMethod')->andReturn($method);
1813  return $request;
1814  }
1815 
1823  public function testVerifyLicenseNotAdmin()
1824  {
1825  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1826  $request = $this->getEmptyRequest("POST");
1827 
1828  $this->expectException(HttpForbiddenException::class);
1829  $this->licenseController->verifyLicense($request, new ResponseHelper(),[]);
1830 
1831  }
1832 
1833 
1842  {
1843  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1844 
1845  $license = $this->getLicenseArgs()["license"];
1846  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1847  $body = $this->streamFactory->createStream(json_encode([
1848  "parentShortname" => $parentLicense->getShortName(),
1849  ]));
1850  $request = $this->getRequestWithBody("POST", $body);
1851  $this->expectException(HttpBadRequestException::class);
1852  $this->licenseController->verifyLicense($request, new ResponseHelper(), [
1853  "shortname" => ""
1854  ]);
1855  }
1856 
1857 
1858 
1868  {
1869  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1870  $license = $this->getLicenseArgs()["license"];
1871  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1872 
1873  $body = $this->streamFactory->createStream(json_encode([
1874  "parentShortname" => $parentLicense->getShortName(),
1875  ]));
1876 
1877  $this->licenseDao->shouldReceive("getLicenseByShortName")
1878  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1879  $this->licenseDao->shouldReceive("getLicenseByShortName")
1880  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1881 
1882  $this->licenseCandidatePlugin->shouldReceive("verifyCandidate")
1883  ->withArgs([$license->getId(),$license->getShortName(), $parentLicense->getId()])
1884  ->andReturn(false);
1885  $request = $this->getRequestWithBody("POST", $body);
1886 
1887  $this->expectException(HttpBadRequestException::class);
1888  $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1889 
1890  }
1899  public function testVerifyNotFoundLicense()
1900  {
1901  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1902  $license = $this->getLicenseArgs()["license"];
1903  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1904 
1905  $body = $this->streamFactory->createStream(json_encode([
1906  "parentShortname" => $parentLicense->getShortName(),
1907  ]));
1908 
1909  $this->licenseDao->shouldReceive("getLicenseByShortName")
1910  ->withArgs([$license->getShortName(),$this->groupId])->andReturn([]);
1911  $this->licenseDao->shouldReceive("getLicenseByShortName")
1912  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1913 
1914  $request = $this->getRequestWithBody("POST", $body);
1915 
1916  $this->expectException(HttpNotFoundException::class);
1917  $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1918 
1919  }
1920 
1929  public function testVerifyLicense()
1930  {
1931  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
1932  $license = $this->getLicenseArgs()["license"];
1933  $parentLicense = $this->getLicenseArgs()["parentLicense"];
1934 
1935  $body = $this->streamFactory->createStream(json_encode([
1936  "parentShortname" => $parentLicense->getShortName(),
1937  ]));
1938 
1939  $this->licenseDao->shouldReceive("getLicenseByShortName")
1940  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
1941  $this->licenseDao->shouldReceive("getLicenseByShortName")
1942  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
1943 
1944  $this->licenseCandidatePlugin->shouldReceive("verifyCandidate")
1945  ->withArgs([$license->getId(),$license->getShortName(), $parentLicense->getId()])
1946  ->andReturn(true);
1947  $request = $this->getRequestWithBody("POST", $body);
1948 
1949  $info = new Info(200, 'Successfully verified candidate ('.$license->getShortName().')'.' as variant of ('.$parentLicense->getShortName().').', InfoType::INFO);
1950  $expectedResponse = (new ResponseHelper())->withJson([
1951  "code" => $info->getCode(),
1952  "message" => $info->getMessage(),
1953  "type" => $info->getType()
1954  ]);
1955 
1956  $actualResponse = $this->licenseController->verifyLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
1957 
1958  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
1959  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
1960 
1961  }
1962 
1967  public function getLicenseArgs()
1968  {
1969  $license = new License(1,
1970  "MIT",
1971  "MIT License",
1972  "GNU GENERAL PUBLIC LICENSE Copyright (C) 1989 Free Software ",
1973  );
1974 
1975  $parentLicense = new License(3,
1976  "MPL-2.0",
1977  "MPL-2.0 License",
1978  "GNU GENERAL PUBLIC LICENSE Copyright (C) 1989 Free Software ",
1979  );
1980 
1981  return [
1982  "license" => $license,
1983  "parentLicense" => $parentLicense
1984  ];
1985  }
1986 
1994  public function testMergeLicenseNotAdmin()
1995  {
1996  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
1997  $body = $this->streamFactory->createStream(json_encode([]));
1998  $request = $this->getRequestWithBody("POST", $body);
1999 
2000  $this->expectException(HttpForbiddenException::class);
2001  $this->licenseController->mergeLicense($request, new ResponseHelper(), []);
2002 
2003  }
2004 
2013  {
2014  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2015  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2016 
2017  $body = $this->streamFactory->createStream(json_encode([
2018  "parentShortname" => $parentLicense->getShortName(),
2019  ]));
2020  $request = $this->getRequestWithBody("POST", $body);
2021 
2022  $this->expectException(HttpBadRequestException::class);
2023  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => ""]);
2024 
2025  }
2026 
2035  {
2036  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2037  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2038 
2039  $body = $this->streamFactory->createStream(json_encode([
2040  "parentShortname" => $parentLicense->getShortName(),
2041  ]));
2042  $request = $this->getRequestWithBody("POST", $body);
2043 
2044  $this->expectException(HttpBadRequestException::class);
2045  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $parentLicense->getShortName()]);
2046 
2047  }
2048 
2056  public function testMergeNotFoundLicense()
2057  {
2058  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2059  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2060  $license = $this->getLicenseArgs()["license"];
2061 
2062  $body = $this->streamFactory->createStream(json_encode([
2063  "parentShortname" => $parentLicense->getShortName(),
2064  ]));
2065  $this->licenseDao->shouldReceive("getLicenseByShortName")
2066  ->withArgs([$license->getShortName(),$this->groupId])->andReturn(null);
2067  $this->licenseDao->shouldReceive("getLicenseByShortName")
2068  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2069 
2070  $request = $this->getRequestWithBody("POST", $body);
2071 
2072  $this->expectException(HttpNotFoundException::class);
2073  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2074 
2075  }
2076 
2086  {
2087  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2088  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2089  $license = $this->getLicenseArgs()["license"];
2090 
2091  $body = $this->streamFactory->createStream(json_encode([
2092  "parentShortname" => $parentLicense->getShortName(),
2093  ]));
2094  $this->licenseDao->shouldReceive("getLicenseByShortName")
2095  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
2096  $this->licenseDao->shouldReceive("getLicenseByShortName")
2097  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2098  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2099  ->withArgs([$license->getId()])->andReturn([]);
2100 
2101  $request = $this->getRequestWithBody("POST", $body);
2102 
2103  $this->expectException(HttpNotFoundException::class);
2104  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2105 
2106  }
2107 
2116  {
2117  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2118  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2119  $license = $this->getLicenseArgs()["license"];
2120 
2121  $vars = [
2122  "rf_shortname" => "AGPL-1.0-or-later",
2123  "rf_text" => "License by OJO.",
2124  "shortname" => "AGPL-1.0-or-later",
2125  ];
2126  $body = $this->streamFactory->createStream(json_encode([
2127  "parentShortname" => $parentLicense->getShortName(),
2128  ]));
2129  $this->licenseDao->shouldReceive("getLicenseByShortName")
2130  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
2131  $this->licenseDao->shouldReceive("getLicenseByShortName")
2132  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2133  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2134  ->withArgs([$license->getId()])->andReturn($vars);
2135  $this->licenseCandidatePlugin->shouldReceive("mergeCandidate")
2136  ->withArgs([$license->getId(), $parentLicense->getId(), $vars ])->andReturn(false);
2137  $request = $this->getRequestWithBody("POST", $body);
2138 
2139  $this->expectException(HttpInternalServerErrorException::class);
2140  $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2141 
2142  }
2143 
2152  public function testMergeLicense()
2153  {
2154  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2155  $parentLicense = $this->getLicenseArgs()["parentLicense"];
2156  $license = $this->getLicenseArgs()["license"];
2157 
2158  $vars = [
2159  "rf_shortname" => "AGPL-1.0-or-later",
2160  "rf_text" => "License by OJO.",
2161  "shortname" => "AGPL-1.0-or-later",
2162  ];
2163  $body = $this->streamFactory->createStream(json_encode([
2164  "parentShortname" => $parentLicense->getShortName(),
2165  ]));
2166  $this->licenseDao->shouldReceive("getLicenseByShortName")
2167  ->withArgs([$license->getShortName(),$this->groupId])->andReturn($license);
2168  $this->licenseDao->shouldReceive("getLicenseByShortName")
2169  ->withArgs([$parentLicense->getShortName(), $this->groupId])->andReturn($parentLicense);
2170  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2171  ->withArgs([$license->getId()])->andReturn($vars);
2172  $this->licenseCandidatePlugin->shouldReceive("mergeCandidate")
2173  ->withArgs([$license->getId(), $parentLicense->getId(), $vars ])->andReturn(true);
2174  $request = $this->getRequestWithBody("PUT", $body);
2175 
2176  $info = new Info(200, "Successfully merged candidate (". $parentLicense->getShortName() .") into (".$license->getShortName() .").", InfoType::INFO);
2177  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
2178 
2179  $actualResponse = $this->licenseController->mergeLicense($request, new ResponseHelper(), ["shortname" => $license->getShortName()]);
2180  $this->assertEquals(200, $actualResponse->getStatusCode());
2181  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
2182 
2183  }
2184 
2193  {
2194  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_NONE;
2195  $request = $this->getEmptyRequest("GET");
2196  $this->expectException(HttpForbiddenException::class);
2197  $this->licenseController->getSuggestedLicense($request, new ResponseHelper(), []);
2198  }
2199 
2209  {
2210  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2211  $body = $this->streamFactory->createStream(json_encode([
2212  "referenceText" => ""
2213  ]));
2214 
2215  $licenseCandidate = [
2216  "rf_pk" => 2,
2217  "rf_spdx_id"=> 4,
2218  "rf_fullname" => "GFDL-1.3-no-invariants-or-later",
2219  "rf_shortname" => "GFDL-1.1-no-invariants-only",
2220  "rf_text" => "License by OJO.",
2221  "rf_url" => "",
2222  "rf_notes" => "",
2223  "rf_risk" => "",
2224 
2225  ];
2226 
2227  $this->licenseCandidatePlugin->shouldReceive("suggestLicenseId")
2228  ->withArgs([$licenseCandidate['rf_text'],true])->andReturn([[2,3,5,4],[]]);
2229  $this->licenseCandidatePlugin->shouldReceive("getDataRow")
2230  ->withArgs([2,"ONLY license-ref"])->andReturn($licenseCandidate);
2231  $expectedResponse = (new ResponseHelper())->withJson([
2232  'id' => intval($licenseCandidate['rf_pk']),
2233  'spdxName' => $licenseCandidate['rf_spdx_id'],
2234  'shortName' => $licenseCandidate['rf_shortname'],
2235  'fullName' => $licenseCandidate['rf_fullname'],
2236  'text' => $licenseCandidate['rf_text'],
2237  'url' => $licenseCandidate['rf_url'],
2238  'notes' => $licenseCandidate['rf_notes'],
2239  'risk' => intval($licenseCandidate['rf_risk']),
2240  "highlights" => []
2241  ], 200);
2242 
2243  $request = $this->getRequestWithBody("GET", $body);
2244  $this->expectException(HttpBadRequestException::class);
2245  $actualResponse = $this->licenseController->getSuggestedLicense($request, new ResponseHelper(), []);
2246  $this->assertEquals(200,$actualResponse->getStatusCode());
2247  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
2248 
2249  }
2250 
2251 
2252 
2253 
2254 
2255 
2262  public function testGetCandidatesV1()
2263  {
2264  $this->testGetCandidates(ApiVersion::V1);
2265  }
2272  public function testGetCandidatesV2()
2273  {
2274  $this->testGetCandidates();
2275  }
2276  private function testGetCandidates($version = ApiVersion::V2)
2277  {
2278  $request = M::mock(Request::class);
2279  $request->shouldReceive('getAttribute')->andReturn($version);
2280  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
2281  $this->licenseCandidatePlugin->shouldReceive('getCandidateArrayData')->andReturn([]);
2282 
2283  $expectedResponse = (new ResponseHelper())->withJson([], 200);
2284  $actualResponse = $this->licenseController->getCandidates($request,
2285  new ResponseHelper(), null);
2286  $this->assertEquals($expectedResponse->getStatusCode(),
2287  $actualResponse->getStatusCode());
2288  $this->assertEquals($this->getResponseJson($expectedResponse),
2289  $this->getResponseJson($actualResponse));
2290  }
2291 
2297  public function testGetCandidatesNoAdminV1()
2298  {
2299  $this->testGetCandidatesNoAdmin(ApiVersion::V1);
2300  }
2306  public function testGetCandidatesNoAdminV2()
2307  {
2308  $this->testGetCandidatesNoAdmin();
2309  }
2310 
2315  private function testGetCandidatesNoAdmin($version = ApiVersion::V2)
2316  {
2317  $request = M::mock(Request::class);
2318  $request->shouldReceive('getAttribute')->andReturn($version);
2319  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_READ;
2320 
2321  $this->expectException(HttpForbiddenException::class);
2322 
2323  $this->licenseController->getCandidates($request,
2324  new ResponseHelper(), null);
2325  }
2326 }
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