UNPKG

dte-signer-sv

Version:

Sign Digital Tax Documents (DTE) for El Salvador's Ministry of Finance

79 lines 3.58 kB
import Certificate from "../lib/certificate"; import { readFileSync } from "fs"; import path from "path"; // Use process.cwd() and relative path for Jest compatibility const testDir = path.join(process.cwd(), "test"); const tempDir = path.join(process.cwd(), "temp"); describe("Certificate", () => { let validCertificateXML; let certificate; beforeAll(() => { // Load test certificate validCertificateXML = readFileSync(path.join(tempDir, "88888888888888.crt"), "utf8"); }); beforeEach(() => { certificate = new Certificate(); }); describe("parseCertificate", () => { it("should parse a valid certificate XML", async () => { const result = await certificate.parseCertificate(validCertificateXML); expect(result).toBe(certificate); }); it("should throw an error for invalid XML", async () => { const invalidXML = "<invalid>not a certificate</invalid>"; await expect(certificate.parseCertificate(invalidXML)).resolves.not.toThrow(); }); it("should handle empty string", async () => { await expect(certificate.parseCertificate("")).rejects.toThrow(); }); }); describe("validatePrivatePassword", () => { beforeEach(async () => { await certificate.parseCertificate(validCertificateXML); }); it("should return true for correct password", () => { const validPassword = "newpassword123"; const result = certificate.validatePrivatePassword(validPassword); expect(result).toBe(true); }); it("should return false for incorrect password", () => { const invalidPassword = "wrongpassword"; const result = certificate.validatePrivatePassword(invalidPassword); expect(result).toBe(false); }); it("should return false for empty password", () => { const result = certificate.validatePrivatePassword(""); expect(result).toBe(false); }); it("should be case sensitive", () => { const result = certificate.validatePrivatePassword("NEWPASSWORD123"); expect(result).toBe(false); }); }); describe("getPrivateKey", () => { beforeEach(async () => { await certificate.parseCertificate(validCertificateXML); }); it("should return the private key as base64 string", () => { const privateKey = certificate.getPrivateKey(); expect(typeof privateKey).toBe("string"); expect(privateKey.length).toBeGreaterThan(0); // Check if it's valid base64 expect(() => Buffer.from(privateKey, "base64")).not.toThrow(); }); it("should return the expected private key", () => { const privateKey = certificate.getPrivateKey(); // The private key should start with MII (typical for RSA private keys in base64) expect(privateKey.substring(0, 3)).toBe("MII"); }); }); describe("Certificate structure validation", () => { it("should have required fields in parsed certificate", async () => { await certificate.parseCertificate(validCertificateXML); // We can't directly access private fields, but we can test the methods work expect(() => certificate.getPrivateKey()).not.toThrow(); expect(() => certificate.validatePrivatePassword("test")).not.toThrow(); }); }); }); //# sourceMappingURL=certificate.test.js.map