web_test.go (4596B)
1 package web 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "manus/bib" 13 "manus/store" 14 ) 15 16 func setup(t *testing.T) (string, http.Handler) { 17 t.Helper() 18 t.Setenv("MANUS_NOGIT", "1") 19 root := t.TempDir() 20 src := filepath.Join(root, "paper") 21 os.MkdirAll(filepath.Join(src, "figures"), 0o755) 22 os.WriteFile(filepath.Join(src, "main.tex"), []byte("\\documentclass{article}\n\\cite{A}\n\\bibliography{BIBnew}\n"), 0o644) 23 os.WriteFile(filepath.Join(src, "figures", "a.svg"), []byte("<svg xmlns='http://www.w3.org/2000/svg'/>"), 0o644) 24 os.WriteFile(filepath.Join(src, "secret.txt"), []byte("x"), 0o644) 25 os.WriteFile(filepath.Join(root, "BIBnew.bib"), []byte("@article{A, title={Glacier flow}, author={B. Smith}, year={2001}, doi={10.5555/a}}\n"), 0o644) 26 err := store.Edit(root, func(s *store.Store) (string, error) { 27 m, err := s.Create("paper", map[string]string{"dir": "paper", "src": ".", "main": "main.tex", "kind": "latex", "stage": "draft"}) 28 if err != nil { 29 return "", err 30 } 31 _, err = s.AddTask(m, "Redraw figure 1") 32 s.AddTask(m, "Check units") 33 return "", err 34 }) 35 if err != nil { 36 t.Fatal(err) 37 } 38 return root, New(root, bib.NewClient()) 39 } 40 41 func do(h http.Handler, method, path string, form url.Values) *httptest.ResponseRecorder { 42 var req *http.Request 43 if form != nil { 44 req = httptest.NewRequest(method, path, strings.NewReader(form.Encode())) 45 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 46 } else { 47 req = httptest.NewRequest(method, path, nil) 48 } 49 req.Host = "127.0.0.1:8090" 50 w := httptest.NewRecorder() 51 h.ServeHTTP(w, req) 52 return w 53 } 54 55 func TestPages(t *testing.T) { 56 _, h := setup(t) 57 for path, want := range map[string]string{ 58 "/": "2 todo", 59 "/m/paper": "Redraw figure 1", 60 "/tasks": "paper#2", 61 "/refs?q=smith": "Glacier flow", 62 } { 63 w := do(h, "GET", path, nil) 64 if w.Code != 200 || !strings.Contains(w.Body.String(), want) { 65 t.Errorf("GET %s: %d, missing %q\n%s", path, w.Code, want, w.Body.String()) 66 } 67 } 68 if w := do(h, "GET", "/m/nope", nil); w.Code != 404 { 69 t.Errorf("unknown manuscript: %d", w.Code) 70 } 71 } 72 73 func TestEdits(t *testing.T) { 74 root, h := setup(t) 75 if w := do(h, "POST", "/m/paper/tasks", url.Values{"title": {"Write abstract"}, "due": {"2026-12-01"}}); w.Code != 303 { 76 t.Fatalf("add task: %d", w.Code) 77 } 78 do(h, "POST", "/m/paper/tasks/1/status", url.Values{"status": {"doing"}, "back": {"/tasks"}}) 79 do(h, "POST", "/m/paper/tasks/2/comments", url.Values{"text": {"see reviewer 2"}}) 80 do(h, "POST", "/m/paper/tasks/1/rels", url.Values{"kind": {"blocks"}, "to": {"#3"}}) 81 do(h, "POST", "/m/paper/fields", url.Values{"journal": {"J. Glaciol."}, "stage": {"submitted"}}) 82 w := do(h, "POST", "/m/paper/tasks/1/status", url.Values{"status": {"bogus"}}) 83 if loc := w.Header().Get("Location"); !strings.Contains(loc, "msg=error") || !strings.HasSuffix(loc, "#t1") { 84 t.Errorf("invalid status redirect = %q", loc) 85 } 86 87 s, err := store.Load(root) 88 if err != nil { 89 t.Fatal(err) 90 } 91 m := s.MS["paper"] 92 if m.Get("journal") != "J. Glaciol." || m.Get("stage") != "submitted" { 93 t.Errorf("fields = %v", m.Fields) 94 } 95 if m.Task(1).Status != "doing" || m.Task(2).Comments[0].Text != "see reviewer 2" || m.Task(3).Due != "2026-12-01" { 96 t.Errorf("tasks = %+v %+v %+v", m.Task(1), m.Task(2), m.Task(3)) 97 } 98 if s.EffectiveStatus(store.Ref{MS: "paper", N: 3}, m.Task(3)) != "blocked" { 99 t.Error("task 3 should be blocked by task 1") 100 } 101 } 102 103 func TestFiles(t *testing.T) { 104 _, h := setup(t) 105 w := do(h, "GET", "/m/paper/file/figures/a.svg", nil) 106 if w.Code != 200 || w.Header().Get("Content-Type") != "image/svg+xml" || w.Header().Get("Content-Security-Policy") == "" { 107 t.Errorf("svg: %d %v", w.Code, w.Header()) 108 } 109 for _, p := range []string{"/m/paper/file/secret.txt", "/m/paper/file/../BIBnew.bib", "/m/paper/file/%2e%2e/x.pdf"} { 110 if w := do(h, "GET", p, nil); w.Code == 200 { 111 t.Errorf("GET %s should fail", p) 112 } 113 } 114 } 115 116 func TestGuards(t *testing.T) { 117 _, h := setup(t) 118 req := httptest.NewRequest("GET", "/", nil) 119 req.Host = "evil.example" 120 w := httptest.NewRecorder() 121 h.ServeHTTP(w, req) 122 if w.Code != http.StatusForbidden { 123 t.Errorf("foreign host: %d", w.Code) 124 } 125 req = httptest.NewRequest("POST", "/m/paper/tasks", strings.NewReader("title=x")) 126 req.Host = "127.0.0.1:8090" 127 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 128 req.Header.Set("Sec-Fetch-Site", "cross-site") 129 w = httptest.NewRecorder() 130 h.ServeHTTP(w, req) 131 if w.Code != http.StatusForbidden { 132 t.Errorf("cross-site post: %d", w.Code) 133 } 134 }