Skip to content

Commit 433fea7

Browse files
Merge pull request #4208 from magento-qwerty/MC-16342
Fixed Issues: - MC-16342: [2.2.x] Fix CompanyUserManagerInterfaceTest failing on mainline
2 parents c2e450f + 1bac985 commit 433fea7

File tree

9 files changed

+239
-182
lines changed

9 files changed

+239
-182
lines changed

app/code/Magento/Sales/Model/Rss/OrderStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function getOrder()
155155
}
156156

157157
$data = (string)$this->request->getParam('data');
158-
if ((string)$this->request->getParam('signature') !== $this->signature->signData($data)) {
158+
if (!$this->signature->isValid($data, (string)$this->request->getParam('signature'))) {
159159
return null;
160160
}
161161
$json = base64_decode($data);

app/code/Magento/Sales/Model/Rss/Signature.php

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,47 @@
88

99
namespace Magento\Sales\Model\Rss;
1010

11-
use Magento\Framework\Config\ConfigOptionsListConstants;
11+
use Magento\Framework\Encryption\EncryptorInterface;
1212

1313
/**
1414
* Class for generating signature.
1515
*/
1616
class Signature
1717
{
1818
/**
19-
* Version of encryption key.
20-
*
21-
* @var int
22-
*/
23-
private $keyVersion;
24-
25-
/**
26-
* Array of encryption keys.
27-
*
28-
* @var string[]
29-
*/
30-
private $keys = [];
31-
32-
/**
33-
* @var \Magento\Framework\App\DeploymentConfig
19+
* @var EncryptorInterface
3420
*/
35-
private $deploymentConfig;
21+
private $encryptor;
3622

3723
/**
38-
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
24+
* @param EncryptorInterface $encryptor
3925
*/
4026
public function __construct(
41-
\Magento\Framework\App\DeploymentConfig $deploymentConfig
27+
EncryptorInterface $encryptor
4228
) {
43-
$this->deploymentConfig = $deploymentConfig;
44-
// load all possible keys
45-
$this->keys = preg_split(
46-
'/\s+/s',
47-
(string)$this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY)
48-
);
49-
$this->keyVersion = count($this->keys) - 1;
29+
$this->encryptor = $encryptor;
5030
}
5131

5232
/**
53-
* Get secret key.
33+
* Sign data.
5434
*
35+
* @param string $data
5536
* @return string
5637
*/
57-
private function getSecretKey(): string
38+
public function signData(string $data): string
5839
{
59-
return (string)$this->keys[$this->keyVersion];
40+
return $this->encryptor->hash($data);
6041
}
6142

6243
/**
63-
* Sign data.
44+
* Check if valid signature is provided for given data.
6445
*
6546
* @param string $data
66-
* @return string
47+
* @param string $signature
48+
* @return bool
6749
*/
68-
public function signData(string $data): string
50+
public function isValid(string $data, string $signature): bool
6951
{
70-
return hash_hmac('sha256', $data, pack('H*', $this->getSecretKey()));
52+
return $this->encryptor->validateHash($data, $signature);
7153
}
7254
}

app/code/Magento/Sales/Test/Unit/Model/Rss/OrderStatusTest.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* Class OrderStatusTest
13+
*
1314
* @package Magento\Sales\Model\Rss
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1516
*/
@@ -92,6 +93,9 @@ class OrderStatusTest extends \PHPUnit\Framework\TestCase
9293
],
9394
];
9495

96+
/**
97+
* @inheritdoc
98+
*/
9599
protected function setUp()
96100
{
97101
$this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
@@ -142,11 +146,18 @@ protected function setUp()
142146
);
143147
}
144148

149+
/**
150+
* Positive scenario.
151+
*/
145152
public function testGetRssData()
146153
{
147154
$this->orderFactory->expects($this->once())->method('create')->willReturn($this->order);
148155
$requestData = base64_encode('{"order_id":1,"increment_id":"100000001","customer_id":1}');
149-
$this->signature->expects($this->any())->method('signData')->willReturn('signature');
156+
$this->signature->expects($this->never())->method('signData');
157+
$this->signature->expects($this->any())
158+
->method('isValid')
159+
->with($requestData, 'signature')
160+
->willReturn(true);
150161

151162
$this->requestInterface->expects($this->any())
152163
->method('getParam')
@@ -177,14 +188,20 @@ public function testGetRssData()
177188
}
178189

179190
/**
191+
* Case when invalid data is provided.
192+
*
180193
* @expectedException \InvalidArgumentException
181194
* @expectedExceptionMessage Order not found.
182195
*/
183196
public function testGetRssDataWithError()
184197
{
185198
$this->orderFactory->expects($this->once())->method('create')->willReturn($this->order);
186199
$requestData = base64_encode('{"order_id":"1","increment_id":true,"customer_id":true}');
187-
$this->signature->expects($this->any())->method('signData')->willReturn('signature');
200+
$this->signature->expects($this->never())->method('signData');
201+
$this->signature->expects($this->any())
202+
->method('isValid')
203+
->with($requestData, 'signature')
204+
->willReturn(true);
188205
$this->requestInterface->expects($this->any())
189206
->method('getParam')
190207
->willReturnMap(
@@ -199,16 +216,20 @@ public function testGetRssDataWithError()
199216
}
200217

201218
/**
219+
* Case when invalid signature is provided.
220+
*
202221
* @expectedException \InvalidArgumentException
203222
* @expectedExceptionMessage Order not found.
204223
*/
205224
public function testGetRssDataWithWrongSignature()
206225
{
207226
$requestData = base64_encode('{"order_id":"1","increment_id":true,"customer_id":true}');
227+
$this->signature->expects($this->never())
228+
->method('signData');
208229
$this->signature->expects($this->any())
209-
->method('signData')
210-
->with($requestData)
211-
->willReturn('wrong_signature');
230+
->method('isValid')
231+
->with($requestData, 'signature')
232+
->willReturn(false);
212233
$this->requestInterface->expects($this->any())
213234
->method('getParam')
214235
->willReturnMap(
@@ -222,6 +243,9 @@ public function testGetRssDataWithWrongSignature()
222243
$this->assertEquals($this->feedData, $this->model->getRssData());
223244
}
224245

246+
/**
247+
* Testing allowed getter.
248+
*/
225249
public function testIsAllowed()
226250
{
227251
$this->scopeConfigInterface->expects($this->once())->method('getValue')
@@ -231,6 +255,8 @@ public function testIsAllowed()
231255
}
232256

233257
/**
258+
* Test caching.
259+
*
234260
* @param string $requestData
235261
* @param string $result
236262
* @dataProvider getCacheKeyDataProvider
@@ -242,12 +268,18 @@ public function testGetCacheKey($requestData, $result)
242268
['data', null, $requestData],
243269
['signature', null, 'signature'],
244270
]);
245-
$this->signature->expects($this->any())->method('signData')->willReturn('signature');
271+
$this->signature->expects($this->never())->method('signData');
272+
$this->signature->expects($this->any())
273+
->method('isValid')
274+
->with($requestData, 'signature')
275+
->willReturn(true);
246276
$this->orderFactory->expects($this->once())->method('create')->will($this->returnValue($this->order));
247277
$this->assertEquals('rss_order_status_data_' . $result, $this->model->getCacheKey());
248278
}
249279

250280
/**
281+
* Test data for caching test.
282+
*
251283
* @return array
252284
*/
253285
public function getCacheKeyDataProvider()
@@ -258,6 +290,9 @@ public function getCacheKeyDataProvider()
258290
];
259291
}
260292

293+
/**
294+
* Test for cache lifetime getter.
295+
*/
261296
public function testGetCacheLifetime()
262297
{
263298
$this->assertEquals(600, $this->model->getCacheLifetime());

app/code/Magento/Sales/Test/Unit/Model/Rss/SignatureTest.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

app/code/Magento/Vault/Test/Unit/Observer/AfterPaymentSaveObserverTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Magento\Vault\Observer\AfterPaymentSaveObserver;
1919
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2020

21+
/**
22+
* Test for payment observer.
23+
*/
2124
class AfterPaymentSaveObserverTest extends \PHPUnit\Framework\TestCase
2225
{
2326
/**
@@ -61,14 +64,18 @@ class AfterPaymentSaveObserverTest extends \PHPUnit\Framework\TestCase
6164
protected $salesOrderPaymentMock;
6265

6366
/**
64-
* @return void
67+
* @inheritdoc
6568
*/
6669
protected function setUp()
6770
{
6871
/** @var Random|MockObject $encryptorRandomGenerator */
6972
$encryptorRandomGenerator = $this->createMock(Random::class);
7073
/** @var DeploymentConfig|MockObject $deploymentConfigMock */
7174
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
75+
$deploymentConfigMock->expects($this->any())
76+
->method('get')
77+
->with(Encryptor::PARAM_CRYPT_KEY)
78+
->willReturn('g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ');
7279
$this->encryptorModel = new Encryptor($encryptorRandomGenerator, $deploymentConfigMock);
7380

7481
$this->paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
@@ -117,6 +124,8 @@ protected function setUp()
117124
}
118125

119126
/**
127+
* Case when payment successfully made.
128+
*
120129
* @param int $customerId
121130
* @param string $createdAt
122131
* @param string $token
@@ -161,6 +170,8 @@ public function testPositiveCase($customerId, $createdAt, $token, $isActive, $me
161170
}
162171

163172
/**
173+
* Data for positiveCase test.
174+
*
164175
* @return array
165176
*/
166177
public function positiveCaseDataProvider()

0 commit comments

Comments
 (0)