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 "encoding/json"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9)
10
11// COL7's server half, COL8 and COL13 (columns and icons spec §1.9,
12// §1.10, §9): a layout drawn as columns with script off; History's
13// words for a layout; the block routes refusing fences, a cut layout,
14// and a post counted by another grammar.
15
16const twoColumns = ":::: columns\n\n::: column\nLeft.\n\n:::\n\n" +
17 "::: column\nRight.\n\n:::\n\n::::"
18
19func TestCOL7ALayoutIsDrawnAsColumns(t *testing.T) {
20 rig := newRig(t)
21 acme := rig.createPage(t, Ana, "Acme", "Top.\n\n"+twoColumns)
22 page := rig.page(t, Bo, acme)
23 if !strings.Contains(page, `<div mapa-columns="2"><div mapa-column>`+
24 `<p>Left.</p></div><div mapa-column><p>Right.</p></div></div>`) {
25 t.Fatal("the layout is not drawn as columns")
26 }
27 if !strings.Contains(rig.page(t, Bo, acme+"/edit"), "::: column") {
28 t.Fatal("Edit as Markdown does not show the fences")
29 }
30}
31
32func TestCOL8HistorySaysWhatHappenedToTheColumns(t *testing.T) {
33 rig := newRig(t)
34 acme := rig.createPage(t, Ana, "Acme", "Left.\n\nRight.")
35 steps := []struct{ body, said string }{
36 {twoColumns, "Added columns"},
37 {strings.TrimSuffix(twoColumns, "::::") + "::: column\nMore.\n\n" +
38 ":::\n\n::::", "Added a column"},
39 {"Left.\n\nRight.", "Removed columns"},
40 }
41 for _, step := range steps {
42 rig.post(t, Ana, acme, url.Values{"body": {step.body},
43 "revision": {revisionAs(t, rig, Ana, acme)}})
44 if history := rig.page(t, Ana, acme+"/history"); !strings.Contains(
45 history, step.said) {
46 t.Errorf("History does not say %q", step.said)
47 }
48 }
49}
50
51func TestCOL13FencesAreNoBlockToMarkOrLink(t *testing.T) {
52 rig := newRig(t)
53 acme := rig.createPage(t, Ana, "Acme", twoColumns)
54 before := rig.count(t, "revisions")
55 for _, block := range []string{"0", "1", "3"} {
56 rig.post(t, Bo, acme+"/blocks", url.Values{"block": {block}})
57 }
58 if rig.count(t, "revisions") != before {
59 t.Fatal("a fence line took a marker")
60 }
61 column := rig.createPage(t, Bo, "Column", "A column.")
62 rig.post(t, Bo, column+"/mentions", url.Values{
63 "source": {strings.TrimPrefix(acme, "/p/")}, "block": {"1"}})
64 if rig.count(t, "revisions") != before+1 {
65 t.Fatal("Link it linked a fence line")
66 }
67}
68
69func TestCOL13MakeTeamPageNeverCutsALayout(t *testing.T) {
70 rig := newRig(t)
71 created := rig.post(t, Ana, "/scratch", url.Values{"body": {twoColumns}})
72 note := created.Header().Get("Location")
73 revision := revisionAs(t, rig, Ana, note)
74 status, reply := rig.postJSON(Ana, note+"/share", url.Values{
75 "title": {"Half"}, "revision": {revision}, "block": {"1", "2", "5"}})
76 if status == http.StatusOK || !strings.Contains(reply["error"].(string),
77 "inside one column") {
78 t.Fatalf("a cut layout was shared: %d %v", status, reply)
79 }
80 status, _ = rig.postJSON(Ana, note+"/share", url.Values{
81 "title": {"Inside"}, "revision": {revision}, "block": {"2"}})
82 if status != http.StatusOK {
83 t.Fatalf("a block inside one column: %d", status)
84 }
85}
86
87func TestCOL13AnotherGrammarIsRefusedAndTheDraftKept(t *testing.T) {
88 rig := newRig(t)
89 acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.")
90 for _, grammar := range []url.Values{{"grammar": {""}},
91 {"grammar": {"1"}}} {
92 for _, route := range []string{"/blocks", "/mentions", "/resolve"} {
93 form := url.Values{"block": {"0"}, "source": {"x"}}
94 for key, value := range grammar {
95 form[key] = value
96 }
97 status, reply := rig.postJSON(Bo, acme+route, form)
98 said, _ := reply["error"].(string)
99 if status != http.StatusConflict ||
100 !strings.Contains(said, "Reload the page") {
101 body, _ := json.Marshal(reply)
102 t.Errorf("%s with grammar %q: %d %s", route,
103 grammar.Get("grammar"), status, body)
104 }
105 }
106 }
107}
108
109// COL13's forms: every server-drawn form that posts a block or hunk
110// index carries the grammar, and a post without it is refused with the
111// sentence, the draft kept.
112func TestCOL13TheFormsCarryTheGrammar(t *testing.T) {
113 rig := newRig(t)
114 created := rig.post(t, Ana, "/scratch", url.Values{"body": {twoColumns}})
115 note := created.Header().Get("Location")
116 acme := rig.createPage(t, Ana, "Acme", "Acme.")
117 rig.createPage(t, Bo, "Notes", "We met Acme.")
118 hidden := `<input type="hidden" name="grammar" value="2">`
119 for _, path := range []string{note + "/share", acme + "/blocks", acme} {
120 if !strings.Contains(rig.page(t, Ana, path), hidden) {
121 t.Errorf("%s has no grammar field", path)
122 }
123 }
124 share := rig.page(t, Ana, note+"/share")
125 if strings.Count(share, `name="block"`) != 3 {
126 t.Errorf("Make team page lists %d rows, not the layout's one "+
127 "and its two blocks", strings.Count(share, `name="block"`))
128 }
129 rig.autosave(t, Ana, note, revisionAs(t, rig, Ana, note), "0",
130 "Typed meanwhile.")
131 drafts := rig.count(t, "drafts")
132 status, reply := rig.postJSON(Ana, note+"/share", url.Values{
133 "grammar": {""}, "title": {"T"}, "block": {"0"},
134 "revision": {revisionAs(t, rig, Ana, note)}})
135 said, _ := reply["error"].(string)
136 if status != http.StatusConflict || !strings.Contains(said, "Reload") ||
137 rig.count(t, "drafts") != drafts {
138 t.Fatalf("share without the grammar: %d %q", status, said)
139 }
140}
141