Files
Gabriel20xx 97c237be80
Lint / Lint (push) Successful in 1m8s
Tests / Go backend (push) Failing after 1m23s
Tests / Dashboard checks (push) Failing after 38s
Docker / Docker Build & Push (push) Failing after 6m29s
feat(sync): paginate Android sync with change tokens
2026-10-20 15:27:53 +00:00

118 lines
5.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { AuthStore } from '../src/auth.js';
// A Database-like object that records every query and answers from canned results.
function fakeDatabase({ rowCount = 1, totpEnabled = false, rowsByCondition = {} } = {}) {
const queries = [];
return {
queries,
async query(text, params = []) {
queries.push({ text, params });
if (text.includes('totp_enabled, false) AS enabled')) {
return { rows: [{ secret: 'S', enabled: totpEnabled, last_used_step: -1 }], rowCount: 1 };
}
for (const [condition, row] of Object.entries(rowsByCondition)) {
if (text.includes(condition)) return { rows: [row], rowCount: 1 };
}
return { rows: [], rowCount };
},
};
}
test('ensureSchema requests the case-insensitive username index, only while no clashing pair exists', async () => {
const database = fakeDatabase();
await new AuthStore(database, { singleUser: false }).ensureSchema();
const statements = database.queries.map((query) => query.text).join('\n');
assert.match(statements, /auth_users_username_lower_idx/);
assert.match(statements, /lower\(username\)/);
assert.match(statements, /HAVING count\(\*\) > 1/);
assert.match(statements, /ADD COLUMN IF NOT EXISTS last_used_step BIGINT NOT NULL DEFAULT -1/);
});
test('setTotpSecret refuses to replace an enabled second factor', async () => {
const database = fakeDatabase({ rowCount: 0, totpEnabled: true });
const store = new AuthStore(database);
await assert.rejects(store.setTotpSecret(1, 'NEWSECRET'), { code: 'AUTH_TOTP_ALREADY_ENABLED' });
assert.match(database.queries[0].text, /NOT totp_enabled/);
});
test('setTotpSecret stores a pending secret while TOTP is off, and ignores an unknown user', async () => {
await new AuthStore(fakeDatabase()).setTotpSecret(1, 'NEWSECRET');
await new AuthStore(fakeDatabase({ rowCount: 0 })).setTotpSecret(2, 'NEWSECRET');
});
test('TOTP enrollment records its code step and a replay is rejected atomically', async () => {
const state = { secret: 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', enabled: false, last_used_step: -1 };
const database = {
async query(text, params = []) {
if (text.startsWith('SELECT COALESCE(totp_secret')) {
return { rows: [{ ...state }], rowCount: 1 };
}
const [step, , secret] = params;
if (text.includes('SET totp_enabled = true')) {
if (!state.enabled && secret === state.secret && state.last_used_step < step) {
state.enabled = true;
state.last_used_step = step;
return { rows: [], rowCount: 1 };
}
return { rows: [], rowCount: 0 };
}
if (text.includes('SET last_used_step = $1')) {
if (state.enabled && secret === state.secret && state.last_used_step < step) {
state.last_used_step = step;
return { rows: [], rowCount: 1 };
}
return { rows: [], rowCount: 0 };
}
throw new Error(`unexpected query: ${text}`);
},
};
const store = new AuthStore(database);
assert.equal(await store.enableTotp(1, '287082', 59000), true);
assert.equal(state.last_used_step, 1);
assert.equal(await store.consumeTotpCode(1, '287082', 59000), false);
assert.equal(state.last_used_step, 1);
});
test('credential writes refuse an empty hash and a username containing @', async () => {
const database = fakeDatabase();
const store = new AuthStore(database);
const writes = [
(username, hash) => store.createUser(username, '', hash),
(username, hash) => store.createManagedUser(username, '', hash),
(username, hash) => store.updateCredentials(1, username, '', hash),
(username, hash) => store.updateUser(1, { username, passwordHash: hash }),
];
for (const write of writes) {
await assert.rejects(write('alice', ''), { code: 'AUTH_EMPTY_PASSWORD_HASH' });
await assert.rejects(write('alice@example.com', 'hash'), { code: 'AUTH_INVALID_USERNAME' });
}
assert.equal(database.queries.length, 0);
});
test('updateUser leaves unspecified fields alone', async () => {
const database = fakeDatabase();
const store = new AuthStore(database);
await store.updateUser(1, { email: 'a@b.com' });
await store.updateUser(1, { role: 'admin' });
assert.equal(database.queries.length, 2);
assert.doesNotMatch(database.queries[0].text, /password_hash|username/);
});
test('findByIdentifier tries the username before the email', async () => {
const database = fakeDatabase({
rowsByCondition: {
'username = $1': { id: 1, username: 'owner@example.com' },
'lower(email) = lower($1)': { id: 2, username: 'owner' },
},
});
const store = new AuthStore(database);
assert.equal((await store.findByIdentifier('owner@example.com')).username, 'owner@example.com');
assert.equal(database.queries.length, 1);
const emailOnly = fakeDatabase({ rowsByCondition: { 'lower(email) = lower($1)': { id: 2, username: 'owner' } } });
assert.equal((await new AuthStore(emailOnly).findByIdentifier('OWNER@example.com')).username, 'owner');
assert.equal(await new AuthStore(fakeDatabase()).findByIdentifier('nobody'), null);
});