mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
refactor(auth): users table through UsersRepository; JWT and MFA lookups repository-backed
This commit is contained in:
@@ -5,7 +5,7 @@ import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { createUser } from '../../../helpers/factories';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import { columnIncrementedBy, columnRef, currentTimestamp, dateAdd, dateOf } from '../../../../src/db/dialect/sql-functions';
|
||||
import { columnIncrementedBy, columnRef, currentTimestamp, dateAdd, dateOf, lower } from '../../../../src/db/dialect/sql-functions';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
@@ -73,6 +73,32 @@ describe('sql-functions (sqlite)', () => {
|
||||
expect(() => currentTimestamp(foreign)).toThrow(/no implementation for platform FakePlatform/);
|
||||
expect(() => columnRef(foreign, 'u.max_uses')).toThrow(/no implementation for platform FakePlatform/);
|
||||
expect(() => columnIncrementedBy(foreign, 'u.used_count', 1)).toThrow(/no implementation for platform FakePlatform/);
|
||||
expect(() => lower(foreign, 'u.email')).toThrow(/no implementation for platform FakePlatform/);
|
||||
});
|
||||
|
||||
// Plan 3b Task 1: UsersRepository's CI (case-insensitive) lookups
|
||||
// (findByEmailCI, findIdByUsernameCI, ...) need `LOWER(col) = ?` as a
|
||||
// `find`/`findOne` filter KEY, not just inside a QueryBuilder `.where()` —
|
||||
// proving it works through `em.find`/`findOne` (the API the repository
|
||||
// actually calls) rather than only through `createQueryBuilder`.
|
||||
it('SQLF-012: lower is usable as a find() filter key, matching case-insensitively', async () => {
|
||||
const { user } = createUser(testDb, { email: 'Mixed.Case@Example.com' });
|
||||
const rows = await t.em.find(Users, { [lower(t.em.getPlatform(), 'email')]: 'mixed.case@example.com' });
|
||||
expect(rows.map((r) => r.id)).toEqual([user.id]);
|
||||
|
||||
// A differently-cased query value that still lowercases to the same
|
||||
// string finds the same row; one that does not, finds nothing.
|
||||
const none = await t.em.find(Users, { [lower(t.em.getPlatform(), 'email')]: 'nobody@example.com' });
|
||||
expect(none).toEqual([]);
|
||||
});
|
||||
|
||||
it('SQLF-013: lower composes with other filter keys in one findOne call', async () => {
|
||||
const { user } = createUser(testDb, { username: 'MixedCaseName' });
|
||||
const row = await t.em.findOne(Users, {
|
||||
[lower(t.em.getPlatform(), 'username')]: 'mixedcasename',
|
||||
id: { $ne: -1 },
|
||||
});
|
||||
expect(row?.id).toBe(user.id);
|
||||
});
|
||||
|
||||
// Plan 3b Task 0: InviteTokensRepository.incrementUsedCount needs a
|
||||
|
||||
@@ -22,15 +22,15 @@ function rawInvite(id: number): unknown {
|
||||
}
|
||||
|
||||
describe('InviteTokensRepository', () => {
|
||||
describe('findValid', () => {
|
||||
it('INVREPO-001: a row read through findValid is the SELECT * row, key for key', async () => {
|
||||
describe('findByToken', () => {
|
||||
it('INVREPO-001: a row read through findByToken is the SELECT * row, key for key', async () => {
|
||||
const created = createInviteToken(testDb, { token: 'find-me' });
|
||||
const row = await invites.findValid('find-me');
|
||||
const row = await invites.findByToken('find-me');
|
||||
expect(row).toStrictEqual(rawInvite(created.id));
|
||||
});
|
||||
|
||||
it('INVREPO-002: an unknown token is null', async () => {
|
||||
expect(await invites.findValid('does-not-exist')).toBeNull();
|
||||
expect(await invites.findByToken('does-not-exist')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vites
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { createUser } from '../../../helpers/factories';
|
||||
import { createUser, createAdmin } from '../../../helpers/factories';
|
||||
import { Users } from '../../../../src/db/entities/Users.entity';
|
||||
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
|
||||
|
||||
@@ -94,4 +94,451 @@ describe('UsersRepository', () => {
|
||||
expect(queries).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Plan 3b Task 1 — every users-table method the auth/oidc/oauth/tokens
|
||||
// cluster needs (see Users.repository.ts for the legacy-statement mapping
|
||||
// per method). One `it` (or a focused group) per method, on real rows.
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
it('USERSREPO-009: findByIdWithPasswordVersion (JV1) reads id/username/email/role/password_version', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET password_version = 3 WHERE id = ?').run(user.id);
|
||||
const row = await users.findByIdWithPasswordVersion(user.id);
|
||||
expect(row).toEqual({ id: user.id, username: user.username, email: user.email, role: 'user', password_version: 3 });
|
||||
expect(await users.findByIdWithPasswordVersion(999999)).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-010: getPasswordVersion reads password_version, null for a missing user', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await users.getPasswordVersion(user.id)).toBe(0);
|
||||
testDb.prepare('UPDATE users SET password_version = 7 WHERE id = ?').run(user.id);
|
||||
expect(await users.getPasswordVersion(user.id)).toBe(7);
|
||||
expect(await users.getPasswordVersion(999999)).toBeNull();
|
||||
});
|
||||
|
||||
describe('countNonGuest (AU4/AU7/O10) — is_guest is INTEGER NOT NULL DEFAULT 0, never observed NULL', () => {
|
||||
it('USERSREPO-011: counts only non-guest users', async () => {
|
||||
createUser(testDb);
|
||||
createUser(testDb);
|
||||
const { user: guest } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id);
|
||||
expect(await users.countNonGuest()).toBe(2);
|
||||
});
|
||||
|
||||
it('USERSREPO-012: a NULL is_guest row cannot exist — the column is NOT NULL DEFAULT 0 (verified against the baseline + guest-members migrations), so a direct INSERT that omits the column, or an explicit NULL, both resolve to 0, never leaving a row this filter would miscount', () => {
|
||||
testDb.prepare('INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, ?)').run('nog', 'nog@test.example.com', 'x', 'user');
|
||||
const stored = testDb.prepare("SELECT is_guest FROM users WHERE username = 'nog'").get() as { is_guest: number };
|
||||
expect(stored.is_guest).toBe(0); // the DEFAULT 0, not a nullable column silently coercing
|
||||
expect(() => testDb.prepare('UPDATE users SET is_guest = NULL WHERE username = ?').run('nog')).toThrow(/NOT NULL constraint failed/);
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-013: findAdminNeedingPasswordChange finds an admin flagged must_change_password, ignores a matching non-admin or an unflagged admin', async () => {
|
||||
expect(await users.findAdminNeedingPasswordChange()).toBeNull();
|
||||
const { user: plainAdmin } = createAdmin(testDb);
|
||||
expect(await users.findAdminNeedingPasswordChange()).toBeNull();
|
||||
const { user: flaggedUser } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET must_change_password = 1 WHERE id = ?').run(flaggedUser.id);
|
||||
expect(await users.findAdminNeedingPasswordChange()).toBeNull(); // not an admin
|
||||
testDb.prepare('UPDATE users SET must_change_password = 1 WHERE id = ?').run(plainAdmin.id);
|
||||
expect(await users.findAdminNeedingPasswordChange()).toBe(plainAdmin.id);
|
||||
});
|
||||
|
||||
describe('findIdByEmailOrUsernameCI (AU9)', () => {
|
||||
it('USERSREPO-014: matches by email OR username, case-insensitively, excluding guests', async () => {
|
||||
const { user } = createUser(testDb, { email: 'Someone@Example.com', username: 'SomeoneName' });
|
||||
expect(await users.findIdByEmailOrUsernameCI('someone@example.com', 'no-match')).toBe(user.id);
|
||||
expect(await users.findIdByEmailOrUsernameCI('no-match@example.com', 'someonename')).toBe(user.id);
|
||||
expect(await users.findIdByEmailOrUsernameCI('nope@example.com', 'nope')).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-015: excludes a guest even on an exact match', async () => {
|
||||
const { user } = createUser(testDb, { email: 'guest1@example.com', username: 'guest1' });
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(user.id);
|
||||
expect(await users.findIdByEmailOrUsernameCI('guest1@example.com', 'guest1')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('insertUser (AU10/O14)', () => {
|
||||
it('USERSREPO-016: local registration shape (AU10) — no oidc/avatar columns', async () => {
|
||||
const row = await users.insertUser({
|
||||
username: 'localuser',
|
||||
email: 'local@example.com',
|
||||
password_hash: 'hashed',
|
||||
role: 'user',
|
||||
first_seen_version: '1.2.3',
|
||||
});
|
||||
expect(row.username).toBe('localuser');
|
||||
expect(row.email).toBe('local@example.com');
|
||||
expect(row.password_hash).toBe('hashed');
|
||||
expect(row.role).toBe('user');
|
||||
expect(row.first_seen_version).toBe('1.2.3');
|
||||
expect(row.login_count).toBe(0);
|
||||
expect(row.oidc_sub).toBeNull();
|
||||
expect(row.oidc_issuer).toBeNull();
|
||||
expect(row.avatar).toBeNull();
|
||||
expect(row.created_at).not.toBeNull();
|
||||
const raw = testDb.prepare('SELECT * FROM users WHERE id = ?').get(row.id);
|
||||
expect(raw).toMatchObject({ username: 'localuser', email: 'local@example.com', role: 'user', login_count: 0 });
|
||||
});
|
||||
|
||||
it('USERSREPO-017: SSO registration shape (O14) — oidc_sub/oidc_issuer/avatar set', async () => {
|
||||
const row = await users.insertUser({
|
||||
username: 'ssouser',
|
||||
email: 'sso@example.com',
|
||||
password_hash: 'unusable-hash',
|
||||
role: 'admin',
|
||||
first_seen_version: '1.2.3',
|
||||
oidc_sub: 'sub-123',
|
||||
oidc_issuer: 'https://idp.example.com',
|
||||
avatar: 'https://idp.example.com/pic.png',
|
||||
});
|
||||
expect(row.oidc_sub).toBe('sub-123');
|
||||
expect(row.oidc_issuer).toBe('https://idp.example.com');
|
||||
expect(row.avatar).toBe('https://idp.example.com/pic.png');
|
||||
expect(row.role).toBe('admin');
|
||||
expect(row.login_count).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByEmailCI (AU12/O4)', () => {
|
||||
it('USERSREPO-018: matches case-insensitively, excludes guests, returns the full row', async () => {
|
||||
const { user } = createUser(testDb, { email: 'Case.Test@Example.com' });
|
||||
const row = await users.findByEmailCI('case.test@example.com');
|
||||
expect(row?.id).toBe(user.id);
|
||||
expect(row?.username).toBe(user.username);
|
||||
expect(await users.findByEmailCI('nope@example.com')).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-019: a guest with a matching email is not returned', async () => {
|
||||
const { user } = createUser(testDb, { email: 'guest2@example.com' });
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(user.id);
|
||||
expect(await users.findByEmailCI('guest2@example.com')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('touchLastLogin (AU13/AU34/AU35/PK12/O16)', () => {
|
||||
it('USERSREPO-020: bumps login_count by exactly 1 and stamps last_login, in one statement', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET login_count = 5 WHERE id = ?').run(user.id);
|
||||
await users.touchLastLogin(user.id);
|
||||
const row = testDb.prepare('SELECT login_count, last_login FROM users WHERE id = ?').get(user.id) as { login_count: number; last_login: string };
|
||||
expect(row.login_count).toBe(6);
|
||||
expect(row.last_login).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('USERSREPO-021: two concurrent calls both land — +2, never +1 (no read-modify-write)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET login_count = 10 WHERE id = ?').run(user.id);
|
||||
await Promise.all([users.touchLastLogin(user.id), users.touchLastLogin(user.id)]);
|
||||
const row = testDb.prepare('SELECT login_count FROM users WHERE id = ?').get(user.id) as { login_count: number };
|
||||
expect(row.login_count).toBe(12);
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-022: findMeRow (AU14) reads the /api/auth/me projection', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET avatar = ?, mfa_enabled = 1, must_change_password = 1 WHERE id = ?').run('pic.png', user.id);
|
||||
const row = await users.findMeRow(user.id);
|
||||
expect(row).toMatchObject({ id: user.id, username: user.username, email: user.email, role: 'user', avatar: 'pic.png', mfa_enabled: 1, must_change_password: 1 });
|
||||
expect(row?.created_at).not.toBeNull();
|
||||
expect(await users.findMeRow(999999)).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-023: getPasswordHashAndVersion (AU15) reads both columns together', async () => {
|
||||
const { user, password: _password } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET password_version = 2 WHERE id = ?').run(user.id);
|
||||
const row = await users.getPasswordHashAndVersion(user.id);
|
||||
expect(row?.password_hash).toBe(user.password_hash);
|
||||
expect(row?.password_version).toBe(2);
|
||||
});
|
||||
|
||||
it('USERSREPO-024: setPassword (AU16/AU43) writes hash+version, clears must_change_password, stamps updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET must_change_password = 1 WHERE id = ?').run(user.id);
|
||||
await users.setPassword(user.id, 'new-hash', 5);
|
||||
const row = testDb.prepare('SELECT password_hash, password_version, must_change_password, updated_at FROM users WHERE id = ?').get(user.id) as {
|
||||
password_hash: string; password_version: number; must_change_password: number; updated_at: string;
|
||||
};
|
||||
expect(row.password_hash).toBe('new-hash');
|
||||
expect(row.password_version).toBe(5);
|
||||
expect(row.must_change_password).toBe(0);
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
it('USERSREPO-025: countAdmins (AU19/O7) counts only role=admin', async () => {
|
||||
createUser(testDb);
|
||||
createAdmin(testDb);
|
||||
createAdmin(testDb);
|
||||
expect(await users.countAdmins()).toBe(2);
|
||||
});
|
||||
|
||||
it('USERSREPO-026: getRole (AU20/AU22) reads the role column', async () => {
|
||||
const { user } = createAdmin(testDb);
|
||||
expect(await users.getRole(user.id)).toBe('admin');
|
||||
expect(await users.getRole(999999)).toBeNull();
|
||||
});
|
||||
|
||||
describe('getMfaEnabled (AU23/AU27/MP2) — distinguishes "no row" from "row present, mfa_enabled unset"', () => {
|
||||
it('USERSREPO-027: returns the row wrapper when the user exists', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await users.getMfaEnabled(user.id)).toEqual({ mfa_enabled: 0 });
|
||||
testDb.prepare('UPDATE users SET mfa_enabled = 1 WHERE id = ?').run(user.id);
|
||||
expect(await users.getMfaEnabled(user.id)).toEqual({ mfa_enabled: 1 });
|
||||
});
|
||||
|
||||
it('USERSREPO-028: returns null (not a row with a null field) when the user does not exist', async () => {
|
||||
expect(await users.getMfaEnabled(999999)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-029: enableMfa (AU28) sets mfa_enabled=1, writes the already-encrypted secret and codes, stamps updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
await users.enableMfa(user.id, 'encrypted-secret', '["hash1","hash2"]');
|
||||
const row = testDb.prepare('SELECT mfa_enabled, mfa_secret, mfa_backup_codes, updated_at FROM users WHERE id = ?').get(user.id) as {
|
||||
mfa_enabled: number; mfa_secret: string; mfa_backup_codes: string; updated_at: string;
|
||||
};
|
||||
expect(row.mfa_enabled).toBe(1);
|
||||
expect(row.mfa_secret).toBe('encrypted-secret');
|
||||
expect(row.mfa_backup_codes).toBe('["hash1","hash2"]');
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
describe('findById (AU30/AU32/PK4/PK10/O15) — full row, PK-only findOne', () => {
|
||||
it('USERSREPO-030: reads every scalar column', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const row = await users.findById(user.id);
|
||||
expect(row?.id).toBe(user.id);
|
||||
expect(row?.username).toBe(user.username);
|
||||
expect(row?.password_hash).toBe(user.password_hash);
|
||||
expect(await users.findById(999999)).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-031: sees a raw UPDATE on the same id in the same request (I1, identity-map regression)', async () => {
|
||||
const { user } = createUser(testDb, { username: 'before' });
|
||||
await users.findById(user.id); // populate the identity map
|
||||
testDb.prepare('UPDATE users SET username = ? WHERE id = ?').run('after', user.id);
|
||||
const { value, queries } = await withQueryCount(() => users.findById(user.id));
|
||||
expect(value?.username).toBe('after');
|
||||
expect(queries).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-032: disableMfa (AU31) clears mfa_enabled/secret/codes, stamps updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET mfa_enabled = 1, mfa_secret = ?, mfa_backup_codes = ? WHERE id = ?').run('s', 'c', user.id);
|
||||
await users.disableMfa(user.id);
|
||||
const row = testDb.prepare('SELECT mfa_enabled, mfa_secret, mfa_backup_codes, updated_at FROM users WHERE id = ?').get(user.id) as {
|
||||
mfa_enabled: number; mfa_secret: string | null; mfa_backup_codes: string | null; updated_at: string;
|
||||
};
|
||||
expect(row.mfa_enabled).toBe(0);
|
||||
expect(row.mfa_secret).toBeNull();
|
||||
expect(row.mfa_backup_codes).toBeNull();
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
describe('setBackupCodesAndTouch (AU33) vs setBackupCodes (AU44) — one stamps updated_at, the other does not', () => {
|
||||
it('USERSREPO-033: setBackupCodesAndTouch writes codes and stamps updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.setBackupCodesAndTouch(user.id, '["a"]');
|
||||
const row = testDb.prepare('SELECT mfa_backup_codes, updated_at FROM users WHERE id = ?').get(user.id) as { mfa_backup_codes: string; updated_at: string };
|
||||
expect(row.mfa_backup_codes).toBe('["a"]');
|
||||
expect(row.updated_at).not.toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
|
||||
it('USERSREPO-034: setBackupCodes writes codes WITHOUT touching updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.setBackupCodes(user.id, '["b"]');
|
||||
const row = testDb.prepare('SELECT mfa_backup_codes, updated_at FROM users WHERE id = ?').get(user.id) as { mfa_backup_codes: string; updated_at: string };
|
||||
expect(row.mfa_backup_codes).toBe('["b"]');
|
||||
expect(row.updated_at).toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findForPasswordReset (AU36) — case-SENSITIVE email, unlike every other email lookup here', () => {
|
||||
it('USERSREPO-035: matches the exact case only', async () => {
|
||||
const { user } = createUser(testDb, { email: 'Exact@Example.com' });
|
||||
const row = await users.findForPasswordReset('Exact@Example.com');
|
||||
expect(row?.id).toBe(user.id);
|
||||
expect(await users.findForPasswordReset('exact@example.com')).toBeNull(); // no LOWER() in the legacy statement
|
||||
});
|
||||
|
||||
it('USERSREPO-036: excludes a guest', async () => {
|
||||
const { user } = createUser(testDb, { email: 'guest3@example.com' });
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(user.id);
|
||||
expect(await users.findForPasswordReset('guest3@example.com')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-037: findResetTarget (AU40) reads the reset-branch projection', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET mfa_enabled = 1, mfa_secret = ?, mfa_backup_codes = ?, password_version = 4 WHERE id = ?').run('s', 'c', user.id);
|
||||
const row = await users.findResetTarget(user.id);
|
||||
expect(row).toEqual({ id: user.id, email: user.email, mfa_enabled: 1, mfa_secret: 's', mfa_backup_codes: 'c', password_version: 4 });
|
||||
});
|
||||
|
||||
it('USERSREPO-038: getPasswordHash (PK15) reads password_hash', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await users.getPasswordHash(user.id)).toBe(user.password_hash);
|
||||
expect(await users.getPasswordHash(999999)).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-039: findIdAndEmail (PK17) reads id+email', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await users.findIdAndEmail(user.id)).toEqual({ id: user.id, email: user.email });
|
||||
});
|
||||
|
||||
describe('findByOidcIdentity (O3)', () => {
|
||||
it('USERSREPO-040: matches on the (sub, issuer) pair', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET oidc_sub = ?, oidc_issuer = ? WHERE id = ?').run('sub-1', 'https://idp.example.com', user.id);
|
||||
const row = await users.findByOidcIdentity('sub-1', 'https://idp.example.com');
|
||||
expect(row?.id).toBe(user.id);
|
||||
expect(await users.findByOidcIdentity('sub-1', 'https://other-idp.example.com')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-041: linkOidcIdentity (O5/O6) writes sub+issuer WITHOUT touching updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.linkOidcIdentity(user.id, 'sub-2', 'https://idp2.example.com');
|
||||
const row = testDb.prepare('SELECT oidc_sub, oidc_issuer, updated_at FROM users WHERE id = ?').get(user.id) as {
|
||||
oidc_sub: string; oidc_issuer: string; updated_at: string;
|
||||
};
|
||||
expect(row.oidc_sub).toBe('sub-2');
|
||||
expect(row.oidc_issuer).toBe('https://idp2.example.com');
|
||||
expect(row.updated_at).toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
|
||||
it('USERSREPO-042: setRole (O8) writes role WITHOUT touching updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.setRole(user.id, 'admin');
|
||||
const row = testDb.prepare('SELECT role, updated_at FROM users WHERE id = ?').get(user.id) as { role: string; updated_at: string };
|
||||
expect(row.role).toBe('admin');
|
||||
expect(row.updated_at).toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
|
||||
it('USERSREPO-043: setAvatarRaw (O9) writes avatar WITHOUT touching updated_at, unlike setAvatar', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.setAvatarRaw(user.id, 'https://idp.example.com/pic.png');
|
||||
const row = testDb.prepare('SELECT avatar, updated_at FROM users WHERE id = ?').get(user.id) as { avatar: string; updated_at: string };
|
||||
expect(row.avatar).toBe('https://idp.example.com/pic.png');
|
||||
expect(row.updated_at).toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
|
||||
describe('findIdByUsernameCI (UP5) vs findIdByUsernameCIAny (O12) — different WHERE, two methods', () => {
|
||||
it('USERSREPO-044: findIdByUsernameCI excludes the given id and any guest', async () => {
|
||||
const { user: a } = createUser(testDb, { username: 'DupeName' });
|
||||
const { user: b } = createUser(testDb, { username: 'other' });
|
||||
expect(await users.findIdByUsernameCI('dupename', b.id)).toBe(a.id);
|
||||
expect(await users.findIdByUsernameCI('dupename', a.id)).toBeNull(); // excludes self
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(a.id);
|
||||
expect(await users.findIdByUsernameCI('dupename', b.id)).toBeNull(); // guest excluded
|
||||
});
|
||||
|
||||
it('USERSREPO-045: findIdByUsernameCIAny has no exclude and no guest filter', async () => {
|
||||
const { user } = createUser(testDb, { username: 'AnyName' });
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(user.id);
|
||||
expect(await users.findIdByUsernameCIAny('anyname')).toBe(user.id); // a guest still matches here
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-046: getApiKeyColumns (UP1) reads role + the four key columns', async () => {
|
||||
const { user } = createAdmin(testDb);
|
||||
testDb.prepare('UPDATE users SET maps_api_key = ?, openweather_api_key = ?, unsplash_api_key = ?, amap_api_key = ? WHERE id = ?')
|
||||
.run('m', 'o', 'u', 'a', user.id);
|
||||
expect(await users.getApiKeyColumns(user.id)).toEqual({ role: 'admin', maps_api_key: 'm', openweather_api_key: 'o', unsplash_api_key: 'u', amap_api_key: 'a' });
|
||||
});
|
||||
|
||||
it('USERSREPO-047: updateMapsKey (UP2) writes maps_api_key + updated_at', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
await users.updateMapsKey(user.id, 'encrypted');
|
||||
const row = testDb.prepare('SELECT maps_api_key, updated_at FROM users WHERE id = ?').get(user.id) as { maps_api_key: string; updated_at: string };
|
||||
expect(row.maps_api_key).toBe('encrypted');
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
it('USERSREPO-048: updateApiKeys (UP3) writes all four columns + updated_at in one statement', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
await users.updateApiKeys(user.id, { maps_api_key: 'm2', openweather_api_key: 'o2', unsplash_api_key: 'u2', amap_api_key: null });
|
||||
const row = testDb.prepare('SELECT maps_api_key, openweather_api_key, unsplash_api_key, amap_api_key, updated_at FROM users WHERE id = ?').get(user.id) as Record<string, unknown>;
|
||||
expect(row).toMatchObject({ maps_api_key: 'm2', openweather_api_key: 'o2', unsplash_api_key: 'u2', amap_api_key: null });
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
it('USERSREPO-049: findProfileWithKeys (UP4/UP8) reads the profile-with-keys projection', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET maps_api_key = ?, mfa_enabled = 1 WHERE id = ?').run('m', user.id);
|
||||
const row = await users.findProfileWithKeys(user.id);
|
||||
expect(row).toMatchObject({ id: user.id, username: user.username, email: user.email, role: 'user', maps_api_key: 'm', mfa_enabled: 1 });
|
||||
});
|
||||
|
||||
it('USERSREPO-050: findIdByEmailCI (UP6) matches case-insensitively, excludes self and guests', async () => {
|
||||
const { user: a } = createUser(testDb, { email: 'Dup@Example.com' });
|
||||
const { user: b } = createUser(testDb, { email: 'other@example.com' });
|
||||
expect(await users.findIdByEmailCI('dup@example.com', b.id)).toBe(a.id);
|
||||
expect(await users.findIdByEmailCI('dup@example.com', a.id)).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-051: patchProfile (UP7) writes only the given bounded columns + updated_at, in one statement', async () => {
|
||||
const { user } = createUser(testDb, { username: 'patchme', email: 'patchme@example.com' });
|
||||
await users.patchProfile(user.id, { username: 'patched', maps_api_key: 'newkey' });
|
||||
const row = testDb.prepare('SELECT username, email, maps_api_key, updated_at FROM users WHERE id = ?').get(user.id) as Record<string, unknown>;
|
||||
expect(row.username).toBe('patched');
|
||||
expect(row.email).toBe('patchme@example.com'); // untouched — not in `changes`
|
||||
expect(row.maps_api_key).toBe('newkey');
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
});
|
||||
|
||||
describe('getAvatar (UP9/UP12) / setAvatar (UP10/UP13)', () => {
|
||||
it('USERSREPO-052: getAvatar reads the avatar column', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await users.getAvatar(user.id)).toBeNull();
|
||||
testDb.prepare('UPDATE users SET avatar = ? WHERE id = ?').run('pic.png', user.id);
|
||||
expect(await users.getAvatar(user.id)).toBe('pic.png');
|
||||
});
|
||||
|
||||
it('USERSREPO-053: setAvatar writes avatar + updated_at (both a filename and NULL for delete)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
await users.setAvatar(user.id, 'new.png');
|
||||
let row = testDb.prepare('SELECT avatar, updated_at FROM users WHERE id = ?').get(user.id) as { avatar: string; updated_at: string };
|
||||
expect(row.avatar).toBe('new.png');
|
||||
expect(row.updated_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||
|
||||
testDb.prepare("UPDATE users SET updated_at = '2020-01-01 00:00:00' WHERE id = ?").run(user.id);
|
||||
await users.setAvatar(user.id, null);
|
||||
row = testDb.prepare('SELECT avatar, updated_at FROM users WHERE id = ?').get(user.id) as { avatar: string | null; updated_at: string };
|
||||
expect(row.avatar).toBeNull();
|
||||
expect(row.updated_at).not.toBe('2020-01-01 00:00:00');
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-054: findProfileBasic (UP11) reads id/username/email/role/avatar', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('UPDATE users SET avatar = ? WHERE id = ?').run('a.png', user.id);
|
||||
expect(await users.findProfileBasic(user.id)).toEqual({ id: user.id, username: user.username, email: user.email, role: 'user', avatar: 'a.png' });
|
||||
});
|
||||
|
||||
describe('listOthersNonGuest (UP14)', () => {
|
||||
it('USERSREPO-055: excludes the given id and every guest, ordered by username', async () => {
|
||||
const { user: me } = createUser(testDb, { username: 'me' });
|
||||
const { user: bravo } = createUser(testDb, { username: 'bravo' });
|
||||
const { user: alpha } = createUser(testDb, { username: 'alpha' });
|
||||
const { user: guest } = createUser(testDb, { username: 'zzz-guest' });
|
||||
testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id);
|
||||
const rows = await users.listOthersNonGuest(me.id);
|
||||
expect(rows.map((r) => r.id)).toEqual([alpha.id, bravo.id]);
|
||||
});
|
||||
});
|
||||
|
||||
it('USERSREPO-056: getRoleAndWeatherKey (UP15) reads role + openweather_api_key', async () => {
|
||||
const { user } = createAdmin(testDb);
|
||||
testDb.prepare('UPDATE users SET openweather_api_key = ? WHERE id = ?').run('w', user.id);
|
||||
expect(await users.getRoleAndWeatherKey(user.id)).toEqual({ role: 'admin', openweather_api_key: 'w' });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user