From 7b64c4e3379bfbe7a743f94fe49a3783599a1270 Mon Sep 17 00:00:00 2001 From: Abraham Ingersoll <586805+aberoham@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:15:14 +0530 Subject: [PATCH] user: read back a user created without names POST /user takes first_name and last_name as optional, and mysql stores the missing ones as NULL. The response schema refuses NULL, so creating, reading, listing and logging in as that user all answered 500. The mysql store now drops NULL names when it reads a row, as the nameserver and zone_record stores do. --- CHANGELOG.md | 1 + lib/user/store/mysql.js | 3 +++ routes/user.test.js | 48 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2475633..4898566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Unreleased +- fix: a user created without names reads back, lists and logs in on mysql - fix: file stores draw permission ids from one sequence, and renumber repeated ids at startup - fix: file store user, group and permission reads hide revoked permissions, and a regrant gets a new id - fix: reject create conflicts and duplicate permission targets diff --git a/lib/user/store/mysql.js b/lib/user/store/mysql.js index 24a0c58..b48a0ee 100644 --- a/lib/user/store/mysql.js +++ b/lib/user/store/mysql.js @@ -9,6 +9,7 @@ import { splitQualifiedUsername } from '../qualified.js' const userDbMap = { id: 'nt_user_id', gid: 'nt_group_id' } const boolFields = ['is_admin', 'deleted'] +const nullableFields = ['first_name', 'last_name'] class UserRepoMySQL extends UserBase { constructor(args = {}) { @@ -61,6 +62,7 @@ class UserRepoMySQL extends UserBase { for (const b of ['is_admin']) { if (u[b] !== undefined) u[b] = u[b] === 1 // int to boolean } + for (const f of nullableFields) if (u[f] === null) delete u[f] const g = { id: u.nt_group_id, name: groupName, @@ -166,6 +168,7 @@ class UserRepoMySQL extends UserBase { for (const b of boolFields) { r[b] = r[b] === 1 } + for (const f of nullableFields) if (r[f] === null) delete r[f] if ([false, undefined].includes(origDeleted)) delete r.deleted const effectivePerm = await Permission.getEffective(r.id) diff --git a/routes/user.test.js b/routes/user.test.js index b11987f..9eb613e 100644 --- a/routes/user.test.js +++ b/routes/user.test.js @@ -18,9 +18,14 @@ before(async () => { }) let userId2 +const namelessUsername = `${userCase.username}3` after(async () => { if (userId2) await User.destroy({ id: userId2 }) + for (const deleted of [false, true]) { + for (const u of await User.get({ username: namelessUsername, gid: userCase.gid, deleted })) + await User.destroy({ id: u.id }) + } await server.stop() }) @@ -123,6 +128,49 @@ describe('user routes', () => { assert.ok([200, 204].includes(res.statusCode)) }) + it('a user created without names can be read, listed and logged in', async () => { + const testCase = JSON.parse(JSON.stringify(userCase)) + delete testCase.id + delete testCase.deleted + delete testCase.first_name + delete testCase.last_name + testCase.username = namelessUsername + + const created = await server.inject({ + method: 'POST', + url: '/user', + headers: auth.headers, + payload: testCase, + }) + assert.equal(created.statusCode, 201) + const id = created.result.user[0].id + assert.equal(created.result.user[0].first_name, undefined) + + const one = await server.inject({ method: 'GET', url: `/user/${id}`, headers: auth.headers }) + assert.equal(one.statusCode, 200) + + const list = await server.inject({ + method: 'GET', + url: `/user?gid=${userCase.gid}`, + headers: auth.headers, + }) + assert.equal(list.statusCode, 200) + assert.ok(list.result.user.some((u) => u.id === id)) + + const login = await server.inject({ + method: 'POST', + url: '/session', + payload: { username: `${testCase.username}@${groupCase.name}`, password: testCase.password }, + }) + assert.equal(login.statusCode, 200) + assert.equal(login.result.user.id, id) + + const headers = { Authorization: `Bearer ${login.result.session.token}` } + const session = await server.inject({ method: 'GET', url: '/session', headers }) + assert.equal(session.statusCode, 200) + await server.inject({ method: 'DELETE', url: '/session', headers }) + }) + it('DELETE /session', async () => { const res = await server.inject({ method: 'DELETE',