-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-2989: Recheck 129 retrive customer token #184
- Merge Pull Request magento/graphql-ce#184 from magento/graphql-ce:recheck-129-retrive-customer-token - Merged commits: 1. 527e4fe 2. ea9b2a8 3. ca0c135 4. c0d1b7b 5. e97301d 6. 85cd239 7. 6a250c1 8. 21e719c 9. ad388f6 10. 277fe08 11. 5dce3bd
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/Account/GenerateCustomerToken.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account; | ||
|
||
use Magento\Framework\Exception\AuthenticationException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
|
||
/** | ||
* Customers Token resolver, used for GraphQL request processing. | ||
*/ | ||
class GenerateCustomerToken implements ResolverInterface | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @param CustomerTokenServiceInterface $customerTokenService | ||
*/ | ||
public function __construct( | ||
CustomerTokenServiceInterface $customerTokenService | ||
) { | ||
$this->customerTokenService = $customerTokenService; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
try { | ||
if (!isset($args['email'])) { | ||
throw new GraphQlInputException(__('"email" value should be specified')); | ||
} | ||
if (!isset($args['password'])) { | ||
throw new GraphQlInputException(__('"password" value should be specified')); | ||
} | ||
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']); | ||
return ['token' => $token]; | ||
} catch (AuthenticationException $e) { | ||
throw new GraphQlAuthorizationException( | ||
__($e->getMessage()) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Customer; | ||
|
||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use PHPUnit\Framework\TestResult; | ||
|
||
/** | ||
* Class GenerateCustomerTokenTest | ||
* @package Magento\GraphQl\Customer | ||
*/ | ||
class GenerateCustomerTokenTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* Verify customer token with valid credentials | ||
* | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
*/ | ||
public function testGenerateCustomerValidToken() | ||
{ | ||
$userName = 'customer@example.com'; | ||
$password = 'password'; | ||
|
||
$mutation | ||
= <<<MUTATION | ||
mutation { | ||
generateCustomerToken( | ||
email: "{$userName}" | ||
password: "{$password}" | ||
) { | ||
token | ||
} | ||
} | ||
MUTATION; | ||
|
||
$response = $this->graphQlQuery($mutation); | ||
$this->assertArrayHasKey('generateCustomerToken', $response); | ||
$this->assertInternalType('array', $response['generateCustomerToken']); | ||
} | ||
|
||
/** | ||
* Verify customer with invalid credentials | ||
*/ | ||
public function testGenerateCustomerTokenWithInvalidCredentials() | ||
{ | ||
$userName = 'customer@example.com'; | ||
$password = 'bad-password'; | ||
|
||
$mutation | ||
= <<<MUTATION | ||
mutation { | ||
generateCustomerToken( | ||
email: "{$userName}" | ||
password: "{$password}" | ||
) { | ||
token | ||
} | ||
} | ||
MUTATION; | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('GraphQL response contains errors: The account sign-in' . ' ' . | ||
'was incorrect or your account is disabled temporarily. Please wait and try again later.'); | ||
$this->graphQlQuery($mutation); | ||
} | ||
} |