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 
34 use Mockery as M;
35 use Slim\Psr7\Factory\StreamFactory;
36 use Slim\Psr7\Headers;
37 use Slim\Psr7\Request;
38 use Slim\Psr7\Uri;
39 use Symfony\Component\HttpFoundation\Response;
40 
45 class LicenseControllerTest extends \PHPUnit\Framework\TestCase
46 {
52 
57  private $userId;
58 
63  private $groupId;
64 
69  private $dbHelper;
70 
75  private $dbManager;
76 
81  private $restHelper;
82 
88 
93  private $licenseDao;
94 
99  private $userDao;
100 
106 
112 
118 
123  private $streamFactory;
124 
130 
131 
136  private $auth;
137 
142  protected function setUp() : void
143  {
144  global $container;
145  $this->userId = 2;
146  $this->groupId = 2;
147  $container = M::mock('ContainerBuilder');
148  $this->dbHelper = M::mock(DbHelper::class);
149  $this->auth = M::mock(Auth::class);
150  $this->dbManager = M::mock(DbManager::class);
151  $this->restHelper = M::mock(RestHelper::class);
152  $this->licenseDao = M::mock(LicenseDao::class);
153  $this->userDao = M::mock(UserDao::class);
154  $this->adminLicenseAckDao = M::mock(LicenseAcknowledgementDao::class);
155  $this->adminLicensePlugin = M::mock('admin_license_from_csv');
156  $this->licenseCandidatePlugin = M::mock('admin_license_candidate');
157  $this->licenseStdCommentDao = M::mock(LicenseStdCommentDao::class);
158 
159  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
160 
161  $this->restHelper->shouldReceive('getPlugin')->withArgs(["admin_license_candidate"])->andReturn($this->licenseCandidatePlugin);
162  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
163  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
164  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
165  $this->restHelper->shouldReceive('getUserDao')->andReturn($this->userDao);
166 
167  $this->restHelper->shouldReceive('getPlugin')
168  ->withArgs(array('admin_license_from_csv'))->andReturn($this->adminLicensePlugin);
169  $container->shouldReceive('get')->withArgs(array(
170  'dao.license.stdc'))->andReturn($this->licenseStdCommentDao);
171  $container->shouldReceive('get')->withArgs(array(
172  'helper.restHelper'))->andReturn($this->restHelper);
173  $container->shouldReceive('get')->withArgs(array(
174  'dao.license.acknowledgement'))->andReturn($this->adminLicenseAckDao);
175  $container->shouldReceive('get')->withArgs(array(
176  'dao.license'))->andReturn($this->licenseDao);
177  $container->shouldReceive('get')->withArgs(array(
178  'dao.license.acknowledgement'))->andReturn($this->adminLicenseAckDao);
179  $this->licenseController = new LicenseController($container);
180  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
181  $this->streamFactory = new StreamFactory();
182  }
183 
188  protected function tearDown() : void
189  {
190  $this->addToAssertionCount(
191  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
192  M::close();
193  }
194 
201  private function getResponseJson($response)
202  {
203  $response->getBody()->seek(0);
204  return json_decode($response->getBody()->getContents(), true);
205  }
206 
212  private function getObligation($id)
213  {
214  return new Obligation($id, 'My obligation', 'Obligation',
215  'This should represent some valid obligation text.', 'yellow');
216  }
217 
223  private function getDaoObligation($id)
224  {
225  return [
226  'ob_pk' => $id,
227  'ob_topic' => 'My obligation',
228  'ob_text' => 'This should represent some valid obligation text.',
229  'ob_active' => true,
230  'rf_fk' => 2,
231  'ob_type' => 'Obligation',
232  'ob_classification' => 'yellow',
233  'ob_comment' => "",
234  'rf_shortname' => null
235  ];
236  }
237 
245  private function getLicense($shortname, $obligations=false,
246  $emptyObligation=true)
247  {
248  $obligationList = [
249  $this->getObligation(123),
250  $this->getObligation(124)
251  ];
252  $license = null;
253  if ($shortname == "MIT") {
254  $license = new License(22, "MIT", "MIT License",
255  "MIT License Copyright (c) <year> <copyright holders> ...",
256  "https://opensource.org/licenses/MIT", null, 2, false);
257  } else {
258  $license = new License(25, $shortname, "Exotic License",
259  "Exotic license for magical codes", "", null, 0, true);
260  }
261  if ($obligations) {
262  if ($emptyObligation) {
263  $license->setObligations([]);
264  } else {
265  $license->setObligations($obligationList);
266  }
267  }
268  return $license;
269  }
270 
276  private function getDaoLicense($shortname)
277  {
278  $license = null;
279  if ($shortname == "MIT") {
280  $license = new \Fossology\Lib\Data\License(22, "MIT", "MIT License",
281  2, "MIT License Copyright (c) <year> <copyright holders> ...",
282  "https://opensource.org/licenses/MIT", 1, true);
283  } else {
284  $license = new \Fossology\Lib\Data\License(25, $shortname,
285  "Exotic License", 0, "Exotic license for magical codes", "", 1,
286  false);
287  }
288  return $license;
289  }
290 
296  private function translateLicenseToDb($licenses)
297  {
298  $licenseList = [];
299  foreach ($licenses as $license) {
300  $licenseList[] = [
301  'rf_pk' => $license->getId(),
302  'rf_shortname' => $license->getShortName(),
303  'rf_fullname' => $license->getFullName(),
304  'rf_text' => $license->getText(),
305  'rf_url' => $license->getUrl(),
306  'rf_risk' => $license->getRisk(),
307  'group_fk' => $license->getIsCandidate() ? $this->groupId : 0
308  ];
309  }
310  return $licenseList;
311  }
312 
318  public function testGetLicense()
319  {
320  $licenseShortName = "MIT";
321  $license = $this->getLicense($licenseShortName, true);
322 
323  $requestHeaders = new Headers();
324  $body = $this->streamFactory->createStream();
325  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
326  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
327  $this->licenseDao->shouldReceive('getLicenseByShortName')
328  ->withArgs([$licenseShortName, $this->groupId])
329  ->andReturn($this->getDaoLicense($licenseShortName));
330  $this->licenseDao->shouldReceive('getLicenseObligations')
331  ->withArgs([[22], false])->andReturn([]);
332  $this->licenseDao->shouldReceive('getLicenseObligations')
333  ->withArgs([[22], true])->andReturn([]);
334  $expectedResponse = (new ResponseHelper())->withJson($license->getArray(), 200);
335 
336  $actualResponse = $this->licenseController->getLicense($request,
337  new ResponseHelper(), ['shortname' => $licenseShortName]);
338  $this->assertEquals($expectedResponse->getStatusCode(),
339  $actualResponse->getStatusCode());
340  $this->assertEquals($this->getResponseJson($expectedResponse),
341  $this->getResponseJson($actualResponse));
342  }
343 
350  public function testGetLicenseObligations()
351  {
352  $licenseShortName = "MIT";
353  $license = $this->getLicense($licenseShortName, true, false);
354 
355  $requestHeaders = new Headers();
356  $body = $this->streamFactory->createStream();
357  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
358  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
359  $this->licenseDao->shouldReceive('getLicenseByShortName')
360  ->withArgs([$licenseShortName, $this->groupId])
361  ->andReturn($this->getDaoLicense($licenseShortName));
362  $this->licenseDao->shouldReceive('getLicenseObligations')
363  ->withArgs([[22], false])->andReturn([$this->getDaoObligation(123)]);
364  $this->licenseDao->shouldReceive('getLicenseObligations')
365  ->withArgs([[22], true])->andReturn([$this->getDaoObligation(124)]);
366  $expectedResponse = (new ResponseHelper())->withJson($license->getArray(), 200);
367 
368  $actualResponse = $this->licenseController->getLicense($request,
369  new ResponseHelper(), ['shortname' => $licenseShortName]);
370  $this->assertEquals($expectedResponse->getStatusCode(),
371  $actualResponse->getStatusCode());
372  $this->assertEquals($this->getResponseJson($expectedResponse),
373  $this->getResponseJson($actualResponse));
374  }
375 
381  public function testGetLicenseNotFound()
382  {
383  $licenseShortName = "Bogus";
384 
385  $requestHeaders = new Headers();
386  $body = $this->streamFactory->createStream();
387  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
388  "/license/$licenseShortName"), $requestHeaders, [], [], $body);
389  $this->licenseDao->shouldReceive('getLicenseByShortName')
390  ->withArgs([$licenseShortName, $this->groupId])
391  ->andReturn(null);
392  $this->expectException(HttpNotFoundException::class);
393 
394  $this->licenseController->getLicense($request, new ResponseHelper(),
395  ['shortname' => $licenseShortName]);
396  }
397 
404  public function testGetAllLicense()
405  {
406  $licenses = [
407  $this->getLicense("MIT"),
408  $this->getLicense("Exotic"),
409  $this->getLicense("Exotic2"),
410  $this->getLicense("Exotic3")
411  ];
412 
413  $requestHeaders = new Headers();
414  $body = $this->streamFactory->createStream();
415  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
416  "/license"), $requestHeaders, [], [], $body);
417  $this->dbHelper->shouldReceive('getLicenseCount')
418  ->withArgs(["all", $this->groupId])->andReturn(4);
419  $this->dbHelper->shouldReceive('getLicensesPaginated')
420  ->withArgs([1, 100, "all", $this->groupId, false])
421  ->andReturn($this->translateLicenseToDb($licenses));
422 
423  $responseLicense = [];
424  foreach ($licenses as $license) {
425  $responseLicense[] = $license->getArray();
426  }
427  $expectedResponse = (new ResponseHelper())->withHeader("X-Total-Pages", 1)
428  ->withJson($responseLicense, 200);
429 
430  $actualResponse = $this->licenseController->getAllLicenses($request,
431  new ResponseHelper(), []);
432  $this->assertEquals($expectedResponse->getStatusCode(),
433  $actualResponse->getStatusCode());
434  $this->assertEquals($this->getResponseJson($expectedResponse),
435  $this->getResponseJson($actualResponse));
436  $this->assertEquals($expectedResponse->getHeaders(),
437  $actualResponse->getHeaders());
438  }
439 
447  public function testGetAllLicenseBounds()
448  {
449  $requestHeaders = new Headers();
450  $requestHeaders->setHeader('limit', 5);
451  $requestHeaders->setHeader('page', 2);
452  $body = $this->streamFactory->createStream();
453  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
454  "/license"), $requestHeaders, [], [], $body);
455  $this->dbHelper->shouldReceive('getLicenseCount')
456  ->withArgs(["all", $this->groupId])->andReturn(4);
457  $this->expectException(HttpBadRequestException::class);
458 
459  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
460  }
461 
467  public function testGetAllLicenseFilters()
468  {
469  // All licenses
470  $requestHeaders = new Headers();
471  $body = $this->streamFactory->createStream();
472  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
473  "/license", "kind=all"), $requestHeaders, [], [], $body);
474  $this->dbHelper->shouldReceive('getLicenseCount')
475  ->withArgs(["all", $this->groupId])->andReturn(4)->once();
476  $this->dbHelper->shouldReceive('getLicensesPaginated')
477  ->withArgs([1, 100, "all", $this->groupId, false])
478  ->andReturn([])->once();
479 
480  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
481 
482  // Main licenses
483  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
484  "/license", "kind=main"), $requestHeaders, [], [], $body);
485  $this->dbHelper->shouldReceive('getLicenseCount')
486  ->withArgs(["main", $this->groupId])->andReturn(4)->once();
487  $this->dbHelper->shouldReceive('getLicensesPaginated')
488  ->withArgs([1, 100, "main", $this->groupId, false])
489  ->andReturn([])->once();
490 
491  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
492 
493  // Candidate licenses
494  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
495  "/license", "kind=candidate"), $requestHeaders, [], [], $body);
496  $this->dbHelper->shouldReceive('getLicenseCount')
497  ->withArgs(["candidate", $this->groupId])->andReturn(4)->once();
498  $this->dbHelper->shouldReceive('getLicensesPaginated')
499  ->withArgs([1, 100, "candidate", $this->groupId, false])
500  ->andReturn([])->once();
501 
502  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
503 
504  // wrong filter
505  $request = new Request("GET", new Uri("HTTP", "localhost", 80,
506  "/license", "kind=bogus"), $requestHeaders, [], [], $body);
507  $this->dbHelper->shouldReceive('getLicenseCount')
508  ->withArgs(["all", $this->groupId])->andReturn(4)->once();
509  $this->dbHelper->shouldReceive('getLicensesPaginated')
510  ->withArgs([1, 100, "all", $this->groupId, false])
511  ->andReturn([])->once();
512 
513  $this->licenseController->getAllLicenses($request, new ResponseHelper(), []);
514  }
515 
521  public function testCreateLicense()
522  {
523  $license = $this->getLicense("MIT");
524  $requestBody = $license->getArray();
525  $requestBody["isCandidate"] = true;
526  unset($requestBody['id']);
527 
528  $requestHeaders = new Headers();
529  $requestHeaders->setHeader('Content-Type', 'application/json');
530  $body = $this->streamFactory->createStream();
531  $body->write(json_encode($requestBody));
532  $body->seek(0);
533  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
534  "/license"), $requestHeaders, [], [], $body);
535 
536  $tableName = "license_candidate";
537  $assocData = [
538  "rf_shortname" => $license->getShortName(),
539  "rf_fullname" => $license->getFullName(),
540  "rf_text" => $license->getText(),
541  "rf_md5" => md5($license->getText()),
542  "rf_risk" => $license->getRisk(),
543  "rf_url" => $license->getUrl(),
544  "rf_detector_type" => 1,
545  "group_fk" => $this->groupId,
546  "rf_user_fk_created" => $this->userId,
547  "rf_user_fk_modified" => $this->userId,
548  "marydone" => false
549  ];
550 
551  $sql = "SELECT count(*) cnt FROM ".
552  "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
553 
554  $this->dbManager->shouldReceive('insertTableRow')
555  ->withArgs([$tableName, $assocData, M::any(), "rf_pk"])->andReturn(4);
556  $this->dbManager->shouldReceive('getSingleRow')
557  ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()])
558  ->andReturn(["cnt" => 0]);
559 
560  $info = new Info(201, '4', InfoType::INFO);
561  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
562  $info->getCode());
563 
564  $actualResponse = $this->licenseController->createLicense($request,
565  new ResponseHelper(), []);
566  $this->assertEquals($expectedResponse->getStatusCode(),
567  $actualResponse->getStatusCode());
568  $this->assertEquals($this->getResponseJson($expectedResponse),
569  $this->getResponseJson($actualResponse));
570  }
571 
578  public function testCreateLicenseNoShort()
579  {
580  $license = $this->getLicense("MIT");
581  $requestBody = $license->getArray();
582  $requestBody["isCandidate"] = true;
583  unset($requestBody['id']);
584  unset($requestBody['shortName']);
585 
586  $requestHeaders = new Headers();
587  $requestHeaders->setHeader('Content-Type', 'application/json');
588  $body = $this->streamFactory->createStream();
589  $body->write(json_encode($requestBody));
590  $body->seek(0);
591  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
592  "/license"), $requestHeaders, [], [], $body);
593  $this->expectException(HttpBadRequestException::class);
594 
595  $this->licenseController->createLicense($request, new ResponseHelper(), []);
596  }
597 
604  public function testCreateLicenseNoAdmin()
605  {
606  $license = $this->getLicense("MIT");
607  $requestBody = $license->getArray();
608  unset($requestBody['id']);
609 
610  $requestHeaders = new Headers();
611  $requestHeaders->setHeader('Content-Type', 'application/json');
612  $body = $this->streamFactory->createStream();
613  $body->write(json_encode($requestBody));
614  $body->seek(0);
615  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
616  "/license"), $requestHeaders, [], [], $body);
617  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
618  $this->expectException(HttpForbiddenException::class);
619 
620  $this->licenseController->createLicense($request, new ResponseHelper(), []);
621  }
622 
629  public function testCreateDuplicateLicense()
630  {
631  $license = $this->getLicense("MIT");
632  $requestBody = $license->getArray();
633  $requestBody["isCandidate"] = true;
634  unset($requestBody['id']);
635 
636  $requestHeaders = new Headers();
637  $requestHeaders->setHeader('Content-Type', 'application/json');
638  $body = $this->streamFactory->createStream();
639  $body->write(json_encode($requestBody));
640  $body->seek(0);
641  $request = new Request("POST", new Uri("HTTP", "localhost", 80,
642  "/license"), $requestHeaders, [], [], $body);
643 
644  $tableName = "license_candidate";
645 
646  $sql = "SELECT count(*) cnt FROM ".
647  "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
648 
649  $this->dbManager->shouldReceive('getSingleRow')
650  ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()])
651  ->andReturn(["cnt" => 1]);
652  $this->expectException(HttpConflictException::class);
653 
654  $this->licenseController->createLicense($request, new ResponseHelper(), []);
655  }
656 
662  public function testUpdateLicense()
663  {
664  $license = $this->getDaoLicense("Exotic");
665  $requestBody = [
666  "fullName" => "Exotic License - style",
667  "risk" => 0
668  ];
669 
670  $requestHeaders = new Headers();
671  $requestHeaders->setHeader('Content-Type', 'application/json');
672  $body = $this->streamFactory->createStream();
673  $body->write(json_encode($requestBody));
674  $body->seek(0);
675  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
676  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
677 
678  $tableName = "license_candidate";
679  $assocData = [
680  "rf_fullname" => "Exotic License - style",
681  "rf_risk" => 0
682  ];
683 
684  $this->userDao->shouldReceive('isAdvisorOrAdmin')
685  ->withArgs([$this->userId, $this->groupId])->andReturn(true);
686  $this->licenseDao->shouldReceive('getLicenseByShortName')
687  ->withArgs([$license->getShortName(), $this->groupId])
688  ->andReturn($license);
689  $this->dbHelper->shouldReceive('doesIdExist')
690  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
691  ->andReturn(true);
692  $this->dbManager->shouldReceive('updateTableRow')
693  ->withArgs([$tableName, $assocData, "rf_pk", $license->getId(), M::any()]);
694 
695  $info = new Info(200, "License " . $license->getShortName() . " updated.",
696  InfoType::INFO);
697  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
698  $info->getCode());
699 
700  $actualResponse = $this->licenseController->updateLicense($request,
701  new ResponseHelper(), ["shortname" => $license->getShortName()]);
702  $this->assertEquals($expectedResponse->getStatusCode(),
703  $actualResponse->getStatusCode());
704  $this->assertEquals($this->getResponseJson($expectedResponse),
705  $this->getResponseJson($actualResponse));
706  }
707 
714  public function testUpdateLicenseNonAdvisor()
715  {
716  $license = $this->getDaoLicense("Exotic");
717  $requestBody = [
718  "fullName" => "Exotic License - style",
719  "risk" => 0
720  ];
721 
722  $requestHeaders = new Headers();
723  $requestHeaders->setHeader('Content-Type', 'application/json');
724  $body = $this->streamFactory->createStream();
725  $body->write(json_encode($requestBody));
726  $body->seek(0);
727  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
728  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
729 
730  $this->userDao->shouldReceive('isAdvisorOrAdmin')
731  ->withArgs([$this->userId, $this->groupId])->andReturn(false);
732  $this->licenseDao->shouldReceive('getLicenseByShortName')
733  ->withArgs([$license->getShortName(), $this->groupId])
734  ->andReturn($license);
735  $this->dbHelper->shouldReceive('doesIdExist')
736  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
737  ->andReturn(true);
738  $this->expectException(HttpForbiddenException::class);
739 
740  $this->licenseController->updateLicense($request, new ResponseHelper(),
741  ["shortname" => $license->getShortName()]);
742  }
743 
750  public function testUpdateLicenseNonAdmin()
751  {
752  $license = $this->getDaoLicense("MIT");
753  $requestBody = [
754  "fullName" => "MIT License - style",
755  "risk" => 0
756  ];
757 
758  $requestHeaders = new Headers();
759  $requestHeaders->setHeader('Content-Type', 'application/json');
760  $body = $this->streamFactory->createStream();
761  $body->write(json_encode($requestBody));
762  $body->seek(0);
763  $request = new Request("PATCH", new Uri("HTTP", "localhost", 80,
764  "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
765  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
766 
767  $this->userDao->shouldReceive('isAdvisorOrAdmin')
768  ->withArgs([$this->userId, $this->groupId])->andReturn(true);
769  $this->licenseDao->shouldReceive('getLicenseByShortName')
770  ->withArgs([$license->getShortName(), $this->groupId])
771  ->andReturn($license);
772  $this->dbHelper->shouldReceive('doesIdExist')
773  ->withArgs(["license_candidate", "rf_pk", $license->getId()])
774  ->andReturn(false);
775  $this->expectException(HttpForbiddenException::class);
776 
777  $this->licenseController->updateLicense($request, new ResponseHelper(),
778  ["shortname" => $license->getShortName()]);
779  }
780 
781 
790  public function testImportLicense()
791  {
792 
793  $delimiter = ',';
794  $enclosure = '"';
795 
796  $requestHeaders = new Headers();
797  $requestHeaders->setHeader('Content-Type', 'application/json');
798 
799  $body = $this->streamFactory->createStream(json_encode([]));
800 
801  $request = new Request("POST", new Uri("HTTP", "localhost"),
802  $requestHeaders, [], [], $body);
803 
804  $FILE_INPUT_NAME = "file_input";
805 
806  $this->adminLicensePlugin->shouldReceive('getFileInputName')
807  ->andReturn($FILE_INPUT_NAME);
808 
809  $res = array(true,"random_message",200);
810 
811  $this->adminLicensePlugin->shouldReceive('handleFileUpload')-> withArgs([NULL,$delimiter,$enclosure])
812  ->andReturn($res);
813  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
814  $this->auth->shouldReceive('isAdmin')->andReturn(true);
815 
816  $info = new Info(200, "random_message", InfoType::INFO);
817  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(),
818  $info->getCode());
819  $actualResponse = $this->licenseController->handleImportLicense($request,
820  new ResponseHelper(), []);
821  $this->assertEquals($expectedResponse->getStatusCode(),
822  $actualResponse->getStatusCode());
823  $this->assertEquals($this->getResponseJson($expectedResponse),
824  $this->getResponseJson($actualResponse));
825  }
826 
827 
837  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
838  $id = 1;
839  $this->auth->shouldReceive('isAdmin')->andReturn(true);
840  $this->licenseCandidatePlugin->shouldReceive('getDataRow')->withArgs([$id])->andReturn(true);
841  $res = new Response('true',Response::HTTP_OK,array('Content-type'=>'text/plain'));
842  $this->licenseCandidatePlugin->shouldReceive("doDeleteCandidate")->withArgs([$id,false])->andReturn($res);
843  $expectedResponse = new Info(202,"License candidate will be deleted.", InfoType::INFO);
844  $actualResponse = $this->licenseController->deleteAdminLicenseCandidate(null,
845  new ResponseHelper(), ["id" => $id]);
846  $this->assertEquals($expectedResponse->getCode(),
847  $actualResponse->getStatusCode());
848  $this->assertEquals($expectedResponse->getArray(),
849  $this->getResponseJson($actualResponse));
850  }
851 
861  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
862  $id = 1;
863  $this->auth->shouldReceive('isAdmin')->andReturn(false);
864  $this->expectException(HttpForbiddenException::class);
865 
866  $this->licenseController->deleteAdminLicenseCandidate(null,
867  new ResponseHelper(), ["id" => $id]);
868  }
869 
879  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
880  $id = 1;
881  $this->auth->shouldReceive('isAdmin')->andReturn(true);
882  $this->licenseCandidatePlugin->shouldReceive('getDataRow')->withArgs([$id])->andReturn(false);
883  $res = new Response('true',Response::HTTP_OK,array('Content-type'=>'text/plain'));
884  $this->licenseCandidatePlugin->shouldReceive("doDeleteCandidate")->withArgs([$id])->andReturn($res);
885  $this->expectException(HttpNotFoundException::class);
886 
887  $this->licenseController->deleteAdminLicenseCandidate(null,
888  new ResponseHelper(), ["id" => $id]);
889  }
890 
891 
898  public function testGetCandidates()
899  {
900  $request = M::mock(Request::class);
901  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
902  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
903  $this->licenseCandidatePlugin->shouldReceive('getCandidateArrayData')->andReturn([]);
904 
905  $expectedResponse = (new ResponseHelper())->withJson([], 200);
906  $actualResponse = $this->licenseController->getCandidates($request,
907  new ResponseHelper(), null);
908  $this->assertEquals($expectedResponse->getStatusCode(),
909  $actualResponse->getStatusCode());
910  $this->assertEquals($this->getResponseJson($expectedResponse),
911  $this->getResponseJson($actualResponse));
912  }
913 
919  public function testGetCandidatesNoAdmin()
920  {
921  $request = M::mock(Request::class);
922  $request->shouldReceive('getAttribute')->andReturn(ApiVersion::V1);
923  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_READ;
924 
925  $this->expectException(HttpForbiddenException::class);
926 
927  $this->licenseController->getCandidates($request,
928  new ResponseHelper(), null);
929  }
930 }
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