// Package fichastest tests the app from the outside: real HTTP // requests against the real router, wired exactly as // cmd/fichas/main.go wires it. // // The TDD loop: write a failing test here, make it pass in // internal/fichas, repeat. package fichastest import ( "encoding/base64" "io" "log/slog" "net/http" "net/http/httptest" "net/url" "path/filepath" "strings" "testing" "amadan.net/rastrillo/rastrillo/db" fichas "amadan.net/oficina/fichas/internal/fichas" "amadan.net/oficina/fichas/internal/fichas/seal" ) // testOrigin is the origin newApp wires and post presents as its // same-origin evidence — the pair a browser form submission would be. // testOrigin is loopback so the local-development member is admitted; // LoadConfig refuses it on any other origin. const TestOwner = "ana@example.com" const testOrigin = "http://127.0.0.1:8824" // testSealer is a fixed DEK. Fixed rather than random so that a failure // is reproducible; it never leaves the test binary. func testSealer(t *testing.T) *seal.Sealer { t.Helper() s, err := seal.New(testKey()) if err != nil { t.Fatalf("seal.New: %v", err) } return s } // testConfig wires the app exactly as main.go does, with a local-dev // member so the write paths have somebody to attribute changes to. func testConfig(t *testing.T) fichas.Config { t.Helper() c, err := fichas.LoadConfig(func(k string) string { switch k { case "FICHAS_DEK": return base64.StdEncoding.EncodeToString(testKey()) case "FICHAS_ORIGIN": return testOrigin case "FICHAS_INSTANCE_KEY": return "test-instance-key" case "FICHAS_OWNERS": return TestOwner case "FICHAS_ALLOW_LOGGED_MAIL": // No SMTP in a test, and testOrigin is loopback. Saying so // out loud is the point of the flag. return "1" } return "" }) if err != nil { t.Fatalf("LoadConfig: %v", err) } return c } func testKey() []byte { k := make([]byte, 32) for i := range k { k[i] = 0x7f } return k } // newApp builds the whole app per test over a fresh temp database, // exactly as main.go does. func newApp(t *testing.T) http.Handler { t.Helper() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) mux, err := fichas.AppForTest(d, testConfig(t), logger, TestOwner) if err != nil { t.Fatalf("AppForTest: %v", err) } return mux } func get(t *testing.T, h http.Handler, target string) *httptest.ResponseRecorder { t.Helper() rec := httptest.NewRecorder() h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, nil)) return rec } func post(t *testing.T, h http.Handler, target string, form url.Values) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // The same-origin evidence csrf.Protect requires — every current // browser sends it on a form POST; a test client must too. req.Header.Set("Origin", testOrigin) rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec }