package mapatest import ( "net/http" "net/url" "strings" "testing" "amadan.net/oficina/notion.git/notiontest" ) // Gate 8 over the Notion import's routes (Notion import spec ยง14.2): // each is a person's act, and logs an event of object kind import with // its change. The run's own writes are bookkeeping, the named // exemption; the route that starts it logs "started". // importTables are the import's tables, beside gate 8's. var importTables = []string{"notion_connections", "notion_runs", "notion_pages"} func importSnapshot(t *testing.T, rig *rig) string { t.Helper() saved := recorded recorded = append(append([]string{}, saved...), importTables...) defer func() { recorded = saved }() return snapshot(t, rig) } // holding runs act with the fake's lock held, so a run it starts waits // at its first request and is still active. func holding(server *notiontest.Server, act func()) { server.Lock() defer server.Unlock() act() } func TestGate8ImportRoutesLogEvents(t *testing.T) { ws := notiontest.NewWorkspace("gate8") ws.Add(ws.Page("", notiontest.Text("Gate")), notiontest.Para( notiontest.Text("Eight."))) rig, server := newImportRig(t, ws) token := url.Values{"token": {importToken}} driven := map[string]bool{} act := func(path string, form url.Values, inside func()) { before, events := importSnapshot(t, rig), rig.count(t, "events") response := rig.post(t, Ana, path, form) if inside != nil { inside() } rig.built.WaitImports() if response.Code >= 400 { t.Errorf("%s answered %d", path, response.Code) } if importSnapshot(t, rig) != before && rig.count(t, "events") == events { t.Errorf("%s changed the database and logged no event", path) } driven["POST "+strings.Replace(path, "/import/notion", "/import/notion", 1)] = true } act("/import/notion/connect", token, nil) act("/import/notion/start", nil, nil) act("/import/notion/connect", token, nil) holding(server, func() { act("/import/notion/start", nil, func() { act("/import/notion/stop", nil, nil) }) }) act("/import/notion/resume", nil, nil) holding(server, func() { act("/import/notion/start", nil, func() { act("/import/notion/stop", nil, nil) }) }) act("/import/notion/startover", nil, nil) act("/import/notion/disconnect", nil, nil) act("/import/notion/connect", token, nil) act("/import/notion/finish", nil, nil) act("/import/notion/forget", nil, nil) for _, route := range rig.built.WriteRoutes() { if strings.HasPrefix(route, "POST /import/") && !driven[route] { t.Errorf("%s is an import write route gate 8 never drives", route) } } if rig.count(t, "events WHERE object_kind = 'import'") < 12 { t.Error("the import's acts logged too few events") } } // The import page reads, and writes nothing: a GET never writes // (gate 10), during a run included. func TestTheImportPageWritesNothing(t *testing.T) { ws := notiontest.NewWorkspace("reads") ws.Add(ws.Page("", notiontest.Text("Reads")), notiontest.Para( notiontest.Text("Only."))) rig, _ := newImportRig(t, ws) rig.importAll(t) before := importSnapshot(t, rig) for _, path := range []string{"/import/notion", "/import/notion/finish", "/import/notion/disconnect", "/import/notion/forget"} { if response := rig.get(t, Ana, path); response.Code != http.StatusOK { t.Errorf("GET %s: %d", path, response.Code) } } if importSnapshot(t, rig) != before { t.Error("a GET of the import's pages wrote") } }