e5c6c389ea
- unit: fixture shapes, mock API (GET/POST/PUT/DELETE), mock DB operations - integration: real DB CRUD via testDb (list, filter, create, get, update, delete) - e2e: handler calls directly with mock FreshContext + real DB covers auth (employee vs non-employee), 400/403/404 cases - adds test:e2e deno task and CI step - adds tests/helpers/handler.ts with makeEmployeeContext, makeContextWithAffiliation, makeGetRequest, makeJsonRequest utilities Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
// Helper pour les tests E2E — appel direct des handlers Fresh
|
|
// sans lancer de serveur HTTP
|
|
|
|
import { FreshContext } from "$fresh/server.ts";
|
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
|
import { CasContent } from "$root/defaults/interfaces.ts";
|
|
|
|
const BASE_EMPLOYEE_SESSION: CasContent = {
|
|
amuCampus: "",
|
|
amuComposante: "",
|
|
amuDateValidation: "",
|
|
coGroup: "",
|
|
eduPersonPrimaryAffiliation: "employee",
|
|
eduPersonPrincipalName: "test.user@polytech.fr",
|
|
mail: "test.user@polytech.fr",
|
|
displayName: "Test User",
|
|
givenName: "Test",
|
|
memberOf: [],
|
|
sn: "User",
|
|
supannCivilite: "M.",
|
|
supannEntiteAffectation: "",
|
|
supannEtuAnneeInscription: "",
|
|
supannEtuEtape: "",
|
|
uid: "test.user",
|
|
};
|
|
|
|
/**
|
|
* Crée un FreshContext mock authentifié en tant qu'employee.
|
|
*/
|
|
export function makeEmployeeContext(
|
|
params: Record<string, string> = {},
|
|
): FreshContext<AuthenticatedState> {
|
|
return {
|
|
params,
|
|
state: {
|
|
isAuthenticated: true,
|
|
session: { ...BASE_EMPLOYEE_SESSION },
|
|
availablePages: {},
|
|
},
|
|
render: () => Promise.resolve(new Response()),
|
|
renderNotFound: () => Promise.resolve(new Response(null, { status: 404 })),
|
|
next: () => Promise.resolve(new Response()),
|
|
} as unknown as FreshContext<AuthenticatedState>;
|
|
}
|
|
|
|
/**
|
|
* Crée un FreshContext mock avec un affiliation personnalisée.
|
|
*/
|
|
export function makeContextWithAffiliation(
|
|
affiliation: string,
|
|
params: Record<string, string> = {},
|
|
): FreshContext<AuthenticatedState> {
|
|
const ctx = makeEmployeeContext(params);
|
|
(ctx.state as AuthenticatedState).session.eduPersonPrimaryAffiliation =
|
|
affiliation;
|
|
return ctx;
|
|
}
|
|
|
|
/**
|
|
* Crée une Request GET simple.
|
|
*/
|
|
export function makeGetRequest(
|
|
path: string,
|
|
searchParams?: Record<string, string>,
|
|
): Request {
|
|
const url = new URL(`http://localhost${path}`);
|
|
if (searchParams) {
|
|
for (const [k, v] of Object.entries(searchParams)) {
|
|
url.searchParams.set(k, v);
|
|
}
|
|
}
|
|
return new Request(url.toString());
|
|
}
|
|
|
|
/**
|
|
* Crée une Request POST/PUT avec un corps JSON.
|
|
*/
|
|
export function makeJsonRequest(
|
|
path: string,
|
|
method: string,
|
|
body: unknown,
|
|
): Request {
|
|
return new Request(`http://localhost${path}`, {
|
|
method,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|