import { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types'; import type { Response } from 'supertest'; export interface AccessTokenBody { accessToken: string; } export interface UserBody { id: string; email: string; name: string; role: string; isActive: boolean; createdAt: string; deactivatedAt: string | null; } export interface LoginResult { accessToken: string; refreshTokenCookie: string; } export function extractCookie(response: Response, name: string): string { const setCookie = (response.headers['set-cookie'] as unknown as string[] | undefined) ?? []; const raw = setCookie.find((cookie) => cookie.startsWith(`${name}=`)); if (!raw) { throw new Error(`Expected a Set-Cookie header for "${name}"`); } return raw.split(';')[0]; } export async function login( app: INestApplication, email: string, password: string, ): Promise { const response = await request(app.getHttpServer()) .post('/api/v1/auth/login') .send({ email, password }) .expect(200); return { accessToken: (response.body as AccessTokenBody).accessToken, refreshTokenCookie: extractCookie(response, 'refreshToken'), }; }