Skip to content

Commit 6fc2735

Browse files
author
Dmytro Vilchynskyi
committed
MAGETWO-70725: Admin token does not expire after 'Admin Token Lifetime (hours)'
- fix static tests
1 parent bc727b1 commit 6fc2735

File tree

2 files changed

+61
-45
lines changed

2 files changed

+61
-45
lines changed

app/code/Magento/Webapi/Model/Authorization/TokenUserContext.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
use Magento\Authorization\Model\UserContextInterface;
1010
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\Oauth\Exception;
1211
use Magento\Integration\Model\Oauth\Token;
1312
use Magento\Integration\Model\Oauth\TokenFactory;
1413
use Magento\Integration\Api\IntegrationServiceInterface;
1514
use Magento\Framework\Webapi\Request;
15+
use Magento\Framework\Stdlib\DateTime\DateTime as Date;
16+
use Magento\Framework\Stdlib\DateTime;
17+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
1618

1719
/**
1820
* A user context determined by tokens in a HTTP request Authorization header.
@@ -50,46 +52,50 @@ class TokenUserContext implements UserContextInterface
5052
protected $integrationService;
5153

5254
/**
53-
* @var \Magento\Framework\Stdlib\DateTime
55+
* @var DateTime
5456
*/
5557
private $dateTime;
5658

5759
/**
58-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
60+
* @var Date
5961
*/
6062
private $date;
6163

6264
/**
63-
* @var \Magento\Integration\Helper\Oauth\Data
65+
* @var OauthHelper
6466
*/
6567
private $oauthHelper;
6668

6769
/**
6870
* Initialize dependencies.
6971
*
72+
* TokenUserContext constructor.
7073
* @param Request $request
7174
* @param TokenFactory $tokenFactory
7275
* @param IntegrationServiceInterface $integrationService
76+
* @param DateTime|null $dateTime
77+
* @param Date|null $date
78+
* @param OauthHelper|null $oauthHelper
7379
*/
7480
public function __construct(
7581
Request $request,
7682
TokenFactory $tokenFactory,
7783
IntegrationServiceInterface $integrationService,
78-
\Magento\Framework\Stdlib\DateTime $dateTime = null,
79-
\Magento\Framework\Stdlib\DateTime\DateTime $date = null,
80-
\Magento\Integration\Helper\Oauth\Data $oauthHelper = null
84+
DateTime $dateTime = null,
85+
Date $date = null,
86+
OauthHelper $oauthHelper = null
8187
) {
8288
$this->request = $request;
8389
$this->tokenFactory = $tokenFactory;
8490
$this->integrationService = $integrationService;
8591
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(
86-
\Magento\Framework\Stdlib\DateTime::class
92+
DateTime::class
8793
);
8894
$this->date = $date ?: ObjectManager::getInstance()->get(
89-
\Magento\Framework\Stdlib\DateTime\DateTime::class
95+
Date::class
9096
);
9197
$this->oauthHelper = $oauthHelper ?: ObjectManager::getInstance()->get(
92-
\Magento\Integration\Helper\Oauth\Data::class
98+
OauthHelper::class
9399
);
94100
}
95101

@@ -115,14 +121,13 @@ public function getUserType()
115121
* Check if token is expired.
116122
*
117123
* @param Token $token
118-
*
119124
* @return bool
120125
*/
121-
private function isTokenExpired(Token $token)
126+
private function isTokenExpired(Token $token): bool
122127
{
123-
if ($token->getUserType() == \Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN) {
128+
if ($token->getUserType() == UserContextInterface::USER_TYPE_ADMIN) {
124129
$tokenTtl = $this->oauthHelper->getAdminTokenLifetime();
125-
} elseif ($token->getUserType() == \Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER) {
130+
} elseif ($token->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
126131
$tokenTtl = $this->oauthHelper->getCustomerTokenLifetime();
127132
} else {
128133
// other user-type tokens are considered always valid

app/code/Magento/Webapi/Test/Unit/Model/Authorization/TokenUserContextTest.php

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,78 @@
66

77
namespace Magento\Webapi\Test\Unit\Model\Authorization;
88

9+
use Magento\Webapi\Model\Authorization\TokenUserContext;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
911
use Magento\Authorization\Model\UserContextInterface;
12+
use Magento\Integration\Model\Oauth\TokenFactory;
13+
use Magento\Integration\Model\Oauth\Token;
14+
use Magento\Integration\Api\IntegrationServiceInterface;
15+
use Magento\Framework\Webapi\Request;
16+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
17+
use Magento\Framework\Stdlib\DateTime\DateTime as Date;
18+
use Magento\Framework\Stdlib\DateTime;
19+
use Magento\Integration\Model\Integration;
1020

1121
/**
12-
* Tests \Magento\Webapi\Model\Authorization\TokenUserContext
22+
* Tests TokenUserContext
1323
*/
1424
class TokenUserContextTest extends \PHPUnit\Framework\TestCase
1525
{
1626
/**
17-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
27+
* @var ObjectManager
1828
*/
1929
protected $objectManager;
2030

2131
/**
22-
* @var \Magento\Webapi\Model\Authorization\TokenUserContext
32+
* @var TokenUserContext
2333
*/
2434
protected $tokenUserContext;
2535

2636
/**
27-
* @var \Magento\Integration\Model\Oauth\TokenFactory|\PHPUnit_Framework_MockObject_MockObject
37+
* @var TokenFactory|\PHPUnit_Framework_MockObject_MockObject
2838
*/
2939
protected $tokenFactory;
3040

3141
/**
32-
* @var \Magento\Integration\Api\IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
42+
* @var IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
3343
*/
3444
protected $integrationService;
3545

3646
/**
37-
* @var \Magento\Framework\Webapi\Request|\PHPUnit_Framework_MockObject_MockObject
47+
* @var Request|\PHPUnit_Framework_MockObject_MockObject
3848
*/
3949
protected $request;
4050

4151
/**
42-
* @var \Magento\Integration\Helper\Oauth\Data|\PHPUnit_Framework_MockObject_MockObject
52+
* @var OauthHelper|\PHPUnit_Framework_MockObject_MockObject
4353
*/
4454
private $oauthHelperMock;
4555

4656
/**
47-
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
57+
* @var Date|\PHPUnit_Framework_MockObject_MockObject
4858
*/
4959
private $dateMock;
5060

5161
/**
52-
* @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject
62+
* @var DateTime|\PHPUnit_Framework_MockObject_MockObject
5363
*/
5464
private $dateTimeMock;
5565

5666
protected function setUp()
5767
{
58-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
68+
$this->objectManager = new ObjectManager($this);
5969

60-
$this->request = $this->getMockBuilder(\Magento\Framework\Webapi\Request::class)
70+
$this->request = $this->getMockBuilder(Request::class)
6171
->disableOriginalConstructor()
6272
->setMethods(['getHeader'])
6373
->getMock();
6474

65-
$this->tokenFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\TokenFactory::class)
75+
$this->tokenFactory = $this->getMockBuilder(TokenFactory::class)
6676
->disableOriginalConstructor()
6777
->setMethods(['create'])
6878
->getMock();
6979

70-
$this->integrationService = $this->getMockBuilder(\Magento\Integration\Api\IntegrationServiceInterface::class)
80+
$this->integrationService = $this->getMockBuilder(IntegrationServiceInterface::class)
7181
->disableOriginalConstructor()
7282
->setMethods(
7383
[
@@ -83,17 +93,17 @@ protected function setUp()
8393
)
8494
->getMock();
8595

86-
$this->oauthHelperMock = $this->getMockBuilder(\Magento\Integration\Helper\Oauth\Data::class)
96+
$this->oauthHelperMock = $this->getMockBuilder(OauthHelper::class)
8797
->disableOriginalConstructor()
8898
->setMethods(['getAdminTokenLifetime', 'getCustomerTokenLifetime'])
8999
->getMock();
90100

91-
$this->dateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
101+
$this->dateMock = $this->getMockBuilder(Date::class)
92102
->disableOriginalConstructor()
93103
->setMethods(['gmtTimestamp'])
94104
->getMock();
95105

96-
$this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
106+
$this->dateTimeMock = $this->getMockBuilder(DateTime::class)
97107
->disableOriginalConstructor()
98108
->setMethods(['strToTime'])
99109
->getMock();
@@ -109,7 +119,7 @@ function ($str) {
109119
);
110120

111121
$this->tokenUserContext = $this->objectManager->getObject(
112-
\Magento\Webapi\Model\Authorization\TokenUserContext::class,
122+
TokenUserContext::class,
113123
[
114124
'request' => $this->request,
115125
'tokenFactory' => $this->tokenFactory,
@@ -160,7 +170,7 @@ public function testNoTokenInDatabase()
160170
->with('Authorization')
161171
->will($this->returnValue("Bearer {$bearerToken}"));
162172

163-
$token = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
173+
$token = $this->getMockBuilder(Token::class)
164174
->disableOriginalConstructor()
165175
->setMethods(['loadByToken', 'getId', '__wakeup'])
166176
->getMock();
@@ -188,7 +198,7 @@ public function testRevokedToken()
188198
->with('Authorization')
189199
->will($this->returnValue("Bearer {$bearerToken}"));
190200

191-
$token = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
201+
$token = $this->getMockBuilder(Token::class)
192202
->disableOriginalConstructor()
193203
->setMethods(['loadByToken', 'getId', 'getRevoked', '__wakeup'])
194204
->getMock();
@@ -222,7 +232,7 @@ public function testValidToken($userType, $userId, $expectedUserType, $expectedU
222232
->with('Authorization')
223233
->will($this->returnValue("Bearer {$bearerToken}"));
224234

225-
$token = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
235+
$token = $this->getMockBuilder(Token::class)
226236
->disableOriginalConstructor()
227237
->setMethods(
228238
[
@@ -255,7 +265,7 @@ public function testValidToken($userType, $userId, $expectedUserType, $expectedU
255265

256266
switch ($userType) {
257267
case UserContextInterface::USER_TYPE_INTEGRATION:
258-
$integration = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
268+
$integration = $this->getMockBuilder(Integration::class)
259269
->disableOriginalConstructor()
260270
->setMethods(['getId', '__wakeup'])
261271
->getMock();
@@ -333,7 +343,7 @@ public function testExpiredToken($tokenData, $tokenTtl, $currentTime, $expectedU
333343
->with('Authorization')
334344
->will($this->returnValue("Bearer {$bearerToken}"));
335345

336-
$token = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
346+
$token = $this->getMockBuilder(Token::class)
337347
->disableOriginalConstructor()
338348
->setMethods(
339349
[
@@ -378,7 +388,7 @@ public function testExpiredToken($tokenData, $tokenTtl, $currentTime, $expectedU
378388

379389
switch ($tokenData['user_type']) {
380390
case UserContextInterface::USER_TYPE_INTEGRATION:
381-
$integration = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
391+
$integration = $this->getMockBuilder(Integration::class)
382392
->disableOriginalConstructor()
383393
->setMethods(['getId', '__wakeup'])
384394
->getMock();
@@ -411,7 +421,8 @@ public function testExpiredToken($tokenData, $tokenTtl, $currentTime, $expectedU
411421
}
412422

413423
/**
414-
* Data provider for expired token test
424+
* Data provider for expired token test.
425+
*
415426
* @return array
416427
*/
417428
public function getExpiredTestTokenData()
@@ -426,7 +437,7 @@ public function getExpiredTestTokenData()
426437
],
427438
'tokenTtl' => 1,
428439
'currentTime' => $time,
429-
'expedtedUserType' => null,
440+
'expectedUserType' => null,
430441
'expectedUserId' => null,
431442
],
432443
'token_vigent_admin' => [
@@ -437,7 +448,7 @@ public function getExpiredTestTokenData()
437448
],
438449
'tokenTtl' => 1,
439450
'currentTime' => $time,
440-
'expedtedUserType' => UserContextInterface::USER_TYPE_ADMIN,
451+
'expectedUserType' => UserContextInterface::USER_TYPE_ADMIN,
441452
'expectedUserId' => 1234,
442453
],
443454
'token_expired_customer' => [
@@ -448,7 +459,7 @@ public function getExpiredTestTokenData()
448459
],
449460
'tokenTtl' => 1,
450461
'currentTime' => $time,
451-
'expedtedUserType' => null,
462+
'expectedUserType' => null,
452463
'expectedUserId' => null,
453464
],
454465
'token_vigent_customer' => [
@@ -459,7 +470,7 @@ public function getExpiredTestTokenData()
459470
],
460471
'tokenTtl' => 1,
461472
'currentTime' => $time,
462-
'expedtedUserType' => UserContextInterface::USER_TYPE_CUSTOMER,
473+
'expectedUserType' => UserContextInterface::USER_TYPE_CUSTOMER,
463474
'expectedUserId' => 1234,
464475
],
465476
'token_expired_integration' => [
@@ -481,7 +492,7 @@ public function getExpiredTestTokenData()
481492
],
482493
'tokenTtl' => 1,
483494
'currentTime' => $time,
484-
'expedtedUserType' => UserContextInterface::USER_TYPE_INTEGRATION,
495+
'expectedUserType' => UserContextInterface::USER_TYPE_INTEGRATION,
485496
'expectedUserId' => 1234,
486497
],
487498
'token_expired_guest' => [
@@ -492,7 +503,7 @@ public function getExpiredTestTokenData()
492503
],
493504
'tokenTtl' => 1,
494505
'currentTime' => $time,
495-
'expedtedUserType' => null,
506+
'expectedUserType' => null,
496507
'expectedUserId' => null,
497508
],
498509
'token_vigent_guest' => [
@@ -503,7 +514,7 @@ public function getExpiredTestTokenData()
503514
],
504515
'tokenTtl' => 1,
505516
'currentTime' => $time,
506-
'expedtedUserType' => null,
517+
'expectedUserType' => null,
507518
'expectedUserId' => null,
508519
],
509520
];

0 commit comments

Comments
 (0)