97 lines
2.5 KiB
JavaScript
97 lines
2.5 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
require('../src/common');
|
|
|
|
const registerUsersRoutes = require('../src/web/routes/admin/users');
|
|
|
|
test('user edit save and new goes to the blank create page', async () => {
|
|
const handlers = {};
|
|
const app = {
|
|
get() {},
|
|
post(path, ...routeHandlers) {
|
|
handlers[path] = routeHandlers;
|
|
}
|
|
};
|
|
|
|
const deps = {
|
|
pool: {
|
|
async query(sql) {
|
|
if (sql.includes('SELECT id FROM a_users WHERE id = ? LIMIT 1')) {
|
|
return [[{ id: 7 }]];
|
|
}
|
|
if (sql.includes('SELECT id FROM a_users WHERE username = ? AND id <> ? LIMIT 1')) {
|
|
return [[]];
|
|
}
|
|
return [[]];
|
|
},
|
|
async getConnection() {
|
|
return {
|
|
async beginTransaction() {},
|
|
async query(sql) {
|
|
if (sql.includes('UPDATE a_users SET name = ?, username = ?, account_locked = ?, modified_by = ? WHERE id = ?')) {
|
|
return [{ affectedRows: 1 }];
|
|
}
|
|
return [[]];
|
|
},
|
|
async commit() {},
|
|
async rollback() {},
|
|
release() {}
|
|
};
|
|
}
|
|
},
|
|
common: {
|
|
validateMaxLength(value) {
|
|
return String(value || '').trim();
|
|
},
|
|
fetchDuplicateName: async () => null
|
|
},
|
|
pages: {},
|
|
formatDashboardDate: () => '',
|
|
getAuditUserId: () => 1,
|
|
hashPassword: () => ({ hash: 'hash', salt: 'salt', iterations: 1 }),
|
|
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/username'];
|
|
assert.equal(Array.isArray(routeHandlers), true);
|
|
|
|
const req = {
|
|
params: { id: '7' },
|
|
body: {
|
|
name: 'User',
|
|
username: 'user',
|
|
save_action: 'new'
|
|
},
|
|
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/new?message=User%20updated.');
|
|
}); |