Initial Commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { AuthError } from "../error-codes/error-codes.js";
|
||||
import { mapBetterAuthUserToPrincipal } from "./principal.js";
|
||||
|
||||
describe("mapBetterAuthUserToPrincipal", () => {
|
||||
it("maps a complete user/session pair", { timeout: 5000 }, () => {
|
||||
const p = mapBetterAuthUserToPrincipal({
|
||||
user: {
|
||||
id: "user:1",
|
||||
email: "a@b.com",
|
||||
emailVerified: true,
|
||||
name: "Alice",
|
||||
image: "img://a",
|
||||
roles: ["admin", "owner"],
|
||||
},
|
||||
session: { id: "s1", userId: "user:1", activeOrganizationId: "t1" },
|
||||
});
|
||||
expect(p).toEqual({
|
||||
id: "user:1",
|
||||
email: "a@b.com",
|
||||
emailVerified: true,
|
||||
roles: ["admin", "owner"],
|
||||
tenantId: "t1",
|
||||
metadata: { name: "Alice", image: "img://a" },
|
||||
});
|
||||
});
|
||||
|
||||
it("omits optional fields when null/undefined", { timeout: 5000 }, () => {
|
||||
const p = mapBetterAuthUserToPrincipal({
|
||||
user: { id: "u2", email: null, emailVerified: null },
|
||||
session: { activeOrganizationId: null },
|
||||
});
|
||||
expect(p).toEqual({ id: "u2", emailVerified: false, roles: [] });
|
||||
expect(p.email).toBeUndefined();
|
||||
expect(p.tenantId).toBeUndefined();
|
||||
expect(p.metadata).toBeUndefined();
|
||||
});
|
||||
|
||||
it("treats emailVerified === false correctly", { timeout: 5000 }, () => {
|
||||
const p = mapBetterAuthUserToPrincipal({
|
||||
user: { id: "u3", emailVerified: false },
|
||||
session: {},
|
||||
});
|
||||
expect(p.emailVerified).toBe(false);
|
||||
});
|
||||
|
||||
it("throws AUTH_PRINCIPAL_MAPPING_FAILED on missing id", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
expect(() =>
|
||||
mapBetterAuthUserToPrincipal({
|
||||
user: { id: "" },
|
||||
session: {},
|
||||
})
|
||||
).toThrow(AuthError);
|
||||
});
|
||||
|
||||
it("throws when id is wrong type", { timeout: 5000 }, () => {
|
||||
expect(() =>
|
||||
mapBetterAuthUserToPrincipal({
|
||||
user: { id: 42 as unknown as string },
|
||||
session: {},
|
||||
})
|
||||
).toThrow(AuthError);
|
||||
});
|
||||
|
||||
it("emits only present metadata fields", { timeout: 5000 }, () => {
|
||||
const p = mapBetterAuthUserToPrincipal({
|
||||
user: { id: "u4", name: "X" },
|
||||
session: {},
|
||||
});
|
||||
expect(p.metadata).toEqual({ name: "X" });
|
||||
});
|
||||
|
||||
it("ignores empty-string tenantId / email", { timeout: 5000 }, () => {
|
||||
const p = mapBetterAuthUserToPrincipal({
|
||||
user: { id: "u5", email: "" },
|
||||
session: { activeOrganizationId: "" },
|
||||
});
|
||||
expect(p.email).toBeUndefined();
|
||||
expect(p.tenantId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user