| 1 | // Package mapatest tests the app from the outside: real HTTP requests |
| 2 | // against the real router, wired as cmd/mapa/main.go wires it. |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/base64" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "path/filepath" |
| 13 | "regexp" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo/blobs" |
| 19 | "amadan.net/rastrillo/rastrillo/db" |
| 20 | |
| 21 | "amadan.net/oficina/mapa/internal/mapa" |
| 22 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 23 | ) |
| 24 | |
| 25 | // The two people every gate needs: Ana writes private notes, Bo must |
| 26 | // learn nothing from them. |
| 27 | const ( |
| 28 | Ana = "ana@example.com" |
| 29 | Bo = "bo@example.com" |
| 30 | ) |
| 31 | |
| 32 | const testOrigin = "http://127.0.0.1:8834" |
| 33 | |
| 34 | func testKey() []byte { |
| 35 | key := make([]byte, 32) |
| 36 | for index := range key { |
| 37 | key[index] = 0x5a |
| 38 | } |
| 39 | return key |
| 40 | } |
| 41 | |
| 42 | // testEnv is the environment main reads, for a test. |
| 43 | func testEnv(owners string) map[string]string { |
| 44 | return map[string]string{ |
| 45 | "MAPA_DEK": base64.StdEncoding.EncodeToString(testKey()), |
| 46 | "MAPA_ORIGIN": testOrigin, |
| 47 | "MAPA_INSTANCE_KEY": "test-instance-key", |
| 48 | "MAPA_OWNERS": owners, |
| 49 | "MAPA_ALLOW_LOGGED_MAIL": "1", |
| 50 | "MAPA_TIME_ZONE": "Europe/Dublin", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func testConfig(t *testing.T, owners string) mapa.Config { |
| 55 | t.Helper() |
| 56 | env := testEnv(owners) |
| 57 | config, err := mapa.LoadConfig(func(key string) string { |
| 58 | return env[key] |
| 59 | }) |
| 60 | if err != nil { |
| 61 | t.Fatalf("LoadConfig: %v", err) |
| 62 | } |
| 63 | return config |
| 64 | } |
| 65 | |
| 66 | // rig is one app over one database, with Ana and Bo admitted. |
| 67 | type rig struct { |
| 68 | built mapa.Built |
| 69 | db *db.DB |
| 70 | dir string |
| 71 | } |
| 72 | |
| 73 | func newRig(t *testing.T) *rig { |
| 74 | t.Helper() |
| 75 | return newRigWith(t, nil) |
| 76 | } |
| 77 | |
| 78 | // newRigWith is newRig with its config changed first: IMG2 binds a |
| 79 | // fake S3 instead of the directory every other gate uses. |
| 80 | func newRigWith(t *testing.T, change func(*mapa.Config, string)) *rig { |
| 81 | t.Helper() |
| 82 | dir := t.TempDir() |
| 83 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 84 | database, err := db.Open(filepath.Join(dir, "mapa.db"), logger) |
| 85 | if err != nil { |
| 86 | t.Fatalf("db.Open: %v", err) |
| 87 | } |
| 88 | t.Cleanup(func() { database.Close() }) |
| 89 | config := testConfig(t, Ana+","+Bo) |
| 90 | // Tests keep images in a directory beside the database, as |
| 91 | // development does (images decision 4). |
| 92 | images, err := blobs.Dir(filepath.Join(dir, "images")) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | config.Images.Objects = images |
| 97 | if change != nil { |
| 98 | change(&config, dir) |
| 99 | } |
| 100 | built, err := mapa.AppForTest(database, config, logger, Ana) |
| 101 | if err != nil { |
| 102 | t.Fatalf("AppForTest: %v", err) |
| 103 | } |
| 104 | return &rig{built: built, db: database, dir: dir} |
| 105 | } |
| 106 | |
| 107 | func (rig *rig) get(t *testing.T, |
| 108 | who, target string) *httptest.ResponseRecorder { |
| 109 | t.Helper() |
| 110 | recorder := httptest.NewRecorder() |
| 111 | request := httptest.NewRequest(http.MethodGet, target, nil) |
| 112 | rig.built.As(who).ServeHTTP(recorder, request) |
| 113 | return recorder |
| 114 | } |
| 115 | |
| 116 | func (rig *rig) post(t *testing.T, who, target string, |
| 117 | form url.Values) *httptest.ResponseRecorder { |
| 118 | t.Helper() |
| 119 | form = counted(target, form) |
| 120 | request := httptest.NewRequest(http.MethodPost, target, |
| 121 | strings.NewReader(form.Encode())) |
| 122 | request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 123 | // The same-origin evidence csrf.Protect requires. |
| 124 | request.Header.Set("Origin", testOrigin) |
| 125 | recorder := httptest.NewRecorder() |
| 126 | rig.built.As(who).ServeHTTP(recorder, request) |
| 127 | return recorder |
| 128 | } |
| 129 | |
| 130 | // count is a table's row count. |
| 131 | func (rig *rig) count(t *testing.T, table string) int64 { |
| 132 | t.Helper() |
| 133 | var rows int64 |
| 134 | if err := rig.db.G.Table(table).Count(&rows).Error; err != nil { |
| 135 | t.Fatalf("counting %s: %v", table, err) |
| 136 | } |
| 137 | return rows |
| 138 | } |
| 139 | |
| 140 | // countedRoute is a route that posts block or hunk indices (columns and |
| 141 | // icons spec D17): its forms and the editor send the grammar. |
| 142 | var countedRoute = regexp.MustCompile(`/(share|blocks|mentions|resolve)$`) |
| 143 | |
| 144 | // counted adds the grammar field the app's own forms carry, unless the |
| 145 | // test names one itself (COL13 sends none, or an old one). |
| 146 | func counted(target string, form url.Values) url.Values { |
| 147 | path, _, _ := strings.Cut(target, "?") |
| 148 | if !countedRoute.MatchString(path) || form.Has("grammar") { |
| 149 | return form |
| 150 | } |
| 151 | out := url.Values{} |
| 152 | for key, values := range form { |
| 153 | out[key] = values |
| 154 | } |
| 155 | out.Set("grammar", strconv.Itoa(markup.Grammar)) |
| 156 | return out |
| 157 | } |
| 158 | |