mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 22:16:26 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
import { Users } from '../../../src/db/entities/Users.entity';
|
|
import { encrypt_api_key } from '../../../src/nest/common/crypto/apiKeyCrypto';
|
|
import { encryptMfaSecret } from '../../../src/nest/common/crypto/mfaCrypto';
|
|
import { nextSeq, type FactoryOrm } from './context';
|
|
import { createRow, findRow, updateRows } from './rows';
|
|
import type { EntityDTO } from '@mikro-orm/core';
|
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
export type UserRow = EntityDTO<Users>;
|
|
|
|
export interface UserOverrides {
|
|
/** Pin the id, for suites whose session cookie is signed for a fixed user. */
|
|
id?: number;
|
|
username?: string;
|
|
email?: string;
|
|
password?: string;
|
|
role?: 'admin' | 'user';
|
|
password_version?: number;
|
|
is_guest?: number;
|
|
display_name?: string | null;
|
|
must_change_password?: number;
|
|
}
|
|
|
|
/**
|
|
* A user with a known password, stored as a bcrypt hash at cost 4 so tests
|
|
* stay fast. Defaults are unique per worker: `testuser<n>`,
|
|
* `user<n>@test.example.com`, `TestPass<n>!`.
|
|
*/
|
|
export async function makeUser(
|
|
orm: FactoryOrm,
|
|
overrides: UserOverrides = {},
|
|
): Promise<{ user: UserRow; password: string }> {
|
|
const n = nextSeq('user');
|
|
const password = overrides.password ?? `TestPass${n}!`;
|
|
const user = await createRow(orm, Users, {
|
|
...(overrides.id !== undefined ? { id: overrides.id } : {}),
|
|
username: overrides.username ?? `testuser${n}`,
|
|
email: overrides.email ?? `user${n}@test.example.com`,
|
|
password_hash: bcrypt.hashSync(password, 4),
|
|
role: overrides.role ?? 'user',
|
|
password_version: overrides.password_version ?? 0,
|
|
is_guest: overrides.is_guest ?? 0,
|
|
display_name: overrides.display_name ?? null,
|
|
must_change_password: overrides.must_change_password ?? 0,
|
|
});
|
|
return { user, password };
|
|
}
|
|
|
|
export function makeAdmin(
|
|
orm: FactoryOrm,
|
|
overrides: Omit<UserOverrides, 'role'> = {},
|
|
): Promise<{ user: UserRow; password: string }> {
|
|
return makeUser(orm, { ...overrides, role: 'admin' });
|
|
}
|
|
|
|
/** The fixed base32 TOTP secret `makeUserWithMfa` enables, so a test can compute valid codes. */
|
|
export const KNOWN_MFA_SECRET = 'JBSWY3DPEHPK3PXP';
|
|
|
|
/** A user with MFA already on, written directly so no rate-limited endpoint is involved. */
|
|
export async function makeUserWithMfa(
|
|
orm: FactoryOrm,
|
|
overrides: UserOverrides = {},
|
|
): Promise<{ user: UserRow; password: string; totpSecret: string }> {
|
|
const { user, password } = await makeUser(orm, overrides);
|
|
await updateRows(orm, Users, { id: user.id }, { mfa_enabled: 1, mfa_secret: encryptMfaSecret(KNOWN_MFA_SECRET) });
|
|
return { user: await readUser(orm, user.id), password, totpSecret: KNOWN_MFA_SECRET };
|
|
}
|
|
|
|
/** The user row as it is now; fails the test when it is gone. */
|
|
export async function readUser(orm: FactoryOrm, id: number): Promise<UserRow> {
|
|
const row = await findRow(orm, Users, { id });
|
|
if (!row) throw new Error(`readUser: no user ${id}`);
|
|
return row;
|
|
}
|
|
|
|
/** Immich credentials, with the API key encrypted the way the settings route stores it. */
|
|
export async function setImmichCredentials(
|
|
orm: FactoryOrm,
|
|
userId: number,
|
|
url: string,
|
|
apiKey: string,
|
|
): Promise<void> {
|
|
await updateRows(orm, Users, { id: userId }, { immich_url: url, immich_api_key: encrypt_api_key(apiKey) });
|
|
}
|
|
|
|
/** Synology credentials, with the password encrypted the way the settings route stores it. */
|
|
export async function setSynologyCredentials(
|
|
orm: FactoryOrm,
|
|
userId: number,
|
|
url: string,
|
|
username: string,
|
|
password: string,
|
|
): Promise<void> {
|
|
await updateRows(
|
|
orm,
|
|
Users,
|
|
{ id: userId },
|
|
{ synology_url: url, synology_username: username, synology_password: encrypt_api_key(password) },
|
|
);
|
|
}
|