1<?php 2/*************************************************************** 3 * Copyright (C) 2021 Siemens AG 4 * Author: Gaurav Mishra <mishra.gaurav@siemens.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * version 2 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 ***************************************************************/ 19/** 20 * @file 21 * @brief Tests for LicenseController 22 */ 23 24namespace Fossology\UI\Api\Test\Controllers; 25 26use Mockery as M; 27use Fossology\Lib\Auth\Auth; 28use Fossology\Lib\Dao\LicenseDao; 29use Fossology\Lib\Dao\UserDao; 30use Fossology\Lib\Db\DbManager; 31use Fossology\UI\Api\Controllers\LicenseController; 32use Fossology\UI\Api\Helper\DbHelper; 33use Fossology\UI\Api\Helper\RestHelper; 34use Fossology\UI\Api\Models\Info; 35use Fossology\UI\Api\Models\InfoType; 36use Fossology\UI\Api\Models\License; 37use Fossology\UI\Api\Models\Obligation; 38use Slim\Http\Headers; 39use Slim\Http\Body; 40use Slim\Http\Request; 41use Slim\Http\Uri; 42use Slim\Http\Response; 43 44/** 45 * @class LicenseControllerTest 46 * @brief Unit tests for LicenseController 47 */ 48class LicenseControllerTest extends \PHPUnit\Framework\TestCase 49{ 50 /** 51 * @var integer $assertCountBefore 52 * Assertions before running tests 53 */ 54 private $assertCountBefore; 55 56 /** 57 * @var integer $userId 58 * User ID to mock 59 */ 60 private $userId; 61 62 /** 63 * @var integer $groupId 64 * Group ID to mock 65 */ 66 private $groupId; 67 68 /** 69 * @var DbHelper $dbHelper 70 * DbHelper mock 71 */ 72 private $dbHelper; 73 74 /** 75 * @var DbManager $dbManager 76 * Dbmanager mock 77 */ 78 private $dbManager; 79 80 /** 81 * @var RestHelper $restHelper 82 * RestHelper mock 83 */ 84 private $restHelper; 85 86 /** 87 * @var LicenseController $licenseController 88 * LicenseController mock 89 */ 90 private $licenseController; 91 92 /** 93 * @var LicenseDao $licenseDao 94 * LicenseDao mock 95 */ 96 private $licenseDao; 97 98 /** 99 * @var UserDao $userDao 100 * UserDao mock 101 */ 102 private $userDao; 103 104 /** 105 * @brief Setup test objects 106 * @see PHPUnit_Framework_TestCase::setUp() 107 */ 108 protected function setUp() 109 { 110 global $container; 111 $this->userId = 2; 112 $this->groupId = 2; 113 $container = M::mock('ContainerBuilder'); 114 $this->dbHelper = M::mock(DbHelper::class); 115 $this->dbManager = M::mock(DbManager::class); 116 $this->restHelper = M::mock(RestHelper::class); 117 $this->licenseDao = M::mock(LicenseDao::class); 118 $this->userDao = M::mock(UserDao::class); 119 120 $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager); 121 122 $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper); 123 $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId); 124 $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId); 125 $this->restHelper->shouldReceive('getUserDao')->andReturn($this->userDao); 126 127 $container->shouldReceive('get')->withArgs(array( 128 'helper.restHelper'))->andReturn($this->restHelper); 129 $container->shouldReceive('get')->withArgs(array( 130 'dao.license'))->andReturn($this->licenseDao); 131 $this->licenseController = new LicenseController($container); 132 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount(); 133 } 134 135 /** 136 * @brief Remove test objects 137 * @see PHPUnit_Framework_TestCase::tearDown() 138 */ 139 protected function tearDown() 140 { 141 $this->addToAssertionCount( 142 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore); 143 M::close(); 144 } 145 146 /** 147 * Helper function to get JSON array from response 148 * 149 * @param Response $response 150 * @return array Decoded response 151 */ 152 private function getResponseJson($response) 153 { 154 $response->getBody()->seek(0); 155 return json_decode($response->getBody()->getContents(), true); 156 } 157 158 /** 159 * Helper function to generate obligations 160 * @param integer $id 161 * @return Obligation 162 */ 163 private function getObligation($id) 164 { 165 return new Obligation($id, 'My obligation', 'Obligation', 166 'This should represent some valid obligation text.', 'yellow'); 167 } 168 169 /** 170 * Helper function to generate obligations for License Dao 171 * @param integer $id 172 * @return Obligation 173 */ 174 private function getDaoObligation($id) 175 { 176 return [ 177 'ob_pk' => $id, 178 'ob_topic' => 'My obligation', 179 'ob_text' => 'This should represent some valid obligation text.', 180 'ob_active' => true, 181 'rf_fk' => 2, 182 'ob_type' => 'Obligation', 183 'ob_classification' => 'yellow', 184 'ob_comment' => "", 185 'rf_shortname' => null 186 ]; 187 } 188 189 /** 190 * Helper function to generate licenses 191 * @param string $shortname 192 * @param boolean $obligations 193 * @param boolean $emptyObligation 194 * @return License 195 */ 196 private function getLicense($shortname, $obligations=false, 197 $emptyObligation=true) 198 { 199 $obligationList = [ 200 $this->getObligation(123), 201 $this->getObligation(124) 202 ]; 203 $license = null; 204 if ($shortname == "MIT") { 205 $license = new License(22, "MIT", "MIT License", 206 "MIT License Copyright (c) <year> <copyright holders> ...", 207 "https://opensource.org/licenses/MIT", null, 2, false); 208 } else { 209 $license = new License(25, $shortname, "Exotic License", 210 "Exotic license for magical codes", "", null, 0, true); 211 } 212 if ($obligations) { 213 if ($emptyObligation) { 214 $license->setObligations([]); 215 } else { 216 $license->setObligations($obligationList); 217 } 218 } 219 return $license; 220 } 221 222 /** 223 * Helper function to generate licenses from LicenseDao 224 * @param string $shortname 225 * @return \Fossology\Lib\Data\License 226 */ 227 private function getDaoLicense($shortname) 228 { 229 $license = null; 230 if ($shortname == "MIT") { 231 $license = new \Fossology\Lib\Data\License(22, "MIT", "MIT License", 232 2, "MIT License Copyright (c) <year> <copyright holders> ...", 233 "https://opensource.org/licenses/MIT", 1, true); 234 } else { 235 $license = new \Fossology\Lib\Data\License(25, $shortname, 236 "Exotic License", 0, "Exotic license for magical codes", "", 1, 237 false); 238 } 239 return $license; 240 } 241 242 /** 243 * Helper function to translate License to DB array 244 * @param array $licenses 245 * @return array 246 */ 247 private function traslateLicenseToDb($licenses) 248 { 249 $licenseList = []; 250 foreach ($licenses as $license) { 251 $licenseList[] = [ 252 'rf_pk' => $license->getId(), 253 'rf_shortname' => $license->getShortName(), 254 'rf_fullname' => $license->getFullName(), 255 'rf_text' => $license->getText(), 256 'rf_url' => $license->getUrl(), 257 'rf_risk' => $license->getRisk(), 258 'group_fk' => $license->getIsCandidate() ? $this->groupId : 0 259 ]; 260 } 261 return $licenseList; 262 } 263 264 /** 265 * @test 266 * -# Test for LicenseController::getLicense() to fetch single license 267 * -# Check if response is 200 268 */ 269 public function testGetLicense() 270 { 271 $licenseShortName = "MIT"; 272 $license = $this->getLicense($licenseShortName, true); 273 274 $requestHeaders = new Headers(); 275 $body = new Body(fopen('php://temp', 'r+')); 276 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 277 "/license/$licenseShortName"), $requestHeaders, [], [], $body); 278 $this->licenseDao->shouldReceive('getLicenseByShortName') 279 ->withArgs([$licenseShortName, $this->groupId]) 280 ->andReturn($this->getDaoLicense($licenseShortName)); 281 $this->licenseDao->shouldReceive('getLicenseObligations') 282 ->withArgs([[22], false])->andReturn([]); 283 $this->licenseDao->shouldReceive('getLicenseObligations') 284 ->withArgs([[22], true])->andReturn([]); 285 $expectedResponse = (new Response())->withJson($license->getArray(), 200); 286 287 $actualResponse = $this->licenseController->getLicense($request, 288 new Response(), ['shortname' => $licenseShortName]); 289 $this->assertEquals($expectedResponse->getStatusCode(), 290 $actualResponse->getStatusCode()); 291 $this->assertEquals($this->getResponseJson($expectedResponse), 292 $this->getResponseJson($actualResponse)); 293 } 294 295 /** 296 * @test 297 * -# Test for LicenseController::getLicense() to fetch single license 298 * -# The license now has obligations 299 * -# Check if response is 200 300 */ 301 public function testGetLicenseObligations() 302 { 303 $licenseShortName = "MIT"; 304 $license = $this->getLicense($licenseShortName, true, false); 305 306 $requestHeaders = new Headers(); 307 $body = new Body(fopen('php://temp', 'r+')); 308 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 309 "/license/$licenseShortName"), $requestHeaders, [], [], $body); 310 $this->licenseDao->shouldReceive('getLicenseByShortName') 311 ->withArgs([$licenseShortName, $this->groupId]) 312 ->andReturn($this->getDaoLicense($licenseShortName)); 313 $this->licenseDao->shouldReceive('getLicenseObligations') 314 ->withArgs([[22], false])->andReturn([$this->getDaoObligation(123)]); 315 $this->licenseDao->shouldReceive('getLicenseObligations') 316 ->withArgs([[22], true])->andReturn([$this->getDaoObligation(124)]); 317 $expectedResponse = (new Response())->withJson($license->getArray(), 200); 318 319 $actualResponse = $this->licenseController->getLicense($request, 320 new Response(), ['shortname' => $licenseShortName]); 321 $this->assertEquals($expectedResponse->getStatusCode(), 322 $actualResponse->getStatusCode()); 323 $this->assertEquals($this->getResponseJson($expectedResponse), 324 $this->getResponseJson($actualResponse)); 325 } 326 327 /** 328 * @test 329 * -# Test for LicenseController::getLicense(), license not found 330 * -# Check if response is 404 331 */ 332 public function testGetLicenseNotFound() 333 { 334 $licenseShortName = "Bogus"; 335 336 $requestHeaders = new Headers(); 337 $body = new Body(fopen('php://temp', 'r+')); 338 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 339 "/license/$licenseShortName"), $requestHeaders, [], [], $body); 340 $this->licenseDao->shouldReceive('getLicenseByShortName') 341 ->withArgs([$licenseShortName, $this->groupId]) 342 ->andReturn(null); 343 $info = new Info(404, 344 "No license found with short name '{$licenseShortName}'.", 345 InfoType::ERROR); 346 $expectedResponse = (new Response())->withJson($info->getArray(), 347 $info->getCode()); 348 349 $actualResponse = $this->licenseController->getLicense($request, 350 new Response(), ['shortname' => $licenseShortName]); 351 $this->assertEquals($expectedResponse->getStatusCode(), 352 $actualResponse->getStatusCode()); 353 $this->assertEquals($this->getResponseJson($expectedResponse), 354 $this->getResponseJson($actualResponse)); 355 } 356 357 /** 358 * @test 359 * -# Test for LicenseController::getAllLicenses() to fetch all licenses 360 * -# Check if response is 200 361 * -# Check if pagination headers are set 362 */ 363 public function testGetAllLicense() 364 { 365 $licenses = [ 366 $this->getLicense("MIT"), 367 $this->getLicense("Exotic"), 368 $this->getLicense("Exotic2"), 369 $this->getLicense("Exotic3") 370 ]; 371 372 $requestHeaders = new Headers(); 373 $body = new Body(fopen('php://temp', 'r+')); 374 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 375 "/license"), $requestHeaders, [], [], $body); 376 $this->dbHelper->shouldReceive('getLicenseCount') 377 ->withArgs(["all", $this->groupId])->andReturn(4); 378 $this->dbHelper->shouldReceive('getLicensesPaginated') 379 ->withArgs([1, 100, "all", $this->groupId, false]) 380 ->andReturn($this->traslateLicenseToDb($licenses)); 381 382 $responseLicense = []; 383 foreach ($licenses as $license) { 384 $responseLicense[] = $license->getArray(); 385 } 386 $expectedResponse = (new Response())->withHeader("X-Total-Pages", 1) 387 ->withJson($responseLicense, 200); 388 389 $actualResponse = $this->licenseController->getAllLicenses($request, 390 new Response(), []); 391 $this->assertEquals($expectedResponse->getStatusCode(), 392 $actualResponse->getStatusCode()); 393 $this->assertEquals($this->getResponseJson($expectedResponse), 394 $this->getResponseJson($actualResponse)); 395 $this->assertEquals($expectedResponse->getHeaders(), 396 $actualResponse->getHeaders()); 397 } 398 399 /** 400 * @test 401 * -# Test for LicenseController::getAllLicenses() to fetch all licenses 402 * -# The page requested is out of bounds 403 * -# Check if response is 400 404 * -# Check if pagination headers are set 405 */ 406 public function testGetAllLicenseBounds() 407 { 408 $requestHeaders = new Headers(); 409 $requestHeaders->set('limit', 5); 410 $requestHeaders->set('page', 2); 411 $body = new Body(fopen('php://temp', 'r+')); 412 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 413 "/license"), $requestHeaders, [], [], $body); 414 $this->dbHelper->shouldReceive('getLicenseCount') 415 ->withArgs(["all", $this->groupId])->andReturn(4); 416 417 $info = new Info(400, "Can not exceed total pages: 1", InfoType::ERROR); 418 $expectedResponse = (new Response())->withHeader("X-Total-Pages", 1) 419 ->withJson($info->getArray(), $info->getCode()); 420 421 $actualResponse = $this->licenseController->getAllLicenses($request, 422 new Response(), []); 423 $this->assertEquals($expectedResponse->getStatusCode(), 424 $actualResponse->getStatusCode()); 425 $this->assertEquals($this->getResponseJson($expectedResponse), 426 $this->getResponseJson($actualResponse)); 427 $this->assertEquals($expectedResponse->getHeaders(), 428 $actualResponse->getHeaders()); 429 } 430 431 /** 432 * @test 433 * -# Test for LicenseController::getAllLicenses() with kind filter 434 * -# Check if proper parameters are passed to DbHelper 435 */ 436 public function testGetAllLicenseFilters() 437 { 438 // All licenses 439 $requestHeaders = new Headers(); 440 $body = new Body(fopen('php://temp', 'r+')); 441 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 442 "/license", "kind=all"), $requestHeaders, [], [], $body); 443 $this->dbHelper->shouldReceive('getLicenseCount') 444 ->withArgs(["all", $this->groupId])->andReturn(4)->once(); 445 $this->dbHelper->shouldReceive('getLicensesPaginated') 446 ->withArgs([1, 100, "all", $this->groupId, false]) 447 ->andReturn([])->once(); 448 449 $this->licenseController->getAllLicenses($request, new Response(), []); 450 451 // Main licenses 452 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 453 "/license", "kind=main"), $requestHeaders, [], [], $body); 454 $this->dbHelper->shouldReceive('getLicenseCount') 455 ->withArgs(["main", $this->groupId])->andReturn(4)->once(); 456 $this->dbHelper->shouldReceive('getLicensesPaginated') 457 ->withArgs([1, 100, "main", $this->groupId, false]) 458 ->andReturn([])->once(); 459 460 $this->licenseController->getAllLicenses($request, new Response(), []); 461 462 // Candidate licenses 463 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 464 "/license", "kind=candidate"), $requestHeaders, [], [], $body); 465 $this->dbHelper->shouldReceive('getLicenseCount') 466 ->withArgs(["candidate", $this->groupId])->andReturn(4)->once(); 467 $this->dbHelper->shouldReceive('getLicensesPaginated') 468 ->withArgs([1, 100, "candidate", $this->groupId, false]) 469 ->andReturn([])->once(); 470 471 $this->licenseController->getAllLicenses($request, new Response(), []); 472 473 // wrong filter 474 $request = new Request("GET", new Uri("HTTP", "localhost", 80, 475 "/license", "kind=bogus"), $requestHeaders, [], [], $body); 476 $this->dbHelper->shouldReceive('getLicenseCount') 477 ->withArgs(["all", $this->groupId])->andReturn(4)->once(); 478 $this->dbHelper->shouldReceive('getLicensesPaginated') 479 ->withArgs([1, 100, "all", $this->groupId, false]) 480 ->andReturn([])->once(); 481 482 $this->licenseController->getAllLicenses($request, new Response(), []); 483 } 484 485 /** 486 * @test 487 * -# Test for LicenseController::createLicense() to create new license 488 * -# Check if response is 201 489 */ 490 public function testCreateLicense() 491 { 492 $license = $this->getLicense("MIT"); 493 $requestBody = $license->getArray(); 494 $requestBody["isCandidate"] = true; 495 unset($requestBody['id']); 496 497 $requestHeaders = new Headers(); 498 $requestHeaders->set('Content-Type', 'application/json'); 499 $body = new Body(fopen('php://temp', 'wr+')); 500 $body->write(json_encode($requestBody)); 501 $body->seek(0); 502 $request = new Request("POST", new Uri("HTTP", "localhost", 80, 503 "/license"), $requestHeaders, [], [], $body); 504 505 $tableName = "license_candidate"; 506 $assocData = [ 507 "rf_shortname" => $license->getShortName(), 508 "rf_fullname" => $license->getFullName(), 509 "rf_text" => $license->getText(), 510 "rf_md5" => md5($license->getText()), 511 "rf_risk" => $license->getRisk(), 512 "rf_url" => $license->getUrl(), 513 "rf_detector_type" => 1, 514 "group_fk" => $this->groupId, 515 "rf_user_fk_created" => $this->userId, 516 "rf_user_fk_modified" => $this->userId, 517 "marydone" => false 518 ]; 519 520 $sql = "SELECT count(*) cnt FROM ". 521 "$tableName WHERE rf_shortname = $1 AND group_fk = $2;"; 522 523 $this->dbManager->shouldReceive('insertTableRow') 524 ->withArgs([$tableName, $assocData, M::any(), "rf_pk"])->andReturn(4); 525 $this->dbManager->shouldReceive('getSingleRow') 526 ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()]) 527 ->andReturn(["cnt" => 0]); 528 529 $info = new Info(201, '4', InfoType::INFO); 530 $expectedResponse = (new Response())->withJson($info->getArray(), 531 $info->getCode()); 532 533 $actualResponse = $this->licenseController->createLicense($request, 534 new Response(), []); 535 $this->assertEquals($expectedResponse->getStatusCode(), 536 $actualResponse->getStatusCode()); 537 $this->assertEquals($this->getResponseJson($expectedResponse), 538 $this->getResponseJson($actualResponse)); 539 } 540 541 /** 542 * @test 543 * -# Test for LicenseController::createLicense() to create new license 544 * -# The request body is malformed 545 * -# Check if response is 400 546 */ 547 public function testCreateLicenseNoShort() 548 { 549 $license = $this->getLicense("MIT"); 550 $requestBody = $license->getArray(); 551 $requestBody["isCandidate"] = true; 552 unset($requestBody['id']); 553 unset($requestBody['shortName']); 554 555 $requestHeaders = new Headers(); 556 $requestHeaders->set('Content-Type', 'application/json'); 557 $body = new Body(fopen('php://temp', 'wr+')); 558 $body->write(json_encode($requestBody)); 559 $body->seek(0); 560 $request = new Request("POST", new Uri("HTTP", "localhost", 80, 561 "/license"), $requestHeaders, [], [], $body); 562 563 $info = new Info(400, "Property 'shortName' is required.", 564 InfoType::ERROR); 565 $expectedResponse = (new Response())->withJson($info->getArray(), 566 $info->getCode()); 567 568 $actualResponse = $this->licenseController->createLicense($request, 569 new Response(), []); 570 $this->assertEquals($expectedResponse->getStatusCode(), 571 $actualResponse->getStatusCode()); 572 $this->assertEquals($this->getResponseJson($expectedResponse), 573 $this->getResponseJson($actualResponse)); 574 } 575 576 /** 577 * @test 578 * -# Test for LicenseController::createLicense() to create new license 579 * -# Non admin user can't create main license 580 * -# Check if response is 403 581 */ 582 public function testCreateLicenseNoAdmin() 583 { 584 $license = $this->getLicense("MIT"); 585 $requestBody = $license->getArray(); 586 unset($requestBody['id']); 587 588 $requestHeaders = new Headers(); 589 $requestHeaders->set('Content-Type', 'application/json'); 590 $body = new Body(fopen('php://temp', 'wr+')); 591 $body->write(json_encode($requestBody)); 592 $body->seek(0); 593 $request = new Request("POST", new Uri("HTTP", "localhost", 80, 594 "/license"), $requestHeaders, [], [], $body); 595 $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE; 596 597 $info = new Info(403, "Need to be admin to create non-candidate license.", 598 InfoType::ERROR); 599 $expectedResponse = (new Response())->withJson($info->getArray(), 600 $info->getCode()); 601 602 $actualResponse = $this->licenseController->createLicense($request, 603 new Response(), []); 604 $this->assertEquals($expectedResponse->getStatusCode(), 605 $actualResponse->getStatusCode()); 606 $this->assertEquals($this->getResponseJson($expectedResponse), 607 $this->getResponseJson($actualResponse)); 608 } 609 610 /** 611 * @test 612 * -# Test for LicenseController::createLicense() to create new license 613 * -# Simulate duplicate license name 614 * -# Check if response is 409 615 */ 616 public function testCreateDuplicateLicense() 617 { 618 $license = $this->getLicense("MIT"); 619 $requestBody = $license->getArray(); 620 $requestBody["isCandidate"] = true; 621 unset($requestBody['id']); 622 623 $requestHeaders = new Headers(); 624 $requestHeaders->set('Content-Type', 'application/json'); 625 $body = new Body(fopen('php://temp', 'wr+')); 626 $body->write(json_encode($requestBody)); 627 $body->seek(0); 628 $request = new Request("POST", new Uri("HTTP", "localhost", 80, 629 "/license"), $requestHeaders, [], [], $body); 630 631 $tableName = "license_candidate"; 632 633 $sql = "SELECT count(*) cnt FROM ". 634 "$tableName WHERE rf_shortname = $1 AND group_fk = $2;"; 635 636 $this->dbManager->shouldReceive('getSingleRow') 637 ->withArgs([$sql, [$license->getShortName(), $this->groupId], M::any()]) 638 ->andReturn(["cnt" => 1]); 639 640 $info = new Info(409, "License with shortname '" . 641 $license->getShortName() . "' already exists!", InfoType::ERROR); 642 $expectedResponse = (new Response())->withJson($info->getArray(), 643 $info->getCode()); 644 645 $actualResponse = $this->licenseController->createLicense($request, 646 new Response(), []); 647 $this->assertEquals($expectedResponse->getStatusCode(), 648 $actualResponse->getStatusCode()); 649 $this->assertEquals($this->getResponseJson($expectedResponse), 650 $this->getResponseJson($actualResponse)); 651 } 652 653 /** 654 * @test 655 * -# Test for LicenseController::updateLicense() to edit a license 656 * -# Check if response is 200 657 */ 658 public function testUpdateLicense() 659 { 660 $license = $this->getDaoLicense("Exotic"); 661 $requestBody = [ 662 "fullName" => "Exotic License - style", 663 "risk" => 0 664 ]; 665 666 $requestHeaders = new Headers(); 667 $requestHeaders->set('Content-Type', 'application/json'); 668 $body = new Body(fopen('php://temp', 'wr+')); 669 $body->write(json_encode($requestBody)); 670 $body->seek(0); 671 $request = new Request("PATCH", new Uri("HTTP", "localhost", 80, 672 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body); 673 674 $tableName = "license_candidate"; 675 $assocData = [ 676 "rf_fullname" => "Exotic License - style", 677 "rf_risk" => 0 678 ]; 679 680 $this->userDao->shouldReceive('isAdvisorOrAdmin') 681 ->withArgs([$this->userId, $this->groupId])->andReturn(true); 682 $this->licenseDao->shouldReceive('getLicenseByShortName') 683 ->withArgs([$license->getShortName(), $this->groupId]) 684 ->andReturn($license); 685 $this->dbHelper->shouldReceive('doesIdExist') 686 ->withArgs(["license_candidate", "rf_pk", $license->getId()]) 687 ->andReturn(true); 688 $this->dbManager->shouldReceive('updateTableRow') 689 ->withArgs([$tableName, $assocData, "rf_pk", $license->getId(), M::any()]); 690 691 $info = new Info(200, "License " . $license->getShortName() . " updated.", 692 InfoType::INFO); 693 $expectedResponse = (new Response())->withJson($info->getArray(), 694 $info->getCode()); 695 696 $actualResponse = $this->licenseController->updateLicense($request, 697 new Response(), ["shortname" => $license->getShortName()]); 698 $this->assertEquals($expectedResponse->getStatusCode(), 699 $actualResponse->getStatusCode()); 700 $this->assertEquals($this->getResponseJson($expectedResponse), 701 $this->getResponseJson($actualResponse)); 702 } 703 704 /** 705 * @test 706 * -# Test for LicenseController::updateLicense() to edit a license 707 * -# User is not admin/advisor of the group 708 * -# Check if response is 403 709 */ 710 public function testUpdateLicenseNonAdvisor() 711 { 712 $license = $this->getDaoLicense("Exotic"); 713 $requestBody = [ 714 "fullName" => "Exotic License - style", 715 "risk" => 0 716 ]; 717 718 $requestHeaders = new Headers(); 719 $requestHeaders->set('Content-Type', 'application/json'); 720 $body = new Body(fopen('php://temp', 'wr+')); 721 $body->write(json_encode($requestBody)); 722 $body->seek(0); 723 $request = new Request("PATCH", new Uri("HTTP", "localhost", 80, 724 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body); 725 726 $this->userDao->shouldReceive('isAdvisorOrAdmin') 727 ->withArgs([$this->userId, $this->groupId])->andReturn(false); 728 $this->licenseDao->shouldReceive('getLicenseByShortName') 729 ->withArgs([$license->getShortName(), $this->groupId]) 730 ->andReturn($license); 731 $this->dbHelper->shouldReceive('doesIdExist') 732 ->withArgs(["license_candidate", "rf_pk", $license->getId()]) 733 ->andReturn(true); 734 735 $info = new Info(403, "Operation not permitted for this group.", 736 InfoType::ERROR); 737 $expectedResponse = (new Response())->withJson($info->getArray(), 738 $info->getCode()); 739 740 $actualResponse = $this->licenseController->updateLicense($request, 741 new Response(), ["shortname" => $license->getShortName()]); 742 $this->assertEquals($expectedResponse->getStatusCode(), 743 $actualResponse->getStatusCode()); 744 $this->assertEquals($this->getResponseJson($expectedResponse), 745 $this->getResponseJson($actualResponse)); 746 } 747 748 /** 749 * @test 750 * -# Test for LicenseController::updateLicense() to edit a license 751 * -# User is not admin 752 * -# Check if response is 403 753 */ 754 public function testUpdateLicenseNonAdmin() 755 { 756 $license = $this->getDaoLicense("MIT"); 757 $requestBody = [ 758 "fullName" => "MIT License - style", 759 "risk" => 0 760 ]; 761 762 $requestHeaders = new Headers(); 763 $requestHeaders->set('Content-Type', 'application/json'); 764 $body = new Body(fopen('php://temp', 'wr+')); 765 $body->write(json_encode($requestBody)); 766 $body->seek(0); 767 $request = new Request("PATCH", new Uri("HTTP", "localhost", 80, 768 "/license/" . $license->getShortName()), $requestHeaders, [], [], $body); 769 $_SESSION[Auth::USER_LEVEL] = Auth::PERM_WRITE; 770 771 $this->userDao->shouldReceive('isAdvisorOrAdmin') 772 ->withArgs([$this->userId, $this->groupId])->andReturn(true); 773 $this->licenseDao->shouldReceive('getLicenseByShortName') 774 ->withArgs([$license->getShortName(), $this->groupId]) 775 ->andReturn($license); 776 $this->dbHelper->shouldReceive('doesIdExist') 777 ->withArgs(["license_candidate", "rf_pk", $license->getId()]) 778 ->andReturn(false); 779 780 $info = new Info(403, "Only admin can edit main licenses.", 781 InfoType::ERROR); 782 $expectedResponse = (new Response())->withJson($info->getArray(), 783 $info->getCode()); 784 785 $actualResponse = $this->licenseController->updateLicense($request, 786 new Response(), ["shortname" => $license->getShortName()]); 787 $this->assertEquals($expectedResponse->getStatusCode(), 788 $actualResponse->getStatusCode()); 789 $this->assertEquals($this->getResponseJson($expectedResponse), 790 $this->getResponseJson($actualResponse)); 791 } 792} 793