Skip to content

Commit

Permalink
Merge pull request #4336 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[Magento Community Engineering] Community Contributions - GraphQL
  • Loading branch information
naydav authored Jun 12, 2019
2 parents e11f0e4 + 299a4e8 commit dffe94d
Show file tree
Hide file tree
Showing 21 changed files with 1,153 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type CustomizableAreaValue @doc(description: "CustomizableAreaValue defines the
}

type CategoryTree implements CategoryInterface @doc(description: "Category Tree implementation.") {
children: [CategoryTree] @doc(description: "Child categories tree.") @resolve(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
children: [CategoryTree] @doc(description: "Child categories tree.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\CategoryTree")
}

type CustomizableDateOption implements CustomizableOptionInterface @doc(description: "CustomizableDateOption contains information about a date picker that is defined as part of a customizable option.") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Validator\EmailAddress as EmailValidator;

/**
* Is Customer Email Available
Expand All @@ -24,13 +25,21 @@ class IsEmailAvailable implements ResolverInterface
*/
private $accountManagement;

/**
* @var EmailValidator
*/
private $emailValidator;

/**
* @param AccountManagementInterface $accountManagement
* @param EmailValidator $emailValidator
*/
public function __construct(
AccountManagementInterface $accountManagement
AccountManagementInterface $accountManagement,
EmailValidator $emailValidator
) {
$this->accountManagement = $accountManagement;
$this->emailValidator = $emailValidator;
}

/**
Expand All @@ -47,6 +56,10 @@ public function resolve(
throw new GraphQlInputException(__('Email must be specified'));
}

if (!$this->emailValidator->isValid($args['email'])) {
throw new GraphQlInputException(__('Email is invalid'));
}

try {
$isEmailAvailable = $this->accountManagement->isEmailAvailable($args['email']);
} catch (LocalizedException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,10 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
}
QUERY;
$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available');
$this->expectExceptionMessage(
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1043,8 +1045,10 @@ public function testQueryPageOutOfBoundException()
QUERY;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available.');
$this->expectExceptionMessage(
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available.'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1075,8 +1079,10 @@ public function testQueryWithNoSearchOrFilterArgumentException()
QUERY;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
'required.');
$this->expectExceptionMessage(
'GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
'required.'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1140,7 +1146,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
private function assertProductItems(array $filteredProducts, array $actualResponse)
{
$productItemsInResponse = array_map(null, $actualResponse['products']['items'], $filteredProducts);

// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
$this->assertResponseFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
namespace Magento\GraphQl\Customer;

use Magento\TestFramework\TestCase\GraphQlAbstract;
use PHPUnit\Framework\TestResult;

/**
* Class GenerateCustomerTokenTest
* @package Magento\GraphQl\Customer
* API-functional tests cases for generateCustomerToken mutation
*/
class GenerateCustomerTokenTest extends GraphQlAbstract
{
Expand All @@ -23,87 +21,95 @@ class GenerateCustomerTokenTest extends GraphQlAbstract
*/
public function testGenerateCustomerValidToken()
{
$userName = 'customer@example.com';
$email = 'customer@example.com';
$password = 'password';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$userName}"
password: "{$password}"
) {
token
}
}
MUTATION;
$mutation = $this->getQuery($email, $password);

$response = $this->graphQlMutation($mutation);
$this->assertArrayHasKey('generateCustomerToken', $response);
$this->assertInternalType('array', $response['generateCustomerToken']);
}

/**
* Verify customer with invalid credentials
* Test customer with invalid data.
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
*
* @dataProvider dataProviderInvalidCustomerInfo
* @param string $email
* @param string $password
* @param string $message
*/
public function testGenerateCustomerTokenWithInvalidCredentials()
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message)
{
$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.');
$mutation = $this->getQuery($email, $password);
$this->expectExceptionMessage($message);
$this->graphQlMutation($mutation);
}

/**
* Verify customer with empty email
* Test customer token regeneration.
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testGenerateCustomerTokenWithEmptyEmail()
public function testRegenerateCustomerToken()
{
$email = '';
$password = 'bad-password';
$email = 'customer@example.com';
$password = 'password';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$email}"
password: "{$password}"
) {
token
}
}
MUTATION;
$mutation = $this->getQuery($email, $password);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "email" value.');
$this->graphQlMutation($mutation);
$response1 = $this->graphQlMutation($mutation);
$token1 = $response1['generateCustomerToken']['token'];

$response2 = $this->graphQlMutation($mutation);
$token2 = $response2['generateCustomerToken']['token'];

$this->assertNotEquals($token1, $token2, 'Tokens should not be identical!');
}

/**
* Verify customer with empty password
* @return array
*/
public function testGenerateCustomerTokenWithEmptyPassword()
public function dataProviderInvalidCustomerInfo(): array
{
$email = 'customer@example.com';
$password = '';
return [
'invalid_email' => [
'invalid_email@example.com',
'password',
'The account sign-in was incorrect or your account is disabled temporarily. ' .
'Please wait and try again later.'
],
'empty_email' => [
'',
'password',
'Specify the "email" value.'
],
'invalid_password' => [
'customer@example.com',
'invalid_password',
'The account sign-in was incorrect or your account is disabled temporarily. ' .
'Please wait and try again later.'
],
'empty_password' => [
'customer@example.com',
'',
'Specify the "password" value.'

]
];
}

$mutation
= <<<MUTATION
/**
* @param string $email
* @param string $password
* @return string
*/
private function getQuery(string $email, string $password) : string
{
return <<<MUTATION
mutation {
generateCustomerToken(
email: "{$email}"
Expand All @@ -113,9 +119,5 @@ public function testGenerateCustomerTokenWithEmptyPassword()
}
}
MUTATION;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "password" value.');
$this->graphQlMutation($mutation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test email availability functionality
*/
class IsEmailAvailableTest extends GraphQlAbstract
{
/**
Expand All @@ -31,6 +34,9 @@ public function testEmailNotAvailable()
self::assertFalse($response['isEmailAvailable']['is_email_available']);
}

/**
* Verify email availability
*/
public function testEmailAvailable()
{
$query =
Expand All @@ -47,4 +53,55 @@ public function testEmailAvailable()
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
self::assertTrue($response['isEmailAvailable']['is_email_available']);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage GraphQL response contains errors: Email must be specified
*/
public function testEmailAvailableEmptyValue()
{
$query =
<<<QUERY
{
isEmailAvailable(email: "") {
is_email_available
}
}
QUERY;
$this->graphQlQuery($query);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Field "isEmailAvailable" argument "email" of type "String!" is required
*/
public function testEmailAvailableMissingValue()
{
$query =
<<<QUERY
{
isEmailAvailable {
is_email_available
}
}
QUERY;
$this->graphQlQuery($query);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage GraphQL response contains errors: Email is invalid
*/
public function testEmailAvailableInvalidValue()
{
$query =
<<<QUERY
{
isEmailAvailable(email: "invalid-email") {
is_email_available
}
}
QUERY;
$this->graphQlQuery($query);
}
}
Loading

0 comments on commit dffe94d

Please sign in to comment.