1
0
Fork 0
fahrPool/web/lib/firma/queries.test.ts

38 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { SupabaseClient } from "@supabase/supabase-js";
import { pruefeFirmenzugang } from "./queries";
type Antworten = { profil: { role: string; firma_id: string | null } | null; firma: { id: string } | null };
function fakeClient(a: Antworten): SupabaseClient {
return {
from(tabelle: string) {
const daten = tabelle === "profiles" ? a.profil : a.firma;
const kette = {
select: () => kette,
eq: () => kette,
maybeSingle: async () => ({ data: daten, error: null }),
};
return kette;
},
} as unknown as SupabaseClient;
}
describe("pruefeFirmenzugang", () => {
it("Superadmin", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: { role: "superadmin", firma_id: null }, firma: null }), "u")).toBe("superadmin");
});
it("aktive Firma", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: { role: "fahrer", firma_id: "f" }, firma: { id: "f" } }), "u")).toBe("ok");
});
it("deaktivierte Firma (RLS liefert keine Zeile)", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: { role: "fahrer", firma_id: "f" }, firma: null }), "u")).toBe("gesperrt");
});
it("Nutzer ohne Firma", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: { role: "readonly", firma_id: null }, firma: null }), "u")).toBe("gesperrt");
});
it("kein Profil", async () => {
expect(await pruefeFirmenzugang(fakeClient({ profil: null, firma: null }), "u")).toBe("gesperrt");
});
});