Skip to content

Commit e6043eb

Browse files
Rename global mutation, add test case for incorrect 'current' password
1 parent e745e91 commit e6043eb

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Query {
66
}
77

88
type Mutation {
9-
changePassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\ChangePassword") @doc(description:"Changes password for logged in customer")
9+
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\ChangePassword") @doc(description:"Changes password for logged in customer")
1010
}
1111

1212
type Customer @doc(description: "Customer defines the customer name and address and other details") {

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

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,11 @@ public function testCustomerChangeValidPassword()
3434
$oldCustomerPassword = 'password';
3535
$newCustomerPassword = 'anotherPassword1';
3636

37-
$query = <<<QUERY
38-
mutation {
39-
changePassword(
40-
currentPassword: "$oldCustomerPassword",
41-
newPassword: "$newCustomerPassword"
42-
) {
43-
id
44-
email
45-
firstname
46-
lastname
47-
}
48-
}
49-
QUERY;
37+
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
38+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
5039

51-
/** @var CustomerTokenServiceInterface $customerTokenService */
52-
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
53-
$customerToken = $customerTokenService->createCustomerAccessToken($customerEmail, $oldCustomerPassword);
54-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
5540
$response = $this->graphQlQuery($query, [], '', $headerMap);
56-
$this->assertEquals($customerEmail, $response['changePassword']['email']);
41+
$this->assertEquals($customerEmail, $response['changeCustomerPassword']['email']);
5742

5843
try {
5944
// registry contains the old password hash so needs to be reset
@@ -67,22 +52,12 @@ public function testCustomerChangeValidPassword()
6752

6853
public function testGuestUserCannotChangePassword()
6954
{
70-
$query = <<<QUERY
71-
mutation {
72-
changePassword(
73-
currentPassword: "currentpassword",
74-
newPassword: "newpassword"
75-
) {
76-
id
77-
email
78-
firstname
79-
lastname
80-
}
81-
}
82-
QUERY;
55+
$query = $this->getChangePassQuery('currentpassword', 'newpassword');
8356
$this->expectException(\Exception::class);
84-
$this->expectExceptionMessage('GraphQL response contains errors: Current customer' . ' ' .
85-
'does not have access to the resource "customer"');
57+
$this->expectExceptionMessage(
58+
'GraphQL response contains errors: Current customer' . ' ' .
59+
'does not have access to the resource "customer"'
60+
);
8661
$this->graphQlQuery($query);
8762
}
8863

@@ -95,11 +70,44 @@ public function testChangeWeakPassword()
9570
$oldCustomerPassword = 'password';
9671
$newCustomerPassword = 'weakpass';
9772

73+
$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
74+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
75+
76+
$this->expectException(\Exception::class);
77+
$this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
78+
79+
$this->graphQlQuery($query, [], '', $headerMap);
80+
}
81+
82+
/**
83+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
84+
*/
85+
public function testCannotChangeWithIncorrectPassword()
86+
{
87+
$customerEmail = 'customer@example.com';
88+
$oldCustomerPassword = 'password';
89+
$newCustomerPassword = 'anotherPassword1';
90+
$incorrectPassword = 'password-incorrect';
91+
92+
$query = $this->getChangePassQuery($incorrectPassword, $newCustomerPassword);
93+
94+
// acquire authentication with correct password
95+
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
96+
97+
$this->expectException(\Exception::class);
98+
$this->expectExceptionMessageRegExp('/The password doesn\'t match this account. Verify the password.*/');
99+
100+
// but try to change with incorrect 'old' password
101+
$this->graphQlQuery($query, [], '', $headerMap);
102+
}
103+
104+
private function getChangePassQuery($currentPassword, $newPassword)
105+
{
98106
$query = <<<QUERY
99107
mutation {
100-
changePassword(
101-
currentPassword: "$oldCustomerPassword",
102-
newPassword: "$newCustomerPassword"
108+
changeCustomerPassword(
109+
currentPassword: "$currentPassword",
110+
newPassword: "$newPassword"
103111
) {
104112
id
105113
email
@@ -109,15 +117,15 @@ public function testChangeWeakPassword()
109117
}
110118
QUERY;
111119

120+
return $query;
121+
}
122+
123+
private function getCustomerAuthHeaders($customerEmail, $oldCustomerPassword)
124+
{
112125
/** @var CustomerTokenServiceInterface $customerTokenService */
113126
$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
114127
$customerToken = $customerTokenService->createCustomerAccessToken($customerEmail, $oldCustomerPassword);
115-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
116-
117-
$this->expectException(\Exception::class);
118-
$this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
119-
120-
$this->graphQlQuery($query, [], '', $headerMap);
128+
return ['Authorization' => 'Bearer ' . $customerToken];
121129
}
122130

123131
protected function setUp()

0 commit comments

Comments
 (0)