| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/notion.git/notiontest" |
| 10 | ) |
| 11 | |
| 12 | // Gate 8 over the Notion import's routes (Notion import spec §14.2): |
| 13 | // each is a person's act, and logs an event of object kind import with |
| 14 | // its change. The run's own writes are bookkeeping, the named |
| 15 | // exemption; the route that starts it logs "started". |
| 16 | |
| 17 | // importTables are the import's tables, beside gate 8's. |
| 18 | var importTables = []string{"notion_connections", "notion_runs", |
| 19 | "notion_pages"} |
| 20 | |
| 21 | func importSnapshot(t *testing.T, rig *rig) string { |
| 22 | t.Helper() |
| 23 | saved := recorded |
| 24 | recorded = append(append([]string{}, saved...), importTables...) |
| 25 | defer func() { recorded = saved }() |
| 26 | return snapshot(t, rig) |
| 27 | } |
| 28 | |
| 29 | // holding runs act with the fake's lock held, so a run it starts waits |
| 30 | // at its first request and is still active. |
| 31 | func holding(server *notiontest.Server, act func()) { |
| 32 | server.Lock() |
| 33 | defer server.Unlock() |
| 34 | act() |
| 35 | } |
| 36 | |
| 37 | func TestGate8ImportRoutesLogEvents(t *testing.T) { |
| 38 | ws := notiontest.NewWorkspace("gate8") |
| 39 | ws.Add(ws.Page("", notiontest.Text("Gate")), notiontest.Para( |
| 40 | notiontest.Text("Eight."))) |
| 41 | rig, server := newImportRig(t, ws) |
| 42 | token := url.Values{"token": {importToken}} |
| 43 | driven := map[string]bool{} |
| 44 | act := func(path string, form url.Values, inside func()) { |
| 45 | before, events := importSnapshot(t, rig), rig.count(t, "events") |
| 46 | response := rig.post(t, Ana, path, form) |
| 47 | if inside != nil { |
| 48 | inside() |
| 49 | } |
| 50 | rig.built.WaitImports() |
| 51 | if response.Code >= 400 { |
| 52 | t.Errorf("%s answered %d", path, response.Code) |
| 53 | } |
| 54 | if importSnapshot(t, rig) != before && |
| 55 | rig.count(t, "events") == events { |
| 56 | t.Errorf("%s changed the database and logged no event", path) |
| 57 | } |
| 58 | driven["POST "+strings.Replace(path, "/import/notion", |
| 59 | "/import/notion", 1)] = true |
| 60 | } |
| 61 | act("/import/notion/connect", token, nil) |
| 62 | act("/import/notion/start", nil, nil) |
| 63 | act("/import/notion/connect", token, nil) |
| 64 | holding(server, func() { |
| 65 | act("/import/notion/start", nil, func() { |
| 66 | act("/import/notion/stop", nil, nil) |
| 67 | }) |
| 68 | }) |
| 69 | act("/import/notion/resume", nil, nil) |
| 70 | holding(server, func() { |
| 71 | act("/import/notion/start", nil, func() { |
| 72 | act("/import/notion/stop", nil, nil) |
| 73 | }) |
| 74 | }) |
| 75 | act("/import/notion/startover", nil, nil) |
| 76 | act("/import/notion/disconnect", nil, nil) |
| 77 | act("/import/notion/connect", token, nil) |
| 78 | act("/import/notion/finish", nil, nil) |
| 79 | act("/import/notion/forget", nil, nil) |
| 80 | for _, route := range rig.built.WriteRoutes() { |
| 81 | if strings.HasPrefix(route, "POST /import/") && !driven[route] { |
| 82 | t.Errorf("%s is an import write route gate 8 never drives", |
| 83 | route) |
| 84 | } |
| 85 | } |
| 86 | if rig.count(t, "events WHERE object_kind = 'import'") < 12 { |
| 87 | t.Error("the import's acts logged too few events") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // The import page reads, and writes nothing: a GET never writes |
| 92 | // (gate 10), during a run included. |
| 93 | func TestTheImportPageWritesNothing(t *testing.T) { |
| 94 | ws := notiontest.NewWorkspace("reads") |
| 95 | ws.Add(ws.Page("", notiontest.Text("Reads")), notiontest.Para( |
| 96 | notiontest.Text("Only."))) |
| 97 | rig, _ := newImportRig(t, ws) |
| 98 | rig.importAll(t) |
| 99 | before := importSnapshot(t, rig) |
| 100 | for _, path := range []string{"/import/notion", "/import/notion/finish", |
| 101 | "/import/notion/disconnect", "/import/notion/forget"} { |
| 102 | if response := rig.get(t, Ana, path); response.Code != http.StatusOK { |
| 103 | t.Errorf("GET %s: %d", path, response.Code) |
| 104 | } |
| 105 | } |
| 106 | if importSnapshot(t, rig) != before { |
| 107 | t.Error("a GET of the import's pages wrote") |
| 108 | } |
| 109 | } |
| 110 | |