freshyo/apps/backend/tests/helpers/sessions.ts
2026-10-20 00:31:05 +05:30

115 lines
3.3 KiB
TypeScript

/**
* Principal sessions.
*
* Each login runs once per Jest process and is cached, so a bad credential
* fails once with a clear message instead of on every request.
*/
import { apiClient, assertServerUp } from './api'
import { staffCredentials, superadminCredentials, userCredentials } from './env'
export interface StaffSession {
token: string
staffId: number
name: string
regionId: number
}
export interface UserSession {
token: string
userId: number
}
export interface SuperadminSession {
token: string
superadminId: number
username: string
}
let staffPromise: Promise<StaffSession> | null = null
let userPromise: Promise<UserSession> | null = null
let superadminPromise: Promise<SuperadminSession> | null = null
/** Clear cached sessions (for suites that deliberately break auth). */
export function resetSessions(): void {
staffPromise = null
userPromise = null
superadminPromise = null
}
export function staffSession(): Promise<StaffSession> {
staffPromise ??= loginStaff()
return staffPromise
}
export function userSession(): Promise<UserSession> {
userPromise ??= loginUser()
return userPromise
}
export function superadminSession(): Promise<SuperadminSession> {
superadminPromise ??= loginSuperadmin()
return superadminPromise
}
async function loginStaff(): Promise<StaffSession> {
await assertServerUp()
const { name, password } = staffCredentials()
const res = await apiClient().admin.staffUser.login.mutate({ name, password })
if (!res?.token || !res?.staff?.id) {
throw new Error('[api-tests] admin.staffUser.login returned no token/staff id')
}
return {
token: res.token,
staffId: Number(res.staff.id),
name: res.staff.name,
regionId: Number((res.staff as { regionId?: number }).regionId),
}
}
async function loginUser(): Promise<UserSession> {
await assertServerUp()
const { identifier, password } = userCredentials()
const res = await apiClient().user.auth.login.mutate({ identifier, password })
const token = res?.data?.token
const userId = res?.data?.user?.id
if (!token || !userId) {
throw new Error('[api-tests] user.auth.login returned no token/user id')
}
return { token, userId: Number(userId) }
}
async function loginSuperadmin(): Promise<SuperadminSession> {
await assertServerUp()
const { username, password } = superadminCredentials()
const res = await apiClient().superadmin.auth.login.mutate({ username, password })
if (!res?.token || !res?.superadmin?.id) {
throw new Error('[api-tests] superadmin.auth.login returned no token/superadmin id')
}
return {
token: res.token,
superadminId: Number(res.superadmin.id),
username: res.superadmin.username,
}
}
/** Typed client authenticated as the seeded staff admin. */
export async function staffClient() {
const session = await staffSession()
return apiClient(session.token)
}
/** Typed client authenticated as the seeded user. */
export async function userClient() {
const session = await userSession()
return apiClient(session.token)
}
/** Typed client authenticated as the seeded superadmin. */
export async function superadminClient() {
const session = await superadminSession()
return apiClient(session.token)
}
/** Anonymous client (no Authorization header). */
export const anonClient = () => apiClient()