feat(api): password change endpoint and source device id on uploads
P3 Task 3 契约增补批次(单一事实来源 openapi.yaml + cn 实现 + e2e):
- POST /v1/auth/password-change(operationId changePassword,需求 4.1 的
P1 契约缺口补齐):Bearer + {oldPassword 1-128, newPassword 8-128} → 204;
旧密码错误 401 AUTH_INVALID_CREDENTIALS;成功后事务内更新 argon2 哈希、
撤销该用户除当前会话外的全部其他会话(当前会话保留,本人不下线)、写
审计 auth.password_change。
- sourceDeviceId(A19 设备来源语义):UploadInitRequest 可选 8-64 位设备
标识;upload_sessions 落列(VARCHAR(64) utf8mb4_0900_bin,可空,存量
行 NULL 兼容);GET /uploads 条目回显;幂等重放不比对(first-writer,
首次落库值不随重放漂移)。
- e2e:auth 增 4 例(未认证 401 / 新密码过短 400 / 旧密码错误 401 且会话
无副作用 / 成功后他端会话撤销当前保留 + 新旧密码登录裁决 + 审计);
uploads 增 1 例(携带落库回显、换设备重放保留首值、缺省 null、长度
越界 400)。api e2e 179 → 184;contracts gen/redocly/单测、worker、
sync 门全绿。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -91,6 +91,10 @@ describe('auth (e2e)', () => {
|
||||
await prisma.users.create({
|
||||
data: { account: 'gina', passwordHash: hash, displayName: 'Gina' },
|
||||
});
|
||||
// password-change 专用账号(P3 Task 3;改密用例放在最后,成功后旧密码失效)。
|
||||
await prisma.users.create({
|
||||
data: { account: 'hank', passwordHash: hash, displayName: 'Hank' },
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -280,4 +284,98 @@ describe('auth (e2e)', () => {
|
||||
expect(res.status).toBe(401);
|
||||
expect(res.body.code).toBe(ErrorCodes.AUTH_INVALID_CREDENTIALS);
|
||||
});
|
||||
|
||||
// ── 修改密码(P3 Task 3 契约增补)──
|
||||
|
||||
const changePassword = (token: string, oldPassword: string, newPassword: string) =>
|
||||
request(app.getHttpServer())
|
||||
.post(`/${API_PREFIX}/auth/password-change`)
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.send({ oldPassword, newPassword });
|
||||
|
||||
it('修改密码:未认证 → 401 AUTH_TOKEN_EXPIRED', async () => {
|
||||
const res = await request(app.getHttpServer())
|
||||
.post(`/${API_PREFIX}/auth/password-change`)
|
||||
.send({ oldPassword: PASSWORD, newPassword: 'brand-new-pass-9' });
|
||||
expect(res.status).toBe(401);
|
||||
expect(res.body.code).toBe(ErrorCodes.AUTH_TOKEN_EXPIRED);
|
||||
});
|
||||
|
||||
it('修改密码:newPassword 少于 8 位 → 400 VALIDATION_FAILED', async () => {
|
||||
const loginRes = await login('hank', PASSWORD);
|
||||
const token: string = loginRes.body.sessionToken;
|
||||
|
||||
const res = await changePassword(token, PASSWORD, 'short7');
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe(ErrorCodes.VALIDATION_FAILED);
|
||||
|
||||
// 密码未变:旧密码仍可登录(新密码不可)。
|
||||
const relogin = await login('hank', PASSWORD);
|
||||
expect(relogin.status).toBe(200);
|
||||
});
|
||||
|
||||
it('修改密码:旧密码错误 → 401 AUTH_INVALID_CREDENTIALS,当前会话不受影响', async () => {
|
||||
const loginRes = await login('hank', PASSWORD);
|
||||
const token: string = loginRes.body.sessionToken;
|
||||
|
||||
const res = await changePassword(token, 'wrong-old-password', 'brand-new-pass-9');
|
||||
expect(res.status).toBe(401);
|
||||
expect(res.body.code).toBe(ErrorCodes.AUTH_INVALID_CREDENTIALS);
|
||||
|
||||
// 会话未被撤销(错误旧密码不产生任何副作用)。
|
||||
const refresh = await request(app.getHttpServer())
|
||||
.post(`/${API_PREFIX}/auth/refresh`)
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
expect(refresh.status).toBe(200);
|
||||
});
|
||||
|
||||
it('修改密码:成功 → 204;其他会话撤销、当前会话保留;新密码可登录、旧密码拒绝;写审计', async () => {
|
||||
// 双会话:tokenA 为当前会话(发起改密),tokenB 为应被撤销的其他会话。
|
||||
const loginA = await login('hank', PASSWORD);
|
||||
const tokenA: string = loginA.body.sessionToken;
|
||||
const loginB = await login('hank', PASSWORD);
|
||||
const tokenB: string = loginB.body.sessionToken;
|
||||
const NEW_PASSWORD = 'brand-new-pass-9';
|
||||
|
||||
const res = await changePassword(tokenA, PASSWORD, NEW_PASSWORD);
|
||||
expect(res.status).toBe(204);
|
||||
expect(res.body).toEqual({});
|
||||
|
||||
// 其他会话:即刻失效(revokedAt 落库)。
|
||||
const refreshB = await request(app.getHttpServer())
|
||||
.post(`/${API_PREFIX}/auth/refresh`)
|
||||
.set('Authorization', `Bearer ${tokenB}`);
|
||||
expect(refreshB.status).toBe(401);
|
||||
expect(refreshB.body.code).toBe(ErrorCodes.AUTH_TOKEN_EXPIRED);
|
||||
const sessionB = await prisma.sessions.findUnique({
|
||||
where: { tokenHash: sha256Hex(tokenB) },
|
||||
select: { revokedAt: true },
|
||||
});
|
||||
expect(sessionB?.revokedAt).not.toBeNull();
|
||||
|
||||
// 当前会话:保留(改密不踢本人下线)。
|
||||
const refreshA = await request(app.getHttpServer())
|
||||
.post(`/${API_PREFIX}/auth/refresh`)
|
||||
.set('Authorization', `Bearer ${tokenA}`);
|
||||
expect(refreshA.status).toBe(200);
|
||||
|
||||
// 新旧密码登录裁决。
|
||||
const reloginNew = await login('hank', NEW_PASSWORD);
|
||||
expect(reloginNew.status).toBe(200);
|
||||
const reloginOld = await login('hank', PASSWORD);
|
||||
expect(reloginOld.status).toBe(401);
|
||||
expect(reloginOld.body.code).toBe(ErrorCodes.AUTH_INVALID_CREDENTIALS);
|
||||
|
||||
// 审计:auth.password_change(actor=本人)。
|
||||
const hankId = (
|
||||
await prisma.users.findUnique({
|
||||
where: { account: 'hank' },
|
||||
select: { id: true },
|
||||
})
|
||||
)?.id;
|
||||
const audit = await prisma.audit_logs.findFirst({
|
||||
where: { action: 'auth.password_change', actorId: hankId },
|
||||
});
|
||||
expect(audit).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user