35 use Slim\Psr7\Factory\StreamFactory;
36 use Slim\Psr7\Headers;
37 use Slim\Psr7\Request;
39 use Symfony\Component\HttpFoundation\Response;
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);
159 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
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);
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);
180 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
181 $this->streamFactory =
new StreamFactory();
190 $this->addToAssertionCount(
191 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
203 $response->getBody()->seek(0);
204 return json_decode($response->getBody()->getContents(),
true);
214 return new Obligation($id,
'My obligation',
'Obligation',
215 'This should represent some valid obligation text.',
'yellow');
227 'ob_topic' =>
'My obligation',
228 'ob_text' =>
'This should represent some valid obligation text.',
231 'ob_type' =>
'Obligation',
232 'ob_classification' =>
'yellow',
234 'rf_shortname' => null
246 $emptyObligation=
true)
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);
258 $license =
new License(25, $shortname,
"Exotic License",
259 "Exotic license for magical codes",
"",
null, 0,
true);
262 if ($emptyObligation) {
263 $license->setObligations([]);
265 $license->setObligations($obligationList);
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);
284 $license = new \Fossology\Lib\Data\License(25, $shortname,
285 "Exotic License", 0,
"Exotic license for magical codes",
"", 1,
299 foreach ($licenses as $license) {
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
320 $licenseShortName =
"MIT";
321 $license = $this->
getLicense($licenseShortName,
true);
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])
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);
336 $actualResponse = $this->licenseController->getLicense($request,
338 $this->assertEquals($expectedResponse->getStatusCode(),
339 $actualResponse->getStatusCode());
352 $licenseShortName =
"MIT";
353 $license = $this->
getLicense($licenseShortName,
true,
false);
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])
362 $this->licenseDao->shouldReceive(
'getLicenseObligations')
364 $this->licenseDao->shouldReceive(
'getLicenseObligations')
366 $expectedResponse = (
new ResponseHelper())->withJson($license->getArray(), 200);
368 $actualResponse = $this->licenseController->getLicense($request,
370 $this->assertEquals($expectedResponse->getStatusCode(),
371 $actualResponse->getStatusCode());
383 $licenseShortName =
"Bogus";
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])
392 $this->expectException(HttpNotFoundException::class);
394 $this->licenseController->getLicense($request,
new ResponseHelper(),
395 [
'shortname' => $licenseShortName]);
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])
423 $responseLicense = [];
424 foreach ($licenses as $license) {
425 $responseLicense[] = $license->getArray();
427 $expectedResponse = (
new ResponseHelper())->withHeader(
"X-Total-Pages", 1)
428 ->withJson($responseLicense, 200);
430 $actualResponse = $this->licenseController->getAllLicenses($request,
432 $this->assertEquals($expectedResponse->getStatusCode(),
433 $actualResponse->getStatusCode());
436 $this->assertEquals($expectedResponse->getHeaders(),
437 $actualResponse->getHeaders());
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);
459 $this->licenseController->getAllLicenses($request,
new ResponseHelper(), []);
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();
480 $this->licenseController->getAllLicenses($request,
new ResponseHelper(), []);
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();
491 $this->licenseController->getAllLicenses($request,
new ResponseHelper(), []);
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();
502 $this->licenseController->getAllLicenses($request,
new ResponseHelper(), []);
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();
513 $this->licenseController->getAllLicenses($request,
new ResponseHelper(), []);
524 $requestBody = $license->getArray();
525 $requestBody[
"isCandidate"] =
true;
526 unset($requestBody[
'id']);
528 $requestHeaders =
new Headers();
529 $requestHeaders->setHeader(
'Content-Type',
'application/json');
530 $body = $this->streamFactory->createStream();
531 $body->write(json_encode($requestBody));
533 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost", 80,
534 "/license"), $requestHeaders, [], [], $body);
536 $tableName =
"license_candidate";
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,
551 $sql =
"SELECT count(*) cnt FROM ".
552 "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
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]);
560 $info =
new Info(201,
'4', InfoType::INFO);
561 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
564 $actualResponse = $this->licenseController->createLicense($request,
566 $this->assertEquals($expectedResponse->getStatusCode(),
567 $actualResponse->getStatusCode());
581 $requestBody = $license->getArray();
582 $requestBody[
"isCandidate"] =
true;
583 unset($requestBody[
'id']);
584 unset($requestBody[
'shortName']);
586 $requestHeaders =
new Headers();
587 $requestHeaders->setHeader(
'Content-Type',
'application/json');
588 $body = $this->streamFactory->createStream();
589 $body->write(json_encode($requestBody));
591 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost", 80,
592 "/license"), $requestHeaders, [], [], $body);
593 $this->expectException(HttpBadRequestException::class);
595 $this->licenseController->createLicense($request,
new ResponseHelper(), []);
607 $requestBody = $license->getArray();
608 unset($requestBody[
'id']);
610 $requestHeaders =
new Headers();
611 $requestHeaders->setHeader(
'Content-Type',
'application/json');
612 $body = $this->streamFactory->createStream();
613 $body->write(json_encode($requestBody));
615 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost", 80,
616 "/license"), $requestHeaders, [], [], $body);
618 $this->expectException(HttpForbiddenException::class);
620 $this->licenseController->createLicense($request,
new ResponseHelper(), []);
632 $requestBody = $license->getArray();
633 $requestBody[
"isCandidate"] =
true;
634 unset($requestBody[
'id']);
636 $requestHeaders =
new Headers();
637 $requestHeaders->setHeader(
'Content-Type',
'application/json');
638 $body = $this->streamFactory->createStream();
639 $body->write(json_encode($requestBody));
641 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost", 80,
642 "/license"), $requestHeaders, [], [], $body);
644 $tableName =
"license_candidate";
646 $sql =
"SELECT count(*) cnt FROM ".
647 "$tableName WHERE rf_shortname = $1 AND group_fk = $2;";
649 $this->
dbManager->shouldReceive(
'getSingleRow')
650 ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()])
651 ->andReturn([
"cnt" => 1]);
652 $this->expectException(HttpConflictException::class);
654 $this->licenseController->createLicense($request,
new ResponseHelper(), []);
666 "fullName" =>
"Exotic License - style",
670 $requestHeaders =
new Headers();
671 $requestHeaders->setHeader(
'Content-Type',
'application/json');
672 $body = $this->streamFactory->createStream();
673 $body->write(json_encode($requestBody));
675 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost", 80,
676 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
678 $tableName =
"license_candidate";
680 "rf_fullname" =>
"Exotic License - style",
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()])
692 $this->
dbManager->shouldReceive(
'updateTableRow')
693 ->withArgs([$tableName, $assocData,
"rf_pk", $license->getId(), M::any()]);
695 $info =
new Info(200,
"License " . $license->getShortName() .
" updated.",
697 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
700 $actualResponse = $this->licenseController->updateLicense($request,
702 $this->assertEquals($expectedResponse->getStatusCode(),
703 $actualResponse->getStatusCode());
718 "fullName" =>
"Exotic License - style",
722 $requestHeaders =
new Headers();
723 $requestHeaders->setHeader(
'Content-Type',
'application/json');
724 $body = $this->streamFactory->createStream();
725 $body->write(json_encode($requestBody));
727 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost", 80,
728 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
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()])
738 $this->expectException(HttpForbiddenException::class);
740 $this->licenseController->updateLicense($request,
new ResponseHelper(),
741 [
"shortname" => $license->getShortName()]);
754 "fullName" =>
"MIT License - style",
758 $requestHeaders =
new Headers();
759 $requestHeaders->setHeader(
'Content-Type',
'application/json');
760 $body = $this->streamFactory->createStream();
761 $body->write(json_encode($requestBody));
763 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost", 80,
764 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body);
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()])
775 $this->expectException(HttpForbiddenException::class);
777 $this->licenseController->updateLicense($request,
new ResponseHelper(),
778 [
"shortname" => $license->getShortName()]);
796 $requestHeaders =
new Headers();
797 $requestHeaders->setHeader(
'Content-Type',
'application/json');
799 $body = $this->streamFactory->createStream(json_encode([]));
801 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
802 $requestHeaders, [], [], $body);
804 $FILE_INPUT_NAME =
"file_input";
806 $this->adminLicensePlugin->shouldReceive(
'getFileInputName')
807 ->andReturn($FILE_INPUT_NAME);
809 $res = array(
true,
"random_message",200);
811 $this->adminLicensePlugin->shouldReceive(
'handleFileUpload')-> withArgs([NULL,$delimiter,$enclosure])
814 $this->auth->shouldReceive(
'isAdmin')->andReturn(
true);
816 $info =
new Info(200,
"random_message", InfoType::INFO);
817 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
819 $actualResponse = $this->licenseController->handleImportLicense($request,
821 $this->assertEquals($expectedResponse->getStatusCode(),
822 $actualResponse->getStatusCode());
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,
846 $this->assertEquals($expectedResponse->getCode(),
847 $actualResponse->getStatusCode());
848 $this->assertEquals($expectedResponse->getArray(),
849 $this->getResponseJson($actualResponse));
863 $this->auth->shouldReceive(
'isAdmin')->andReturn(
false);
864 $this->expectException(HttpForbiddenException::class);
866 $this->licenseController->deleteAdminLicenseCandidate(
null,
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);
887 $this->licenseController->deleteAdminLicenseCandidate(
null,
900 $request = M::mock(Request::class);
901 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
903 $this->licenseCandidatePlugin->shouldReceive(
'getCandidateArrayData')->andReturn([]);
906 $actualResponse = $this->licenseController->getCandidates($request,
908 $this->assertEquals($expectedResponse->getStatusCode(),
909 $actualResponse->getStatusCode());
921 $request = M::mock(Request::class);
922 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
925 $this->expectException(HttpForbiddenException::class);
927 $this->licenseController->getCandidates($request,
Contains the constants and helpers for authentication of user.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Different type of infos provided by REST.
Info model to contain general error and return values.
Unit tests for LicenseController.
getResponseJson($response)
testCreateLicenseNoAdmin()
testGetAllLicenseBounds()
testCreateLicenseNoShort()
testGetAllLicenseFilters()
testUpdateLicenseNonAdmin()
testUpdateLicenseNonAdvisor()
setUp()
Setup test objects.
tearDown()
Remove test objects.
translateLicenseToDb($licenses)
testDeleteAdminLicenseCandidateIsAdmin()
getDaoLicense($shortname)
testCreateDuplicateLicense()
getLicense($shortname, $obligations=false, $emptyObligation=true)
testGetCandidatesNoAdmin()
testGetLicenseObligations()
testDeleteAdminLicenseCandidateNotAdmin()
testDeleteAdminLicenseCandidateNotFound()
fo_dbManager * dbManager
fo_dbManager object