Skip to content

Commit 9c22673

Browse files
author
Cari Spruiell
committed
Merge remote-tracking branch 'origin/MAGETWO-36005-TestCoverage-API-S49' into MAGETWO-36005-TestCoverage-API-S49
2 parents 412affa + 89c021b commit 9c22673

File tree

3 files changed

+622
-14
lines changed

3 files changed

+622
-14
lines changed

app/code/Magento/Integration/Test/Unit/Model/AuthorizationServiceTest.php

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,29 @@ class AuthorizationServiceTest extends \PHPUnit_Framework_TestCase
1818
*/
1919
const ROLE_ID = 1;
2020

21-
/** @var \PHPUnit_Framework_MockObject_MockObject|Role */
21+
/**
22+
* Sample integration id
23+
*/
24+
const INTEGRATION_ID = 22;
25+
26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject|Role
28+
*/
2229
protected $roleMock;
2330

24-
/** @var AuthorizationService */
31+
/**
32+
* @var AuthorizationService
33+
*/
2534
protected $integrationAuthorizationService;
2635

27-
/** @var \PHPUnit_Framework_MockObject_MockObject|Rules */
36+
/**
37+
* @var \PHPUnit_Framework_MockObject_MockObject|Rules
38+
*/
2839
protected $rulesMock;
2940

30-
/** @var \PHPUnit_Framework_MockObject_MockObject|RootResource */
41+
/**
42+
* @var \PHPUnit_Framework_MockObject_MockObject|RootResource
43+
*/
3144
protected $rootAclResourceMock;
3245

3346
/**
@@ -39,14 +52,14 @@ protected function setUp()
3952
{
4053
$this->roleMock = $this->getMock(
4154
'Magento\Authorization\Model\Role',
42-
['load', 'delete', '__wakeup', 'getId'],
55+
['load', 'delete', '__wakeup', 'getId', 'save'],
4356
[],
4457
'',
4558
false
4659
);
4760
$this->roleMock->expects($this->any())->method('load')->will($this->returnSelf());
4861
$this->roleMock->expects($this->any())->method('delete')->will($this->returnSelf());
49-
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
62+
$this->roleMock->expects($this->any())->method('save')->will($this->returnSelf());
5063

5164
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\RoleFactory $roleFactoryMock */
5265
$roleFactoryMock = $this->getMock(
@@ -106,45 +119,112 @@ protected function setUp()
106119

107120
public function testRemovePermissions()
108121
{
109-
$integrationId = 22;
110-
$roleName = UserContextInterface::USER_TYPE_INTEGRATION . $integrationId;
122+
$roleName = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
111123
$this->roleMock->expects($this->once())->method('load')->with($roleName)->will($this->returnSelf());
112-
$this->integrationAuthorizationService->removePermissions($integrationId);
124+
$this->integrationAuthorizationService->removePermissions(self::INTEGRATION_ID);
125+
}
126+
127+
/**
128+
* @expectedException \Magento\Framework\Exception\LocalizedException
129+
* @expectedExceptionMessage Error happened while deleting role and permissions. Check exception log for details.
130+
*/
131+
public function testRemovePermissionsException()
132+
{
133+
$roleName = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
134+
$this->roleMock->expects($this->once())
135+
->method('load')
136+
->with($roleName)
137+
->will($this->throwException(new \Exception));
138+
$this->integrationAuthorizationService->removePermissions(self::INTEGRATION_ID);
113139
}
114140

115141
public function testGrantPermissions()
116142
{
117-
$integrationId = 22;
118143
$this->resources = [
119144
'Magento_Sales::sales',
120145
'Magento_Sales::sales_operations',
121146
'Magento_Cart::cart',
122147
'Magento_Cart::manage'
123148
];
124149

150+
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
125151
$this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
126152
$this->rulesMock->expects($this->any())
127153
->method('setResources')
128154
->with($this->resources)
129155
->will($this->returnSelf());
130156
$this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
131157

132-
$this->integrationAuthorizationService->grantPermissions($integrationId, $this->resources);
158+
$this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
159+
}
160+
161+
public function testGrantPermissionsNoRole()
162+
{
163+
$calculatedRoleId = UserContextInterface::USER_TYPE_INTEGRATION . self::INTEGRATION_ID;
164+
165+
$this->resources = [
166+
'Magento_Sales::sales',
167+
'Magento_Sales::sales_operations',
168+
'Magento_Cart::cart',
169+
'Magento_Cart::manage'
170+
];
171+
172+
//Return invalid role
173+
$this->roleMock->expects($this->any())
174+
->method('getId')
175+
->will($this->onConsecutiveCalls(null, $calculatedRoleId));
176+
// Verify if the method is called with the newly created role
177+
$this->rulesMock->expects($this->any())
178+
->method('setRoleId')
179+
->with($calculatedRoleId)
180+
->will($this->returnSelf());
181+
182+
$this->rulesMock->expects($this->any())
183+
->method('setResources')
184+
->with($this->resources)
185+
->will($this->returnSelf());
186+
$this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
187+
188+
$this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
189+
}
190+
191+
/**
192+
* @expectedException \Magento\Framework\Exception\LocalizedException
193+
* @expectedExceptionMessage Error happened while granting permissions. Check exception log for details.
194+
*/
195+
public function testGrantPermissionsException()
196+
{
197+
$this->resources = [
198+
'Magento_Sales::sales',
199+
'Magento_Sales::sales_operations',
200+
'Magento_Cart::cart',
201+
'Magento_Cart::manage'
202+
];
203+
204+
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
205+
$this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
206+
$this->rulesMock->expects($this->any())
207+
->method('setResources')
208+
->with($this->resources)
209+
->will($this->returnSelf());
210+
$this->rulesMock->expects($this->any())->method('saveRel')->will($this->throwException(new \Exception));
211+
212+
$this->integrationAuthorizationService->grantPermissions(self::INTEGRATION_ID, $this->resources);
133213
}
134214

135215
public function testGrantAllPermissions()
136216
{
137-
$integrationId = 22;
138217
$rootResource = 'Magento_All:all';
139218

140219
$this->rootAclResourceMock->expects($this->any())->method('getId')->will($this->returnValue($rootResource));
220+
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(self::ROLE_ID));
141221
$this->rulesMock->expects($this->any())->method('setRoleId')->with(self::ROLE_ID)->will($this->returnSelf());
142222
$this->rulesMock->expects($this->any())
143223
->method('setResources')
144224
->with([$rootResource])
145225
->will($this->returnSelf());
146226
$this->rulesMock->expects($this->any())->method('saveRel')->will($this->returnSelf());
147227

148-
$this->integrationAuthorizationService->grantAllPermissions($integrationId);
228+
$this->integrationAuthorizationService->grantAllPermissions(self::INTEGRATION_ID);
149229
}
150230
}

app/code/Magento/Integration/Test/Unit/Model/Oauth/Token/ProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\Integration\Test\Unit\Model\Oauth;
7+
namespace Magento\Integration\Test\Unit\Model\Oauth\Token;
88

99
use Magento\Authorization\Model\UserContextInterface;
1010
use Magento\Integration\Model\Oauth\Token;

0 commit comments

Comments
 (0)