render_test.gno
11.05 Kb · 242 lines
1package mandate
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8 "gno.land/p/nt/uassert/v0"
9)
10
11const base = "/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate"
12
13func has(t *testing.T, page, want, what string) {
14 t.Helper()
15 if !strings.Contains(page, want) {
16 t.Fatalf("%s: page does not contain %q\n--- page ---\n%s", what, want, page)
17 }
18}
19
20func hasNot(t *testing.T, page, bad, what string) {
21 t.Helper()
22 if strings.Contains(page, bad) {
23 t.Fatalf("%s: page must not contain %q\n--- page ---\n%s", what, bad, page)
24 }
25}
26
27// renders calls Render and fails the test (instead of crashing) on a panic.
28func renders(t *testing.T, path string) string {
29 t.Helper()
30 var out string
31 if r := revive(func() { out = Render(path) }); r != nil {
32 t.Fatalf("Render(%q) panicked: %v", path, r)
33 }
34 return out
35}
36
37func TestRenderEmpty(cur realm, t *testing.T) {
38 resetState()
39 home := renders(t, "")
40 has(t, home, "# Mandate", "title")
41 has(t, home, "It is direct debit, not escrow.", "honest limit: not escrow")
42 has(t, home, "0.1 GNOT", "fee allowance")
43 has(t, home, "At most 16 mandates per wallet", "16 sessions")
44 has(t, home, "Revoke any time.", "revoke")
45 has(t, home, "nothing will ever be deployed there", "sub-path rule")
46 has(t, home, "| 0 | 0 | 0 | 0 GNOT |", "empty totals")
47 has(t, home, "No plan has been paid for in its current or last period.", "empty list")
48 has(t, home, "Built on [gno.land](https://gno.land)", "footer")
49 has(t, renders(t, "plans"), "No plans on this page.", "empty all-plans page")
50 for _, p := range []string{
51 "x", "plan", "plan/", "plan/1", "plan/abc", "plan/-1", "plan/1/2", "payer/", "payer/g1bad", "payee/xyz",
52 "help/x", "../help", "plan/18446744073709551616",
53 } {
54 has(t, renders(t, p), "# Not found", "not found: "+p)
55 }
56 has(t, renders(t, "payer/"+payer.String()), "No mandates on this page.", "empty payer")
57 has(t, renders(t, "payee/"+payee.String()), "No plans from this address.", "empty payee")
58}
59
60func TestRenderHelp(cur realm, t *testing.T) {
61 resetState()
62 h := renders(t, "help")
63 net := "-broadcast -chainid onyx-1 -remote https://rpc.onyx.testnets.gno.land:443"
64 has(t, h, "gnokey maketx call -pkgpath gno.land/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate -func Offer -args \"TITLE\" -args 1000000 -args 604800 -gas-fee 100000ugnot -gas-wanted 60000000 "+net+" YOUR_KEY", "offer command")
65 has(t, h, "the joiner included, are refused", "format characters explained")
66 has(t, h, "-func SetCollectorKey -args GPUB_OF_COLLECTOR_KEY", "collector key command")
67 has(t, h, "-func Pull -send AMOUNTugnot -master SUPPORTER_ADDRESS -gas-fee 20000ugnot -gas-wanted 20000000 "+net+" COLLECTOR_KEY", "pull command")
68 has(t, h, "gnokey maketx session create -pubkey COLLECTOR_GPUB -allow-paths vm/exec:gno.land/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate -spend-limit PRICE_PLUS_100000ugnot -spend-period PERIOD_SECONDS -expires-at UNIX_TIME", "session command")
69 has(t, h, "gnokey maketx session revoke -pubkey COLLECTOR_GPUB", "revoke")
70 has(t, h, "-func Cancel -args YOUR_ADDRESS -args COLLECTOR_ADDRESS", "cancel")
71 has(t, h, "gnokey query auth/accounts/PAYER/session/COLLECTOR_ADDRESS -remote https://rpc.onyx.testnets.gno.land:443", "session check")
72 has(t, h, "PaidThrough(address(\"PAYER\"), PLAN_ID)", "qeval check")
73 hasNot(t, h, "127.0.0.1", "no local node")
74 hasNot(t, h, "-chainid dev", "no dev chain")
75 // Every gnokey line that talks to a node names onyx-1 or the Onyx RPC.
76 for _, line := range strings.Split(h, "\n") {
77 if strings.HasPrefix(line, "gnokey maketx") {
78 has(t, line, net, "pinned: "+line)
79 }
80 if strings.HasPrefix(line, "gnokey query") {
81 has(t, line, "-remote https://rpc.onyx.testnets.gno.land:443", "pinned: "+line)
82 }
83 }
84}
85
86func TestRenderPages(cur realm, t *testing.T) {
87 resetState()
88 pid := offer(cur, payee, "Weekly coffee", gnot, 7*day)
89 demo := offer(cur, payee, "Demo plan", gnot, 60)
90 p, _ := GetPlan(pid)
91
92 // No collector key yet: no subscribe commands.
93 pp := renders(t, "plan/1")
94 has(t, pp, "# Plan #1: Weekly coffee", "plan title")
95 has(t, pp, "**1 GNOT every week** to ["+payee.String()+"]("+base+":payee/"+payee.String()+")", "price line")
96 has(t, pp, "has not published a collector key", "no key")
97 hasNot(t, pp, "session create", "no command without key")
98 has(t, renders(t, "plan/2"), "a test plan: the period is under a day", "short period flagged")
99 hasNot(t, pp, "a test plan", "week plan not flagged")
100
101 setKey(cur, payee, gpubA)
102 subscribe(cur, payer, pid, sessA, now()+30*day)
103 subscribe(cur, payer2, pid, sessB, now()+30*day)
104 pullOK(payer, sessA, gnot)
105 cancel(cur, payer2, payer2, sessB)
106
107 pp = renders(t, "plan/1")
108 exp := itoa(now() + 365*day)
109 has(t, pp, "gnokey maketx session create -pubkey "+gpubA+" -allow-paths vm/exec:gno.land/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate -spend-limit 1100000ugnot -spend-period 604800 -expires-at "+exp+" -gas-fee 100000ugnot -gas-wanted 60000000 -broadcast -chainid onyx-1 -remote https://rpc.onyx.testnets.gno.land:443 YOUR_KEY", "session create")
110 has(t, pp, "gnokey maketx call -pkgpath gno.land/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate -func Subscribe -args 1 -args "+sessA.String()+" -args "+exp+" -gas-fee 100000ugnot", "subscribe command")
111 has(t, pp, "at most 1.1 GNOT every week", "cap in words")
112 has(t, pp, "1 payment in the current period", "paid count")
113 has(t, pp, "0 active mandates have not paid yet", "unpaid count")
114 has(t, pp, "| ["+short(payer)+"]("+base+":payer/"+payer.String()+") |", "active supporter listed")
115 hasNot(t, pp, short(payer2), "cancelled supporter not listed")
116 has(t, pp, "1 payment, 1 GNOT in total", "plan totals")
117 uassert.Equal(t, uint64(2), demo)
118 _ = p
119
120 pr := renders(t, "payer/"+payer.String())
121 has(t, pr, "## Mandate 1: [plan #1 Weekly coffee]("+base+":plan/1)", "mandate heading")
122 has(t, pr, "Status here: **active**", "status")
123 has(t, pr, "gnokey maketx session revoke -pubkey "+gpubA+" -gas-fee 100000ugnot -gas-wanted 60000000 -broadcast -chainid onyx-1 -remote https://rpc.onyx.testnets.gno.land:443 YOUR_KEY", "revoke command")
124 has(t, pr, "-func Cancel -args "+payer.String()+" -args "+sessA.String()+" -gas-fee", "cancel command")
125 has(t, pr, "Payments: 1, 1 GNOT in total", "payer totals")
126 // Unknown session public key: show how to find it.
127 setKey(cur, payee, gpubB)
128 subscribe(cur, payer2, demo, sessA, now()+day)
129 p2 := renders(t, "payer/"+payer2.String())
130 has(t, p2, "Status here: **cancelled**", "cancelled mandate")
131 has(t, p2, "gnokey query auth/accounts/"+payer2.String()+"/sessions -remote https://rpc.onyx.testnets.gno.land:443", "list sessions")
132
133 pe := renders(t, "payee/"+payee.String())
134 has(t, pe, "| 2 | 1 | 1 GNOT |", "payee totals")
135 has(t, pe, "Collector key: `"+sessB.String()+"` (`"+gpubB+"`)", "collector key shown")
136 has(t, pe, "| [#1 Weekly coffee]("+base+":plan/1) | 1 GNOT every week | 1 / 0 | 1 |", "plan row")
137
138 home := renders(t, "")
139 has(t, home, "| 2 | 3 | 1 | 1 GNOT |", "home totals")
140 has(t, renders(t, "plans"), "| [#2 Demo plan]("+base+":plan/2) | 1 GNOT every minute |", "all plans list")
141}
142
143// Titles cannot inject Markdown, HTML, links, mentions or math anywhere they
144// are shown (plan page heading, payer heading, home and payee tables).
145func TestRenderInjection(cur realm, t *testing.T) {
146 resetState()
147 setKey(cur, payee, gpubA)
148 evil := []string{
149 "# heading",
150 "[click](https://evil.example)",
151 "",
152 "<script>alert(1)</script>",
153 "a | b | c",
154 "$x^2$ and $$y$$",
155 "@moul look",
156 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 leading address",
157 "pay g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 now",
158 "`code` and ```fence",
159 "*bold* _it_ ~~s~~",
160 "> quote",
161 "<https://auto.link>",
162 "& A",
163 }
164 for _, title := range evil {
165 pid := offer(cur, payee, title, gnot, day)
166 subscribe(cur, payer, pid, sessA, now()+day)
167 cancel(cur, payer, payer, sessA)
168 pages := map[string]string{
169 "plan": renders(t, "plan/"+utoa(pid)),
170 "payer": renders(t, "payer/"+payer.String()),
171 "payee": renders(t, "payee/"+payee.String()),
172 "home": renders(t, ""),
173 "plans": renders(t, "plans"),
174 }
175 for name, pg := range pages {
176 what := name + " " + title
177 hasNot(t, pg, "](https://evil", what)
178 hasNot(t, pg, "<script>", what)
179 hasNot(t, pg, "\n# heading", what)
180 hasNot(t, strings.ReplaceAll(pg, `\$`, ""), "$", what) // every $ escaped
181 hasNot(t, strings.ReplaceAll(pg, `\@`, ""), "@", what) // every @ escaped
182 hasNot(t, pg, " g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", what)
183 hasNot(t, stripKnown(pg), "<", what) // no raw "<": no HTML, no autolink
184 hasNot(t, pg, "& A", what)
185 if name == "plans" || name == "payee" { // tables: a "|" must not split the cell
186 hasNot(t, pg, "| b |", what)
187 }
188 }
189 has(t, pages["plan"], ": "+esc(title)+"\n", "escaped heading")
190 }
191 uassert.Equal(t, "\\# heading", esc("# heading"))
192 uassert.Equal(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", esc("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
193 uassert.Equal(t, "a g1x", gnowebSafe("a g1x"))
194 uassert.Equal(t, `\$1`, gnowebSafe("$1"))
195 uassert.Equal(t, `\@x`, gnowebSafe("@x"))
196}
197
198// stripKnown removes escaped "<" and the realm's own placeholders in code
199// spans, so any "<" left would come from a title.
200func stripKnown(pg string) string {
201 for _, k := range []string{`\<`, "`/r/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/mandate:payer/<your address>`", "`gnokey maketx session revoke -pubkey <its gpub1 key> ... YOUR_KEY`"} {
202 pg = strings.ReplaceAll(pg, k, "")
203 }
204 return pg
205}
206
207func TestRenderPaging(cur realm, t *testing.T) {
208 resetState()
209 setKey(cur, payee, gpubA)
210 for i := 0; i < 21; i++ {
211 offer(cur, payee, "Plan "+itoa(int64(i+1)), gnot, day)
212 }
213 for i := 0; i < 21; i++ {
214 who := testutils.TestAddress("md_r" + itoa(int64(i)))
215 subscribe(cur, who, 1, sessA, now()+day)
216 }
217 // all subscribers pay once; plan 1's supporters then span two pages of 20
218 for i := 0; i < 21; i++ {
219 pullOK(testutils.TestAddress("md_r"+itoa(int64(i))), sessA, gnot)
220 }
221 all := renders(t, "plans")
222 has(t, all, "[Next page]("+base+":plans?page=2)", "all plans next")
223 h2 := renders(t, "plans?page=2")
224 has(t, h2, "[Previous page]("+base+":plans?page=1)", "all plans previous")
225 has(t, h2, "[#1 Plan 1]("+base+":plan/1)", "oldest on page 2")
226 has(t, renders(t, ""), "[#1 Plan 1]("+base+":plan/1) | 1 GNOT every day |", "paid plan on home")
227 plan := renders(t, "plan/1")
228 has(t, plan, "21 payments in the current period", "count")
229 has(t, plan, "| ["+short(testutils.TestAddress("md_r20"))+"]", "newest first")
230 has(t, plan, "[More supporters]("+base+":plan/1?from=0)", "plan cursor")
231 has(t, renders(t, "plan/1?from=0"), "| ["+short(testutils.TestAddress("md_r0"))+"]", "oldest on the next page")
232 // PayingSupporters pages the same way: 20 newest, then the oldest one
233 ps, next := PayingSupporters(1, -1)
234 uassert.Equal(t, PageSize, len(ps))
235 uassert.Equal(t, testutils.TestAddress("md_r20").String(), ps[0].Payer.String())
236 uassert.Equal(t, 0, next)
237 ps, next = PayingSupporters(1, next)
238 uassert.Equal(t, 1, len(ps))
239 uassert.Equal(t, testutils.TestAddress("md_r0").String(), ps[0].Payer.String())
240 uassert.Equal(t, -1, next)
241 has(t, renders(t, "payee/"+payee.String()), "[Next page]("+base+":payee/"+payee.String()+"?page=2)", "payee next")
242}