import { expect, test } from "bun:test"; import { parseConfig } from "../src/server/config.ts"; const hash = "$argon2id$v=19$m=65536,t=2,p=1$c2FsdA$aGFzaA"; const s3 = ` domain = "kepr.example" data_dir = "data" admin_password_hash = "${hash}" [bucket] endpoint = "https://s3.fr-par.scw.cloud" name = "koh" region = "fr-par" access_key_id = "SCWREAD" secret_access_key = "secret" `; test("parses the ยง19 keys with an S3 bucket", () => { expect(parseConfig(s3)).toEqual({ domain: "kepr.example", dataDir: "data", adminPasswordHash: hash, bucket: { kind: "s3", endpoint: "https://s3.fr-par.scw.cloud", name: "koh", region: "fr-par", accessKeyId: "SCWREAD", secretAccessKey: "secret", }, }); }); test("parses optional S3 write keys", () => { const config = parseConfig( `${s3}[bucket.write]\naccess_key_id = "SCWWRITE"\nsecret_access_key = "wsecret"\n`, ); expect(config.bucket).toMatchObject({ write: { accessKeyId: "SCWWRITE", secretAccessKey: "wsecret" }, }); expect(() => parseConfig(`${s3}[bucket.write]\naccess_key_id = ""\nsecret_access_key = "x"\n`), ).toThrow("access_key_id is required"); expect(() => parseConfig( `domain = "d"\ndata_dir = "data"\nadmin_password_hash = "${hash}"\n[bucket]\nfolder = "/tmp/b"\n[bucket.write]\naccess_key_id = "x"\nsecret_access_key = "y"\n`, ), ).toThrow("cannot be combined"); }); test("parses a folder source", () => { const text = `domain = "d"\ndata_dir = "data"\nadmin_password_hash = "${hash}"\n[bucket]\nfolder = "/tmp/b"\n`; expect(parseConfig(text).bucket).toEqual({ kind: "folder", path: "/tmp/b" }); }); test("fails loudly, naming the problem", () => { expect(() => parseConfig("domain = ")).toThrow("not valid TOML"); expect(() => parseConfig(s3.replace('domain = "kepr.example"', ""))).toThrow( "domain is required", ); expect(() => parseConfig(s3.replace(/\[bucket\][\s\S]*/, ""))).toThrow("[bucket] is missing"); expect(() => parseConfig(s3.replace('region = "fr-par"', ""))).toThrow( "[bucket] region is required", ); expect(() => parseConfig(s3.replace(hash, "plaintext"))).toThrow("Argon2id"); expect(() => parseConfig(`${s3}folder = "/tmp/b"\n`)).toThrow("cannot be combined"); expect(() => parseConfig(s3.replace("https://s3.fr-par.scw.cloud", "nope"))).toThrow( "must be a URL", ); }); test("refuses a domain that is not a bare hostname, since install.sh embeds it as is", () => { for (const domain of ["kepr.example:8443", "kepr.example:65535", "localhost", "a-b.c1.example"]) { expect(parseConfig(s3.replace("kepr.example", domain)).domain).toBe(domain); } for (const domain of [ "kepr.example/x", "https://kepr.example", "kepr.example$(id)", "kepr.example; rm x", "kepr.example\\nx", "-kepr.example", "kepr..example", "kepr.example:", "kepr.example:0", "kepr.example:65536", "kepr.example:99999", "user@kepr.example", ]) { expect(() => parseConfig(s3.replace("kepr.example", domain))).toThrow("bare hostname"); } });