- Extract auth DTOs, cookie/JWT helpers and the generic duration parser. - Configure the session cookie and map domain auth failures to HTTP responses. - Add typed Swagger responses and focused regression coverage.
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { AuthSession } from "../src/auth/auth.service";
|
|
import {
|
|
clearedSessionCookie,
|
|
cookieToken,
|
|
requestToken,
|
|
sessionCookie,
|
|
} from "../src/auth/auth.helpers";
|
|
import { Public, IS_PUBLIC_KEY } from "../src/auth/public.decorator";
|
|
|
|
describe("authentication helpers", () => {
|
|
const request = {
|
|
secure: false,
|
|
get: (name: string) =>
|
|
name === "x-forwarded-proto" ? undefined : undefined,
|
|
} as never;
|
|
const session: AuthSession = {
|
|
user: {
|
|
id: "u",
|
|
username: "user",
|
|
createdAt: new Date(0),
|
|
updatedAt: new Date(0),
|
|
},
|
|
token: "a.token.value",
|
|
expiresAt: new Date(Date.now() + 60_000),
|
|
};
|
|
|
|
it("sets and clears a configurable cookie name", () => {
|
|
expect(sessionCookie(request, session, "custom_session")).toContain(
|
|
"custom_session=a.token.value",
|
|
);
|
|
expect(clearedSessionCookie(request, "custom_session")).toContain(
|
|
"custom_session=;",
|
|
);
|
|
});
|
|
|
|
it("reads only the selected cookie and supports bearer credentials", () => {
|
|
expect(
|
|
cookieToken("other=x; custom_session=encoded%20value", "custom_session"),
|
|
).toBe("encoded value");
|
|
expect(
|
|
requestToken(
|
|
{
|
|
get: (name: string) =>
|
|
name === "authorization" ? "Bearer token" : undefined,
|
|
} as never,
|
|
"x",
|
|
),
|
|
).toBe("token");
|
|
});
|
|
|
|
it("stores public-route metadata under a Symbol key", () => {
|
|
class PublicController {}
|
|
Public()(PublicController);
|
|
expect(typeof IS_PUBLIC_KEY).toBe("symbol");
|
|
expect(Reflect.getMetadata(IS_PUBLIC_KEY, PublicController)).toBe(true);
|
|
});
|
|
});
|