Skip to content

Commit a90b493

Browse files
author
Matthew O'Loughlin
committed
GraphQl-673: Add email validity check on isEmailAvailable query, and add test coverage for negative cases
1 parent 02bca98 commit a90b493

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/IsEmailAvailable.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Validator\EmailAddress as EmailValidator;
1617

1718
/**
1819
* Is Customer Email Available
@@ -24,13 +25,21 @@ class IsEmailAvailable implements ResolverInterface
2425
*/
2526
private $accountManagement;
2627

28+
/**
29+
* @var EmailValidator
30+
*/
31+
private $emailValidator;
32+
2733
/**
2834
* @param AccountManagementInterface $accountManagement
35+
* @param EmailValidator $emailValidator
2936
*/
3037
public function __construct(
31-
AccountManagementInterface $accountManagement
38+
AccountManagementInterface $accountManagement,
39+
EmailValidator $emailValidator
3240
) {
3341
$this->accountManagement = $accountManagement;
42+
$this->emailValidator = $emailValidator;
3443
}
3544

3645
/**
@@ -44,7 +53,11 @@ public function resolve(
4453
array $args = null
4554
) {
4655
if (!isset($args['email']) || empty($args['email'])) {
47-
throw new GraphQlInputException(__('"Email should be specified'));
56+
throw new GraphQlInputException(__('Email should be specified'));
57+
}
58+
59+
if (!$this->emailValidator->isValid($args['email'])) {
60+
throw new GraphQlInputException(__('Email is invalid'));
4861
}
4962

5063
try {

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/IsEmailAvailableTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,55 @@ public function testEmailAvailable()
4747
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
4848
self::assertTrue($response['isEmailAvailable']['is_email_available']);
4949
}
50+
51+
/**
52+
* @expectedException \Exception
53+
* @expectedExceptionMessage GraphQL response contains errors: Email should be specified
54+
*/
55+
public function testEmailAvailableEmptyValue()
56+
{
57+
$query =
58+
<<<QUERY
59+
{
60+
isEmailAvailable(email: "") {
61+
is_email_available
62+
}
63+
}
64+
QUERY;
65+
$response = $this->graphQlQuery($query);
66+
}
67+
68+
/**
69+
* @expectedException \Exception
70+
* @expectedExceptionMessage GraphQL response contains errors: Field "isEmailAvailable" argument "email" of type "String!" is required but not provided.
71+
*/
72+
public function testEmailAvailableMissingValue()
73+
{
74+
$query =
75+
<<<QUERY
76+
{
77+
isEmailAvailable {
78+
is_email_available
79+
}
80+
}
81+
QUERY;
82+
$response = $this->graphQlQuery($query);
83+
}
84+
85+
/**
86+
* @expectedException \Exception
87+
* @expectedExceptionMessage GraphQL response contains errors: Email is invalid
88+
*/
89+
public function testEmailAvailableInvalidValue()
90+
{
91+
$query =
92+
<<<QUERY
93+
{
94+
isEmailAvailable(email: "invalid-email") {
95+
is_email_available
96+
}
97+
}
98+
QUERY;
99+
$response = $this->graphQlQuery($query);
100+
}
50101
}

0 commit comments

Comments
 (0)