test(tasks): preserve mail Wiki path diagnostics and regression coverage
This commit is contained in:
1 parent
b61fbc38a4
commit
02edb51b8a
5 files changed
+379
No files matched your search
@@ -0,0 +1,102 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Exercise the actual CLI executable boundary, full production router and
|
||||
// browser-session authentication. All mail, credentials and storage are synthetic.
|
||||
func TestNeoMuttMailViewerFlow(t *testing.T) {
|
||||
if _, err := exec.LookPath("python3"); err != nil {
|
||||
t.Skip("python3 not installed")
|
||||
}
|
||||
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)
|
||||
}
|
||||
browser, err := s.store.CreateBrowserAuthSession(domain, p.UUID, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := s.handler()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
if r.URL.Path == "/fixture-browser-login" {
|
||||
s.setAuthCookie(w, r, browser)
|
||||
http.Redirect(w, r, r.URL.Query().Get("return"), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
input, _ := json.Marshal(map[string]any{"origin": server.URL, "browser": browser, "root": t.TempDir(), "chromium": os.Getenv("PAGESWIKI_BROWSER_TEST") == "1"})
|
||||
cmd := exec.CommandContext(ctx, "python3", "pages-cli/test_neomutt_viewer.py")
|
||||
cmd.Stdin = bytes.NewReader(input)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("NeoMutt flow: %v\n%s", err, out)
|
||||
}
|
||||
if !strings.Contains(string(out), "MAIL_VIEWER_OK") {
|
||||
t.Fatalf("missing verification: %s", out)
|
||||
}
|
||||
t.Log(string(out))
|
||||
// Capture creates only the leaf document: prefix and date/list directories
|
||||
// remain virtual. Exercise the parent URLs under both documented settings.
|
||||
request := func(path string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost"+strings.ReplaceAll(path, " ", "%20"), nil)
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: browser})
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
for _, enabled := range []bool{false, true} {
|
||||
value, expected := "false", http.StatusNotFound
|
||||
if enabled {
|
||||
value, expected = "true", http.StatusOK
|
||||
}
|
||||
if err := s.store.SetConfigInDomain(domain, "virtual_folder_pages", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := request("/tasks/"); w.Code != http.StatusMovedPermanently || w.Header().Get("Location") != "/tasks" {
|
||||
t.Fatalf("tasks trailing slash: %d %s", w.Code, w.Header().Get("Location"))
|
||||
}
|
||||
for _, path := range []string{"/tasks", "/tasks/example.list", "/tasks/2026/01", "/mail", "/mail/example.list", "/private/mail", "/private/mail/2026", "/private/mail/2026/01", "/受信 メール", "/docs/mail", "/api/mail"} {
|
||||
w := request(path)
|
||||
if w.Code != expected || !strings.Contains(strings.ToLower(w.Body.String()), "<!doctype html>") {
|
||||
t.Fatalf("parent %s virtual=%t: %d %s", path, enabled, w.Code, w.Body.String())
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `{"detail":"Not Found"}`) {
|
||||
t.Fatal("unexpected JSON error")
|
||||
}
|
||||
}
|
||||
t.Logf("parent prefix/list/year/month: virtual_folder_pages=%t status=%d HTML", enabled, expected)
|
||||
}
|
||||
if err := s.store.SetConfigInDomain(domain, "virtual_folder_pages", "false"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, err := s.store.CreatePageAtPathInDomain(domain, "/private/mail")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(id, []byte("<h1>Synthetic mail portal</h1>{{children}}"), "", "und", "text/html", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := request("/private/mail"); w.Code != 200 || !strings.Contains(w.Body.String(), "Synthetic mail portal") {
|
||||
t.Fatalf("explicit parent document: %d", w.Code)
|
||||
}
|
||||
t.Log("explicit parent document: status=200 HTML with virtual folders disabled")
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user