- Add lint, formatting, vet, and publish workflow checks - Run Go database tests with PostgreSQL and add Java tests - Make consumer sync opt-in; update docs and Go dependency
84 lines
3.6 KiB
JavaScript
84 lines
3.6 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 }], 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/);
|
|
});
|
|
|
|
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('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);
|
|
});
|