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