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); prisma = module.get(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(service, "callWithRetry") .mockImplementation((op: () => Promise) => { 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); }); }); });