Skip to content

Commit c9c994c

Browse files
committed
ACP2E-636: Cannot get graphql customer token
1 parent 3a74eb5 commit c9c994c

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/LoginAsCustomerGraphQl/GenerateLoginCustomerTokenTest.php

100755100644
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ public function testGenerateCustomerValidTokenLoginAsCustomerEnabled()
6262
$this->assertIsArray($response['generateCustomerTokenAsAdmin']);
6363
}
6464

65+
/**
66+
* Verify customer token as admin with store
67+
*
68+
* @magentoApiDataFixture Magento/LoginAsCustomer/_files/admin.php
69+
* @magentoConfigFixture admin_store login_as_customer/general/enabled 1
70+
* @magentoApiDataFixture Magento/LoginAsCustomer/_files/customer_with_second_store.php
71+
* @throws Exception
72+
*/
73+
public function testGenerateCustomerValidTokenAsAdminWithStore()
74+
{
75+
$customerEmail = '[email protected]';
76+
77+
$mutation = $this->getQuery($customerEmail);
78+
79+
$response = $this->graphQlMutation(
80+
$mutation,
81+
[],
82+
'',
83+
$this->getAdminHeaderAuthenticationWithStore(
84+
'TestAdmin1',
85+
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
86+
'fixture_second_store'
87+
)
88+
);
89+
$this->assertArrayHasKey('generateCustomerTokenAsAdmin', $response);
90+
$this->assertIsArray($response['generateCustomerTokenAsAdmin']);
91+
}
92+
6593
/**
6694
* Verify with Admin email ID and Magento_LoginAsCustomer::login is disabled
6795
*
@@ -224,4 +252,28 @@ private function getAdminHeaderAuthentication(string $userName, string $password
224252
);
225253
}
226254
}
255+
256+
/**
257+
* To get admin access token with store
258+
*
259+
* @param string $userName
260+
* @param string $password
261+
* @param string $storeCode
262+
* @return string[]
263+
* @throws AuthenticationException
264+
*/
265+
private function getAdminHeaderAuthenticationWithStore(string $userName, string $password, string $storeCode)
266+
{
267+
try {
268+
$adminAccessToken = $this->adminTokenService->createAdminAccessToken($userName, $password);
269+
return ['Authorization' => 'Bearer ' . $adminAccessToken, 'store' => $storeCode];
270+
} catch (\Exception $e) {
271+
throw new AuthenticationException(
272+
__(
273+
'The account sign-in was incorrect or your account is disabled temporarily. '
274+
. 'Please wait and try again later.'
275+
)
276+
);
277+
}
278+
}
227279
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Magento\Customer\Model\CustomerRegistry;
10+
use Magento\Customer\Api\Data\CustomerExtensionFactory;
11+
use Magento\Store\Model\Store;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
14+
15+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_store.php');
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
$store = $objectManager->get(Store::class);
19+
$store->load('fixture_second_store', 'code');
20+
21+
//$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
22+
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
23+
$repository = $objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
24+
$customer = $objectManager->create(\Magento\Customer\Model\Customer::class);
25+
26+
/** @var CustomerRegistry $customerRegistry */
27+
$customerRegistry = $objectManager->get(CustomerRegistry::class);
28+
/** @var Magento\Customer\Model\Customer $customer */
29+
$customer->setWebsiteId($store->getWebsiteId())
30+
->setId(10)
31+
->setEmail('[email protected]')
32+
->setPassword('password')
33+
->setGroupId($store->getGroupId())
34+
->setStoreId($store->getId())
35+
->setIsActive(1)
36+
->setPrefix('Mr.')
37+
->setFirstname('John')
38+
->setMiddlename('A')
39+
->setLastname('Smith')
40+
->setSuffix('Esq.')
41+
->setDefaultBilling(1)
42+
->setDefaultShipping(1)
43+
->setTaxvat('12')
44+
->setGender(0);
45+
46+
$extension = $customer->getExtensionAttributes();
47+
if ($extension === null) {
48+
$extension = $objectManager->get(CustomerExtensionFactory::class)->create();
49+
}
50+
51+
$extension->setAssistanceAllowed(2);
52+
$customer->setExtensionAttributes($extension);
53+
54+
$customer->isObjectNew(true);
55+
$customer->save();
56+
57+
$customerRegistry->remove($customer->getId());
58+
/** @var \Magento\JwtUserToken\Api\RevokedRepositoryInterface $revokedRepo */
59+
$revokedRepo = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
60+
->get(\Magento\JwtUserToken\Api\RevokedRepositoryInterface::class);
61+
$revokedRepo->saveRevoked(
62+
new \Magento\JwtUserToken\Api\Data\Revoked(
63+
\Magento\Authorization\Model\UserContextInterface::USER_TYPE_CUSTOMER,
64+
(int) $customer->getId(),
65+
time() - 3600 * 24
66+
)
67+
);

0 commit comments

Comments
 (0)