refactor(auth): address server review feedback
- 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.
This commit is contained in:
1 parent
72e127888c
commit
1830feb71b
18 files changed
+399
-208
No files matched your search
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user