- 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.
215 lines
7.0 KiB
TypeScript
215 lines
7.0 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { SslController } from "./ssl.controller";
|
|
import { SslService, SslCertInfo } from "./ssl.service";
|
|
import { BadRequestException } from "@nestjs/common";
|
|
|
|
describe("SslController (Admin)", () => {
|
|
let controller: SslController;
|
|
let service: SslService;
|
|
|
|
const mockSslService = {
|
|
getStatus: jest.fn(),
|
|
updateCertificates: jest.fn(),
|
|
testOnlineDomainSsl: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [SslController],
|
|
providers: [
|
|
{
|
|
provide: SslService,
|
|
useValue: mockSslService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<SslController>(SslController);
|
|
service = module.get<SslService>(SslService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("getStatus", () => {
|
|
it("should return SSL certificate status wrapped in success: true", () => {
|
|
const mockStatus: SslCertInfo = {
|
|
exists: true,
|
|
subject: "CN=canina.ir",
|
|
issuer: "Let's Encrypt Authority X3",
|
|
validFrom: "2026-01-01T00:00:00.000Z",
|
|
validTo: "2026-04-01T00:00:00.000Z",
|
|
validFromShamsi: "۱۱ دی ۱۴۰۴",
|
|
validToShamsi: "۱۲ فروردین ۱۴۰۵",
|
|
daysRemaining: 75,
|
|
isExpired: false,
|
|
domains: ["canina.ir", "www.canina.ir"],
|
|
fingerprint: "AB:CD:EF:...",
|
|
serialNumber: "1234567890",
|
|
};
|
|
|
|
mockSslService.getStatus.mockReturnValue(mockStatus);
|
|
|
|
const result = controller.getStatus();
|
|
|
|
expect(service.getStatus).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual({ success: true, data: mockStatus });
|
|
});
|
|
|
|
it("should return exists: false when no cert is installed", () => {
|
|
const mockStatus: SslCertInfo = { exists: false };
|
|
mockSslService.getStatus.mockReturnValue(mockStatus);
|
|
|
|
const result = controller.getStatus();
|
|
|
|
expect(service.getStatus).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual({ success: true, data: { exists: false } });
|
|
});
|
|
});
|
|
|
|
describe("updateByText", () => {
|
|
it("should update SSL certificate using raw PEM text and private key", () => {
|
|
const certificate =
|
|
"-----BEGIN CERTIFICATE-----\nMIIB...fake-cert\n-----END CERTIFICATE-----";
|
|
const privateKey =
|
|
"-----BEGIN PRIVATE KEY-----\nMIIE...fake-key\n-----END PRIVATE KEY-----";
|
|
|
|
const updateResponse = {
|
|
success: true,
|
|
message: "گواهی SSL با موفقیت بهروزرسانی و در سرور ذخیره شد.",
|
|
status: {
|
|
exists: true,
|
|
subject: "CN=stage.canina.ir",
|
|
daysRemaining: 90,
|
|
isExpired: false,
|
|
},
|
|
};
|
|
|
|
mockSslService.updateCertificates.mockReturnValue(updateResponse);
|
|
|
|
const result = controller.updateByText(certificate, privateKey);
|
|
|
|
expect(service.updateCertificates).toHaveBeenCalledWith(
|
|
certificate,
|
|
privateKey,
|
|
);
|
|
expect(result).toEqual(updateResponse);
|
|
});
|
|
});
|
|
|
|
describe("uploadFiles", () => {
|
|
it("should throw BadRequestException if both cert and key files/text are missing", () => {
|
|
expect(() => controller.uploadFiles([], undefined, undefined)).toThrow(
|
|
BadRequestException,
|
|
);
|
|
expect(() => controller.uploadFiles([], undefined, undefined)).toThrow(
|
|
"ارسال هر دو فایل گواهی (Fullchain/CRT) و کلید خصوصی (Private Key) الزامی است.",
|
|
);
|
|
expect(service.updateCertificates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should throw BadRequestException if only certificate is provided and private key is missing", () => {
|
|
const certFile = {
|
|
fieldname: "certFile",
|
|
originalname: "fullchain.pem",
|
|
buffer: Buffer.from(
|
|
"-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
|
|
),
|
|
} as Express.Multer.File;
|
|
|
|
expect(() =>
|
|
controller.uploadFiles([certFile], undefined, undefined),
|
|
).toThrow(BadRequestException);
|
|
expect(service.updateCertificates).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should parse uploaded certificate and private key files and pass them to updateCertificates", () => {
|
|
const certPem =
|
|
"-----BEGIN CERTIFICATE-----\nMIIB...fake-cert\n-----END CERTIFICATE-----";
|
|
const keyPem =
|
|
"-----BEGIN PRIVATE KEY-----\nMIIE...fake-key\n-----END PRIVATE KEY-----";
|
|
|
|
const certFile = {
|
|
fieldname: "fullchain",
|
|
originalname: "fullchain.crt",
|
|
buffer: Buffer.from(certPem),
|
|
} as Express.Multer.File;
|
|
|
|
const keyFile = {
|
|
fieldname: "privkey",
|
|
originalname: "private.key",
|
|
buffer: Buffer.from(keyPem),
|
|
} as Express.Multer.File;
|
|
|
|
const updateResponse = {
|
|
success: true,
|
|
message: "گواهی SSL با موفقیت بهروزرسانی و در سرور ذخیره شد.",
|
|
status: { exists: true },
|
|
};
|
|
|
|
mockSslService.updateCertificates.mockReturnValue(updateResponse);
|
|
|
|
const result = controller.uploadFiles(
|
|
[certFile, keyFile],
|
|
undefined,
|
|
undefined,
|
|
);
|
|
|
|
expect(service.updateCertificates).toHaveBeenCalledWith(certPem, keyPem);
|
|
expect(result).toEqual(updateResponse);
|
|
});
|
|
|
|
it("should accept cert and key from body text fallback if files array does not have them", () => {
|
|
const certPem =
|
|
"-----BEGIN CERTIFICATE-----\nMIIB...text-cert\n-----END CERTIFICATE-----";
|
|
const keyPem =
|
|
"-----BEGIN PRIVATE KEY-----\nMIIE...text-key\n-----END PRIVATE KEY-----";
|
|
|
|
const updateResponse = {
|
|
success: true,
|
|
message: "Updated via text fallback",
|
|
};
|
|
mockSslService.updateCertificates.mockReturnValue(updateResponse);
|
|
|
|
const result = controller.uploadFiles([], certPem, keyPem);
|
|
|
|
expect(service.updateCertificates).toHaveBeenCalledWith(certPem, keyPem);
|
|
expect(result).toEqual(updateResponse);
|
|
});
|
|
});
|
|
|
|
describe("testDomain", () => {
|
|
it("should throw BadRequestException if domain is empty or missing", () => {
|
|
expect(() => controller.testDomain("")).toThrow(BadRequestException);
|
|
expect(() => controller.testDomain(undefined as any)).toThrow(
|
|
"نام دامنه الزامی است",
|
|
);
|
|
expect(service.testOnlineDomainSsl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should query online domain SSL configuration", async () => {
|
|
const domain = "canina.ir";
|
|
const domainResult = {
|
|
onlineCheck: true,
|
|
domain: "canina.ir",
|
|
authorized: true,
|
|
validTo: "2026-05-01T00:00:00.000Z",
|
|
issuer: "Let's Encrypt",
|
|
subject: "canina.ir",
|
|
};
|
|
|
|
mockSslService.testOnlineDomainSsl.mockResolvedValue(domainResult);
|
|
|
|
const result = await controller.testDomain(domain);
|
|
|
|
expect(service.testOnlineDomainSsl).toHaveBeenCalledWith(domain);
|
|
expect(result).toEqual(domainResult);
|
|
});
|
|
});
|
|
});
|