forked from Yara724/api
83 lines
3.2 KiB
TypeScript
83 lines
3.2 KiB
TypeScript
import { ExecutionContext, UnauthorizedException } from "@nestjs/common";
|
|
import { RoleEnum } from "src/Types&Enums/role.enum";
|
|
import { LocalActorAuthGuard } from "./actor-local.guard";
|
|
|
|
describe("LocalActorAuthGuard", () => {
|
|
const credentials = { username: "new-account", password: "test-password" };
|
|
let auth: { loginFromCredentials: jest.Mock };
|
|
let jwt: { verifyAsync: jest.Mock };
|
|
let guard: LocalActorAuthGuard;
|
|
|
|
const context = (request: Record<string, unknown>) =>
|
|
({ switchToHttp: () => ({ getRequest: () => request }) }) as ExecutionContext;
|
|
|
|
beforeEach(() => {
|
|
auth = { loginFromCredentials: jest.fn() };
|
|
jwt = { verifyAsync: jest.fn() };
|
|
guard = new LocalActorAuthGuard(auth as any, jwt as any);
|
|
});
|
|
|
|
it.each([undefined, "Bearer previous-session", "Bearer expired-session"])(
|
|
"authenticates the submitted login credentials with header %s",
|
|
async (authorization) => {
|
|
const loginData = { sub: "new-account", access_token: "new-session" };
|
|
auth.loginFromCredentials.mockResolvedValue(loginData);
|
|
const request = {
|
|
url: "/api/actor/login/?source=panel",
|
|
headers: { authorization },
|
|
body: credentials,
|
|
};
|
|
|
|
await expect(guard.canActivate(context(request))).resolves.toBe(true);
|
|
expect(auth.loginFromCredentials).toHaveBeenCalledWith(credentials);
|
|
expect(jwt.verifyAsync).not.toHaveBeenCalled();
|
|
expect(request["user"]).toBe(loginData);
|
|
expect(request["identity"]).toBe(loginData);
|
|
},
|
|
);
|
|
|
|
it("does not let a previous token bypass failed credentials or captcha", async () => {
|
|
auth.loginFromCredentials.mockRejectedValue(new UnauthorizedException());
|
|
jwt.verifyAsync.mockResolvedValue({ sub: "old-account", role: RoleEnum.EXPERT });
|
|
|
|
await expect(
|
|
guard.canActivate(context({
|
|
route: { path: "/actor/login" },
|
|
headers: { authorization: "Bearer previous-session" },
|
|
body: credentials,
|
|
})),
|
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
|
expect(jwt.verifyAsync).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("continues verifying bearer tokens for protected actor routes", async () => {
|
|
const payload = { sub: "expert-id", role: RoleEnum.EXPERT };
|
|
jwt.verifyAsync.mockResolvedValue(payload);
|
|
const request = {
|
|
url: "/actor/profile",
|
|
headers: { authorization: "Bearer current-session" },
|
|
};
|
|
await expect(guard.canActivate(context(request))).resolves.toBe(true);
|
|
expect(jwt.verifyAsync).toHaveBeenCalledWith("current-session", expect.any(Object));
|
|
expect(auth.loginFromCredentials).not.toHaveBeenCalled();
|
|
expect(request["user"]).toBe(payload);
|
|
});
|
|
|
|
it("rejects protected actor routes without a token", async () => {
|
|
await expect(
|
|
guard.canActivate(context({ url: "/actor/profile", headers: {} })),
|
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
|
expect(auth.loginFromCredentials).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects expired tokens on protected routes", async () => {
|
|
jwt.verifyAsync.mockRejectedValue(new Error("expired"));
|
|
await expect(
|
|
guard.canActivate(context({
|
|
url: "/actor/profile",
|
|
headers: { authorization: "Bearer expired-session" },
|
|
})),
|
|
).rejects.toBeInstanceOf(UnauthorizedException);
|
|
});
|
|
});
|