Skip to content

Commit 43d9268

Browse files
author
Wang, Tao
committed
feat: skip password check when user has no password
1 parent 01df0d6 commit 43d9268

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/user/user.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ export class UserController {
311311
});
312312
}
313313

314-
if (dto.oldPassword && !this.userService.checkPassword(user.password, dto.oldPassword)) {
314+
if (
315+
user.password &&
316+
dto.oldPassword &&
317+
!this.userService.checkPassword(user.password, dto.oldPassword)
318+
) {
315319
throw new BadRequestException({
316320
code: ErrorCodes.WRONG_OLD_PASSWORD,
317321
message: 'Old password not match.',

test/user.e2e-spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,37 @@ describe('User crud (e2e)', () => {
202202
.expect(400);
203203
});
204204

205+
it('Update password for user without password', async () => {
206+
const userDoc = mockUser();
207+
await namespaceService.upsertByKey(userDoc.ns, {
208+
name: faker.company.name(),
209+
});
210+
211+
const user = await userService.create({ ...userDoc, password: undefined });
212+
213+
// should set password successfully when no old password exists
214+
await request(app.getHttpServer())
215+
.post(`/users/${user.id}/@updatePassword`)
216+
.send({ newPassword: '^tR123456' })
217+
.set('Content-Type', 'application/json')
218+
.set('x-api-key', auth.apiKey)
219+
.set('Accept', 'application/json')
220+
.expect(204);
221+
222+
// should skip old password verification when user has no password
223+
const noPasswordUser = await userService.create({
224+
...mockUser(),
225+
password: undefined,
226+
});
227+
await request(app.getHttpServer())
228+
.post(`/users/${noPasswordUser.id}/@updatePassword`)
229+
.send({ oldPassword: 'anything1@Aa', newPassword: '^tR123456' })
230+
.set('Content-Type', 'application/json')
231+
.set('x-api-key', auth.apiKey)
232+
.set('Accept', 'application/json')
233+
.expect(204);
234+
});
235+
205236
it('Upsert user by id', async () => {
206237
const userId = `import-${nanoid(10)}`;
207238
const userDoc = mockUser();

0 commit comments

Comments
 (0)