Skip to content

Commit 71364e4

Browse files
committed
GraphQL-672: [Test coverage] Change customer password
1 parent f0026be commit 71364e4

File tree

3 files changed

+99
-36
lines changed

3 files changed

+99
-36
lines changed

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

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\GraphQl\Customer;
99

1010
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
1112
use Magento\Customer\Model\CustomerAuthUpdate;
1213
use Magento\Customer\Model\CustomerRegistry;
1314
use Magento\Framework\Exception\AuthenticationException;
@@ -42,12 +43,18 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
4243
*/
4344
private $customerAuthUpdate;
4445

46+
/**
47+
* @var CustomerRepositoryInterface
48+
*/
49+
private $customerRepository;
50+
4551
protected function setUp()
4652
{
4753
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
4854
$this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);
4955
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
5056
$this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
57+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
5158
}
5259

5360
/**
@@ -56,19 +63,19 @@ protected function setUp()
5663
public function testChangePassword()
5764
{
5865
$customerEmail = '[email protected]';
59-
$oldCustomerPassword = 'password';
60-
$newCustomerPassword = 'anotherPassword1';
66+
$currentPassword = 'password';
67+
$newPassword = 'anotherPassword1';
6168

62-
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
63-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
69+
$query = $this->getQuery($currentPassword, $newPassword);
70+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
6471

6572
$response = $this->graphQlMutation($query, [], '', $headerMap);
66-
$this->assertEquals($customerEmail, $response['changeCustomerPassword']['email']);
73+
$this->assertEquals($customerEmail, $response['changePassword']['email']);
6774

6875
try {
6976
// registry contains the old password hash so needs to be reset
7077
$this->customerRegistry->removeByEmail($customerEmail);
71-
$this->accountManagement->authenticate($customerEmail, $newCustomerPassword);
78+
$this->accountManagement->authenticate($customerEmail, $newPassword);
7279
} catch (LocalizedException $e) {
7380
$this->fail('Password was not changed: ' . $e->getMessage());
7481
}
@@ -80,7 +87,7 @@ public function testChangePassword()
8087
*/
8188
public function testChangePasswordIfUserIsNotAuthorizedTest()
8289
{
83-
$query = $this->getChangePassQuery('currentpassword', 'newpassword');
90+
$query = $this->getQuery('currentpassword', 'newpassword');
8491
$this->graphQlMutation($query);
8592
}
8693

@@ -90,11 +97,11 @@ public function testChangePasswordIfUserIsNotAuthorizedTest()
9097
public function testChangeWeakPassword()
9198
{
9299
$customerEmail = '[email protected]';
93-
$oldCustomerPassword = 'password';
94-
$newCustomerPassword = 'weakpass';
100+
$currentPassword = 'password';
101+
$newPassword = 'weakpass';
95102

96-
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
97-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
103+
$query = $this->getQuery($currentPassword, $newPassword);
104+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
98105

99106
$this->expectException(\Exception::class);
100107
$this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
@@ -110,13 +117,13 @@ public function testChangeWeakPassword()
110117
public function testChangePasswordIfPasswordIsInvalid()
111118
{
112119
$customerEmail = '[email protected]';
113-
$oldCustomerPassword = 'password';
114-
$newCustomerPassword = 'anotherPassword1';
115-
$incorrectPassword = 'password-incorrect';
120+
$currentPassword = 'password';
121+
$newPassword = 'anotherPassword1';
122+
$incorrectCurrentPassword = 'password-incorrect';
116123

117-
$query = $this->getChangePassQuery($incorrectPassword, $newCustomerPassword);
124+
$query = $this->getQuery($incorrectCurrentPassword, $newPassword);
118125

119-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
126+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
120127
$this->graphQlMutation($query, [], '', $headerMap);
121128
}
122129

@@ -128,13 +135,13 @@ public function testChangePasswordIfPasswordIsInvalid()
128135
public function testChangePasswordIfCurrentPasswordIsEmpty()
129136
{
130137
$customerEmail = '[email protected]';
131-
$oldCustomerPassword = 'password';
132-
$newCustomerPassword = 'anotherPassword1';
133-
$currentCustomerPassword = '';
138+
$currentPassword = 'password';
139+
$newPassword = 'anotherPassword1';
140+
$incorrectCurrentPassword = '';
134141

135-
$query = $this->getChangePassQuery($currentCustomerPassword, $newCustomerPassword);
142+
$query = $this->getQuery($incorrectCurrentPassword, $newPassword);
136143

137-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
144+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
138145
$this->graphQlMutation($query, [], '', $headerMap);
139146
}
140147

@@ -146,29 +153,33 @@ public function testChangePasswordIfCurrentPasswordIsEmpty()
146153
public function testChangePasswordIfNewPasswordIsEmpty()
147154
{
148155
$customerEmail = '[email protected]';
149-
$currentCustomerPassword = 'password';
150-
$newCustomerPassword = '';
156+
$currentPassword = 'password';
157+
$incorrectNewPassword = '';
151158

152-
$query = $this->getChangePassQuery($currentCustomerPassword, $newCustomerPassword);
159+
$query = $this->getQuery($currentPassword, $incorrectNewPassword);
153160

154-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentCustomerPassword);
161+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
155162
$this->graphQlMutation($query, [], '', $headerMap);
156163
}
157164

158165
/**
166+
* @magentoApiDataFixture Magento/GraphQl/Customer/_files/enable_customer_account_confirmation.php
159167
* @magentoApiDataFixture Magento/Customer/_files/customer.php
160168
* @expectedException \Exception
161-
* @expectedExceptionMessage Account is not confirmed.
169+
* @expectedExceptionMessage This account isn't confirmed. Verify and try again.
162170
*/
163-
public function testChangeCustomerAddressIfAccountIsNotConfirmed()
171+
public function testChangePasswordIfAccountIsNotConfirmed()
164172
{
165173
$customerEmail = '[email protected]';
166-
$currentCustomerPassword = 'password';
167-
$newCustomerPassword = '';
174+
$currentPassword = 'password';
175+
$newPassword = 'anotherPassword1';
168176

169-
$query = $this->getChangePassQuery($currentCustomerPassword, $newCustomerPassword);
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);
170182

171-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentCustomerPassword);
172183
$this->graphQlMutation($query, [], '', $headerMap);
173184
}
174185

@@ -180,13 +191,13 @@ public function testChangeCustomerAddressIfAccountIsNotConfirmed()
180191
public function testChangePasswordIfCustomerIsLocked()
181192
{
182193
$customerEmail = '[email protected]';
183-
$currentCustomerPassword = 'password';
184-
$newCustomerPassword = 'anotherPassword1';
194+
$currentPassword = 'password';
195+
$newPassword = 'anotherPassword1';
185196

186197
$this->lockCustomer(1);
187-
$query = $this->getChangePassQuery($currentCustomerPassword, $newCustomerPassword);
198+
$query = $this->getQuery($currentPassword, $newPassword);
188199

189-
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentCustomerPassword);
200+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
190201
$this->graphQlMutation($query, [], '', $headerMap);
191202
}
192203

@@ -203,13 +214,26 @@ private function lockCustomer(int $customerId): void
203214
$this->customerAuthUpdate->saveAuth($customerId);
204215
}
205216

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+
206230
/**
207231
* @param $currentPassword
208232
* @param $newPassword
209233
*
210234
* @return string
211235
*/
212-
private function getChangePassQuery($currentPassword, $newPassword)
236+
private function getQuery($currentPassword, $newPassword)
213237
{
214238
$query = <<<QUERY
215239
mutation {
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)