| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa" |
| 16 | "amadan.net/oficina/notion.git/notion" |
| 17 | "amadan.net/oficina/notion.git/notiontest" |
| 18 | ) |
| 19 | |
| 20 | // The Notion import from the outside: real requests to the real router, |
| 21 | // with the module's fake as Notion. Nothing here dials anything else. |
| 22 | |
| 23 | // fakeNotion is mapa.NotionAccess over a notiontest server. |
| 24 | type fakeNotion struct{ server *notiontest.Server } |
| 25 | |
| 26 | func (fake fakeNotion) Verify(ctx context.Context, |
| 27 | token string) (notion.Workspace, error) { |
| 28 | client, err := fake.Client(token) |
| 29 | if err != nil { |
| 30 | return notion.Workspace{}, err |
| 31 | } |
| 32 | return client.Me(ctx) |
| 33 | } |
| 34 | |
| 35 | func (fake fakeNotion) Client(token string) (*notion.Client, error) { |
| 36 | return notiontest.Client(token, fake.server.URL) |
| 37 | } |
| 38 | |
| 39 | func (fake fakeNotion) Files() *notion.Files { |
| 40 | return notiontest.Files(fake.server) |
| 41 | } |
| 42 | |
| 43 | // newImportRig is a rig where Ana may import, connected to a fake |
| 44 | // serving ws. |
| 45 | func newImportRig(t *testing.T, ws *notiontest.Workspace) (*rig, |
| 46 | *notiontest.Server) { |
| 47 | t.Helper() |
| 48 | return newImportRigWith(t, ws, nil) |
| 49 | } |
| 50 | |
| 51 | // newImportRigWith is newImportRig with change applied to its config |
| 52 | // first, when there is one. |
| 53 | func newImportRigWith(t *testing.T, ws *notiontest.Workspace, |
| 54 | change func(*mapa.Config, string)) (*rig, *notiontest.Server) { |
| 55 | t.Helper() |
| 56 | dir := t.TempDir() |
| 57 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 58 | database, err := db.Open(filepath.Join(dir, "mapa.db"), logger) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | t.Cleanup(func() { database.Close() }) |
| 63 | env := testEnv(Ana + "," + Bo) |
| 64 | env["MAPA_IMPORTERS"] = Ana |
| 65 | config, err := mapa.LoadConfig(func(key string) string { return env[key] }) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if change != nil { |
| 70 | change(&config, dir) |
| 71 | } |
| 72 | built, err := mapa.AppForTest(database, config, logger, Ana) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | server := notiontest.Start(ws, notion.APIVersion) |
| 77 | t.Cleanup(server.Close) |
| 78 | built.UseNotion(fakeNotion{server: server}) |
| 79 | t.Cleanup(built.WaitImports) |
| 80 | return &rig{built: built, db: database, dir: dir}, server |
| 81 | } |
| 82 | |
| 83 | // importToken is the secret every rig connects with; I11 looks for it. |
| 84 | const importToken = "secret_I11canaryTOKENxyz0123456789" |
| 85 | |
| 86 | // importAll connects as Ana, starts, and waits for the run. |
| 87 | func (rig *rig) importAll(t *testing.T) { |
| 88 | t.Helper() |
| 89 | for _, action := range []struct { |
| 90 | path string |
| 91 | form url.Values |
| 92 | }{{"/import/notion/connect", url.Values{"token": {importToken}}}, |
| 93 | {"/import/notion/start", nil}} { |
| 94 | response := rig.post(t, Ana, action.path, action.form) |
| 95 | if response.Code != http.StatusSeeOther { |
| 96 | t.Fatalf("%s: %d\n%s", action.path, response.Code, |
| 97 | response.Body) |
| 98 | } |
| 99 | } |
| 100 | rig.built.WaitImports() |
| 101 | } |
| 102 | |
| 103 | // importRoutes are the import's routes, as the guard must answer them. |
| 104 | var importRoutes = []string{"GET /import/notion", |
| 105 | "POST /import/notion/connect", "POST /import/notion/start", |
| 106 | "POST /import/notion/startover", "POST /import/notion/resume", |
| 107 | "POST /import/notion/stop", "GET /import/notion/disconnect", |
| 108 | "POST /import/notion/disconnect", "GET /import/notion/finish", |
| 109 | "POST /import/notion/finish", "GET /import/notion/forget", |
| 110 | "POST /import/notion/forget"} |
| 111 | |
| 112 | // I13, who sees Import (spec §7). With MAPA_IMPORTERS unset every import |
| 113 | // route 404s for everyone; with it set, a member not named gets 404 on |
| 114 | // every route, GET and POST, and the File menu offers it only to |
| 115 | // importers. The account menu never does: File is its one place |
| 116 | // (level-up build note 87). |
| 117 | // |
| 118 | // Mutations (make mutate, first red 2026-11-17): importerOnly lets every |
| 119 | // member through; File offers Import to every member (2026-11-22). |
| 120 | func TestI13OnlyImportersSeeImport(t *testing.T) { |
| 121 | plain := newRig(t) |
| 122 | imports, _ := newImportRig(t, notiontest.NewWorkspace("empty")) |
| 123 | for _, route := range importRoutes { |
| 124 | method, path, _ := strings.Cut(route, " ") |
| 125 | for _, check := range []struct { |
| 126 | rig *rig |
| 127 | who string |
| 128 | want int |
| 129 | }{{plain, Ana, 404}, {plain, Bo, 404}, {imports, Bo, 404}} { |
| 130 | response := check.rig.send(check.who, method, path, nil) |
| 131 | if response.StatusCode != check.want { |
| 132 | t.Errorf("%s as %s: %d, want %d", route, check.who, |
| 133 | response.StatusCode, check.want) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | for _, at := range []string{"/", "/pages", "/deleted"} { |
| 138 | page := imports.page(t, Ana, at) |
| 139 | if !strings.Contains(fileMenuOf(page), |
| 140 | importPathHref+`><span estilo-menu-label>Import from Notion…<`) { |
| 141 | t.Errorf("%s: an importer's File menu does not offer Import", at) |
| 142 | } |
| 143 | if strings.Contains(between(page, "<details estilo-account", |
| 144 | "</details>"), importPathHref) { |
| 145 | t.Errorf("%s: the account menu offers Import too", at) |
| 146 | } |
| 147 | for _, rig := range []*rig{plain, imports} { |
| 148 | if strings.Contains(rig.page(t, Bo, at), importPathHref) { |
| 149 | t.Errorf("%s: a member who may not import is offered it", |
| 150 | at) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // fileMenuOf is the File menu's markup on page. |
| 157 | func fileMenuOf(page string) string { |
| 158 | return between(page, `id="mapa-menu-file"`, "</details>") |
| 159 | } |
| 160 | |
| 161 | const importPathHref = `href="/import/notion"` |
| 162 | |
| 163 | // canaryWorkspace is a workspace whose every title, text, file name and |
| 164 | // name is a canary, with the canaries' ids beside it. |
| 165 | func canaryWorkspace() (*notiontest.Workspace, []string) { |
| 166 | ws := notiontest.NewWorkspace("canaries") |
| 167 | ws.Me["bot"].(map[string]any)["workspace_name"] = "Workspacecanary" |
| 168 | root := ws.Page("", notiontest.Text("Zebracanary")) |
| 169 | child := ws.Page(root, notiontest.Text("Okapicanary")) |
| 170 | ws.Add(root, notiontest.Para(notiontest.Text("quokkacanary words "), |
| 171 | notiontest.PageMention(child, "Okapicanary")), |
| 172 | notiontest.Image(ws.Host("filecanary.png", []byte("x")))) |
| 173 | ws.Add(child, notiontest.Rich("toggle", notiontest.Text("nilgaicanary"))) |
| 174 | canaries := []string{"Zebracanary", "Okapicanary", "quokkacanary", |
| 175 | "nilgaicanary", "filecanary", "Workspacecanary", importToken} |
| 176 | for _, id := range []string{root, child} { |
| 177 | canaries = append(canaries, id, strings.ReplaceAll(id, "-", "")) |
| 178 | } |
| 179 | return ws, canaries |
| 180 | } |
| 181 | |
| 182 | // I9, plaintext (spec §15). The new tables' columns are §15's (the |
| 183 | // schema half is TestPlaintextColumnsAreExactlySection4). The file half: |
| 184 | // a full import over canary titles, bodies, a canary token and canary |
| 185 | // Notion ids, run twice, then every byte on disk read, WAL included. |
| 186 | // |
| 187 | // Mutation (make mutate, first red 2026-11-17): the staged tree written |
| 188 | // unsealed. |
| 189 | func TestI9NoNotionPlaintextReachesTheDisk(t *testing.T) { |
| 190 | ws, canaries := canaryWorkspace() |
| 191 | rig, _ := newImportRig(t, ws) |
| 192 | rig.importAll(t) |
| 193 | if response := rig.post(t, Ana, "/import/notion/start", |
| 194 | nil); response.Code != http.StatusSeeOther { |
| 195 | t.Fatalf("second run: %d", response.Code) |
| 196 | } |
| 197 | rig.built.WaitImports() |
| 198 | if rig.count(t, "pages") != 2 { |
| 199 | t.Fatalf("%d pages imported", rig.count(t, "pages")) |
| 200 | } |
| 201 | if err := rig.db.Close(); err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | for _, hit := range scan(t, rig.dir, canaries) { |
| 205 | t.Errorf("plaintext on disk: %s", hit) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // I11, the token is never rendered (memoria N14): no 8-character run of |
| 210 | // it in any response, after connecting, running, a refused token and |
| 211 | // every confirm page. |
| 212 | // |
| 213 | // Mutation (make mutate, first red 2026-11-17): the connection form |
| 214 | // shows the token's last four characters as a mask. |
| 215 | func TestI11TheTokenIsNeverRendered(t *testing.T) { |
| 216 | ws, _ := canaryWorkspace() |
| 217 | rig, server := newImportRig(t, ws) |
| 218 | rig.importAll(t) |
| 219 | var bodies []string |
| 220 | for _, path := range []string{"/import/notion", "/import/notion/finish", |
| 221 | "/import/notion/disconnect", "/import/notion/forget", "/"} { |
| 222 | bodies = append(bodies, rig.page(t, Ana, path)) |
| 223 | } |
| 224 | server.Lock() |
| 225 | server.Workspace.Me["id"] = "" |
| 226 | server.Unlock() |
| 227 | refused := rig.post(t, Ana, "/import/notion/connect", |
| 228 | url.Values{"token": {importToken}}) |
| 229 | bodies = append(bodies, refused.Body.String()) |
| 230 | for _, body := range bodies { |
| 231 | for start := 0; start+8 <= len(importToken); start++ { |
| 232 | if strings.Contains(body, importToken[start:start+8]) { |
| 233 | t.Fatalf("a response holds %q of the token", |
| 234 | importToken[start:start+8]) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | if !strings.Contains(bodies[0], "Connected to") { |
| 239 | t.Error("the import page does not say it is connected") |
| 240 | } |
| 241 | } |
| 242 | |