Files
pages/internal/server/task_wiki_routing_test.go

123 lines
4.1 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
// No Python/Chromium dependency: the production route and authorization stack
// must continue to serve /tasks as Wiki space, not the organizer task UI/API.
func TestTaskWikiPrefixRouting(t *testing.T) {
fixture, token, domain := configFixture(t)
fixture.store.BackgroundJobsDisabled = true
s := New(fixture.store)
p, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
cookie, err := s.store.CreateBrowserAuthSession(domain, p.UUID, 0)
if err != nil {
t.Fatal(err)
}
handler := s.handler()
request := func(method, target string, payload any, authenticated bool) *httptest.ResponseRecorder {
body, err := json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest(method, "http://localhost"+target, bytes.NewReader(body))
if authenticated {
if method == http.MethodGet {
r.AddCookie(&http.Cookie{Name: authCookieName, Value: cookie})
} else {
r.Header.Set("Authorization", "Bearer "+token)
}
}
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
return w
}
prefs, err := s.store.TaskPreferences(t.Context(), domain, p.UUID)
if err != nil {
t.Fatal(err)
}
prefs.WikiPrefix = "/tasks"
if w := request("PUT", tasksAPI+"preferences", prefs, true); w.Code != 200 {
t.Fatalf("preferences: %d %s", w.Code, w.Body)
}
for i, suffix := range []string{"example/123", "2026/11/" + uuid.NewString(), "2026/11/" + strings.Repeat("a", 64), ""} {
input := map[string]any{"request_id": uuid.NewString(), "title": "合成メール", "wiki_body": "Synthetic routing test body"}
if suffix != "" {
input["wiki_suffix"] = suffix
}
w := request("POST", tasksAPI+"capture", input, true)
if w.Code != 201 {
t.Fatalf("capture: %d %s", w.Code, w.Body)
}
var result struct {
Item store.TaskRecord `json:"item"`
Path string `json:"page_path"`
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(result.Path, "/tasks/") {
t.Fatal(result.Path)
}
alias := "/prj/awesome"
if i != 0 {
alias += "/" + strconv.Itoa(i)
}
if err := s.store.AddPathLinkInDomain(domain, result.Item.PageUUID, alias); err != nil {
t.Fatal(err)
}
for _, target := range []string{result.Path, alias} {
if taskRoute(target) {
t.Fatalf("Wiki path captured by organizer routing: %s", target)
}
id, err := s.store.ResolvePathInDomain(domain, target)
if err != nil || id != result.Item.PageUUID {
t.Fatalf("alias identity: %s %v", id, err)
}
w := request("GET", target, nil, true)
if w.Code != 200 || !strings.Contains(w.Body.String(), "Synthetic routing test body") || !strings.Contains(strings.ToLower(w.Body.String()), "<!doctype html>") {
t.Fatalf("viewer %s: %d %s", target, w.Code, w.Body)
}
if w := request("GET", target+"/", nil, true); w.Code != 301 || w.Header().Get("Location") != target {
t.Fatalf("trailing slash %s: %d", target, w.Code)
}
if w := request("GET", target, nil, false); w.Code != 303 || strings.Contains(w.Body.String(), "Synthetic routing test body") {
t.Fatalf("anonymous isolation %s: %d", target, w.Code)
}
}
parent := result.Path[:strings.LastIndex(result.Path, "/")]
if w := request("GET", "/v/browser"+parent, nil, true); w.Code != 200 || !strings.Contains(w.Body.String(), `data-path="`+result.Path+`"`) {
t.Fatalf("listing: %d", w.Code)
}
}
for _, enabled := range []string{"false", "true"} {
if err := s.store.SetConfigInDomain(domain, "virtual_folder_pages", enabled); err != nil {
t.Fatal(err)
}
expected := http.StatusNotFound
if enabled == "true" {
expected = http.StatusOK
}
for _, target := range []string{"/tasks", "/tasks/example", "/tasks/2026", "/tasks/2026/11"} {
w := request("GET", target, nil, true)
if w.Code != expected || !strings.Contains(strings.ToLower(w.Body.String()), "<!doctype html>") {
t.Fatalf("virtual parent %s enabled=%s: %d", target, enabled, w.Code)
}
}
}
}