Skip to content

Commit 4289d5b

Browse files
committed
Merge remote-tracking branch 'engcom/ENGCOM-5207-magento-graphql-ce-705' into graphql-develop-prs-fast
2 parents b8f1cc1 + 9358ae0 commit 4289d5b

File tree

3 files changed

+179
-16
lines changed

3 files changed

+179
-16
lines changed

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

Lines changed: 140 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
namespace Magento\GraphQl\Customer;
99

1010
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Model\CustomerAuthUpdate;
1113
use Magento\Customer\Model\CustomerRegistry;
14+
use Magento\Framework\Exception\AuthenticationException;
1215
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1317
use Magento\Integration\Api\CustomerTokenServiceInterface;
1418
use Magento\TestFramework\Helper\Bootstrap;
1519
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -34,11 +38,23 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
3438
*/
3539
private $customerRegistry;
3640

41+
/**
42+
* @var CustomerAuthUpdate
43+
*/
44+
private $customerAuthUpdate;
45+
46+
/**
47+
* @var CustomerRepositoryInterface
48+
*/
49+
private $customerRepository;
50+
3751
protected function setUp()
3852
{
3953
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
4054
$this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);
4155
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
56+
$this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
57+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
4258
}
4359

4460
/**
@@ -47,19 +63,19 @@ protected function setUp()
4763
public function testChangePassword()
4864
{
4965
$customerEmail = 'customer@example.com';
50-
$oldCustomerPassword = 'password';
51-
$newCustomerPassword = 'anotherPassword1';
66+
$currentPassword = 'password';
67+
$newPassword = 'anotherPassword1';
5268

53-
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
54-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
69+
$query = $this->getQuery($currentPassword, $newPassword);
70+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
5571

5672
$response = $this->graphQlMutation($query, [], '', $headerMap);
5773
$this->assertEquals($customerEmail, $response['changeCustomerPassword']['email']);
5874

5975
try {
6076
// registry contains the old password hash so needs to be reset
6177
$this->customerRegistry->removeByEmail($customerEmail);
62-
$this->accountManagement->authenticate($customerEmail, $newCustomerPassword);
78+
$this->accountManagement->authenticate($customerEmail, $newPassword);
6379
} catch (LocalizedException $e) {
6480
$this->fail('Password was not changed: ' . $e->getMessage());
6581
}
@@ -71,7 +87,7 @@ public function testChangePassword()
7187
*/
7288
public function testChangePasswordIfUserIsNotAuthorizedTest()
7389
{
74-
$query = $this->getChangePassQuery('currentpassword', 'newpassword');
90+
$query = $this->getQuery('currentpassword', 'newpassword');
7591
$this->graphQlMutation($query);
7692
}
7793

@@ -81,11 +97,11 @@ public function testChangePasswordIfUserIsNotAuthorizedTest()
8197
public function testChangeWeakPassword()
8298
{
8399
$customerEmail = 'customer@example.com';
84-
$oldCustomerPassword = 'password';
85-
$newCustomerPassword = 'weakpass';
100+
$currentPassword = 'password';
101+
$newPassword = 'weakpass';
86102

87-
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
88-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
103+
$query = $this->getQuery($currentPassword, $newPassword);
104+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
89105

90106
$this->expectException(\Exception::class);
91107
$this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
@@ -101,17 +117,123 @@ public function testChangeWeakPassword()
101117
public function testChangePasswordIfPasswordIsInvalid()
102118
{
103119
$customerEmail = 'customer@example.com';
104-
$oldCustomerPassword = 'password';
105-
$newCustomerPassword = 'anotherPassword1';
106-
$incorrectPassword = 'password-incorrect';
120+
$currentPassword = 'password';
121+
$newPassword = 'anotherPassword1';
122+
$incorrectCurrentPassword = 'password-incorrect';
123+
124+
$query = $this->getQuery($incorrectCurrentPassword, $newPassword);
125+
126+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
127+
$this->graphQlMutation($query, [], '', $headerMap);
128+
}
129+
130+
/**
131+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
132+
* @expectedException \Exception
133+
* @expectedExceptionMessage Specify the "currentPassword" value.
134+
*/
135+
public function testChangePasswordIfCurrentPasswordIsEmpty()
136+
{
137+
$customerEmail = 'customer@example.com';
138+
$currentPassword = 'password';
139+
$newPassword = 'anotherPassword1';
140+
$incorrectCurrentPassword = '';
141+
142+
$query = $this->getQuery($incorrectCurrentPassword, $newPassword);
143+
144+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
145+
$this->graphQlMutation($query, [], '', $headerMap);
146+
}
147+
148+
/**
149+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
150+
* @expectedException \Exception
151+
* @expectedExceptionMessage Specify the "newPassword" value.
152+
*/
153+
public function testChangePasswordIfNewPasswordIsEmpty()
154+
{
155+
$customerEmail = 'customer@example.com';
156+
$currentPassword = 'password';
157+
$incorrectNewPassword = '';
107158

108-
$query = $this->getChangePassQuery($incorrectPassword, $newCustomerPassword);
159+
$query = $this->getQuery($currentPassword, $incorrectNewPassword);
109160

110-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
161+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
111162
$this->graphQlMutation($query, [], '', $headerMap);
112163
}
113164

114-
private function getChangePassQuery($currentPassword, $newPassword)
165+
/**
166+
* @magentoApiDataFixture Magento/GraphQl/Customer/_files/enable_customer_account_confirmation.php
167+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
168+
* @expectedException \Exception
169+
* @expectedExceptionMessage This account isn't confirmed. Verify and try again.
170+
*/
171+
public function testChangePasswordIfAccountIsNotConfirmed()
172+
{
173+
$customerEmail = 'customer@example.com';
174+
$currentPassword = 'password';
175+
$newPassword = 'anotherPassword1';
176+
177+
/* get header map before setting the customer unconfirmed */
178+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
179+
180+
$this->setCustomerConfirmation(1);
181+
$query = $this->getQuery($currentPassword, $newPassword);
182+
183+
$this->graphQlMutation($query, [], '', $headerMap);
184+
}
185+
186+
/**
187+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
188+
* @expectedException \Exception
189+
* @expectedExceptionMessage The account is locked.
190+
*/
191+
public function testChangePasswordIfCustomerIsLocked()
192+
{
193+
$customerEmail = 'customer@example.com';
194+
$currentPassword = 'password';
195+
$newPassword = 'anotherPassword1';
196+
197+
$this->lockCustomer(1);
198+
$query = $this->getQuery($currentPassword, $newPassword);
199+
200+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
201+
$this->graphQlMutation($query, [], '', $headerMap);
202+
}
203+
204+
/**
205+
* @param int $customerId
206+
*
207+
* @return void
208+
* @throws NoSuchEntityException
209+
*/
210+
private function lockCustomer(int $customerId): void
211+
{
212+
$customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
213+
$customerSecure->setLockExpires('2030-12-31 00:00:00');
214+
$this->customerAuthUpdate->saveAuth($customerId);
215+
}
216+
217+
/**
218+
* @param int $customerId
219+
*
220+
* @return void
221+
* @throws LocalizedException
222+
*/
223+
private function setCustomerConfirmation(int $customerId): void
224+
{
225+
$customer = $this->customerRepository->getById($customerId);
226+
$customer->setConfirmation('d5a21f15bd4cc21bd1b21ef6d9989a38');
227+
$this->customerRepository->save($customer);
228+
}
229+
230+
/**
231+
* @param $currentPassword
232+
* @param $newPassword
233+
*
234+
* @return string
235+
*/
236+
private function getQuery($currentPassword, $newPassword)
115237
{
116238
$query = <<<QUERY
117239
mutation {
@@ -133,7 +255,9 @@ private function getChangePassQuery($currentPassword, $newPassword)
133255
/**
134256
* @param string $email
135257
* @param string $password
258+
*
136259
* @return array
260+
* @throws AuthenticationException
137261
*/
138262
private function getCustomerAuthHeaders(string $email, string $password): array
139263
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
7+
declare(strict_types=1);
8+
9+
use Magento\Customer\Model\AccountConfirmation;
10+
use Magento\Framework\App\Config\Storage\Writer;
11+
use Magento\Framework\App\Config\Storage\WriterInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
/** @var Writer $configWriter */
17+
$configWriter = $objectManager->get(WriterInterface::class);
18+
19+
$configWriter->save(AccountConfirmation::XML_PATH_IS_CONFIRM, 1);
20+
21+
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
22+
$scopeConfig->clean();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// TODO: Should be removed in scope of https://github.com/magento/graphql-ce/issues/167
7+
declare(strict_types=1);
8+
9+
use Magento\Framework\App\Config\Storage\Writer;
10+
use Magento\Framework\App\Config\Storage\WriterInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var Writer $configWriter */
15+
$configWriter = $objectManager->create(WriterInterface::class);
16+
17+
$configWriter->delete('customer/create_account/confirm');

0 commit comments

Comments
 (0)