Files
FFF_vtt_frozen/tests/tconfig.nim
T

139 lines
5.5 KiB
Nim

## Config keys (DESIGN 11.1; names FIXED owner-delegated, #16): flags win over env wins over defaults.
## The bind address is a constant, not a key: the server always listens on 127.0.0.1 (T15).
import std/[unittest, tables, strutils]
import config
proc envOf(pairs: openArray[(string, string)]): EnvLookup =
let t = pairs.toTable
result = proc(k: string): string {.closure.} =
if k in t: t[k] else: ""
let noEnv = envOf([])
proc parseErr(args: openArray[string], env: EnvLookup = noEnv): string =
try:
discard parseConfig(args, env)
"<none>"
except ConfigError as e:
e.msg
suite "config defaults":
test "defaults: dev origin, port 9000, state dir run":
let c = parseConfig([], noEnv)
check c.publicOrigin == DefaultPublicOrigin
check c.publicOrigin == "http://localhost:8080"
check c.port == DefaultPort
check c.port == 9000
check c.stateDir == DefaultStateDir
check c.stateDir == "run"
check c.dbPath == "run/state.db"
test "state dir is joined, not concatenated":
let c = parseConfig(["--state-dir", "/var/lib/fff-vtt"], noEnv)
check c.dbPath == "/var/lib/fff-vtt/state.db"
test "bind address is fixed 127.0.0.1 and not a config key":
check BindAddress == "127.0.0.1"
check "--bind" in parseErr(["--bind=0.0.0.0"])
suite "config env":
test "env sets all three keys":
let c = parseConfig([], envOf([
("FFF_VTT_PUBLIC_ORIGIN", "https://vtt.example"),
("FFF_VTT_PORT", "9100"),
("FFF_VTT_STATE_DIR", "/srv/fff")]))
check c.publicOrigin == "https://vtt.example"
check c.port == 9100
check c.stateDir == "/srv/fff"
test "empty env keys fall back to defaults":
let c = parseConfig([], envOf([("FFF_VTT_PORT", ""), ("FFF_VTT_STATE_DIR", " ")]))
check c.port == DefaultPort
check c.stateDir == DefaultStateDir
test "bad env port is an error, not a default":
check "FFF_VTT_PORT" in parseErr([], envOf([("FFF_VTT_PORT", "http")]))
suite "config flags":
test "flags override env":
let c = parseConfig(["--public-origin=https://a.example", "--port", "9200",
"--state-dir=/tmp/x"], envOf([
("FFF_VTT_PUBLIC_ORIGIN", "https://b.example"),
("FFF_VTT_PORT", "9100"),
("FFF_VTT_STATE_DIR", "/srv/old")]))
check c.publicOrigin == "https://a.example"
check c.port == 9200
check c.stateDir == "/tmp/x"
test "--key=value and --key value are both accepted":
check parseConfig(["--port=9301"], noEnv).port == 9301
check parseConfig(["--port", "9302"], noEnv).port == 9302
test "trailing slash on the origin and on the state dir is dropped":
let c = parseConfig(["--public-origin=https://vtt.example/", "--state-dir=run/test/"], noEnv)
check c.publicOrigin == "https://vtt.example"
check c.stateDir == "run/test"
test "root state dir is not mangled":
check parseConfig(["--state-dir=/"], noEnv).stateDir == "/"
test "last flag wins":
check parseConfig(["--port=9303", "--port=9304"], noEnv).port == 9304
suite "config validation":
test "unknown flag":
check "--nope" in parseErr(["--nope"])
check "--nope" in parseErr(["--nope=1"])
test "positional arguments are refused":
check "unexpected argument" in parseErr(["serve"])
test "port must be a number in 1..65535":
check "--port" in parseErr(["--port=0"])
check "--port" in parseErr(["--port=65536"])
check "--port" in parseErr(["--port=-1"])
check "--port" in parseErr(["--port=90x"])
check "--port" in parseErr(["--port", ""])
check "--port" in parseErr(["--port"]) # missing value
check parseConfig(["--port=1"], noEnv).port == 1
check parseConfig(["--port=65535"], noEnv).port == 65535
test "origin needs an http(s) scheme and a host":
check "--public-origin" in parseErr(["--public-origin=localhost:8080"])
check "--public-origin" in parseErr(["--public-origin=ftp://x"])
check "--public-origin" in parseErr(["--public-origin=http://"])
check "--public-origin" in parseErr(["--public-origin=https:///path"])
test "origin has no path, query or fragment":
check "--public-origin" in parseErr(["--public-origin=https://x.example/path"])
check "--public-origin" in parseErr(["--public-origin=https://x.example?q=1"])
check "--public-origin" in parseErr(["--public-origin=https://x.example#f"])
test "origin host is a DNS name or IPv4 literal, not arbitrary text":
check "--public-origin" in parseErr(["--public-origin=https://bad host"])
check "--public-origin" in parseErr(["--public-origin=https://x.example:abc"])
check parseConfig(["--public-origin=http://10.0.0.5:9000"], noEnv).publicOrigin ==
"http://10.0.0.5:9000"
test "origin keeps an explicit port and lowercases the scheme and host":
check parseConfig(["--public-origin=https://VTT.Example:8443"], noEnv).publicOrigin ==
"https://vtt.example:8443"
test "state dir must be non-empty":
check "--state-dir" in parseErr(["--state-dir="])
check "--state-dir" in parseErr(["--state-dir", " "])
test "--help is not an error and asks for usage, even with a broken env":
check parseConfig(["--help"], noEnv).help
check parseConfig(["-h"], noEnv).help
check not parseConfig([], noEnv).help
check parseConfig(["--help"], envOf([("FFF_VTT_PORT", "http")])).help
test "usage lists the three keys and says the bind is fixed":
let u = usage()
check "--public-origin" in u
check "--port" in u
check "--state-dir" in u
check "FFF_VTT_PUBLIC_ORIGIN" in u
check "127.0.0.1" in u