Skip to content

Commit c385695

Browse files
committed
MQE-2601: Create automated test for: "Verify that admin token and customer token life time can be configured from admin"
1 parent 0b4465c commit c385695

File tree

1 file changed

+131
-9
lines changed

1 file changed

+131
-9
lines changed

TwoFactorAuth/Test/Api/GoogleAuthenticateTest.php

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77

88
namespace Magento\TwoFactorAuth\Test\Api;
99

10+
use Magento\Framework\HTTP\ClientInterface;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\Framework\UrlInterface;
1013
use Magento\Framework\Webapi\Rest\Request;
14+
use Magento\Integration\Model\Oauth\TokenFactory;
15+
use Magento\Integration\Model\ResourceModel\Oauth\Token as TokenResource;
1116
use Magento\TestFramework\Helper\Bootstrap;
1217
use Magento\TestFramework\TestCase\WebapiAbstract;
1318
use Magento\TwoFactorAuth\Api\TfaInterface;
1419
use Magento\TwoFactorAuth\Model\Provider\Engine\Google;
1520
use Magento\User\Model\UserFactory;
1621
use OTPHP\TOTP;
1722

23+
/**
24+
* Class checks google authentication behaviour
25+
*/
1826
class GoogleAuthenticateTest extends WebapiAbstract
1927
{
2028
const SERVICE_VERSION = 'V1';
@@ -37,18 +45,53 @@ class GoogleAuthenticateTest extends WebapiAbstract
3745
*/
3846
private $tfa;
3947

48+
/**
49+
* @var ClientInterface
50+
*/
51+
private $client;
52+
53+
/**
54+
* @var UrlInterface
55+
*/
56+
private $url;
57+
58+
/**
59+
* @var SerializerInterface
60+
*/
61+
private $json;
62+
63+
/**
64+
* @var TokenResource
65+
*/
66+
private $tokenResource;
67+
68+
/**
69+
* @var TokenFactory
70+
*/
71+
private $tokenFactory;
72+
73+
/**
74+
* @inheritdoc
75+
*/
4076
protected function setUp(): void
4177
{
4278
$objectManager = Bootstrap::getObjectManager();
4379
$this->userFactory = $objectManager->get(UserFactory::class);
4480
$this->google = $objectManager->get(Google::class);
4581
$this->tfa = $objectManager->get(TfaInterface::class);
82+
$this->client = $objectManager->get(ClientInterface::class);
83+
$this->url = $objectManager->get(UrlInterface::class);
84+
$this->json = $objectManager->get(SerializerInterface::class);
85+
$this->tokenResource = $objectManager->get(TokenResource::class);
86+
$this->tokenFactory = $objectManager->get(TokenFactory::class);
4687
}
4788

4889
/**
4990
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
91+
*
92+
* @return void
5093
*/
51-
public function testInvalidCredentials()
94+
public function testInvalidCredentials(): void
5295
{
5396
$serviceInfo = $this->buildServiceInfo();
5497

@@ -80,8 +123,10 @@ public function testInvalidCredentials()
80123
/**
81124
* @magentoConfigFixture twofactorauth/general/force_providers duo_security
82125
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
126+
*
127+
* @return void
83128
*/
84-
public function testUnavailableProvider()
129+
public function testUnavailableProvider(): void
85130
{
86131
$serviceInfo = $this->buildServiceInfo();
87132

@@ -109,8 +154,10 @@ public function testUnavailableProvider()
109154
/**
110155
* @magentoConfigFixture twofactorauth/general/force_providers google
111156
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
157+
*
158+
* @return void
112159
*/
113-
public function testInvalidToken()
160+
public function testInvalidToken(): void
114161
{
115162
$userId = $this->getUserId();
116163
$serviceInfo = $this->buildServiceInfo();
@@ -141,8 +188,10 @@ public function testInvalidToken()
141188
/**
142189
* @magentoConfigFixture twofactorauth/general/force_providers google
143190
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
191+
*
192+
* @return void
144193
*/
145-
public function testNotConfiguredProvider()
194+
public function testNotConfiguredProvider(): void
146195
{
147196
$userId = $this->getUserId();
148197
$serviceInfo = $this->buildServiceInfo();
@@ -174,8 +223,10 @@ public function testNotConfiguredProvider()
174223
* @magentoConfigFixture twofactorauth/general/force_providers google
175224
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
176225
* @magentoConfigFixture twofactorauth/google/otp_window 120
226+
*
227+
* @return void
177228
*/
178-
public function testValidToken()
229+
public function testValidToken(): void
179230
{
180231
$userId = $this->getUserId();
181232
$otp = $this->getUserOtp();
@@ -195,6 +246,36 @@ public function testValidToken()
195246
self::assertMatchesRegularExpression('/^[a-z0-9]{32}$/', $response);
196247
}
197248

249+
/**
250+
* @magentoConfigFixture default/oauth/access_token_lifetime/admin 1
251+
* @magentoConfigFixture twofactorauth/general/force_providers google
252+
*
253+
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
254+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
255+
*
256+
* @return void
257+
*/
258+
public function testAdminTokenLifetime(): void
259+
{
260+
$this->tfa->getProviderByCode(Google::CODE)->activate($this->getUserId('webapi_user'));
261+
$otp = $this->getUserOtp('webapi_user');
262+
$serviceInfo = $this->buildServiceInfo();
263+
$requestData = [
264+
'otp' => $otp,
265+
'username' => 'webapi_user',
266+
'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
267+
];
268+
$accessToken = $this->_webApiCall($serviceInfo, $requestData);
269+
$result = $this->doCustomerRequest($accessToken, 1);
270+
$this->assertContains('[email protected]', $this->json->unserialize($result));
271+
$this->updateTokenCreatedTime($accessToken);
272+
$result = $this->doCustomerRequest($accessToken, 1);
273+
$this->assertContains(
274+
'The consumer isn\'t authorized to access %resources.',
275+
$this->json->unserialize($result)
276+
);
277+
}
278+
198279
/**
199280
* @return array
200281
*/
@@ -217,20 +298,61 @@ private function buildServiceInfo(): array
217298
];
218299
}
219300

220-
private function getUserId(): int
301+
/**
302+
* Get user id
303+
*
304+
* @param string $userName
305+
* @return int
306+
*/
307+
private function getUserId($userName = 'customRoleUser'): int
221308
{
222309
$user = $this->userFactory->create();
223-
$user->loadByUsername('customRoleUser');
310+
$user->loadByUsername($userName);
224311

225312
return (int)$user->getId();
226313
}
227314

228-
private function getUserOtp(): string
315+
/**
316+
* Get user otp
317+
*
318+
* @param string $userName
319+
* @return string
320+
*/
321+
private function getUserOtp($userName = 'customRoleUser'): string
229322
{
230323
$user = $this->userFactory->create();
231-
$user->loadByUsername('customRoleUser');
324+
$user->loadByUsername($userName);
232325
$totp = TOTP::create($this->google->getSecretCode($user));
233326

234327
return $totp->now();
235328
}
329+
330+
/**
331+
* Perform request to customers endpoint
332+
*
333+
* @param string $accessToken
334+
* @return string
335+
*/
336+
private function doCustomerRequest(string $accessToken, $customerId): string
337+
{
338+
$this->client->addHeader('Authorization', 'Bearer ' . $accessToken);
339+
$this->client->get($this->url->getBaseUrl() . 'rest/V1/customers/' . $customerId);
340+
341+
return $this->client->getBody();
342+
}
343+
344+
/**
345+
* Update token created time
346+
*
347+
* @param string $accessToken
348+
* @return void
349+
*/
350+
private function updateTokenCreatedTime(string $accessToken): void
351+
{
352+
$token = $this->tokenFactory->create();
353+
$token->loadByToken($accessToken);
354+
$createdAt = (new \DateTime('-1 day'))->format('Y-m-d H:i:s');
355+
$token->setCreatedAt($createdAt);
356+
$this->tokenResource->save($token);
357+
}
236358
}

0 commit comments

Comments
 (0)