oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package equipotest tests the app from the outside: real HTTP
2// requests against the real router, wired exactly as
3// cmd/equipo/main.go wires it.
4//
5// The TDD loop: write a failing test here, make it pass in
6// internal/equipo, repeat.
7package equipotest
8
9import (
10 "io"
11 "log/slog"
12 "net/http"
13 "net/http/httptest"
14 "net/url"
15 "os"
16 "strings"
17 "testing"
18
19 "amadan.net/oficina/equipo/internal/runner/containment"
20
21 "amadan.net/rastrillo/rastrillo/budget"
22 "amadan.net/rastrillo/rastrillo/db"
23 "amadan.net/rastrillo/rastrillo/dbtest"
24 "amadan.net/rastrillo/rastrillo/mail"
25
26 equipo "amadan.net/oficina/equipo/internal/equipo"
27)
28
29// testOrigin is the origin newApp wires and post presents as its
30// same-origin evidence — the pair a browser form submission would be.
31const testOrigin = "http://127.0.0.1:8080"
32
33func testConfig(origin string) equipo.Config {
34 return equipo.Config{Origin: origin, InstanceKey: "test-instance-key", AllowLoggedMail: true, Owners: []string{ownerAddress}}
35}
36
37// schema is migrated once per test binary and copied for each test:
38// migrating a fresh database in every test is the cost Tito Go measured
39// at 207ms a test against 2.5ms for a copy.
40var schema = dbtest.FromSet(equipo.BootSchema)
41
42// TestMain prints this package's time for rastrillo budget test (make
43// test), then removes the template, which outlives every test that
44// copied it.
45func TestMain(m *testing.M) {
46 if len(os.Args) > 1 && os.Args[1] == "__namespace" {
47 os.Exit(containment.NamespaceMain(os.Args[2:]))
48 }
49 code := budget.Main(m)
50 schema.Remove()
51 os.Exit(code)
52}
53
54// newApp builds the whole app per test over its own copy of the
55// migrated database, exactly as main.go does.
56func newApp(t *testing.T) http.Handler {
57 t.Helper()
58 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
59 d, err := db.Open(schema.Path(t), logger)
60 if err != nil {
61 t.Fatalf("db.Open: %v", err)
62 }
63 t.Cleanup(func() { d.Close() })
64 mux, err := equipo.App(d, testConfig(testOrigin), mail.Logged(logger), logger)
65 if err != nil {
66 t.Fatalf("App: %v", err)
67 }
68 return mux
69}
70
71func get(t *testing.T, h http.Handler, target string) *httptest.ResponseRecorder {
72 t.Helper()
73 rec := httptest.NewRecorder()
74 h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, nil))
75 return rec
76}
77
78// post is here before anything calls it: the first form test you write
79// needs the Origin header below, and nothing about a 403 says so.
80//
81//lint:ignore U1000 kept for the app's first form test; see above
82func post(t *testing.T, h http.Handler, target string, form url.Values) *httptest.ResponseRecorder {
83 t.Helper()
84 req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode()))
85 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
86 // The same-origin evidence csrf.Protect requires — every current
87 // browser sends it on a form POST; a test client must too.
88 req.Header.Set("Origin", testOrigin)
89 rec := httptest.NewRecorder()
90 h.ServeHTTP(rec, req)
91 return rec
92}
93