refactor: add missing values to test

This commit is contained in:
Mauricio Siu
2025-01-19 01:21:03 -06:00
parent 52c83fd6fc
commit 43b7db00f9

View File

@@ -1,95 +1,98 @@
import { fs, vol } from "memfs"; import { fs, vol } from "memfs";
vi.mock("node:fs", () => ({ vi.mock("node:fs", () => ({
...fs, ...fs,
default: fs, default: fs,
})); }));
import type { Admin, FileConfig } from "@dokploy/server"; import type { Admin, FileConfig } from "@dokploy/server";
import { import {
createDefaultServerTraefikConfig, createDefaultServerTraefikConfig,
loadOrCreateConfig, loadOrCreateConfig,
updateServerTraefik, updateServerTraefik,
} from "@dokploy/server"; } from "@dokploy/server";
import { beforeEach, expect, test, vi } from "vitest"; import { beforeEach, expect, test, vi } from "vitest";
const baseAdmin: Admin = { const baseAdmin: Admin = {
createdAt: "", cleanupCacheApplications: false,
authId: "", cleanupCacheOnCompose: false,
adminId: "string", cleanupCacheOnPreviews: false,
serverIp: null, createdAt: "",
certificateType: "none", authId: "",
host: null, adminId: "string",
letsEncryptEmail: null, serverIp: null,
sshPrivateKey: null, certificateType: "none",
enableDockerCleanup: false, host: null,
enableLogRotation: false, letsEncryptEmail: null,
serversQuantity: 0, sshPrivateKey: null,
stripeCustomerId: "", enableDockerCleanup: false,
stripeSubscriptionId: "", enableLogRotation: false,
serversQuantity: 0,
stripeCustomerId: "",
stripeSubscriptionId: "",
}; };
beforeEach(() => { beforeEach(() => {
vol.reset(); vol.reset();
createDefaultServerTraefikConfig(); createDefaultServerTraefikConfig();
}); });
test("Should read the configuration file", () => { test("Should read the configuration file", () => {
const config: FileConfig = loadOrCreateConfig("dokploy"); const config: FileConfig = loadOrCreateConfig("dokploy");
expect(config.http?.routers?.["dokploy-router-app"]?.service).toBe( expect(config.http?.routers?.["dokploy-router-app"]?.service).toBe(
"dokploy-service-app", "dokploy-service-app"
); );
}); });
test("Should apply redirect-to-https", () => { test("Should apply redirect-to-https", () => {
updateServerTraefik( updateServerTraefik(
{ {
...baseAdmin, ...baseAdmin,
certificateType: "letsencrypt", certificateType: "letsencrypt",
}, },
"example.com", "example.com"
); );
const config: FileConfig = loadOrCreateConfig("dokploy"); const config: FileConfig = loadOrCreateConfig("dokploy");
expect(config.http?.routers?.["dokploy-router-app"]?.middlewares).toContain( expect(config.http?.routers?.["dokploy-router-app"]?.middlewares).toContain(
"redirect-to-https", "redirect-to-https"
); );
}); });
test("Should change only host when no certificate", () => { test("Should change only host when no certificate", () => {
updateServerTraefik(baseAdmin, "example.com"); updateServerTraefik(baseAdmin, "example.com");
const config: FileConfig = loadOrCreateConfig("dokploy"); const config: FileConfig = loadOrCreateConfig("dokploy");
expect(config.http?.routers?.["dokploy-router-app-secure"]).toBeUndefined(); expect(config.http?.routers?.["dokploy-router-app-secure"]).toBeUndefined();
}); });
test("Should not touch config without host", () => { test("Should not touch config without host", () => {
const originalConfig: FileConfig = loadOrCreateConfig("dokploy"); const originalConfig: FileConfig = loadOrCreateConfig("dokploy");
updateServerTraefik(baseAdmin, null); updateServerTraefik(baseAdmin, null);
const config: FileConfig = loadOrCreateConfig("dokploy"); const config: FileConfig = loadOrCreateConfig("dokploy");
expect(originalConfig).toEqual(config); expect(originalConfig).toEqual(config);
}); });
test("Should remove websecure if https rollback to http", () => { test("Should remove websecure if https rollback to http", () => {
const originalConfig: FileConfig = loadOrCreateConfig("dokploy"); const originalConfig: FileConfig = loadOrCreateConfig("dokploy");
updateServerTraefik( updateServerTraefik(
{ ...baseAdmin, certificateType: "letsencrypt" }, { ...baseAdmin, certificateType: "letsencrypt" },
"example.com", "example.com"
); );
updateServerTraefik({ ...baseAdmin, certificateType: "none" }, "example.com"); updateServerTraefik({ ...baseAdmin, certificateType: "none" }, "example.com");
const config: FileConfig = loadOrCreateConfig("dokploy"); const config: FileConfig = loadOrCreateConfig("dokploy");
expect(config.http?.routers?.["dokploy-router-app-secure"]).toBeUndefined(); expect(config.http?.routers?.["dokploy-router-app-secure"]).toBeUndefined();
expect( expect(
config.http?.routers?.["dokploy-router-app"]?.middlewares, config.http?.routers?.["dokploy-router-app"]?.middlewares
).not.toContain("redirect-to-https"); ).not.toContain("redirect-to-https");
}); });