package server import ( "context" "database/sql" "encoding/json" "errors" "strings" "testing" "time" "github.com/google/uuid" "pageswiki/internal/store" ) func TestTaskMailWikiCapture(t *testing.T) { s, token, domain, actor := taskFixture(t) ctx := context.Background() p, err := s.store.TaskPreferences(ctx, domain, actor) if err != nil { t.Fatal(err) } for _, bad := range []string{"mail", "/a/../b", "/_/api", "/v/blog", "/a?x", "/a//b", "/a\nb"} { p.WikiPrefix = bad if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 422 { t.Fatal(bad, w.Code) } } p.WikiPrefix = "/private/mail" if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil { t.Fatal(err) } id := uuid.NewString() input := map[string]any{"request_id": id, "title": "Mail / {{children}}", "notes": "From: sender@example.invalid", "wiki_body": "Hello\n {{children}}"} w := taskRequest(s, token, "POST", "capture", input, "") if w.Code != 201 { t.Fatal(w.Code, w.Body) } var response struct { Item store.TaskRecord `json:"item"` Path string `json:"page_path"` } if err = json.Unmarshal(w.Body.Bytes(), &response); err != nil { t.Fatal(err) } target := store.TaskCaptureWikiPath(p.WikiPrefix, input["title"].(string), id) if response.Item.PageUUID == "" || response.Path != target || strings.Contains(response.Item.Notes, "bad()") { t.Fatal(w.Body) } rev, err := s.store.GetLatestRevision(response.Item.PageUUID) if err != nil || !strings.Contains(rev.Body, "<script>bad()</script>") || strings.Contains(rev.Body, "{{children}}") { t.Fatal(rev, err) } // A committed retry remains stable even if the preference changes. p, err = s.store.TaskPreferences(ctx, domain, actor) if err != nil { t.Fatal(err) } p.WikiPrefix = "/elsewhere" if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil { t.Fatal(err) } if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), response.Item.PageUUID) { t.Fatal(w.Code, w.Body) } input["wiki_body"] = "different" if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 412 { t.Fatal(w.Code, w.Body) } input["request_id"] = uuid.NewString() input["wiki_prefix"] = "/explicit" if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/explicit/") { t.Fatal(w.Code, w.Body) } } func TestTaskMailWikiRollbackAndAccess(t *testing.T) { s, token, domain, actor := taskFixture(t) ctx := context.Background() id := uuid.NewString() title := "Mail" input := map[string]any{"request_id": id, "title": title, "wiki_body": "Mail body"} if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 || !strings.Contains(w.Body.String(), "wiki_prefix_required") { t.Fatal(w.Code, w.Body) } input["wiki_prefix"] = "/mail" target := store.TaskCaptureWikiPath("/mail", title, id) absent := func() { t.Helper() if _, e := s.store.ResolvePathInDomain(domain, target); !errors.Is(e, sql.ErrNoRows) { t.Fatal("orphan Wiki", e) } if _, e := s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) { t.Fatal("orphan task", e) } } // Failure after document insert must roll back the entire capture, including receipt. if _, e := s.store.DB.Exec(`CREATE TRIGGER reject_capture BEFORE INSERT ON organizer_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); e != nil { t.Fatal(e) } if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 500 { t.Fatal(w.Code, w.Body) } absent() if _, e := s.store.DB.Exec(`DROP TRIGGER reject_capture`); e != nil { t.Fatal(e) } for _, body := range []string{strings.Repeat("x", 256<<10+1), "bad\x00text"} { input["wiki_body"] = body if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 { t.Fatal(w.Code) } absent() } input["wiki_body"] = "Mail body" other, e := s.store.CreatePasswordAccount(domain, "mailother", "long fixture password", "Other", "") if e != nil { t.Fatal(e) } otherToken, e := s.store.CreateAuthSession(domain, other.UUID) if e != nil { t.Fatal(e) } if w := taskRequest(s, otherToken, "POST", "capture", input, ""); w.Code != 403 { t.Fatal("Wiki ACL", w.Code, w.Body) } absent() existing, e := s.store.CreatePageAtPathInDomain(domain, target) if e != nil { t.Fatal(e) } if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 { t.Fatal("collision", w.Code, w.Body) } if got, e := s.store.ResolvePathInDomain(domain, target); e != nil || got != existing { t.Fatal("overwrite", e) } if _, e = s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) { t.Fatal("orphan collision task", e) } input["request_id"] = uuid.NewString() if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 { t.Fatal(w.Code, w.Body) } } func TestTaskMailWikiProjectFallback(t *testing.T) { s, token, domain, actor := taskFixture(t) ctx := context.Background() root, e := s.store.CreatePageAtPathInDomain(domain, "/projects/app") if e != nil { t.Fatal(e) } project := newTaskRecord("project", "App") project.PageUUID = root project.NotesSubpath = "mail" project, e = s.store.SaveTask(ctx, domain, actor, project, 0) if e != nil { t.Fatal(e) } p, e := s.store.TaskPreferences(ctx, domain, actor) if e != nil { t.Fatal(e) } p.ProjectUUID = project.UUID if _, e = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); e != nil { t.Fatal(e) } input := map[string]any{"request_id": uuid.NewString(), "title": "Project mail", "wiki_body": "Body"} if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/projects/app/mail/") { t.Fatal(w.Code, w.Body) } }