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.

blog_test.gno

6.86 Kb · 223 lines
  1package blog
  2
  3import (
  4	"strings"
  5	"testing"
  6	"time"
  7
  8	gnoblog "gno.land/p/gnoland/blog/v0"
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/p/nt/testutils/v0"
 11	"gno.land/p/nt/uassert/v0"
 12)
 13
 14var (
 15	hot   = address("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w")
 16	cold  = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6")
 17	bob   = testutils.TestAddress("bob")
 18	alice = testutils.TestAddress("alice")
 19)
 20
 21const (
 22	d1 = "2026-12-05T00:00:00Z"
 23	d2 = "2026-12-06T00:00:00Z"
 24)
 25
 26// reset puts the realm back to its state right after deployment.
 27func reset() {
 28	owner = cold
 29	pendingOwner = ""
 30	writers = avl.Tree{}
 31	writers.Set(hot.String(), true)
 32	b = &gnoblog.Blog{Title: "Jae Kwon", Prefix: "/r/nym-nobody777/blog:"}
 33}
 34
 35func TestRenderOnlyKnownRoutes(cur realm, t *testing.T) {
 36	reset()
 37	for _, path := range []string{
 38		"t/[click](https:evil.com)",
 39		"t/UPPER",
 40		"t/",
 41		"p/a b",
 42		"p/<b>x</b>",
 43		"x/abc",
 44		"?foo",
 45		"t/" + strings.Repeat("a", 101),
 46	} {
 47		uassert.Equal(t, "404", Render(path), path)
 48	}
 49	uassert.True(t, strings.Contains(Render(""), "No posts."))
 50}
 51
 52func TestWritersPost(cur realm, t *testing.T) {
 53	reset()
 54	testing.SetOriginCaller(bob)
 55	testing.SetRealm(testing.NewUserRealm(bob))
 56	uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
 57		Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro")
 58	})
 59
 60	testing.SetOriginCaller(hot)
 61	testing.SetRealm(testing.NewUserRealm(hot))
 62	uassert.NotAborts(t, cur, func() {
 63		Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro, ai")
 64	})
 65	testing.SetOriginCaller(cold)
 66	testing.SetRealm(testing.NewUserRealm(cold))
 67	uassert.NotAborts(t, cur, func() {
 68		Post(cross(cur), "second", "Second", "body 2", d2, "", "")
 69	})
 70
 71	page := Render("p/hello")
 72	uassert.True(t, strings.Contains(page, "# Hello"), page)
 73	uassert.True(t, strings.Contains(page, "Written by jaekwon"), page)
 74	uassert.True(t, strings.Contains(page, "Published by "+hot.String()), page)
 75	uassert.True(t, strings.Contains(page, "[#ai](/r/nym-nobody777/blog:t/ai)"), page)
 76	uassert.True(t, strings.Contains(Render("t/intro"), "Hello"))
 77	uassert.True(t, strings.Contains(Render(""), "Second"))
 78}
 79
 80func TestPostValidation(cur realm, t *testing.T) {
 81	reset()
 82	testing.SetOriginCaller(hot)
 83	testing.SetRealm(testing.NewUserRealm(hot))
 84	uassert.AbortsWithMessage(t, cur, "slug must be 1 to 100 characters of a-z, 0-9, - or _", func() {
 85		Post(cross(cur), "Bad Slug", "T", "b", d1, "", "")
 86	})
 87	uassert.AbortsWithMessage(t, cur, "tags must be 1 to 100 characters of a-z, 0-9, - or _", func() {
 88		Post(cross(cur), "ok", "T", "b", d1, "", "fine,[evil](x)")
 89	})
 90	uassert.AbortsWithMessage(t, cur, "post title is missing", func() {
 91		Post(cross(cur), "ok", "  ", "b", d1, "", "")
 92	})
 93}
 94
 95func TestEditLeavesNoStaleCopy(cur realm, t *testing.T) {
 96	reset()
 97	testing.SetOriginCaller(hot)
 98	testing.SetRealm(testing.NewUserRealm(hot))
 99	Post(cross(cur), "hello", "Hello", "body", d1, "", "old")
100	Edit(cross(cur), "hello", "Hello again", "new body", d2, "", "new")
101
102	uassert.Equal(t, 1, b.Posts.Size())
103	uassert.Equal(t, 1, b.PostsPublished.Size())
104	uassert.Equal(t, 1, b.PostsAlphabetical.Size())
105	home := Render("")
106	uassert.Equal(t, 1, strings.Count(home, "/r/nym-nobody777/blog:p/hello"), home)
107	uassert.True(t, strings.Contains(home, "Hello again"), home)
108	uassert.True(t, strings.Contains(Render("t/old"), "No posts."))
109	uassert.True(t, strings.Contains(Render("t/new"), "Hello again"))
110
111	// An empty pubDate keeps the post's date.
112	Edit(cross(cur), "hello", "Hello again", "newer body", "", "", "")
113	uassert.Equal(t, d2, b.GetPost("hello").CreatedAt.Format(time.RFC3339))
114
115	testing.SetOriginCaller(bob)
116	testing.SetRealm(testing.NewUserRealm(bob))
117	uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
118		Edit(cross(cur), "hello", "Hijacked", "x", "", "", "")
119	})
120	testing.SetOriginCaller(hot)
121	testing.SetRealm(testing.NewUserRealm(hot))
122	uassert.AbortsWithMessage(t, cur, "no such post", func() {
123		Edit(cross(cur), "missing", "T", "b", "", "", "")
124	})
125}
126
127func TestRemove(cur realm, t *testing.T) {
128	reset()
129	testing.SetOriginCaller(hot)
130	testing.SetRealm(testing.NewUserRealm(hot))
131	Post(cross(cur), "hello", "Hello", "body", d1, "", "")
132
133	testing.SetOriginCaller(bob)
134	testing.SetRealm(testing.NewUserRealm(bob))
135	uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
136		Remove(cross(cur), "hello")
137	})
138
139	testing.SetOriginCaller(hot)
140	testing.SetRealm(testing.NewUserRealm(hot))
141	Remove(cross(cur), "hello")
142	uassert.Equal(t, "404", Render("p/hello"))
143	uassert.True(t, strings.Contains(Render(""), "No posts."))
144	uassert.AbortsWithMessage(t, cur, "no such post", func() {
145		Remove(cross(cur), "hello")
146	})
147}
148
149func TestOwnerManagesWriters(cur realm, t *testing.T) {
150	reset()
151	testing.SetOriginCaller(hot)
152	testing.SetRealm(testing.NewUserRealm(hot))
153	uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() {
154		AddWriter(cross(cur), bob)
155	})
156
157	testing.SetOriginCaller(cold)
158	testing.SetRealm(testing.NewUserRealm(cold))
159	AddWriter(cross(cur), bob)
160	uassert.True(t, IsWriter(bob))
161	testing.SetOriginCaller(bob)
162	testing.SetRealm(testing.NewUserRealm(bob))
163	uassert.NotAborts(t, cur, func() {
164		Post(cross(cur), "bobs", "Bob's", "b", d1, "", "")
165	})
166
167	testing.SetOriginCaller(cold)
168	testing.SetRealm(testing.NewUserRealm(cold))
169	RemoveWriter(cross(cur), bob)
170	RemoveWriter(cross(cur), hot)
171	uassert.False(t, IsWriter(bob))
172	uassert.False(t, IsWriter(hot))
173	uassert.True(t, IsWriter(cold))
174	uassert.AbortsWithMessage(t, cur, "not a writer", func() {
175		RemoveWriter(cross(cur), bob)
176	})
177
178	testing.SetOriginCaller(hot)
179	testing.SetRealm(testing.NewUserRealm(hot))
180	uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
181		Post(cross(cur), "x", "X", "b", d2, "", "")
182	})
183}
184
185func TestOwnershipHandover(cur realm, t *testing.T) {
186	reset()
187	testing.SetOriginCaller(cold)
188	testing.SetRealm(testing.NewUserRealm(cold))
189	ProposeOwner(cross(cur), alice)
190	uassert.Equal(t, alice, PendingOwner())
191
192	testing.SetOriginCaller(bob)
193	testing.SetRealm(testing.NewUserRealm(bob))
194	uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() {
195		AcceptOwnership(cross(cur))
196	})
197
198	testing.SetOriginCaller(alice)
199	testing.SetRealm(testing.NewUserRealm(alice))
200	AcceptOwnership(cross(cur))
201	uassert.Equal(t, alice, Owner())
202	uassert.Equal(t, address(""), PendingOwner())
203
204	testing.SetOriginCaller(cold)
205	testing.SetRealm(testing.NewUserRealm(cold))
206	uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() {
207		AddWriter(cross(cur), bob)
208	})
209	testing.SetOriginCaller(alice)
210	testing.SetRealm(testing.NewUserRealm(alice))
211	uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() {
212		AcceptOwnership(cross(cur))
213	})
214}
215
216func TestCodeRealmCannotWrite(cur realm, t *testing.T) {
217	reset()
218	testing.SetOriginCaller(hot)
219	testing.SetRealm(testing.NewCodeRealm("gno.land/r/someone/proxy"))
220	uassert.AbortsWithMessage(t, cur, "must be called directly by a user", func() {
221		Post(cross(cur), "hello", "Hello", "body", d1, "", "")
222	})
223}