| 1 | // Package fichastest tests the app from the outside: real HTTP |
| 2 | // requests against the real router, wired exactly as |
| 3 | // cmd/fichas/main.go wires it. |
| 4 | // |
| 5 | // The TDD loop: write a failing test here, make it pass in |
| 6 | // internal/fichas, repeat. |
| 7 | package fichastest |
| 8 | |
| 9 | import ( |
| 10 | "encoding/base64" |
| 11 | "io" |
| 12 | "log/slog" |
| 13 | "net/http" |
| 14 | "net/http/httptest" |
| 15 | "net/url" |
| 16 | "path/filepath" |
| 17 | "strings" |
| 18 | "testing" |
| 19 | |
| 20 | "amadan.net/rastrillo/rastrillo/db" |
| 21 | |
| 22 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 23 | "amadan.net/oficina/fichas/internal/fichas/seal" |
| 24 | ) |
| 25 | |
| 26 | // testOrigin is the origin newApp wires and post presents as its |
| 27 | // same-origin evidence — the pair a browser form submission would be. |
| 28 | // testOrigin is loopback so the local-development member is admitted; |
| 29 | // LoadConfig refuses it on any other origin. |
| 30 | const TestOwner = "ana@example.com" |
| 31 | |
| 32 | const testOrigin = "http://127.0.0.1:8824" |
| 33 | |
| 34 | // testSealer is a fixed DEK. Fixed rather than random so that a failure |
| 35 | // is reproducible; it never leaves the test binary. |
| 36 | func testSealer(t *testing.T) *seal.Sealer { |
| 37 | t.Helper() |
| 38 | s, err := seal.New(testKey()) |
| 39 | if err != nil { |
| 40 | t.Fatalf("seal.New: %v", err) |
| 41 | } |
| 42 | return s |
| 43 | } |
| 44 | |
| 45 | // testConfig wires the app exactly as main.go does, with a local-dev |
| 46 | // member so the write paths have somebody to attribute changes to. |
| 47 | func testConfig(t *testing.T) fichas.Config { |
| 48 | t.Helper() |
| 49 | c, err := fichas.LoadConfig(func(k string) string { |
| 50 | switch k { |
| 51 | case "FICHAS_DEK": |
| 52 | return base64.StdEncoding.EncodeToString(testKey()) |
| 53 | case "FICHAS_ORIGIN": |
| 54 | return testOrigin |
| 55 | case "FICHAS_INSTANCE_KEY": |
| 56 | return "test-instance-key" |
| 57 | case "FICHAS_OWNERS": |
| 58 | return TestOwner |
| 59 | case "FICHAS_ALLOW_LOGGED_MAIL": |
| 60 | // No SMTP in a test, and testOrigin is loopback. Saying so |
| 61 | // out loud is the point of the flag. |
| 62 | return "1" |
| 63 | } |
| 64 | return "" |
| 65 | }) |
| 66 | if err != nil { |
| 67 | t.Fatalf("LoadConfig: %v", err) |
| 68 | } |
| 69 | return c |
| 70 | } |
| 71 | |
| 72 | func testKey() []byte { |
| 73 | k := make([]byte, 32) |
| 74 | for i := range k { |
| 75 | k[i] = 0x7f |
| 76 | } |
| 77 | return k |
| 78 | } |
| 79 | |
| 80 | // newApp builds the whole app per test over a fresh temp database, |
| 81 | // exactly as main.go does. |
| 82 | func newApp(t *testing.T) http.Handler { |
| 83 | t.Helper() |
| 84 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 85 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 86 | if err != nil { |
| 87 | t.Fatalf("db.Open: %v", err) |
| 88 | } |
| 89 | t.Cleanup(func() { d.Close() }) |
| 90 | mux, err := fichas.AppForTest(d, testConfig(t), logger, TestOwner) |
| 91 | if err != nil { |
| 92 | t.Fatalf("AppForTest: %v", err) |
| 93 | } |
| 94 | return mux |
| 95 | } |
| 96 | |
| 97 | func get(t *testing.T, h http.Handler, target string) *httptest.ResponseRecorder { |
| 98 | t.Helper() |
| 99 | rec := httptest.NewRecorder() |
| 100 | h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, nil)) |
| 101 | return rec |
| 102 | } |
| 103 | |
| 104 | func post(t *testing.T, h http.Handler, target string, form url.Values) *httptest.ResponseRecorder { |
| 105 | t.Helper() |
| 106 | req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode())) |
| 107 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 108 | // The same-origin evidence csrf.Protect requires — every current |
| 109 | // browser sends it on a form POST; a test client must too. |
| 110 | req.Header.Set("Origin", testOrigin) |
| 111 | rec := httptest.NewRecorder() |
| 112 | h.ServeHTTP(rec, req) |
| 113 | return rec |
| 114 | } |
| 115 | |