AuthorizationServiceTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. /**
  9. * Integration authorization service test.
  10. */
  11. class AuthorizationServiceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var AuthorizationService */
  14. protected $_service;
  15. /** @var \Magento\Framework\Authorization */
  16. protected $libAuthorization;
  17. /** @var \Magento\Authorization\Model\UserContextInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $userContextMock;
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  23. $loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->disableOriginalConstructor()->getMock();
  24. $loggerMock->expects($this->any())->method('critical')->will($this->returnSelf());
  25. $this->_service = $objectManager->create(
  26. \Magento\Integration\Model\AuthorizationService::class,
  27. [
  28. 'logger' => $loggerMock
  29. ]
  30. );
  31. $this->userContextMock = $this->getMockForAbstractClass(
  32. \Magento\Authorization\Model\UserContextInterface::class
  33. );
  34. $this->userContextMock
  35. ->expects($this->any())
  36. ->method('getUserType')
  37. ->will($this->returnValue(UserContextInterface::USER_TYPE_INTEGRATION));
  38. $roleLocator = $objectManager->create(
  39. \Magento\Webapi\Model\WebapiRoleLocator::class,
  40. ['userContext' => $this->userContextMock]
  41. );
  42. $this->libAuthorization = $objectManager->create(
  43. \Magento\Framework\Authorization::class,
  44. ['roleLocator' => $roleLocator]
  45. );
  46. }
  47. /**
  48. * @magentoDbIsolation enabled
  49. */
  50. public function testGrantPermissions()
  51. {
  52. $integrationId = rand(1, 1000);
  53. $resources = ['Magento_Sales::create', 'Magento_Cms::page', 'Magento_Backend::dashboard'];
  54. /** Preconditions check */
  55. $this->_ensurePermissionsAreNotGranted($integrationId, $resources);
  56. $this->_service->grantPermissions($integrationId, $resources);
  57. /** Validate that access to the specified resources is granted */
  58. $this->_ensurePermissionsAreGranted($integrationId, $resources);
  59. }
  60. /**
  61. * @param int $integrationId
  62. * @param string[] $initialResources
  63. * @param string[] $newResources
  64. * @magentoDbIsolation enabled
  65. * @dataProvider changePermissionsProvider
  66. */
  67. public function testChangePermissions($integrationId, $initialResources, $newResources)
  68. {
  69. $this->_service->grantPermissions($integrationId, $initialResources);
  70. /** Preconditions check */
  71. $this->_ensurePermissionsAreGranted($integrationId, $initialResources);
  72. $this->_ensurePermissionsAreNotGranted($integrationId, $newResources);
  73. $this->_service->grantPermissions($integrationId, $newResources);
  74. /** Check the results of permissions change */
  75. $this->_ensurePermissionsAreGranted($integrationId, $newResources);
  76. $this->_ensurePermissionsAreNotGranted($integrationId, $initialResources);
  77. }
  78. public function changePermissionsProvider()
  79. {
  80. return [
  81. 'integration' => [
  82. 'integrationId' => rand(1, 1000),
  83. 'initialResources' => ['Magento_Cms::page', 'Magento_Backend::dashboard'],
  84. 'newResources' => ['Magento_Sales::cancel', 'Magento_Cms::page_delete'],
  85. ],
  86. 'integration clear permissions' => [
  87. 'integrationId' => rand(1, 1000),
  88. 'initialResources' => ['Magento_Sales::capture', 'Magento_Cms::page_delete'],
  89. 'newResources' => [],
  90. ]
  91. ];
  92. }
  93. /**
  94. * @magentoDbIsolation enabled
  95. */
  96. public function testGrantAllPermissions()
  97. {
  98. $integrationId = rand(1, 1000);
  99. $this->_service->grantAllPermissions($integrationId);
  100. $this->_ensurePermissionsAreGranted($integrationId, ['Magento_Backend::all']);
  101. }
  102. /**
  103. * Check if user has access to the specified resources.
  104. *
  105. * @param int $integrationId
  106. * @param string[] $resources
  107. */
  108. protected function _ensurePermissionsAreGranted($integrationId, $resources)
  109. {
  110. $this->userContextMock
  111. ->expects($this->any())
  112. ->method('getUserId')
  113. ->will($this->returnValue($integrationId));
  114. foreach ($resources as $resource) {
  115. $this->assertTrue(
  116. $this->libAuthorization->isAllowed($resource),
  117. "Access to resource '{$resource}' is prohibited while it is expected to be granted."
  118. );
  119. }
  120. }
  121. /**
  122. * Check if access to the specified resources is prohibited to the user.
  123. *
  124. * @param int $integrationId
  125. * @param string[] $resources
  126. */
  127. protected function _ensurePermissionsAreNotGranted($integrationId, $resources)
  128. {
  129. $this->userContextMock
  130. ->expects($this->any())
  131. ->method('getUserId')
  132. ->will($this->returnValue($integrationId));
  133. foreach ($resources as $resource) {
  134. $this->assertFalse(
  135. $this->libAuthorization->isAllowed($resource),
  136. "Access to resource '{$resource}' is expected to be prohibited."
  137. );
  138. }
  139. }
  140. }