Skip to content

Commit e08fadc

Browse files
committed
add confirm password
1 parent 7d53feb commit e08fadc

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

src/users/dto/update-user-password.dto.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export class UpdateUserPasswordDto {
1515
@IsString()
1616
@IsNotEmpty()
1717
newPassword: string;
18+
19+
/**
20+
* Confirmation of the new password. This should match the new password.
21+
*/
22+
@IsString()
23+
@IsNotEmpty()
24+
confirmPassword: string;
1825
}
1926

2027
export class AdminUpdateUserPasswordDto {
@@ -24,4 +31,11 @@ export class AdminUpdateUserPasswordDto {
2431
@IsString()
2532
@IsNotEmpty()
2633
newPassword: string;
34+
35+
/**
36+
* Confirmation of the new password. This should match the new password.
37+
*/
38+
@IsString()
39+
@IsNotEmpty()
40+
confirmPassword: string;
2741
}

src/users/users.controller.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ export class UsersController {
127127
user._id,
128128
);
129129

130+
if (
131+
updateUserPasswordDto.newPassword !==
132+
updateUserPasswordDto.confirmPassword
133+
) {
134+
throw new BadRequestException(
135+
"New password and confirmation password do not match",
136+
);
137+
}
138+
130139
const validUser = await this.authService.validateUser(
131140
user.username,
132141
updateUserPasswordDto.currentPassword,
@@ -281,6 +290,14 @@ export class UsersController {
281290
[Action.UserUpdateAny],
282291
user._id,
283292
);
293+
if (
294+
updateUserPasswordDto.newPassword !==
295+
updateUserPasswordDto.confirmPassword
296+
) {
297+
throw new BadRequestException(
298+
"New password and confirmation password do not match",
299+
);
300+
}
284301

285302
const targetUser = await this.usersService.findById(id).catch((err) => {
286303
throw new BadRequestException(err.message);

test/Users.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ describe("2370: Change password", () => {
139139
.send({
140140
currentPassword: "wrongOldPassword",
141141
newPassword: TestData.Accounts["user1"]["password"],
142+
confirmPassword: TestData.Accounts["user1"]["password"],
142143
})
143144
.set("Accept", "application/json")
144145
.set({ Authorization: `Bearer ${accessTokenUser1}` })
@@ -151,12 +152,32 @@ describe("2370: Change password", () => {
151152
});
152153
});
153154

154-
it("0020: should change own password successfully", async () => {
155+
it("0020: should fail when new password and confirmation password do not match", async () => {
156+
return request(appUrl)
157+
.post("/api/v3/users/password")
158+
.send({
159+
currentPassword: "wrongOldPassword",
160+
newPassword: TestData.Accounts["user1"]["password"],
161+
confirmPassword: "wrongConfirmPassword",
162+
})
163+
.set("Accept", "application/json")
164+
.set({ Authorization: `Bearer ${accessTokenUser1}` })
165+
.expect(TestData.BadRequestStatusCode)
166+
.then((res) => {
167+
res.body.should.have.property(
168+
"message",
169+
"New password and confirmation password do not match",
170+
);
171+
});
172+
});
173+
174+
it("0030: should change own password successfully", async () => {
155175
return request(appUrl)
156176
.post("/api/v3/users/password")
157177
.send({
158178
currentPassword: TestData.Accounts["user1"]["password"],
159179
newPassword: "testpassword",
180+
confirmPassword: "testpassword",
160181
})
161182
.set("Accept", "application/json")
162183
.set({ Authorization: `Bearer ${accessTokenUser1}` })
@@ -168,12 +189,14 @@ describe("2370: Change password", () => {
168189
);
169190
});
170191
});
171-
it("0030: oidc user should fail to change password", async () => {
192+
193+
it("0040: oidc user should fail to change password", async () => {
172194
return request(appUrl)
173195
.post("/api/v3/users/password")
174196
.send({
175197
currentPassword: TestData.Accounts["user2"]["password"],
176198
newPassword: "testpassword",
199+
confirmPassword: "testpassword",
177200
})
178201
.set("Accept", "application/json")
179202
.set({ Authorization: `Bearer ${accessTokenUser2}` })
@@ -186,11 +209,30 @@ describe("2370: Change password", () => {
186209
});
187210
});
188211

189-
it("0040: admin should be able to change user password", async () => {
212+
it("0050: admin should fail to change password for user when new and confirmation passwords do not match", async () => {
213+
return request(appUrl)
214+
.patch(`/api/v3/users/${userIdUser1}/password`)
215+
.send({
216+
newPassword: TestData.Accounts["user1"]["password"],
217+
confirmPassword: "wrongConfirmPassword",
218+
})
219+
.set("Accept", "application/json")
220+
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
221+
.expect(TestData.BadRequestStatusCode)
222+
.then((res) => {
223+
res.body.should.have.property(
224+
"message",
225+
"New password and confirmation password do not match",
226+
);
227+
});
228+
});
229+
230+
it("0060: admin should be able to change user password", async () => {
190231
return request(appUrl)
191232
.patch(`/api/v3/users/${userIdUser1}/password`)
192233
.send({
193234
newPassword: TestData.Accounts["user1"]["password"],
235+
confirmPassword: TestData.Accounts["user1"]["password"],
194236
})
195237
.set("Accept", "application/json")
196238
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
@@ -203,11 +245,12 @@ describe("2370: Change password", () => {
203245
});
204246
});
205247

206-
it("0050: admin should fail to change oidc user password", async () => {
248+
it("0070: admin should fail to change oidc user password", async () => {
207249
return request(appUrl)
208250
.patch(`/api/v3/users/${userIdUser2}/password`)
209251
.send({
210252
newPassword: "testpassword",
253+
confirmPassword: "testpassword",
211254
})
212255
.set("Accept", "application/json")
213256
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })

0 commit comments

Comments
 (0)