Skip to content

Commit e567000

Browse files
committed
Added scenario for locked customer and fixed some minors issues
1 parent 1e6c6e0 commit e567000

File tree

4 files changed

+140
-41
lines changed

4 files changed

+140
-41
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

1010
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
1112
use Magento\Customer\Model\AccountManagement;
13+
use Magento\Customer\Model\AuthenticationInterface;
1214
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Framework\GraphQl\Config\Element\Field;
1416
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -23,6 +25,16 @@
2325
*/
2426
class RequestPasswordResetEmail implements ResolverInterface
2527
{
28+
/**
29+
* @var AuthenticationInterface
30+
*/
31+
private $authentication;
32+
33+
/**
34+
* @var CustomerRepositoryInterface
35+
*/
36+
private $customerRepository;
37+
2638
/**
2739
* @var AccountManagementInterface
2840
*/
@@ -36,13 +48,19 @@ class RequestPasswordResetEmail implements ResolverInterface
3648
/**
3749
* RequestPasswordResetEmail constructor.
3850
*
39-
* @param AccountManagementInterface $customerAccountManagement
40-
* @param EmailValidator $emailValidator
51+
* @param AuthenticationInterface $authentication
52+
* @param CustomerRepositoryInterface $customerRepository
53+
* @param AccountManagementInterface $customerAccountManagement
54+
* @param EmailValidator $emailValidator
4155
*/
4256
public function __construct(
57+
AuthenticationInterface $authentication,
58+
CustomerRepositoryInterface $customerRepository,
4359
AccountManagementInterface $customerAccountManagement,
4460
EmailValidator $emailValidator
4561
) {
62+
$this->authentication = $authentication;
63+
$this->customerRepository = $customerRepository;
4664
$this->customerAccountManagement = $customerAccountManagement;
4765
$this->emailValidator = $emailValidator;
4866
}
@@ -76,13 +94,23 @@ public function resolve(
7694
throw new GraphQlInputException(__('Email is invalid'));
7795
}
7896

97+
try {
98+
$customer = $this->customerRepository->get($args['email']);
99+
} catch (LocalizedException $e) {
100+
throw new GraphQlInputException(__('Cannot reset customer password'), $e);
101+
}
102+
103+
if (true === $this->authentication->isLocked($customer->getId())) {
104+
throw new GraphQlInputException(__("The current customer isn't authorized"));
105+
}
106+
79107
try {
80108
return $this->customerAccountManagement->initiatePasswordReset(
81109
$args['email'],
82110
AccountManagement::EMAIL_RESET
83111
);
84112
} catch (LocalizedException $e) {
85-
throw new GraphQlInputException(__($e->getMessage()), $e);
113+
throw new GraphQlInputException(__("Cannot reset customer password"), $e);
86114
}
87115
}
88116
}

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

1010
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Model\AuthenticationInterface;
1113
use Magento\Framework\Exception\LocalizedException;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
1315
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -17,6 +19,9 @@
1719
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1820
use Magento\Framework\Validator\EmailAddress as EmailValidator;
1921

22+
/**
23+
* Class Resolver for ResetPassword
24+
*/
2025
class ResetPassword implements ResolverInterface
2126
{
2227
/**
@@ -30,15 +35,31 @@ class ResetPassword implements ResolverInterface
3035
private $emailValidator;
3136

3237
/**
33-
* RequestPasswordResetEmail constructor.
38+
* @var AuthenticationInterface
39+
*/
40+
private $authentication;
41+
42+
/**
43+
* @var CustomerRepositoryInterface
44+
*/
45+
private $customerRepository;
46+
47+
/**
48+
* ResetPassword constructor.
3449
*
35-
* @param AccountManagementInterface $customerAccountManagement
36-
* @param EmailValidator $emailValidator
50+
* @param AuthenticationInterface $authentication
51+
* @param CustomerRepositoryInterface $customerRepository
52+
* @param AccountManagementInterface $customerAccountManagement
53+
* @param EmailValidator $emailValidator
3754
*/
3855
public function __construct(
56+
AuthenticationInterface $authentication,
57+
CustomerRepositoryInterface $customerRepository,
3958
AccountManagementInterface $customerAccountManagement,
4059
EmailValidator $emailValidator
4160
) {
61+
$this->authentication = $authentication;
62+
$this->customerRepository = $customerRepository;
4263
$this->customerAccountManagement = $customerAccountManagement;
4364
$this->emailValidator = $emailValidator;
4465
}
@@ -80,14 +101,24 @@ public function resolve(
80101
throw new GraphQlInputException(__('newPassword must be specified'));
81102
}
82103

104+
try {
105+
$customer = $this->customerRepository->get($args['email']);
106+
} catch (LocalizedException $e) {
107+
throw new GraphQlInputException(__('Cannot set customer password'), $e);
108+
}
109+
110+
if (true === $this->authentication->isLocked($customer->getId())) {
111+
throw new GraphQlInputException(__("The current customer isn't authorized"));
112+
}
113+
83114
try {
84115
return $this->customerAccountManagement->resetPassword(
85116
$args['email'],
86117
$args['resetPasswordToken'],
87118
$args['newPassword']
88119
);
89120
} catch (LocalizedException $e) {
90-
throw new GraphQlInputException(__($e->getMessage()), $e);
121+
throw new GraphQlInputException(__('Cannot set customer password'), $e);
91122
}
92123
}
93124
}

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@
77

88
namespace Magento\GraphQl\Customer;
99

10+
use Magento\TestFramework\Helper\Bootstrap;
1011
use Magento\TestFramework\TestCase\GraphQlAbstract;
1112

1213
class RequestPasswordResetEmailTest extends GraphQlAbstract
1314
{
15+
/**
16+
* @var LockCustomer
17+
*/
18+
private $lockCustomer;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->lockCustomer = Bootstrap::getObjectManager()->get(LockCustomer::class);
25+
}
1426
/**
1527
* @magentoApiDataFixture Magento/Customer/_files/customer.php
1628
*/
@@ -30,6 +42,9 @@ public function testCustomerAccountWithEmailAvailable()
3042

3143
/**
3244
* Check if customer account is not available
45+
*
46+
* @expectedException \Exception
47+
* @expectedExceptionMessage Cannot reset customer password
3348
*/
3449
public function testCustomerAccountWithEmailNotAvailable()
3550
{
@@ -39,12 +54,14 @@ public function testCustomerAccountWithEmailNotAvailable()
3954
requestPasswordResetEmail(email: "[email protected]")
4055
}
4156
QUERY;
42-
$this->assertMessage('No such entity with email = [email protected], websiteId = 1');
4357
$this->graphQlMutation($query);
4458
}
4559

4660
/**
4761
* Check if email value empty
62+
*
63+
* @expectedException \Exception
64+
* @expectedExceptionMessage Email must be specified
4865
*/
4966
public function testEmailAvailableEmptyValue()
5067
{
@@ -53,12 +70,14 @@ public function testEmailAvailableEmptyValue()
5370
requestPasswordResetEmail(email: "")
5471
}
5572
QUERY;
56-
$this->assertMessage('Email must be specified');
5773
$this->graphQlMutation($query);
5874
}
5975

6076
/**
6177
* Check if email is invalid
78+
*
79+
* @expectedException \Exception
80+
* @expectedExceptionMessage Email is invalid
6281
*/
6382
public function testEmailAvailableInvalidValue()
6483
{
@@ -67,35 +86,27 @@ public function testEmailAvailableInvalidValue()
6786
requestPasswordResetEmail(email: "invalid-email")
6887
}
6988
QUERY;
70-
$this->assertMessage('Email is invalid');
7189
$this->graphQlMutation($query);
7290
}
7391

7492
/**
75-
* Check if email contain right type
93+
* Check if email was sent for lock customer
94+
*
95+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
96+
*
97+
* @expectedException \Exception
98+
* @expectedExceptionMessage The current customer isn't authorized
7699
*/
77-
public function testEmailAvailableTypeValue()
100+
public function testRequestPasswordResetEmailForLockCustomer()
78101
{
79-
$query = <<<QUERY
102+
$this->lockCustomer->execute(1);
103+
$query =
104+
<<<QUERY
80105
mutation {
81-
requestPasswordResetEmail (email: 12345)
106+
requestPasswordResetEmail(email: "[email protected]")
82107
}
83108
QUERY;
84-
self::expectException(\Exception::class);
85-
self::expectExceptionMessage(
86-
'GraphQL response contains errors: Field "requestPasswordResetEmail" argument "email" requires type String!'
87-
);
88-
$this->graphQlMutation($query);
89-
}
90109

91-
/**
92-
* Checks Exception and ExceptionMessages
93-
*
94-
* @param $message
95-
*/
96-
private function assertMessage($message)
97-
{
98-
self::expectException(\Exception::class);
99-
self::expectExceptionMessage("GraphQL response contains errors: {$message}");
110+
$this->graphQlMutation($query);
100111
}
101112
}

0 commit comments

Comments
 (0)