Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit e86f3f1

Browse files
author
Valeriy Naida
authored
ENGCOM-2989: Recheck 129 retrive customer token #184
2 parents 8849e67 + 3f8dfd8 commit e86f3f1

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account;
9+
10+
use Magento\Framework\Exception\AuthenticationException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Integration\Api\CustomerTokenServiceInterface;
16+
17+
/**
18+
* Customers Token resolver, used for GraphQL request processing.
19+
*/
20+
class GenerateCustomerToken implements ResolverInterface
21+
{
22+
/**
23+
* @var CustomerTokenServiceInterface
24+
*/
25+
private $customerTokenService;
26+
27+
/**
28+
* @param CustomerTokenServiceInterface $customerTokenService
29+
*/
30+
public function __construct(
31+
CustomerTokenServiceInterface $customerTokenService
32+
) {
33+
$this->customerTokenService = $customerTokenService;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(
40+
Field $field,
41+
$context,
42+
ResolveInfo $info,
43+
array $value = null,
44+
array $args = null
45+
) {
46+
try {
47+
if (!isset($args['email'])) {
48+
throw new GraphQlInputException(__('"email" value should be specified'));
49+
}
50+
if (!isset($args['password'])) {
51+
throw new GraphQlInputException(__('"password" value should be specified'));
52+
}
53+
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
54+
return ['token' => $token];
55+
} catch (AuthenticationException $e) {
56+
throw new GraphQlAuthorizationException(
57+
__($e->getMessage())
58+
);
59+
}
60+
}
61+
}

app/code/Magento/CustomerGraphQl/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"php": "~7.1.3||~7.2.0",
77
"magento/module-customer": "*",
88
"magento/module-authorization": "*",
9+
"magento/module-integration": "*",
910
"magento/framework": "*"
1011
},
1112
"suggest": {

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ type Query {
55
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account")
66
}
77

8+
type Mutation {
9+
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
10+
}
11+
12+
type CustomerToken {
13+
token: String @doc(description: "The customer token")
14+
}
15+
816
type Customer @doc(description: "Customer defines the customer name and address and other details") {
917
created_at: String @doc(description: "Timestamp indicating when the account was created")
1018
group_id: Int @doc(description: "The group assigned to the user. Default values are 0 (Not logged in), 1 (General), 2 (Wholesale), and 3 (Retailer)")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Customer;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
use PHPUnit\Framework\TestResult;
12+
13+
/**
14+
* Class GenerateCustomerTokenTest
15+
* @package Magento\GraphQl\Customer
16+
*/
17+
class GenerateCustomerTokenTest extends GraphQlAbstract
18+
{
19+
/**
20+
* Verify customer token with valid credentials
21+
*
22+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
23+
*/
24+
public function testGenerateCustomerValidToken()
25+
{
26+
$userName = '[email protected]';
27+
$password = 'password';
28+
29+
$mutation
30+
= <<<MUTATION
31+
mutation {
32+
generateCustomerToken(
33+
email: "{$userName}"
34+
password: "{$password}"
35+
) {
36+
token
37+
}
38+
}
39+
MUTATION;
40+
41+
$response = $this->graphQlQuery($mutation);
42+
$this->assertArrayHasKey('generateCustomerToken', $response);
43+
$this->assertInternalType('array', $response['generateCustomerToken']);
44+
}
45+
46+
/**
47+
* Verify customer with invalid credentials
48+
*/
49+
public function testGenerateCustomerTokenWithInvalidCredentials()
50+
{
51+
$userName = '[email protected]';
52+
$password = 'bad-password';
53+
54+
$mutation
55+
= <<<MUTATION
56+
mutation {
57+
generateCustomerToken(
58+
email: "{$userName}"
59+
password: "{$password}"
60+
) {
61+
token
62+
}
63+
}
64+
MUTATION;
65+
66+
$this->expectException(\Exception::class);
67+
$this->expectExceptionMessage('GraphQL response contains errors: The account sign-in' . ' ' .
68+
'was incorrect or your account is disabled temporarily. Please wait and try again later.');
69+
$this->graphQlQuery($mutation);
70+
}
71+
}

0 commit comments

Comments
 (0)