96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
jest.mock("uuid", () => ({
|
|
v4: jest.fn(() => "mocked-uuid"),
|
|
}));
|
|
|
|
jest.mock("@noble/post-quantum/ml-kem.js", () => ({
|
|
ml_kem768: {
|
|
keygen: jest.fn(),
|
|
encapsulate: jest.fn(),
|
|
decapsulate: jest.fn(),
|
|
},
|
|
}));
|
|
|
|
jest.mock("jose", () => ({
|
|
SignJWT: jest.fn().mockReturnValue({
|
|
setProtectedHeader: jest.fn().mockReturnThis(),
|
|
setIssuedAt: jest.fn().mockReturnThis(),
|
|
setExpirationTime: jest.fn().mockReturnThis(),
|
|
sign: jest.fn().mockResolvedValue("mocked-jwt"),
|
|
}),
|
|
jwtVerify: jest.fn(),
|
|
}));
|
|
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { AuthGuard } from "../auth/guards/auth.guard";
|
|
import { AuthenticatedRequest } from "../common/interfaces/request.interface";
|
|
import { ApiKeysController } from "./api-keys.controller";
|
|
import { ApiKeysService } from "./api-keys.service";
|
|
|
|
describe("ApiKeysController", () => {
|
|
let controller: ApiKeysController;
|
|
let service: ApiKeysService;
|
|
|
|
const mockApiKeysService = {
|
|
create: jest.fn(),
|
|
findAll: jest.fn(),
|
|
revoke: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [ApiKeysController],
|
|
providers: [{ provide: ApiKeysService, useValue: mockApiKeysService }],
|
|
})
|
|
.overrideGuard(AuthGuard)
|
|
.useValue({ canActivate: () => true })
|
|
.compile();
|
|
|
|
controller = module.get<ApiKeysController>(ApiKeysController);
|
|
service = module.get<ApiKeysService>(ApiKeysService);
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("create", () => {
|
|
it("should call service.create", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
const dto = { name: "Key Name", expiresAt: "2026-01-20T12:00:00Z" };
|
|
await controller.create(req, dto);
|
|
expect(service.create).toHaveBeenCalledWith(
|
|
"user-uuid",
|
|
"Key Name",
|
|
new Date(dto.expiresAt),
|
|
);
|
|
});
|
|
|
|
it("should call service.create without expiresAt", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
const dto = { name: "Key Name" };
|
|
await controller.create(req, dto);
|
|
expect(service.create).toHaveBeenCalledWith(
|
|
"user-uuid",
|
|
"Key Name",
|
|
undefined,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("findAll", () => {
|
|
it("should call service.findAll", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
await controller.findAll(req);
|
|
expect(service.findAll).toHaveBeenCalledWith("user-uuid");
|
|
});
|
|
});
|
|
|
|
describe("revoke", () => {
|
|
it("should call service.revoke", async () => {
|
|
const req = { user: { sub: "user-uuid" } } as AuthenticatedRequest;
|
|
await controller.revoke(req, "key-id");
|
|
expect(service.revoke).toHaveBeenCalledWith("user-uuid", "key-id");
|
|
});
|
|
});
|
|
});
|