const test = require('node:test'); const assert = require('node:assert/strict'); require('../src/common'); const registerUsersRoutes = require('../src/web/routes/admin/users'); const { validatePasswordStrength } = require('../src/auth'); test('user password reset redirects back to the selected user edit page', async () => { const handlers = {}; const app = { post(path, ...routeHandlers) { handlers[path] = routeHandlers; }, get() {} }; const deps = { pool: { async query(sql) { if (sql.includes('SELECT id FROM a_users WHERE id = ? LIMIT 1')) { return [[{ id: 7 }]]; } if (sql.includes('UPDATE a_users SET password_hash = ?, password_salt = ?, password_iterations = ?, modified_by = ? WHERE id = ?')) { return [{ affectedRows: 1 }]; } if (sql.includes('DELETE FROM a_sessions WHERE user_id = ?')) { return [{ affectedRows: 1 }]; } return [[]]; } }, common: { fetchDuplicateName: async () => null }, pages: {}, formatDashboardDate: () => '', getAuditUserId: () => 3, hashPassword: () => ({ hash: 'hash', salt: 'salt', iterations: 1 }), validatePasswordStrength: validatePasswordStrength, readArrayField: () => [], rbacData: { fetchRoles: async () => [], fetchUsersWithRolesPage: async () => ({ users: [], totalItems: 0, currentPage: 1 }), fetchUserWithRoles: async () => ({ id: 7, name: 'User', username: 'user', roleIds: [], inUse: false }), syncUserRoles: async () => {} }, requirePermission() { return function (_req, _res, next) { next(); }; } }; registerUsersRoutes(app, deps); const routeHandlers = handlers['/settings/users/:id/password']; assert.equal(Array.isArray(routeHandlers), true); const req = { params: { id: '7' }, body: { password: 'NewPassword!1', confirm_password: 'NewPassword!1' }, currentUser: { id: 1 } }; const res = { redirect(url) { this.redirectedTo = url; }, status() { return this; }, send() { return this; } }; await routeHandlers[1](req, res, () => {}); assert.equal(res.redirectedTo, '/settings/users/7/edit?message=Password%20updated.'); });