Files
canina/backend/src/common/revalidation/revalidation.service.spec.ts
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

114 lines
3.6 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { RevalidationService } from "./revalidation.service";
describe("RevalidationService", () => {
let service: RevalidationService;
const originalEnv = process.env;
beforeEach(async () => {
process.env = { ...originalEnv };
process.env.REVALIDATION_SECRET = "test-secret-123";
process.env.FRONTEND_INTERNAL_URL = "http://frontend_mock:8080";
const module: TestingModule = await Test.createTestingModule({
providers: [RevalidationService],
}).compile();
service = module.get<RevalidationService>(RevalidationService);
});
afterEach(() => {
process.env = originalEnv;
jest.restoreAllMocks();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("revalidateTag", () => {
it("should send POST request with tag and secret header and return true on 200", async () => {
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
ok: true,
status: 200,
} as any);
const result = await service.revalidateTag("products");
expect(result).toBe(true);
expect(mockFetch).toHaveBeenCalledWith(
"http://frontend_mock:8080/api/revalidate",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
"x-revalidate-secret": "test-secret-123",
}),
body: JSON.stringify({ tag: "products", secret: "test-secret-123" }),
}),
);
});
it("should return false if frontend responds with non-ok status", async () => {
jest.spyOn(global, "fetch" as any).mockResolvedValue({
ok: false,
status: 500,
statusText: "Internal Error",
} as any);
const result = await service.revalidateTag("products");
expect(result).toBe(false);
});
it("should catch network errors gracefully and return false", async () => {
jest
.spyOn(global, "fetch" as any)
.mockRejectedValue(new Error("Connection refused"));
const result = await service.revalidateTag("products");
expect(result).toBe(false);
});
});
describe("revalidatePath", () => {
it("should send POST request with path and secret and return true on success", async () => {
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
ok: true,
status: 200,
} as any);
const result = await service.revalidatePath("/shop");
expect(result).toBe(true);
expect(mockFetch).toHaveBeenCalledWith(
"http://frontend_mock:8080/api/revalidate",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ path: "/shop", secret: "test-secret-123" }),
}),
);
});
});
describe("revalidateProduct", () => {
it("should trigger all tags and paths for product slug and artNo", async () => {
const tagSpy = jest
.spyOn(service, "revalidateTag")
.mockResolvedValue(true);
const pathSpy = jest
.spyOn(service, "revalidatePath")
.mockResolvedValue(true);
await service.revalidateProduct("canivita-tonic", "ART-100");
expect(tagSpy).toHaveBeenCalledWith("products");
expect(tagSpy).toHaveBeenCalledWith("home-data");
expect(tagSpy).toHaveBeenCalledWith("product-canivita-tonic");
expect(tagSpy).toHaveBeenCalledWith("product-ART-100");
expect(pathSpy).toHaveBeenCalledWith("/shop");
expect(pathSpy).toHaveBeenCalledWith("/");
expect(pathSpy).toHaveBeenCalledWith("/shop/canivita-tonic");
expect(pathSpy).toHaveBeenCalledWith("/shop/ART-100");
});
});
});