package server import ( "context" "database/sql" "encoding/json" "errors" "strings" "testing" "github.com/google/uuid" "pageswiki/internal/store" ) func TestTaskMailLayouts(t *testing.T) { s, token, domain, actor := taskFixture(t) for _, suffix := range []string{"hogehoge/123", "v/0", "2026/01/" + uuid.NewString(), "2026/12/" + strings.Repeat("a", 64)} { id := uuid.NewString() input := map[string]any{"request_id": id, "title": "日本語の件名", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": suffix} w := taskRequest(s, token, "POST", "capture", input, "") if w.Code != 201 { t.Fatal(suffix, w.Code, w.Body) } var response struct { Path string `json:"page_path"` Item store.TaskRecord `json:"item"` } if e := json.Unmarshal(w.Body.Bytes(), &response); e != nil { t.Fatal(e) } if response.Path != "/mail/"+suffix { t.Fatal(response.Path) } if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 { t.Fatal("replay", w.Code, w.Body) } // A different Message-ID produces a different request UUID, never an overwrite. other := uuid.NewString() input["request_id"] = other input["wiki_body"] = "Different mail" if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 { t.Fatal("collision", w.Code, w.Body) } if _, e := s.store.GetTask(context.Background(), domain, actor, other); !errors.Is(e, sql.ErrNoRows) { t.Fatal("partial task", e) } rev, e := s.store.GetLatestRevision(response.Item.PageUUID) if e != nil || strings.Contains(rev.Body, "Different") { t.Fatal("page overwritten", e) } } for _, suffix := range []string{"", "../123", "/list/123", "a/../../123", "list/001", "list/-1", "list/1?x", "list%2f/1", "list/1/2", "_/123", "2026/13/" + uuid.NewString(), "0000/01/" + uuid.NewString(), "2026/01/not-an-id"} { input := map[string]any{"request_id": uuid.NewString(), "title": "Bad", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": suffix} if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 { t.Fatal(suffix, w.Code, w.Body) } } input := map[string]any{"request_id": uuid.NewString(), "title": "No body", "wiki_suffix": "list/123"} if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 { t.Fatal(w.Code, w.Body) } other, e := s.store.CreatePasswordAccount(domain, "mail-layout-other", "fixture password long", "Other", "") if e != nil { t.Fatal(e) } otherToken, e := s.store.CreateAuthSession(domain, other.UUID) if e != nil { t.Fatal(e) } input = map[string]any{"request_id": uuid.NewString(), "title": "No Wiki permission", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": "private/123"} if w := taskRequest(s, otherToken, "POST", "capture", input, ""); w.Code != 403 { t.Fatal(w.Code, w.Body) } }