oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "html"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9)
10
11// createPage makes a team page through the real route and returns its
12// path.
13func (rig *rig) createPage(t *testing.T, who, title, body string) string {
14 t.Helper()
15 created := rig.post(t, who, "/pages",
16 url.Values{"title": {title}, "body": {body}})
17 if created.Code != http.StatusSeeOther {
18 t.Fatalf("creating %q: %d\n%s", title, created.Code,
19 created.Body.String())
20 }
21 return created.Header().Get("Location")
22}
23
24func (rig *rig) page(t *testing.T, who, path string) string {
25 t.Helper()
26 shown := rig.get(t, who, path)
27 if shown.Code != http.StatusOK {
28 t.Fatalf("GET %s: %d", path, shown.Code)
29 }
30 return shown.Body.String()
31}
32
33func TestATeamPageIsCreatedReadAndEditedByEveryone(t *testing.T) {
34 rig := newRig(t)
35 acme := rig.createPage(t, Ana, "Acme", "Customer since 2024.")
36 body := rig.page(t, Bo, acme)
37 if !strings.Contains(body, "<span mapa-title-text>Acme</span></h1>") ||
38 !strings.Contains(body, "Customer since 2024.") {
39 t.Fatalf("Bo cannot read Ana's team page:\n%s", body)
40 }
41 edit := rig.page(t, Bo, acme+"/edit")
42 revision := between(edit, `name="revision" value="`, `"`)
43 saved := rig.post(t, Bo, acme, url.Values{"revision": {revision},
44 "body": {"Customer since 2024. Renewal in March."}})
45 if saved.Code != http.StatusSeeOther {
46 t.Fatalf("Bo's save: %d", saved.Code)
47 }
48 if !strings.Contains(rig.page(t, Ana, acme), "Renewal in March.") {
49 t.Fatal("Ana does not see Bo's edit")
50 }
51 stale := rig.post(t, Ana, acme, url.Values{"revision": {revision},
52 "body": {"overwrite"}})
53 if stale.Header().Get("Location") != acme+"/conflict" {
54 t.Fatalf("a stale save to the same block went to %q",
55 stale.Header().Get("Location"))
56 }
57 if !strings.Contains(rig.page(t, Ana, acme+"/conflict"), "overwrite") {
58 t.Fatal("the conflict view does not show the person's text")
59 }
60}
61
62func TestAPendingLinkOffersToCreateItsPage(t *testing.T) {
63 rig := newRig(t)
64 notes := rig.createPage(t, Ana, "Notes", "Ask about [[Halden Robotics]].")
65 body := rig.page(t, Bo, notes)
66 href := `href="/new?title=Halden+Robotics" mapa-chip="pending"`
67 if !strings.Contains(body, href) {
68 t.Fatalf("no pending link:\n%s", body)
69 }
70 offer := rig.page(t, Bo, "/new?title=Halden+Robotics")
71 if !strings.Contains(offer, `value="Halden Robotics"`) {
72 t.Fatal("following a pending link does not offer to create it")
73 }
74 halden := rig.createPage(t, Bo, "Halden Robotics", "")
75 if !strings.Contains(rig.page(t, Ana, notes), `href="`+halden+`"`) {
76 t.Fatal("the pending link did not bind to its new page")
77 }
78 if !strings.Contains(rig.page(t, Ana, halden), `>Notes</a>`) {
79 t.Fatal("Halden Robotics does not show its backlink")
80 }
81}
82
83func TestATakenTitleSaysSoAndLinksToTheHolder(t *testing.T) {
84 rig := newRig(t)
85 acme := rig.createPage(t, Ana, "Acme", "")
86 again := rig.post(t, Bo, "/pages", url.Values{"title": {"ACME"}})
87 if again.Code != http.StatusUnprocessableEntity ||
88 !strings.Contains(again.Body.String(), `href="`+acme+`"`) {
89 t.Fatalf("a taken title: %d", again.Code)
90 }
91 date := rig.post(t, Bo, "/pages", url.Values{"title": {"2026-11-12"}})
92 if date.Code != http.StatusUnprocessableEntity {
93 t.Fatalf("a date title: %d", date.Code)
94 }
95}
96
97// A body that comes with a refused POST /pages rides along in a hidden
98// field (level-up build note 77; Fable's L4 on ux/fixes-1): escaped, so
99// nothing in it is markup, and with its lines intact, so sending the
100// form again with a free title makes the page with exactly that text.
101func TestARefusedNewPageKeepsItsBodyHidden(t *testing.T) {
102 rig := newRig(t)
103 rig.createPage(t, Ana, "Acme", "")
104 body := "Line one & <script>alert(1)</script>\n\n" +
105 "\"Quoted\" 'line' three\n- [ ] a task\n"
106 refused := rig.post(t, Bo, "/pages",
107 url.Values{"title": {"acme"}, "body": {body}})
108 shown := refused.Body.String()
109 if refused.Code != http.StatusUnprocessableEntity {
110 t.Fatalf("a taken title answered %d", refused.Code)
111 }
112 field := between(shown, `<input type="hidden" name="body" value="`,
113 `">`)
114 if strings.ContainsAny(field, `<>"`) || strings.Contains(shown,
115 "<script>alert") {
116 t.Fatalf("the hidden body is not escaped: %q", field)
117 }
118 if kept := html.UnescapeString(field); kept != body {
119 t.Fatalf("the hidden body came back as %q, not %q", kept, body)
120 }
121 again := rig.createPage(t, Bo, "Acme notes", html.UnescapeString(field))
122 if got := islandOf(t, rig.page(t, Ana, again)).Source; got !=
123 strings.TrimRight(body, "\n") {
124 t.Fatalf("the page made from the kept body holds %q", got)
125 }
126}
127
128func TestRenameOffersToUpdateLinksThenDoes(t *testing.T) {
129 rig := newRig(t)
130 acme := rig.createPage(t, Ana, "Acme", "")
131 notes := rig.createPage(t, Ana, "Notes", "Met [[Acme]].")
132 renamed := rig.post(t, Bo, acme+"/rename",
133 url.Values{"title": {"Acme Ltd"}})
134 if renamed.Header().Get("Location") != acme+"/links" {
135 t.Fatalf("rename went to %q", renamed.Header().Get("Location"))
136 }
137 offer := rig.page(t, Bo, acme+"/links")
138 if !strings.Contains(offer, "1 page you can edit link here") {
139 t.Fatalf("the offer does not count what Bo may edit:\n%s", offer)
140 }
141 updated := rig.post(t, Bo, acme+"/links", nil)
142 if updated.Header().Get("Location") != acme+"?done=links&n=1" {
143 t.Fatalf("update links: %q", updated.Header().Get("Location"))
144 }
145 if !strings.Contains(rig.page(t, Ana, notes+"/edit"), "Met [[Acme Ltd]].") {
146 t.Fatal("the link text was not updated")
147 }
148}
149
150func TestDeleteHasAConfirmPageAndRestoreMakesANewPage(t *testing.T) {
151 rig := newRig(t)
152 acme := rig.createPage(t, Ana, "Acme", "Kept text.")
153 confirm := rig.page(t, Bo, acme+"/delete")
154 if !strings.Contains(confirm, `method="post"`) {
155 t.Fatal("delete has no confirm form")
156 }
157 if rig.post(t, Bo, acme+"/delete", nil).Code != http.StatusSeeOther {
158 t.Fatal("delete failed")
159 }
160 if rig.get(t, Ana, acme).Code != http.StatusNotFound {
161 t.Fatal("a deleted page still opens")
162 }
163 oldID := strings.TrimPrefix(acme, "/p/")
164 restored := rig.post(t, Ana, "/deleted/"+oldID,
165 url.Values{"title": {"Acme"}})
166 location := restored.Header().Get("Location")
167 if restored.Code != http.StatusSeeOther || location == acme {
168 t.Fatalf("restore: %d to %q; want a new page", restored.Code,
169 location)
170 }
171 if !strings.Contains(rig.page(t, Bo, location), "Kept text.") {
172 t.Fatal("the restored page lost its text")
173 }
174}
175
176func TestAMissingOrMalformedPageIs404(t *testing.T) {
177 rig := newRig(t)
178 for _, path := range []string{"/p/0", "/p/x", "/p/999", "/p/999/edit",
179 "/p/999/rename", "/p/999/delete", "/deleted/999", "/nowhere"} {
180 if code := rig.get(t, Ana, path).Code; code != http.StatusNotFound {
181 t.Errorf("GET %s: %d, want 404", path, code)
182 }
183 }
184}
185
186// between is the text after open and before the next close.
187func between(text, open, close string) string {
188 _, after, _ := strings.Cut(text, open)
189 value, _, _ := strings.Cut(after, close)
190 return value
191}
192