Files
uberbringer/internal/pkg/config/config_test.go
T
57_WolveandClaude Opus 5.5 9b91109550
Test / Lint, test and build (push) Successful in 3m17s
Adopt norn's CI, release, API and docs practices; bare module path
Module
- The module path is now the bare "uberbringer"; imports read
  uberbringer/internal/... The banner keeps the Gitea URL.

CI and releases
- Workflows move to .gitea/workflows: test.yml (every branch push:
  gofmt, goimports, tidy, vet, staticcheck, race tests, goreleaser
  snapshot), security.yml (govulncheck weekly and on main) and a hardened
  release.yml (v* tags on main only, tags fetched, goreleaser pinned to
  2.20.3). The broken labeler workflow is removed.
- go.mod pins toolchain go1.27.4, which fixes GO-2026-6859; workflows
  check that the running Go matches it.
- .goreleaser.yaml: tidy check that never writes, reproducible file times,
  prerelease: auto, a release header linking CHANGELOG.md, and archives
  that ship CHANGELOG.md, the example config and the systemd unit.
- Makefile mirrors the CI gate; .dockerignore is an allowlist.

API and tests
- Every error answers {"error", "code", "details"} with a stable code from
  server/errors.go; the framework's own error text is never sent.
- Panics and unexpected errors are logged and reported to Sentry once;
  refusals are not logged as failures. /healthz sends no-store.
- TestRoutesConform requires auth, rate limiting and the body limit on
  every /api/v1 route.
- Race-prone tests no longer rely on freed ports or sleeps; serve takes
  an injected listener.
- The test config uses a placeholder secret instead of a real one.

Install and packaging
- install.sh resolves the latest release through the Gitea API, verifies
  checksums, requests a certificate only when none exists, installs the
  hardened packaging/uberbringer.service and writes the first token to a
  0600 file instead of printing it. It is now executable in git.

Docs
- CLAUDE.md, CHANGELOG.md (0.2.0 with breaking changes and upgrade
  steps), docs/design.md, and docs/plans/formatters.md for the planned
  CP437 blocks, modules and ntfy printing. README updated throughout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:27:48 -06:00

176 lines
5.0 KiB
Go

package config_test
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"uberbringer/internal/pkg/config"
)
// validSecret is a placeholder that satisfies the 32-byte minimum.
const validSecret = "test-only-secret-test-only-secret-0123"
func writeConfig(t *testing.T, body string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return path
}
func TestLoadInstallScriptConfigGetsDefaults(t *testing.T) {
t.Setenv("SENTRY_DSN", "")
// The shape install.sh writes; every other setting comes from the
// defaults.
path := writeConfig(t, `
[webserver]
bind_ip = "127.0.0.1"
port = 8080
[api]
secret = "`+validSecret+`"
[printer]
address = "192.168.1.245"
`)
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.Port != 9100 || cfg.Printer.Model != "tm-t88" || cfg.Printer.CodePage != "cp437" {
t.Errorf("printer defaults not applied: %+v", cfg.Printer)
}
if cfg.WebServer.RateWindow != time.Minute || cfg.Queue.Size != 100 {
t.Errorf("defaults not applied: %+v %+v", cfg.WebServer, cfg.Queue)
}
if cfg.Images.AllowPrivateNetworks {
t.Error("private networks must be blocked by default")
}
}
func TestLoadOverridesAndDurations(t *testing.T) {
path := writeConfig(t, `
[api]
secret = "`+validSecret+`"
[printer]
address = "printer.lan"
dial_timeout = "750ms"
paper_width = 384
[images]
allowed_hosts = ["images.lan"]
[sentry]
dsn = "https://key@sentry.example/1"
[log]
format = "json"
`)
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.DialTimeout != 750*time.Millisecond {
t.Errorf("DialTimeout = %v, want 750ms", cfg.Printer.DialTimeout)
}
if cfg.Printer.PaperWidth != 384 || cfg.Log.Format != "json" {
t.Errorf("overrides not applied: %+v %+v", cfg.Printer, cfg.Log)
}
if len(cfg.Images.AllowedHosts) != 1 || cfg.Images.AllowedHosts[0] != "images.lan" {
t.Errorf("AllowedHosts = %v", cfg.Images.AllowedHosts)
}
if cfg.Sentry.DSN != "https://key@sentry.example/1" {
t.Errorf("DSN = %q", cfg.Sentry.DSN)
}
}
func TestLoadSentryDSNFromEnvironment(t *testing.T) {
t.Setenv("SENTRY_DSN", "https://env@sentry.example/2")
path := writeConfig(t, "[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"p\"\n")
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Sentry.DSN != "https://env@sentry.example/2" {
t.Errorf("DSN = %q, want value from SENTRY_DSN", cfg.Sentry.DSN)
}
}
func TestLoadErrors(t *testing.T) {
tests := []struct {
name string
body string
wantErr string
}{
{
name: "missing secret",
body: "[printer]\naddress = \"p\"\n",
wantErr: "api.secret must be at least 32 bytes",
},
{
name: "short secret",
body: "[api]\nsecret = \"short\"\n[printer]\naddress = \"p\"\n",
wantErr: "api.secret must be at least 32 bytes",
},
{
name: "missing printer address",
body: "[api]\nsecret = \"" + validSecret + "\"\n",
wantErr: "printer.address is required",
},
{
name: "unknown key",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nadress = \"typo\"\n",
wantErr: "unknown keys: printer.adress",
},
{
name: "bad duration",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = \"soon\"\n",
wantErr: "dial_timeout",
},
{
name: "bad trusted proxy",
body: "[webserver]\ntrusted_proxies = [\"nginx\"]\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
wantErr: `"nginx" is not an IP or CIDR`,
},
{
name: "bad log level",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[log]\nlevel = \"loud\"\n",
wantErr: "log.level",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := config.Load(writeConfig(t, tt.body))
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Load error = %v, want it to contain %q", err, tt.wantErr)
}
})
}
}
// The documented example must stay loadable: every key in it must exist.
func TestExampleConfigLoads(t *testing.T) {
example, err := os.ReadFile(filepath.Join("..", "..", "..", "configs", "config.example.toml"))
if err != nil {
t.Fatal(err)
}
body := strings.Replace(string(example), `secret = ""`, `secret = "`+validSecret+`"`, 1)
body = strings.Replace(body, `address = ""`, `address = "192.168.1.245"`, 1)
cfg, err := config.Load(writeConfig(t, body))
if err != nil {
t.Fatalf("example config: %v", err)
}
if def := config.Default(); cfg.WebServer.RateLimit != def.WebServer.RateLimit ||
cfg.Images.MaxBytes != def.Images.MaxBytes || cfg.Printer.Model != def.Printer.Model {
t.Error("example config values drifted from the defaults it documents")
}
}
func TestLoadMissingFile(t *testing.T) {
_, err := config.Load(filepath.Join(t.TempDir(), "nope.toml"))
if err == nil {
t.Fatal("expected error for missing file")
}
}