// Package mapatest tests the app from the outside: real HTTP requests // against the real router, wired as cmd/mapa/main.go wires it. package mapatest import ( "encoding/base64" "io" "log/slog" "net/http" "net/http/httptest" "net/url" "path/filepath" "regexp" "strconv" "strings" "testing" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/rastrillo/rastrillo/db" "amadan.net/oficina/mapa/internal/mapa" "amadan.net/oficina/mapa/internal/mapa/markup" ) // The two people every gate needs: Ana writes private notes, Bo must // learn nothing from them. const ( Ana = "ana@example.com" Bo = "bo@example.com" ) const testOrigin = "http://127.0.0.1:8834" func testKey() []byte { key := make([]byte, 32) for index := range key { key[index] = 0x5a } return key } // testEnv is the environment main reads, for a test. func testEnv(owners string) map[string]string { return map[string]string{ "MAPA_DEK": base64.StdEncoding.EncodeToString(testKey()), "MAPA_ORIGIN": testOrigin, "MAPA_INSTANCE_KEY": "test-instance-key", "MAPA_OWNERS": owners, "MAPA_ALLOW_LOGGED_MAIL": "1", "MAPA_TIME_ZONE": "Europe/Dublin", } } func testConfig(t *testing.T, owners string) mapa.Config { t.Helper() env := testEnv(owners) config, err := mapa.LoadConfig(func(key string) string { return env[key] }) if err != nil { t.Fatalf("LoadConfig: %v", err) } return config } // rig is one app over one database, with Ana and Bo admitted. type rig struct { built mapa.Built db *db.DB dir string } func newRig(t *testing.T) *rig { t.Helper() return newRigWith(t, nil) } // newRigWith is newRig with its config changed first: IMG2 binds a // fake S3 instead of the directory every other gate uses. func newRigWith(t *testing.T, change func(*mapa.Config, string)) *rig { t.Helper() dir := t.TempDir() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) database, err := db.Open(filepath.Join(dir, "mapa.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { database.Close() }) config := testConfig(t, Ana+","+Bo) // Tests keep images in a directory beside the database, as // development does (images decision 4). images, err := blobs.Dir(filepath.Join(dir, "images")) if err != nil { t.Fatal(err) } config.Images.Objects = images if change != nil { change(&config, dir) } built, err := mapa.AppForTest(database, config, logger, Ana) if err != nil { t.Fatalf("AppForTest: %v", err) } return &rig{built: built, db: database, dir: dir} } func (rig *rig) get(t *testing.T, who, target string) *httptest.ResponseRecorder { t.Helper() recorder := httptest.NewRecorder() request := httptest.NewRequest(http.MethodGet, target, nil) rig.built.As(who).ServeHTTP(recorder, request) return recorder } func (rig *rig) post(t *testing.T, who, target string, form url.Values) *httptest.ResponseRecorder { t.Helper() form = counted(target, form) request := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode())) request.Header.Set("Content-Type", "application/x-www-form-urlencoded") // The same-origin evidence csrf.Protect requires. request.Header.Set("Origin", testOrigin) recorder := httptest.NewRecorder() rig.built.As(who).ServeHTTP(recorder, request) return recorder } // count is a table's row count. func (rig *rig) count(t *testing.T, table string) int64 { t.Helper() var rows int64 if err := rig.db.G.Table(table).Count(&rows).Error; err != nil { t.Fatalf("counting %s: %v", table, err) } return rows } // countedRoute is a route that posts block or hunk indices (columns and // icons spec D17): its forms and the editor send the grammar. var countedRoute = regexp.MustCompile(`/(share|blocks|mentions|resolve)$`) // counted adds the grammar field the app's own forms carry, unless the // test names one itself (COL13 sends none, or an old one). func counted(target string, form url.Values) url.Values { path, _, _ := strings.Cut(target, "?") if !countedRoute.MatchString(path) || form.Has("grammar") { return form } out := url.Values{} for key, values := range form { out[key] = values } out.Set("grammar", strconv.Itoa(markup.Grammar)) return out }