Files
mtusi2026lab7/student-tests/public-endpoint.test.ts

44 lines
1.4 KiB
TypeScript

import { test, expect } from "bun:test";
import { publicLBEndpoint } from "../scripts/public-endpoint";
function fixture(port = "18443") {
return {
Config: {
Labels: {
"com.docker.compose.project": "mtuci-vp7-security",
"com.docker.compose.service": "lb",
},
},
State: { Running: true },
NetworkSettings: {
Networks: {
private: { IPAddress: "172.20.0.2" },
edge: { IPAddress: "172.21.0.2" },
},
Ports: { "8443/tcp": [{ HostIp: "127.0.0.1", HostPort: port }] },
},
};
}
test("selects the actual published loopback listener despite two LB networks", () => {
expect(publicLBEndpoint(fixture())).toEqual({
host: "127.0.0.1",
port: 18443,
servername: "localhost",
});
});
test("rejects invalid published ports", () => {
for (const port of ["0", "65536", "-1", "x", "18443/secret", ""])
expect(() => publicLBEndpoint(fixture(port))).toThrow();
});
test("rejects unpublished, public, stopped and foreign-resource endpoints", () => {
for (const modify of [
(x: any) => (x.NetworkSettings.Ports = null),
(x: any) => (x.NetworkSettings.Ports["8443/tcp"][0].HostIp = "0.0.0.0"),
(x: any) => (x.State.Running = false),
(x: any) => (x.Config.Labels["com.docker.compose.project"] = "other"),
]) {
const f = fixture();
modify(f);
expect(() => publicLBEndpoint(f)).toThrow();
}
});