Skip to content

Commit

Permalink
ENGCOM-2989: Recheck 129 retrive customer token #184
Browse files Browse the repository at this point in the history
 - 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
magento-engcom-team committed Sep 17, 2018
2 parents ca42048 + 5dce3bd commit 3f8dfd8
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
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())
);
}
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"php": "~7.1.3||~7.2.0",
"magento/module-customer": "*",
"magento/module-authorization": "*",
"magento/module-integration": "*",
"magento/framework": "*"
},
"suggest": {
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ type Query {
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account")
}

type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
}

type CustomerToken {
token: String @doc(description: "The customer token")
}

type Customer @doc(description: "Customer defines the customer name and address and other details") {
created_at: String @doc(description: "Timestamp indicating when the account was created")
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)")
Expand Down
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);
}
}

0 comments on commit 3f8dfd8

Please sign in to comment.