FOSSology  4.7.1
Open Source License Compliance by Open Source Software
LicenseCompatibilityRuleControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
13 
29 use Mockery as M;
30 use Slim\Psr7\Factory\StreamFactory;
31 use Slim\Psr7\Headers;
32 use Slim\Psr7\Request;
33 use Slim\Psr7\Uri;
34 
39 class LicenseCompatibilityRuleControllerTest extends \PHPUnit\Framework\TestCase
40 {
46 
51  private $dbHelper;
52 
57  private $dbManager;
58 
63  private $restHelper;
64 
70 
76 
81  private $ruleController;
82 
87  private $streamFactory;
88 
93  protected function setUp() : void
94  {
95  global $container, $SysConf;
96  $container = M::mock('ContainerBuilder');
97  $this->dbHelper = M::mock(DbHelper::class);
98  $this->dbManager = M::mock(DbManager::class);
99  $this->restHelper = M::mock(RestHelper::class);
100  $this->compatibilityDao = M::mock(CompatibilityDao::class);
101  $this->licenseYamlPlugin = M::mock('admin_license_from_yaml');
102 
103  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
104  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
105  $this->restHelper->shouldReceive('getPlugin')
106  ->withArgs(array('admin_license_from_yaml'))
107  ->andReturn($this->licenseYamlPlugin);
108 
109  $container->shouldReceive('get')->withArgs(array(
110  'helper.restHelper'))->andReturn($this->restHelper);
111  $container->shouldReceive('get')->withArgs(array(
112  'dao.compatibility'))->andReturn($this->compatibilityDao);
113 
114  $SysConf['SYSCONFIG']['LicenseTypes'] = "Permissive, Strong Copyleft";
115 
116  $this->ruleController = new LicenseCompatibilityRuleController($container);
117  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
118  $this->streamFactory = new StreamFactory();
119  }
120 
125  protected function tearDown() : void
126  {
127  $this->addToAssertionCount(
128  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
129  M::close();
130  }
131 
138  private function getResponseJson($response)
139  {
140  $response->getBody()->seek(0);
141  return json_decode($response->getBody()->getContents(), true);
142  }
143 
150  private function getRuleRow($ruleId)
151  {
152  return [
153  "lr_pk" => $ruleId,
154  "first_rf_fk" => 306,
155  "second_rf_fk" => null,
156  "first_rf_shortname" => "MIT",
157  "second_rf_shortname" => null,
158  "first_type" => "Permissive",
159  "second_type" => "Strong Copyleft",
160  "comment" => "Rule $ruleId",
161  "compatibility" => "t"
162  ];
163  }
164 
173  private function createJsonRequest($method, $body,
174  $version = ApiVersion::V2)
175  {
176  $requestHeaders = new Headers();
177  $requestHeaders->setHeader('Content-Type', 'application/json');
178  $request = new Request($method, new Uri("HTTP", "localhost"),
179  $requestHeaders, [], [],
180  $this->streamFactory->createStream(json_encode($body)));
181  return $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
182  }
183 
191  private function createGetRequest($query = "", $version = ApiVersion::V2)
192  {
193  $request = new Request("GET",
194  new Uri("HTTP", "localhost", null, "/license-compatibility-rules",
195  $query),
196  new Headers(), [], [], $this->streamFactory->createStream());
197  return $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
198  }
199 
205  public function testGetRulesV1()
206  {
207  $this->testGetRules(ApiVersion::V1);
208  }
209 
215  public function testGetRulesV2()
216  {
217  $this->testGetRules();
218  }
219 
224  private function testGetRules($version = ApiVersion::V2)
225  {
226  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
227  $rows = [$this->getRuleRow(1), $this->getRuleRow(2)];
228 
229  $this->compatibilityDao->shouldReceive('getTotalRulesCount')
230  ->withArgs([""])->andReturn(2);
231  $this->compatibilityDao->shouldReceive('getAllRules')
232  ->withArgs([100, 0, ""])->andReturn($rows);
233 
234  $expected = [];
235  foreach ($rows as $row) {
236  $expected[] = LicenseCompatibilityRule::fromArray($row)
237  ->getArray($version);
238  }
239 
240  $actualResponse = $this->ruleController->getRules(
241  $this->createGetRequest("", $version), new ResponseHelper(), []);
242 
243  $this->assertEquals(200, $actualResponse->getStatusCode());
244  $this->assertEquals(1,
245  intval($actualResponse->getHeaderLine('X-Total-Pages')));
246  $this->assertEquals($expected, $this->getResponseJson($actualResponse));
247  }
248 
256  {
257  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
258  $rows = [$this->getRuleRow(3)];
259 
260  $this->compatibilityDao->shouldReceive('getTotalRulesCount')
261  ->withArgs(["%copyleft%"])->andReturn(3);
262  $this->compatibilityDao->shouldReceive('getAllRules')
263  ->withArgs([2, 2, "%copyleft%"])->andReturn($rows);
264 
265  $actualResponse = $this->ruleController->getRules(
266  $this->createGetRequest("page=2&limit=2&search=copyleft"),
267  new ResponseHelper(), []);
268 
269  $this->assertEquals(200, $actualResponse->getStatusCode());
270  $this->assertEquals(2,
271  intval($actualResponse->getHeaderLine('X-Total-Pages')));
272  $this->assertCount(1, $this->getResponseJson($actualResponse));
273  }
274 
281  public function testGetRulesInvalidLimit()
282  {
283  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
284  $this->expectException(HttpBadRequestException::class);
285 
286  $this->ruleController->getRules($this->createGetRequest("limit=-1"),
287  new ResponseHelper(), []);
288  }
289 
296  public function testGetRulesZeroLimit()
297  {
298  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
299  $this->expectException(HttpBadRequestException::class);
300 
301  $this->ruleController->getRules($this->createGetRequest("limit=0"),
302  new ResponseHelper(), []);
303  }
304 
311  public function testGetRulesNonNumericLimit()
312  {
313  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
314  $this->expectException(HttpBadRequestException::class);
315 
316  $this->ruleController->getRules($this->createGetRequest("limit=abc"),
317  new ResponseHelper(), []);
318  }
319 
325  public function testGetRulesZeroPage()
326  {
327  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
328  $this->compatibilityDao->shouldReceive('getTotalRulesCount')
329  ->withArgs([""])->andReturn(2);
330  $this->expectException(HttpBadRequestException::class);
331 
332  $this->ruleController->getRules($this->createGetRequest("page=0"),
333  new ResponseHelper(), []);
334  }
335 
342  public function testGetRulesPageOutOfRange()
343  {
344  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
345  $this->compatibilityDao->shouldReceive('getTotalRulesCount')
346  ->withArgs([""])->andReturn(2);
347  $this->expectException(HttpBadRequestException::class);
348 
349  $this->ruleController->getRules($this->createGetRequest("page=5&limit=2"),
350  new ResponseHelper(), []);
351  }
352 
358  public function testGetRulesNotAdmin()
359  {
360  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
361  $this->expectException(HttpForbiddenException::class);
362 
363  $this->ruleController->getRules($this->createGetRequest(),
364  new ResponseHelper(), []);
365  }
366 
372  public function testCreateRule()
373  {
374  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
375  $this->dbHelper->shouldReceive('doesIdExist')
376  ->withArgs(["license_ref", "rf_pk", 306])->andReturn(true);
377  $this->compatibilityDao->shouldReceive('insertRule')
378  ->withArgs([306, null, null, "Strong Copyleft", "New rule", true])
379  ->andReturn(9);
380 
381  $request = $this->createJsonRequest("POST", [
382  "firstLicenseId" => 306,
383  "secondType" => "Strong Copyleft",
384  "comment" => "New rule",
385  "compatibility" => true
386  ]);
387 
388  $expectedResponse = new Info(201, "Rule 9 added successfully.",
389  InfoType::INFO);
390  $actualResponse = $this->ruleController->createRule($request,
391  new ResponseHelper(), []);
392 
393  $this->assertEquals($expectedResponse->getCode(),
394  $actualResponse->getStatusCode());
395  $this->assertEquals($expectedResponse->getArray(),
396  $this->getResponseJson($actualResponse));
397  }
398 
406  {
407  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
408  $this->compatibilityDao->shouldReceive('insertRule')
409  ->withArgs([null, null, "Permissive", null, "New rule", false])
410  ->andReturn(10);
411 
412  $request = $this->createJsonRequest("POST", [
413  "first_type" => "Permissive",
414  "comment" => "New rule",
415  "compatibility" => false
416  ], ApiVersion::V1);
417 
418  $actualResponse = $this->ruleController->createRule($request,
419  new ResponseHelper(), []);
420 
421  $this->assertEquals(201, $actualResponse->getStatusCode());
422  }
423 
431  {
432  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
433  $this->expectException(HttpBadRequestException::class);
434 
435  $this->ruleController->createRule($this->createJsonRequest("POST", [
436  "firstType" => "Permissive",
437  "compatibility" => true
438  ]), new ResponseHelper(), []);
439  }
440 
448  {
449  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
450  $this->expectException(HttpBadRequestException::class);
451 
452  $this->ruleController->createRule($this->createJsonRequest("POST", [
453  "firstType" => "Permissive",
454  "comment" => " ",
455  "compatibility" => true
456  ]), new ResponseHelper(), []);
457  }
458 
466  {
467  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
468  $this->expectException(HttpBadRequestException::class);
469 
470  $this->ruleController->createRule($this->createJsonRequest("POST", [
471  "comment" => "Default rule",
472  "compatibility" => true
473  ]), new ResponseHelper(), []);
474  }
475 
483  {
484  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
485  $this->dbHelper->shouldReceive('doesIdExist')
486  ->withArgs(["license_ref", "rf_pk", 999])->andReturn(false);
487  $this->expectException(HttpBadRequestException::class);
488 
489  $this->ruleController->createRule($this->createJsonRequest("POST", [
490  "firstLicenseId" => 999,
491  "comment" => "New rule",
492  "compatibility" => true
493  ]), new ResponseHelper(), []);
494  }
495 
503  {
504  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
505  $this->expectException(HttpBadRequestException::class);
506 
507  $this->ruleController->createRule($this->createJsonRequest("POST", [
508  "firstType" => "Unknown Type",
509  "comment" => "New rule",
510  "compatibility" => true
511  ]), new ResponseHelper(), []);
512  }
513 
520  public function testCreateRuleFailure()
521  {
522  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
523  $this->compatibilityDao->shouldReceive('insertRule')->andReturn(-2);
524  $this->expectException(HttpInternalServerErrorException::class);
525 
526  $this->ruleController->createRule($this->createJsonRequest("POST", [
527  "firstType" => "Permissive",
528  "comment" => "New rule",
529  "compatibility" => true
530  ]), new ResponseHelper(), []);
531  }
532 
539  public function testUpdateRule()
540  {
541  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
542  $ruleId = 4;
543  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([$ruleId])
544  ->andReturn($this->getRuleRow($ruleId));
545  $this->compatibilityDao->shouldReceive('updateRuleFromArray')
546  ->withArgs([[$ruleId => ["comment" => "Updated rule",
547  "result" => false]]])->andReturn(1);
548 
549  $request = $this->createJsonRequest("PUT", [
550  "comment" => "Updated rule",
551  "compatibility" => false
552  ]);
553 
554  $expectedResponse = new Info(200, "Rule $ruleId updated successfully.",
555  InfoType::INFO);
556  $actualResponse = $this->ruleController->updateRule($request,
557  new ResponseHelper(), ["id" => $ruleId]);
558 
559  $this->assertEquals($expectedResponse->getCode(),
560  $actualResponse->getStatusCode());
561  $this->assertEquals($expectedResponse->getArray(),
562  $this->getResponseJson($actualResponse));
563  }
564 
571  public function testUpdateRuleResetLicense()
572  {
573  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
574  $ruleId = 4;
575  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([$ruleId])
576  ->andReturn($this->getRuleRow($ruleId));
577  $this->compatibilityDao->shouldReceive('updateRuleFromArray')
578  ->withArgs([[$ruleId => ["firstLic" => null, "firstType" => null]]])
579  ->andReturn(1);
580 
581  $request = $this->createJsonRequest("PUT", [
582  "firstLicenseId" => null,
583  "firstType" => null
584  ]);
585 
586  $actualResponse = $this->ruleController->updateRule($request,
587  new ResponseHelper(), ["id" => $ruleId]);
588 
589  $this->assertEquals(200, $actualResponse->getStatusCode());
590  }
591 
598  public function testUpdateRuleNotFound()
599  {
600  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
601  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([8])
602  ->andReturn(null);
603  $this->expectException(HttpNotFoundException::class);
604 
605  $this->ruleController->updateRule(
606  $this->createJsonRequest("PUT", ["comment" => "Updated rule"]),
607  new ResponseHelper(), ["id" => 8]);
608  }
609 
616  public function testUpdateRuleEmptyBody()
617  {
618  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
619  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([4])
620  ->andReturn($this->getRuleRow(4));
621  $this->expectException(HttpBadRequestException::class);
622 
623  $this->ruleController->updateRule($this->createJsonRequest("PUT", []),
624  new ResponseHelper(), ["id" => 4]);
625  }
626 
634  {
635  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
636  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([4])
637  ->andReturn($this->getRuleRow(4));
638  $this->expectException(HttpBadRequestException::class);
639 
640  $this->ruleController->updateRule(
641  $this->createJsonRequest("PUT", ["unknownField" => "value"]),
642  new ResponseHelper(), ["id" => 4]);
643  }
644 
651  public function testDeleteRule()
652  {
653  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
654  $ruleId = 4;
655  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([$ruleId])
656  ->andReturn($this->getRuleRow($ruleId));
657  $this->compatibilityDao->shouldReceive('deleteRule')->withArgs([$ruleId])
658  ->andReturn(true);
659 
660  $expectedResponse = new Info(200, "Rule $ruleId deleted successfully.",
661  InfoType::INFO);
662  $actualResponse = $this->ruleController->deleteRule(null,
663  new ResponseHelper(), ["id" => $ruleId]);
664 
665  $this->assertEquals($expectedResponse->getCode(),
666  $actualResponse->getStatusCode());
667  $this->assertEquals($expectedResponse->getArray(),
668  $this->getResponseJson($actualResponse));
669  }
670 
677  public function testDeleteRuleNotFound()
678  {
679  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
680  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([8])
681  ->andReturn(null);
682  $this->expectException(HttpNotFoundException::class);
683 
684  $this->ruleController->deleteRule(null, new ResponseHelper(),
685  ["id" => 8]);
686  }
687 
694  public function testDeleteRuleFailure()
695  {
696  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
697  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([4])
698  ->andReturn($this->getRuleRow(4));
699  $this->compatibilityDao->shouldReceive('deleteRule')->withArgs([4])
700  ->andReturn(false);
701  $this->expectException(HttpInternalServerErrorException::class);
702 
703  $this->ruleController->deleteRule(null, new ResponseHelper(),
704  ["id" => 4]);
705  }
706 
715  public function testImportRules()
716  {
717  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
718  $this->licenseYamlPlugin->shouldReceive('getFileInputName')
719  ->andReturn("fileInput");
720  $this->licenseYamlPlugin->shouldReceive('handleFileUpload')
721  ->withArgs([null, true])
722  ->andReturn([true, "Read yaml: 2 license rules", 200]);
723 
724  $expectedResponse = new Info(200, "Read yaml: 2 license rules",
725  InfoType::INFO);
726  $actualResponse = $this->ruleController->importRules(
727  $this->createJsonRequest("POST", []), new ResponseHelper(), []);
728 
729  $this->assertEquals($expectedResponse->getCode(),
730  $actualResponse->getStatusCode());
731  $this->assertEquals($expectedResponse->getArray(),
732  $this->getResponseJson($actualResponse));
733  }
734 
743  public function testImportRulesInvalidFile()
744  {
745  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
746  $this->licenseYamlPlugin->shouldReceive('getFileInputName')
747  ->andReturn("fileInput");
748  $this->licenseYamlPlugin->shouldReceive('handleFileUpload')
749  ->withArgs([null, true])->andReturn([false, "No file selected", 400]);
750  $this->expectException(HttpBadRequestException::class);
751 
752  $this->ruleController->importRules($this->createJsonRequest("POST", []),
753  new ResponseHelper(), []);
754  }
755 
761  public function testExportRules()
762  {
763  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
764  $rows = [[
765  "firstname" => "MIT",
766  "secondname" => null,
767  "firsttype" => null,
768  "secondtype" => "Strong Copyleft",
769  "compatibility" => "true",
770  "comment" => "Rule 1"
771  ]];
772  $this->compatibilityDao->shouldReceive('getDefaultCompatibility')
773  ->andReturn(false);
774  $this->dbManager->shouldReceive('getRows')->andReturn($rows);
775 
776  $actualResponse = $this->ruleController->exportRules(
777  $this->createGetRequest(), new ResponseHelper(), []);
778 
779  $this->assertEquals(200, $actualResponse->getStatusCode());
780  $this->assertEquals("text/x-yaml; charset=UTF-8",
781  $actualResponse->getHeaderLine('Content-type'));
782  $actualResponse->getBody()->seek(0);
783  $this->assertEquals(yaml_emit(["default" => false, "rules" => $rows]),
784  $actualResponse->getBody()->getContents());
785  }
786 
793  public function testExportRulesNotFound()
794  {
795  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_ADMIN;
796  $this->compatibilityDao->shouldReceive('getRuleById')->withArgs([8])
797  ->andReturn(null);
798  $this->expectException(HttpNotFoundException::class);
799 
800  $this->ruleController->exportRules($this->createGetRequest("id=8"),
801  new ResponseHelper(), []);
802  }
803 
810  public function testExportRulesNotAdmin()
811  {
812  $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE;
813  $this->expectException(HttpForbiddenException::class);
814 
815  $this->ruleController->exportRules($this->createGetRequest(),
816  new ResponseHelper(), []);
817  }
818 }
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
Model to hold a single license compatibility rule.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16