Skip to content

Commit 00c5fdd

Browse files
Cari Spruiellisitnikov
authored andcommitted
MAGETWO-51292: [GITHUB] OAuth Token Exchange Expiration Period Is Not Calculated Correctly #3449
- fix static test failures - update unit tests
1 parent 82745db commit 00c5fdd

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

app/code/Magento/Integration/Model/OauthService.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class OauthService implements \Magento\Integration\Api\OauthServiceInterface
6161
*/
6262
protected $_tokenProvider;
6363

64+
/**
65+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
66+
*/
67+
private $_dateHelper;
68+
6469
/**
6570
* Initialize dependencies.
6671
*
@@ -94,6 +99,22 @@ public function __construct(
9499
}
95100

96101
/**
102+
* The getter function to get the new DateTime dependency
103+
*
104+
* @return \Magento\Framework\Stdlib\DateTime\DateTime
105+
*
106+
* @deprecated
107+
*/
108+
private function getDateHelper()
109+
{
110+
if ($this->_dateHelper === null) {
111+
$this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
112+
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
113+
}
114+
return $this->_dateHelper;
115+
}
116+
117+
/*
97118
* {@inheritdoc}
98119
*/
99120
public function createConsumer($consumerData)
@@ -193,7 +214,7 @@ public function postToConsumer($consumerId, $endpointUrl)
193214
{
194215
try {
195216
$consumer = $this->_consumerFactory->create()->load($consumerId);
196-
$consumer->setUpdatedAt(date_create());
217+
$consumer->setUpdatedAt($this->getDateHelper()->gmtDate());
197218
$consumer->save();
198219
if (!$consumer->getId()) {
199220
throw new \Magento\Framework\Oauth\Exception(

app/code/Magento/Integration/Model/ResourceModel/Oauth/Consumer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
4545
/**
4646
* Compute time in seconds since consumer was created.
4747
*
48+
* @deprecated
49+
*
4850
* @param int $consumerId - The consumer id
4951
* @return int - time lapsed in seconds
5052
*/
@@ -60,6 +62,12 @@ public function getTimeInSecondsSinceCreation($consumerId)
6062
return $connection->fetchOne($select);
6163
}
6264

65+
/**
66+
* Compute time in seconds since token exchange started.
67+
*
68+
* @param int $consumerId - The consumer id
69+
* @return int - time lapsed in seconds
70+
*/
6371
public function getTimeInSecondsSinceTokenExchangeStarted($consumerId)
6472
{
6573
$connection = $this->getConnection();

app/code/Magento/Integration/Test/Unit/Helper/Oauth/ConsumerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ public function testPostToConsumer()
152152
)->will(
153153
$this->returnSelf()
154154
);
155+
156+
$dateHelperMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\DateTime')
157+
->disableOriginalConstructor()
158+
->getMock();
159+
$dateHelperMock->expects($this->any())->method('gmtDate');
160+
161+
$dateHelper = new \ReflectionProperty('Magento\Integration\Model\OauthService', '_dateHelper');
162+
$dateHelper->setAccessible(true);
163+
$dateHelper->setValue($this->_oauthService, $dateHelperMock);
164+
155165
$this->_consumerMock->expects($this->once())->method('getId')->will($this->returnValue($consumerId));
156166
$this->_consumerMock->expects($this->once())->method('getData')->will($this->returnValue($consumerData));
157167
$this->_httpClientMock->expects(

app/code/Magento/Integration/Test/Unit/Model/Oauth/ConsumerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function setUp()
110110

111111
$this->resourceMock = $this->getMock(
112112
'Magento\Integration\Model\ResourceModel\Oauth\Consumer',
113-
['getTimeInSecondsSinceCreation', 'getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries'],
113+
['getTimeInSecondsSinceTokenExchangeStarted', 'getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries'],
114114
[],
115115
'',
116116
false,
@@ -216,20 +216,20 @@ public function testValidateInvalidConsumerSecret()
216216
public function testGetConsumerExpirationPeriodValid()
217217
{
218218
$this->resourceMock->expects($this->once())
219-
->method('getTimeInSecondsSinceCreation')
219+
->method('getTimeInSecondsSinceTokenExchangeStarted')
220220
->will($this->returnValue(30));
221221

222-
$this->consumerModel->setCreatedAt(time());
222+
$this->consumerModel->setUpdatedAt(time());
223223
$this->assertTrue($this->consumerModel->isValidForTokenExchange());
224224
}
225225

226226
public function testGetConsumerExpirationPeriodExpired()
227227
{
228228
$this->resourceMock->expects($this->once())
229-
->method('getTimeInSecondsSinceCreation')
229+
->method('getTimeInSecondsSinceTokenExchangeStarted')
230230
->will($this->returnValue(400));
231231

232-
$this->consumerModel->setCreatedAt(time());
232+
$this->consumerModel->setUpdatedAt(time());
233233
$this->assertFalse($this->consumerModel->isValidForTokenExchange());
234234
}
235235
}

0 commit comments

Comments
 (0)