This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
GraphQL-129: Retrieve Customer token #180
Merged
magento-engcom-team
merged 5 commits into
magento:2.3-develop
from
pfantini:129-retrive-customer-token
Sep 18, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
527e4fe
GraphQl-129: Retrieve Customer token
chrom ea9b2a8
GraphQL-129: Change generate token schema and add graphql exception
pfantini ca0c135
GraphQL-129. Add generate customer token test
pfantini c0d1b7b
GrapgQL-129: Add composer dependency and fix codestyle issues
pfantini e97301d
GraphQL-129: Change resolve to return array, update schema and test
pfantini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
<?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\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\Exception\AuthenticationException; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Customers Token resolver, used for GraphQL request processing. | ||
*/ | ||
class GenerateCustomerToken implements ResolverInterface | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param CustomerTokenServiceInterface $customerTokenService | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
CustomerTokenServiceInterface $customerTokenService, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->customerTokenService = $customerTokenService; | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
): Value { | ||
try { | ||
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']); | ||
$result = function () use ($token) { | ||
return !empty($token) ? ['token' => $token] : ''; | ||
}; | ||
return $this->valueFactory->create($result); | ||
} 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
<?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 | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
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 | ||
* | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls, merge mainline
Magento\Framework\GraphQl\Query\ResolverInterface
was changedIn this case we need to return
string
valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I change returned value to string or array the test
Magento\GraphQl\IntrospectionQueryTest::testIntrospectionQueryWithFieldArgs
fails