Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render_test.gno

3.30 Kb · 100 lines
  1package usernames
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/uassert/v0"
  8)
  9
 10func TestRenderHome(cur realm, t *testing.T) {
 11	seed(cur, t)
 12
 13	got := Render("")
 14	uassert.True(t, strings.Contains(got, "[toolbox](/r/moul/toolbox)"))
 15	uassert.True(t, strings.Contains(got, "# Usernames"))
 16	uassert.True(t, strings.Contains(got, `<gno-form path="lookup">`))
 17	uassert.True(t, strings.Contains(got, `<gno-input name="q"`))
 18	uassert.True(t, strings.Contains(got, "gnokey query vm/qeval"))
 19}
 20
 21func TestRenderResultByPathAndByQuery(cur realm, t *testing.T) {
 22	seed(cur, t)
 23
 24	byPath := Render("moul")
 25	byQuery := Render("lookup?q=moul")
 26	uassert.Equal(t, byPath, byQuery)
 27	uassert.True(t, strings.Contains(byPath, "[@moul](/u/moul)"))
 28	uassert.True(t, strings.Contains(byPath, moulAddr.String()))
 29	uassert.True(t, strings.Contains(byPath, "Canonical"))
 30}
 31
 32func TestRenderResultByAddress(cur realm, t *testing.T) {
 33	seed(cur, t)
 34
 35	got := Render(moulAddr.String())
 36	uassert.True(t, strings.Contains(got, "[@moul](/u/moul)"))
 37}
 38
 39func TestRenderNotFound(cur realm, t *testing.T) {
 40	seed(cur, t)
 41
 42	got := Render("nobodyholdsthis")
 43	uassert.True(t, strings.Contains(got, "Nothing registered under"))
 44	uassert.True(t, strings.Contains(got, "deleted"))
 45
 46	addr := Render(plainAddr.String())
 47	uassert.True(t, strings.Contains(addr, "That is a valid address"))
 48	uassert.True(t, strings.Contains(addr, "/r/gnoland/users"))
 49}
 50
 51func TestRenderTooManyParts(cur realm, t *testing.T) {
 52	seed(cur, t)
 53
 54	got := Render("a/b")
 55	uassert.True(t, strings.Contains(got, "Nothing registered under"))
 56}
 57
 58// TestRenderEscapesInput: the query parameter is attacker-chosen and a live
 59// realm can never be fixed, so this is the test that must be seen to fail
 60// before it is believed.
 61//
 62// The property is that the echo is INERT, not that particular bytes are
 63// absent. A code span makes link syntax inert without removing it, so an
 64// assertion that the substring is gone would pass for the wrong reason under
 65// one escaper and fail for the wrong reason under the other.
 66func TestRenderEscapesInput(cur realm, t *testing.T) {
 67	seed(cur, t)
 68
 69	const link = "[x](javascript:alert(1))"
 70	got := Render("lookup?q=" + "%5Bx%5D%28javascript%3Aalert%281%29%29")
 71	uassert.True(t, strings.Contains(got, "`"+link+"`"))
 72	uassert.False(t, strings.Contains(got, " "+link+" "))
 73
 74	// A backtick in the input cannot close the span early: the fence widens.
 75	tick := Render("lookup?q=a%60b")
 76	uassert.True(t, strings.Contains(tick, "``"))
 77	uassert.False(t, strings.Contains(tick, "under `a`b`."))
 78
 79	// A heading marker cannot open a block of its own: the echo stays on the
 80	// line it was put on.
 81	head := Render("lookup?q=%0A%23%20pwned")
 82	uassert.False(t, strings.Contains(head, "\n# pwned"))
 83}
 84
 85// TestRenderOmitsTheCommandForUnsafeInput: nothing escapes inside a fenced
 86// code block, so an input that cannot go in one does not get one.
 87func TestRenderOmitsTheCommandForUnsafeInput(cur realm, t *testing.T) {
 88	seed(cur, t)
 89
 90	uassert.True(t, strings.Contains(Render("moul"), "gnokey query vm/qeval"))
 91	uassert.False(t, strings.Contains(Render("lookup?q=a%60b"), "gnokey query vm/qeval"))
 92}
 93
 94func TestRenderNeverEmitsTwoBlankLines(cur realm, t *testing.T) {
 95	seed(cur, t)
 96
 97	for _, path := range []string{"", "moul", "nobodyholdsthis", moulAddr.String(), "a/b"} {
 98		uassert.False(t, strings.Contains(Render(path), "\n\n\n"))
 99	}
100}