AuthorizationServiceTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model;
  7. use Magento\Authorization\Model\ResourceModel\Rules;
  8. use Magento\Authorization\Model\Role;
  9. use Magento\Authorization\Model\UserContextInterface;
  10. use Magento\Framework\Acl\RootResource;
  11. use Magento\Integration\Model\AuthorizationService;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class AuthorizationServiceTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * Sample role Id
  19. */
  20. const ROLE_ID = 1;
  21. /**
  22. * Sample integration id
  23. */
  24. const INTEGRATION_ID = 22;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|Role
  27. */
  28. protected $roleMock;
  29. /**
  30. * @var AuthorizationService
  31. */
  32. protected $integrationAuthorizationService;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|Rules
  35. */
  36. protected $rulesMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject|RootResource
  39. */
  40. protected $rootAclResourceMock;
  41. /**
  42. * @var array
  43. */
  44. protected $resources;
  45. protected function setUp()
  46. {
  47. $this->roleMock = $this->createPartialMock(
  48. \Magento\Authorization\Model\Role::class,
  49. ['load', 'delete', '__wakeup', 'getId', 'save']
  50. );
  51. $this->roleMock->expects($this->any())->method('load')->will($this->returnSelf());
  52. $this->roleMock->expects($this->any())->method('delete')->will($this->returnSelf());
  53. $this->roleMock->expects($this->any())->method('save')->will($this->returnSelf());
  54. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\RoleFactory $roleFactoryMock */
  55. $roleFactoryMock = $this->createPartialMock(\Magento\Authorization\Model\RoleFactory::class, ['create']);
  56. $roleFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->roleMock));
  57. $roleCollectionFactoryMock = $this->createPartialMock(
  58. \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class,
  59. ['create']
  60. );
  61. $roleCollectionMock = $this->createPartialMock(
  62. \Magento\Authorization\Model\ResourceModel\Role\Collection::class,
  63. ['setUserFilter', 'getFirstItem']
  64. );
  65. $roleCollectionMock->expects($this->any())->method('setUserFilter')->will($this->returnSelf());
  66. $roleCollectionMock->expects($this->any())->method('getFirstItem')->will($this->returnValue($this->roleMock));
  67. $roleCollectionFactoryMock->expects($this->any())
  68. ->method('create')
  69. ->will($this->returnValue($roleCollectionMock));
  70. $rulesFactoryMock = $this->createPartialMock(\Magento\Authorization\Model\RulesFactory::class, ['create']);
  71. $this->rulesMock = $this->createPartialMock(
  72. \Magento\Authorization\Model\Rules::class,
  73. ['setRoleId', 'setResources', 'saveRel']
  74. );
  75. $rulesFactoryMock->expects($this->any())
  76. ->method('create')
  77. ->will($this->returnValue($this->rulesMock));
  78. $this->rootAclResourceMock = $this->createPartialMock(\Magento\Framework\Acl\RootResource::class, ['getId']);
  79. $this->integrationAuthorizationService = new AuthorizationService(
  80. $this->createMock(\Magento\Framework\Acl\Builder::class),
  81. $roleFactoryMock,
  82. $roleCollectionFactoryMock,
  83. $rulesFactoryMock,
  84. $this->createMock(\Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory::class),
  85. $this->createMock(\Psr\Log\LoggerInterface::class),
  86. $this->rootAclResourceMock
  87. );
  88. }
  89. public function testRemovePermissions()
  90. {
  91. $roleName = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
  92. $this->roleMock->expects($this->once())->method('load')->with($roleName)->will($this->returnSelf());
  93. $this->integrationAuthorizationService->removePermissions(self::INTEGRATION_ID);
  94. }
  95. /**
  96. * @expectedException \Magento\Framework\Exception\LocalizedException
  97. * @expectedExceptionMessage Something went wrong while deleting roles and permissions.
  98. */
  99. public function testRemovePermissionsException()
  100. {
  101. $roleName = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
  102. $this->roleMock->expects($this->once())
  103. ->method('load')
  104. ->with($roleName)
  105. ->will($this->throwException(new \Exception()));
  106. $this->integrationAuthorizationService->removePermissions(self::INTEGRATION_ID);
  107. }
  108. public function testGrantPermissions()
  109. {
  110. $this->resources = [
  111. 'Magento_Sales::sales',
  112. 'Magento_Sales::sales_operations',
  113. 'Magento_Cart::cart',
  114. 'Magento_Cart::manage'
  115. ];
  116. $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
  117. $this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
  118. $this->rulesMock->expects($this->any())
  119. ->method('setResources')
  120. ->with($this->resources)
  121. ->will($this->returnSelf());
  122. $this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
  123. $result = $this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
  124. $this->assertNull($result);
  125. }
  126. public function testGrantPermissionsNoRole()
  127. {
  128. $calculatedRoleId = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
  129. $this->resources = [
  130. 'Magento_Sales::sales',
  131. 'Magento_Sales::sales_operations',
  132. 'Magento_Cart::cart',
  133. 'Magento_Cart::manage'
  134. ];
  135. //Return invalid role
  136. $this->roleMock->expects($this->any())
  137. ->method('getId')
  138. ->will($this->onConsecutiveCalls(null, $calculatedRoleId));
  139. // Verify if the method is called with the newly created role
  140. $this->rulesMock->expects($this->any())
  141. ->method('setRoleId')
  142. ->with($calculatedRoleId)
  143. ->will($this->returnSelf());
  144. $this->rulesMock->expects($this->any())
  145. ->method('setResources')
  146. ->with($this->resources)
  147. ->will($this->returnSelf());
  148. $this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
  149. $result = $this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
  150. $this->assertNull($result);
  151. }
  152. /**
  153. * @expectedException \Magento\Framework\Exception\LocalizedException
  154. */
  155. public function testGrantPermissionsException()
  156. {
  157. $this->resources = [
  158. 'Magento_Sales::sales',
  159. 'Magento_Sales::sales_operations',
  160. 'Magento_Cart::cart',
  161. 'Magento_Cart::manage'
  162. ];
  163. $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
  164. $this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
  165. $this->rulesMock->expects($this->any())
  166. ->method('setResources')
  167. ->with($this->resources)
  168. ->will($this->returnSelf());
  169. $this->rulesMock->expects($this->any())->method('saveRel')->will($this->throwException(new \Exception()));
  170. $this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
  171. $this->expectExceptionMessage(
  172. 'An error occurred during the attempt to grant permissions. For details, see the exceptions log.'
  173. );
  174. }
  175. public function testGrantAllPermissions()
  176. {
  177. $rootResource = 'Magento_All:all';
  178. $this->rootAclResourceMock->expects($this->any())->method('getId')->will($this->returnValue($rootResource));
  179. $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
  180. $this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
  181. $this->rulesMock->expects($this->any())
  182. ->method('setResources')
  183. ->with([$rootResource])
  184. ->will($this->returnSelf());
  185. $this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
  186. $result = $this->integrationAuthorizationService->grantAllPermissions(self::INTEGRATION_ID);
  187. $this->assertNull($result);
  188. }
  189. }