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// L8 for images (images spec §4.4, §6): with script off, Upload and
12// insert on every Markdown screen, the Images on this page list, and
13// Remove's confirm page.
14
15// textarea is the Markdown screen's text, unescaped.
16func textarea(page string) string {
17 _, after, _ := strings.Cut(page, `name="body"`)
18 _, after, _ = strings.Cut(after, ">")
19 text, _, _ := strings.Cut(after, "</textarea>")
20 return html.UnescapeString(text)
21}
22
23func TestL8UploadAndInsertOnAPagesMarkdownScreen(t *testing.T) {
24 rig := newRig(t)
25 acme := rig.createPage(t, Ana, "Acme", "Customer since 2024.")
26 edit := rig.page(t, Ana, acme+"/edit")
27 if !strings.Contains(edit, `type="file"`) ||
28 !strings.Contains(edit, `formaction="`+acme+`?upload=1"`) {
29 t.Fatal("the Markdown screen has no Upload and insert")
30 }
31 revision := revisionAs(t, rig, Ana, acme)
32 before := rig.count(t, "revisions")
33 reply := rig.sendFile(t, Ana, acme+"?upload=1", url.Values{
34 "body": {"Customer since 2024."}, "revision": {revision},
35 "alt": {" The signed contract \n"}}, "contract.png", pngOf(t, 1))
36 shown := reply.Body.String()
37 text := textarea(shown)
38 if reply.Code != http.StatusOK || !strings.HasPrefix(text,
39 "Customer since 2024.\n\n![The signed contract](/i/") {
40 t.Fatalf("%d, text %q", reply.Code, text)
41 }
42 if !strings.Contains(shown, `name="revision" value="`+revision+`"`) ||
43 rig.count(t, "revisions") != before || rig.count(t, "drafts") != 0 {
44 t.Fatal("the upload committed, wrote a draft or moved the base")
45 }
46 if !strings.Contains(shown, "Images on this page") ||
47 !strings.Contains(shown, "The signed contract") {
48 t.Error("the list does not name the pending image")
49 }
50 rig.post(t, Ana, acme, url.Values{"body": {text},
51 "revision": {revision}})
52 page := rig.page(t, Bo, acme)
53 if !strings.Contains(page, `<img src="/i/`) {
54 t.Fatal("Save did not attach the image")
55 }
56 id := between(text, "](/i/", ")")
57 removeFromTheList(t, rig, acme, id)
58}
59
60// removeFromTheList walks Remove from the list: its confirm page, then
61// the POST, after which the page draws the image as not available.
62func removeFromTheList(t *testing.T, rig *rig, acme, id string) {
63 t.Helper()
64 list := rig.page(t, Bo, acme+"/edit")
65 link := "/i/" + id + "/remove?back=" + url.QueryEscape(acme+"/edit")
66 if !strings.Contains(strings.ToLower(list), strings.ToLower(link)) {
67 t.Fatalf("the list has no Remove for %s", id)
68 }
69 confirm := rig.page(t, Bo, link)
70 if !strings.Contains(confirm, "Remove this image from Mapa?") ||
71 !strings.Contains(confirm, `action="/i/`+id+`/remove"`) {
72 t.Fatal("no confirm page")
73 }
74 done := rig.post(t, Bo, "/i/"+id+"/remove",
75 url.Values{"back": {acme + "/edit"}})
76 if done.Code != http.StatusSeeOther {
77 t.Fatalf("remove: %d", done.Code)
78 }
79 if !strings.Contains(rig.page(t, Ana, acme),
80 `mapa-image="unavailable"`) {
81 t.Error("a removed image is still drawn")
82 }
83}
84
85func TestL8UploadAndInsertOnAnUnwrittenDay(t *testing.T) {
86 rig := newRig(t)
87 day := "/d/2026-11-20"
88 if !strings.Contains(rig.page(t, Ana, day+"/edit"),
89 `formaction="`+day+`?upload=1"`) {
90 t.Fatal("the day's Markdown screen has no Upload and insert")
91 }
92 reply := rig.sendFile(t, Ana, day+"?upload=1", url.Values{
93 "body": {""}, "revision": {"0"}}, "a.png", pngOf(t, 1))
94 text := textarea(reply.Body.String())
95 if reply.Code != http.StatusOK || !strings.HasPrefix(text, "![](/i/") {
96 t.Fatalf("%d, text %q", reply.Code, text)
97 }
98 if rig.count(t, "pages") != 0 || rig.count(t, "drafts") != 0 {
99 t.Fatal("an upload wrote the day")
100 }
101 rig.post(t, Ana, day, url.Values{"body": {text}, "revision": {"0"}})
102 if !strings.Contains(rig.page(t, Ana, day), `<img src="/i/`) {
103 t.Error("the day's first save did not attach the image")
104 }
105}
106
107func TestL8UploadAndInsertOnANewScratchPage(t *testing.T) {
108 rig := newRig(t)
109 reply := rig.sendFile(t, Ana, "/scratch?upload=1", url.Values{
110 "body": {"Notes"}}, "a.png", pngOf(t, 1))
111 text := textarea(reply.Body.String())
112 if reply.Code != http.StatusOK ||
113 !strings.Contains(reply.Body.String(), "Image added") {
114 t.Fatalf("%d", reply.Code)
115 }
116 made := rig.post(t, Ana, "/scratch", url.Values{"body": {text}})
117 if !strings.Contains(rig.page(t, Ana, made.Header().Get("Location")),
118 `<img src="/i/`) {
119 t.Error("the scratch page's first save did not attach the image")
120 }
121 none := rig.sendFile(t, Ana, "/scratch?upload=1", url.Values{
122 "body": {"kept"}}, "", nil)
123 if none.Code != http.StatusUnprocessableEntity ||
124 textarea(none.Body.String()) != "kept" {
125 t.Errorf("no file: %d %q", none.Code, textarea(none.Body.String()))
126 }
127}
128
129// Fable L5 and L6 on 49d8df3: a file far over 10 MB is refused and the
130// typed text stays; and Upload and insert on a page the person may not
131// open stores nothing.
132func TestL8AnOversizedUploadKeepsTheText(t *testing.T) {
133 rig := newRig(t)
134 huge := append(pngOf(t, 1), make([]byte, 25<<20)...)
135 reply := rig.sendFile(t, Ana, "/scratch?upload=1", url.Values{
136 "body": {"Typed before the upload"}}, "big.png", huge)
137 if reply.Code != http.StatusRequestEntityTooLarge ||
138 textarea(reply.Body.String()) != "Typed before the upload" {
139 t.Fatalf("%d, text %q", reply.Code, textarea(reply.Body.String()))
140 }
141}
142
143func TestL8UploadAndInsertNeedsThePage(t *testing.T) {
144 rig := newRig(t)
145 rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine alone."}})
146 private := rig.page(t, Ana, "/scratch")
147 note := "/p/" + between(private, `href="/p/`, `"`)
148 reply := rig.sendFile(t, Bo, note+"?upload=1", url.Values{
149 "body": {"x"}, "revision": {"1"}}, "a.png", pngOf(t, 1))
150 if reply.Code != http.StatusNotFound || rig.count(t, "images") != 0 {
151 t.Fatalf("%d, %d images stored", reply.Code, rig.count(t, "images"))
152 }
153}
154
155// Fable L1 on 49d8df3: Help says what §1.4, §3.3 and §5 promise: a
156// browser's own copy for up to an hour, a pending image's seven days
157// unless a draft names it, and the colour profile kept.
158func TestHelpSaysWhatImagesKeep(t *testing.T) {
159 help := newRig(t).page(t, Ana, "/help/markdown")
160 for _, want := range []string{"up to an hour", "seven days",
161 "unless a draft of yours still names it", "colour profile is kept"} {
162 if !strings.Contains(help, want) {
163 t.Errorf("Help does not say %q", want)
164 }
165 }
166}
167