- 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.
178 lines
4.8 KiB
TypeScript
178 lines
4.8 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { ZibalService } from "./zibal.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
|
|
describe("ZibalService", () => {
|
|
let service: ZibalService;
|
|
let prisma: PrismaService;
|
|
|
|
const mockPrisma = {
|
|
uiText: {
|
|
findUnique: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
ZibalService,
|
|
{ provide: PrismaService, useValue: mockPrisma },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<ZibalService>(ZibalService);
|
|
prisma = module.get<PrismaService>(PrismaService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
expect(service.getGatewayName()).toBe("zibal");
|
|
});
|
|
|
|
describe("Merchant & URL Resolvers", () => {
|
|
it("should read merchant from db if available", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue({
|
|
key: "ZIBAL_MERCHANT",
|
|
value: "custom-merchant",
|
|
});
|
|
const merchant = await service.getMerchant();
|
|
expect(merchant).toBe("custom-merchant");
|
|
});
|
|
|
|
it("should fallback to default sandbox if db returns null and no env set", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue(null);
|
|
const originalEnv = process.env.ZIBAL_MERCHANT;
|
|
delete process.env.ZIBAL_MERCHANT;
|
|
|
|
const merchant = await service.getMerchant();
|
|
expect(merchant).toBe("zibal");
|
|
|
|
if (originalEnv) process.env.ZIBAL_MERCHANT = originalEnv;
|
|
});
|
|
|
|
it("should get start URL with trackId", () => {
|
|
const url = service.getStartUrl(987654);
|
|
expect(url).toBe("https://gateway.zibal.ir/start/987654");
|
|
});
|
|
});
|
|
|
|
describe("requestPayment", () => {
|
|
it("should send payment request successfully", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue({
|
|
key: "ZIBAL_MERCHANT",
|
|
value: "merchant-123",
|
|
});
|
|
|
|
const mockResponse = {
|
|
trackId: 102030,
|
|
result: 100,
|
|
message: "success",
|
|
};
|
|
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(mockResponse),
|
|
} as any);
|
|
|
|
const result = await service.requestPayment({
|
|
amountRials: 1000000,
|
|
callbackUrl: "https://canina.ir/verify",
|
|
orderId: "ord-123",
|
|
description: "تست پرداخت",
|
|
mobile: "09120000000",
|
|
});
|
|
|
|
expect(result).toEqual(mockResponse);
|
|
expect(global.fetch).toHaveBeenCalledWith(
|
|
"https://gateway.zibal.ir/v1/request",
|
|
expect.objectContaining({
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should throw error when api returns non-ok status", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue({
|
|
key: "ZIBAL_MERCHANT",
|
|
value: "merchant-123",
|
|
});
|
|
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: false,
|
|
status: 400,
|
|
text: () => Promise.resolve("Invalid merchant"),
|
|
} as any);
|
|
|
|
// retry delays overridden to fast
|
|
jest
|
|
.spyOn<any, any>(service, "callWithRetry")
|
|
.mockImplementation((op: () => Promise<unknown>) => {
|
|
return op();
|
|
});
|
|
|
|
await expect(
|
|
service.requestPayment({
|
|
amountRials: 1000000,
|
|
callbackUrl: "https://canina.ir/verify",
|
|
orderId: "ord-123",
|
|
}),
|
|
).rejects.toThrow("Zibal request HTTP 400: Invalid merchant");
|
|
});
|
|
});
|
|
|
|
describe("verifyPayment", () => {
|
|
it("should verify payment successfully", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue({
|
|
key: "ZIBAL_MERCHANT",
|
|
value: "merchant-123",
|
|
});
|
|
|
|
const mockResponse = {
|
|
result: 100,
|
|
amount: 1000000,
|
|
refNumber: "REF123456",
|
|
status: 1,
|
|
message: "success",
|
|
};
|
|
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(mockResponse),
|
|
} as any);
|
|
|
|
const res = await service.verifyPayment("102030");
|
|
expect(res).toEqual(mockResponse);
|
|
});
|
|
});
|
|
|
|
describe("inquiryPayment", () => {
|
|
it("should inquiry payment successfully", async () => {
|
|
mockPrisma.uiText.findUnique.mockResolvedValue({
|
|
key: "ZIBAL_MERCHANT",
|
|
value: "merchant-123",
|
|
});
|
|
|
|
const mockResponse = {
|
|
result: 100,
|
|
amount: 1000000,
|
|
status: 1,
|
|
message: "inquiry success",
|
|
};
|
|
|
|
global.fetch = jest.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(mockResponse),
|
|
} as any);
|
|
|
|
const res = await service.inquiryPayment("102030");
|
|
expect(res).toEqual(mockResponse);
|
|
});
|
|
});
|
|
});
|