- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
141 lines
3.9 KiB
TypeScript
141 lines
3.9 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { ApiKeysController } from "./api-keys.controller";
|
|
import { ApiKeysService } from "./api-keys.service";
|
|
import { CreateApiKeyDto } from "./dto/api-key.dto";
|
|
|
|
describe("ApiKeysController (Admin)", () => {
|
|
let controller: ApiKeysController;
|
|
let service: ApiKeysService;
|
|
|
|
const mockApiKeysService = {
|
|
findAll: jest.fn(),
|
|
create: jest.fn(),
|
|
toggleStatus: jest.fn(),
|
|
delete: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [ApiKeysController],
|
|
providers: [
|
|
{
|
|
provide: ApiKeysService,
|
|
useValue: mockApiKeysService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<ApiKeysController>(ApiKeysController);
|
|
service = module.get<ApiKeysService>(ApiKeysService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("findAll", () => {
|
|
it("should return all api keys wrapped with success flag", async () => {
|
|
const apiKeys = [
|
|
{
|
|
id: "key-1",
|
|
name: "Mobile App Client",
|
|
prefix: "cn_live_1234",
|
|
isActive: true,
|
|
scopes: ["products:read", "orders:create"],
|
|
createdAt: new Date(),
|
|
},
|
|
{
|
|
id: "key-2",
|
|
name: "Accounting ERP Integration",
|
|
prefix: "cn_live_5678",
|
|
isActive: false,
|
|
scopes: ["orders:read"],
|
|
createdAt: new Date(),
|
|
},
|
|
];
|
|
|
|
mockApiKeysService.findAll.mockResolvedValue(apiKeys);
|
|
|
|
const result = await controller.findAll();
|
|
|
|
expect(service.findAll).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual({
|
|
success: true,
|
|
data: apiKeys,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("create", () => {
|
|
it("should create an api key and return success message with raw key details", async () => {
|
|
const dto: CreateApiKeyDto = {
|
|
name: "Warehouse Logistics Service",
|
|
scopes: ["inventory:read", "inventory:update"],
|
|
expiresAt: "2026-12-31T23:59:59.000Z",
|
|
};
|
|
|
|
const createdResponse = {
|
|
id: "key-3",
|
|
name: dto.name,
|
|
key: "cn_live_secret_full_key_token",
|
|
prefix: "cn_live_secr",
|
|
scopes: dto.scopes,
|
|
isActive: true,
|
|
};
|
|
|
|
mockApiKeysService.create.mockResolvedValue(createdResponse);
|
|
|
|
const result = await controller.create(dto);
|
|
|
|
expect(service.create).toHaveBeenCalledWith(dto);
|
|
expect(result).toEqual({
|
|
success: true,
|
|
message:
|
|
"کلید دسترسی با موفقیت ایجاد شد. لطفاً آن را در جای امن ذخیره نمایید.",
|
|
data: createdResponse,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("toggleStatus", () => {
|
|
it("should toggle active status of the specified key", async () => {
|
|
const keyId = "key-1";
|
|
const updatedKey = {
|
|
id: keyId,
|
|
name: "Mobile App Client",
|
|
isActive: false,
|
|
};
|
|
|
|
mockApiKeysService.toggleStatus.mockResolvedValue(updatedKey);
|
|
|
|
const result = await controller.toggleStatus(keyId);
|
|
|
|
expect(service.toggleStatus).toHaveBeenCalledWith(keyId);
|
|
expect(result).toEqual({
|
|
success: true,
|
|
message: "وضعیت کلید با موفقیت بهروزرسانی شد",
|
|
data: updatedKey,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("delete", () => {
|
|
it("should delete key and return success confirmation message", async () => {
|
|
const keyId = "key-2";
|
|
mockApiKeysService.delete.mockResolvedValue({ id: keyId });
|
|
|
|
const result = await controller.delete(keyId);
|
|
|
|
expect(service.delete).toHaveBeenCalledWith(keyId);
|
|
expect(result).toEqual({
|
|
success: true,
|
|
message: "کلید دسترسی با موفقیت حذف شد",
|
|
});
|
|
});
|
|
});
|
|
});
|