12 use Magento\Integration\Model\Oauth\TokenFactory;
54 private $oauthHelperMock;
64 private $dateTimeMock;
70 $this->request = $this->getMockBuilder(Request::class)
71 ->disableOriginalConstructor()
72 ->setMethods([
'getHeader'])
75 $this->tokenFactory = $this->getMockBuilder(TokenFactory::class)
76 ->disableOriginalConstructor()
77 ->setMethods([
'create'])
80 $this->integrationService = $this->getMockBuilder(IntegrationServiceInterface::class)
81 ->disableOriginalConstructor()
89 'findActiveIntegrationByConsumerId',
91 'getSelectedResources',
96 $this->oauthHelperMock = $this->getMockBuilder(OauthHelper::class)
97 ->disableOriginalConstructor()
98 ->setMethods([
'getAdminTokenLifetime',
'getCustomerTokenLifetime'])
101 $this->dateMock = $this->getMockBuilder(Date::class)
102 ->disableOriginalConstructor()
103 ->setMethods([
'gmtTimestamp'])
106 $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
107 ->disableOriginalConstructor()
108 ->setMethods([
'strToTime'])
111 $this->dateTimeMock->expects($this->any())
112 ->method(
'strToTime')
114 $this->returnCallback(
116 return strtotime($str);
121 $this->tokenUserContext = $this->objectManager->getObject(
122 TokenUserContext::class,
124 'request' => $this->request,
125 'tokenFactory' => $this->tokenFactory,
126 'integrationService' => $this->integrationService,
127 'oauthHelper' => $this->oauthHelperMock,
128 'date' => $this->dateMock,
129 'dateTime' => $this->dateTimeMock,
136 $this->request->expects($this->once())
137 ->method(
'getHeader')
138 ->with(
'Authorization')
139 ->will($this->returnValue(
null));
140 $this->assertNull($this->tokenUserContext->getUserType());
141 $this->assertNull($this->tokenUserContext->getUserId());
146 $this->request->expects($this->once())
147 ->method(
'getHeader')
148 ->with(
'Authorization')
149 ->will($this->returnValue(
'Bearer'));
150 $this->assertNull($this->tokenUserContext->getUserType());
151 $this->assertNull($this->tokenUserContext->getUserId());
156 $this->request->expects($this->once())
157 ->method(
'getHeader')
158 ->with(
'Authorization')
159 ->will($this->returnValue(
'Access'));
160 $this->assertNull($this->tokenUserContext->getUserType());
161 $this->assertNull($this->tokenUserContext->getUserId());
166 $bearerToken =
'bearer1234';
168 $this->request->expects($this->once())
169 ->method(
'getHeader')
170 ->with(
'Authorization')
171 ->will($this->returnValue(
"Bearer {$bearerToken}"));
173 $token = $this->getMockBuilder(Token::class)
174 ->disableOriginalConstructor()
175 ->setMethods([
'loadByToken',
'getId',
'__wakeup'])
177 $this->tokenFactory->expects($this->once())
179 ->will($this->returnValue(
$token));
180 $token->expects($this->once())
181 ->method(
'loadByToken')
183 ->will($this->returnSelf());
184 $token->expects($this->once())
186 ->will($this->returnValue(
null));
188 $this->assertNull($this->tokenUserContext->getUserType());
189 $this->assertNull($this->tokenUserContext->getUserId());
194 $bearerToken =
'bearer1234';
196 $this->request->expects($this->once())
197 ->method(
'getHeader')
198 ->with(
'Authorization')
199 ->will($this->returnValue(
"Bearer {$bearerToken}"));
201 $token = $this->getMockBuilder(Token::class)
202 ->disableOriginalConstructor()
203 ->setMethods([
'loadByToken',
'getId',
'getRevoked',
'__wakeup'])
205 $this->tokenFactory->expects($this->once())
207 ->will($this->returnValue(
$token));
208 $token->expects($this->once())
209 ->method(
'loadByToken')
211 ->will($this->returnSelf());
212 $token->expects($this->once())
214 ->will($this->returnValue(1));
215 $token->expects($this->once())
216 ->method(
'getRevoked')
217 ->will($this->returnValue(1));
219 $this->assertNull($this->tokenUserContext->getUserType());
220 $this->assertNull($this->tokenUserContext->getUserId());
226 public function testValidToken($userType, $userId, $expectedUserType, $expectedUserId)
228 $bearerToken =
'bearer1234';
230 $this->request->expects($this->once())
231 ->method(
'getHeader')
232 ->with(
'Authorization')
233 ->will($this->returnValue(
"Bearer {$bearerToken}"));
235 $token = $this->getMockBuilder(Token::class)
236 ->disableOriginalConstructor()
248 $this->tokenFactory->expects($this->once())
250 ->will($this->returnValue(
$token));
251 $token->expects($this->once())
252 ->method(
'loadByToken')
254 ->will($this->returnSelf());
255 $token->expects($this->once())
257 ->will($this->returnValue(1));
258 $token->expects($this->any())
259 ->method(
'getUserType')
260 ->will($this->returnValue($userType));
262 $token->expects($this->any())
263 ->method(
'getCreatedAt')
264 ->willReturn(date(
'Y-m-d H:i:s',
time()));
268 $integration = $this->getMockBuilder(Integration::class)
269 ->disableOriginalConstructor()
270 ->setMethods([
'getId',
'__wakeup'])
275 ->will($this->returnValue($userId));
276 $this->integrationService->expects($this->once())
277 ->method(
'findByConsumerId')
281 $token->expects($this->once())
282 ->method(
'getAdminId')
283 ->will($this->returnValue($userId));
286 $token->expects($this->once())
287 ->method(
'getCustomerId')
288 ->will($this->returnValue($userId));
292 $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
293 $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
296 $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
297 $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
312 'customer token' => [
318 'integration token' => [
324 'guest user type' => [
349 $bearerToken =
'bearer1234';
351 $this->dateMock->expects($this->any())
352 ->method(
'gmtTimestamp')
353 ->willReturn($currentTime);
355 $this->request->expects($this->once())
356 ->method(
'getHeader')
357 ->with(
'Authorization')
358 ->will($this->returnValue(
"Bearer {$bearerToken}"));
360 $token = $this->getMockBuilder(Token::class)
361 ->disableOriginalConstructor()
374 $token->expects($this->once())
375 ->method(
'loadByToken')
377 ->will($this->returnSelf());
379 $token->expects($this->any())
381 ->will($this->returnValue(1));
383 $token->expects($this->any())
384 ->method(
'getUserType')
385 ->will($this->returnValue($tokenData[
'user_type']));
387 $token->expects($this->any())
388 ->method(
'getCreatedAt')
389 ->willReturn($tokenData[
'created_at']);
391 $this->tokenFactory->expects($this->once())
393 ->will($this->returnValue(
$token));
395 $this->oauthHelperMock->expects($this->any())
396 ->method(
'getAdminTokenLifetime')
397 ->willReturn($tokenTtl);
399 $this->oauthHelperMock->expects($this->any())
400 ->method(
'getCustomerTokenLifetime')
401 ->willReturn($tokenTtl);
403 switch ($tokenData[
'user_type']) {
405 $integration = $this->getMockBuilder(Integration::class)
406 ->disableOriginalConstructor()
407 ->setMethods([
'getId',
'__wakeup'])
411 ->will($this->returnValue($tokenData[
'user_id']));
413 $this->integrationService->expects($this->any())
414 ->method(
'findByConsumerId')
418 $token->expects($this->any())
419 ->method(
'getAdminId')
420 ->will($this->returnValue($tokenData[
'user_id']));
423 $token->expects($this->any())
424 ->method(
'getCustomerId')
425 ->will($this->returnValue($tokenData[
'user_id']));
429 $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
430 $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
433 $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
434 $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
446 'token_expired_admin' => [
450 'created_at' => date(
'Y-m-d H:i:s', $time - 3600 - 400),
453 'currentTime' => $time,
454 'expectedUserType' =>
null,
455 'expectedUserId' =>
null,
457 'token_vigent_admin' => [
461 'created_at' => date(
'Y-m-d H:i:s', $time - 400),
464 'currentTime' => $time,
466 'expectedUserId' => 1234,
468 'token_expired_customer' => [
472 'created_at' => date(
'Y-m-d H:i:s', $time - 3600 - 400),
475 'currentTime' => $time,
476 'expectedUserType' =>
null,
477 'expectedUserId' =>
null,
479 'token_vigent_customer' => [
483 'created_at' => date(
'Y-m-d H:i:s', $time - 400),
486 'currentTime' => $time,
488 'expectedUserId' => 1234,
490 'token_expired_integration' => [
494 'created_at' => date(
'Y-m-d H:i:s', $time - 3600 - 400),
497 'currentTime' => $time,
499 'expectedUserId' => 1234,
501 'token_vigent_integration' => [
505 'created_at' => date(
'Y-m-d H:i:s', $time - 400),
508 'currentTime' => $time,
510 'expectedUserId' => 1234,
512 'token_expired_guest' => [
516 'created_at' => date(
'Y-m-d H:i:s', $time - 3600 - 400),
519 'currentTime' => $time,
520 'expectedUserType' =>
null,
521 'expectedUserId' =>
null,
523 'token_vigent_guest' => [
527 'created_at' => date(
'Y-m-d H:i:s', $time - 400),
530 'currentTime' => $time,
531 'expectedUserType' =>
null,
532 'expectedUserId' =>
null,
getExpiredTestTokenData()
testExpiredToken(array $tokenData, int $tokenTtl, int $currentTime, $expectedUserType, $expectedUserId)
testNoAuthorizationHeader()
testValidToken($userType, $userId, $expectedUserType, $expectedUserId)
const USER_TYPE_INTEGRATION