Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

admin_test.gno

5.21 Kb · 219 lines
  1package blog
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/demo/blog"
  7	"gno.land/p/nt/bptree/v0"
  8	"gno.land/p/nt/testutils/v0"
  9	"gno.land/p/nt/uassert/v0"
 10	"gno.land/p/nt/urequire/v0"
 11)
 12
 13// cur is a zero-value realm used as a placeholder when forwarding to
 14// uassert/urequire dispatch helpers that gained an `rlm realm` param.
 15// These tests pass `func()` callbacks (no crossing inside the callback),
 16// so rlm is ignored — a nil realm here is safe.
 17var cur realm
 18
 19// clearState wipes the global state between test calls
 20func clearState(t *testing.T) {
 21	t.Helper()
 22
 23	b = &blog.Blog{
 24		Title:  "Gno.land's blog",
 25		Prefix: "/r/gnoland/blog:",
 26	}
 27	adminAddr = address("g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh")
 28	inPause = false
 29	moderatorList = bptree.NewBPTree32()
 30	commenterList = bptree.NewBPTree32()
 31}
 32
 33func TestBlog_AdminControls(cur realm, t *testing.T) {
 34	t.Run("non-admin call", func(t *testing.T) {
 35		clearState(t)
 36
 37		nonAdmin := testutils.TestAddress("bob")
 38
 39		testing.SetOriginCaller(nonAdmin)
 40		testing.SetRealm(testing.NewUserRealm(nonAdmin))
 41
 42		uassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {
 43			AdminSetInPause(cross(cur), true)
 44		})
 45	})
 46
 47	t.Run("pause toggled", func(t *testing.T) {
 48		clearState(t)
 49
 50		testing.SetOriginCaller(adminAddr)
 51		testing.SetRealm(testing.NewUserRealm(adminAddr))
 52
 53		uassert.NotAborts(t, cur, func() {
 54			AdminSetInPause(cross(cur), true)
 55		})
 56
 57		uassert.True(t, inPause)
 58	})
 59
 60	t.Run("admin set", func(t *testing.T) {
 61		clearState(t)
 62
 63		// Set the new admin
 64		var (
 65			oldAdmin = adminAddr
 66			newAdmin = testutils.TestAddress("alice")
 67		)
 68
 69		testing.SetRealm(testing.NewUserRealm(adminAddr))
 70		AdminSetAdminAddr(cross(cur), newAdmin)
 71
 72		uassert.Equal(t, adminAddr, newAdmin)
 73
 74		// Make sure the old admin can't do anything
 75		testing.SetOriginCaller(oldAdmin)
 76		testing.SetRealm(testing.NewUserRealm(oldAdmin))
 77
 78		uassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {
 79			AdminSetInPause(cross(cur), false)
 80		})
 81	})
 82}
 83
 84func TestBlog_AddRemoveModerator(cur realm, t *testing.T) {
 85	clearState(t)
 86
 87	mod := testutils.TestAddress("mod")
 88
 89	// Add the moderator
 90	testing.SetOriginCaller(adminAddr)
 91	testing.SetRealm(testing.NewUserRealm(adminAddr))
 92	AdminAddModerator(cross(cur), mod)
 93
 94	urequire.True(t, moderatorList.Has(mod.String()))
 95
 96	// Remove the moderator
 97	AdminRemoveModerator(cross(cur), mod)
 98
 99	// Make sure the moderator is disabled
100	isMod := moderatorList.Get(mod.String())
101	uassert.NotNil(t, isMod)
102
103	uassert.False(t, isMod.(bool))
104}
105
106func TestBlog_AddCommenter(cur realm, t *testing.T) {
107	clearState(t)
108
109	var (
110		mod       = testutils.TestAddress("mod")
111		commenter = testutils.TestAddress("comm")
112		rand      = testutils.TestAddress("rand")
113	)
114
115	// Appoint the moderator
116	testing.SetOriginCaller(adminAddr)
117	testing.SetRealm(testing.NewUserRealm(adminAddr))
118	AdminAddModerator(cross(cur), mod)
119
120	// Add a commenter as a mod
121	testing.SetOriginCaller(mod)
122	testing.SetRealm(testing.NewUserRealm(mod))
123	ModAddCommenter(cross(cur), commenter)
124
125	uassert.True(t, commenterList.Has(commenter.String()))
126
127	// Make sure a non-mod can't add commenters
128	testing.SetOriginCaller(rand)
129	testing.SetRealm(testing.NewUserRealm(rand))
130
131	uassert.AbortsWithMessage(t, cur, errNotModerator.Error(), func() {
132		ModAddCommenter(cross(cur), testutils.TestAddress("evil"))
133	})
134
135	// Remove a commenter
136	testing.SetOriginCaller(mod)
137	testing.SetRealm(testing.NewUserRealm(mod))
138	ModDelCommenter(cross(cur), commenter)
139
140	active := commenterList.Get(commenter.String())
141	uassert.False(t, active.(bool))
142}
143
144func TestBlog_ManagePost(cur realm, t *testing.T) {
145	clearState(t)
146
147	var (
148		mod  = testutils.TestAddress("mod")
149		slug = "slug"
150	)
151
152	// Appoint the moderator
153	testing.SetOriginCaller(adminAddr)
154	testing.SetRealm(testing.NewUserRealm(adminAddr))
155	AdminAddModerator(cross(cur), mod)
156
157	// Add the post
158	testing.SetOriginCaller(mod)
159	testing.SetRealm(testing.NewUserRealm(mod))
160	ModAddPost(
161		cross(cur),
162		slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
163	)
164
165	// Make sure the post is present
166	uassert.NotNil(t, b.GetPost(slug))
167
168	// Remove the post
169	ModRemovePost(cross(cur), slug)
170	uassert.TypedNil(t, b.GetPost(slug))
171}
172
173func TestBlog_ManageComment(cur realm, t *testing.T) {
174	clearState(t)
175
176	var (
177		slug = "slug"
178
179		mod       = testutils.TestAddress("mod")
180		commenter = testutils.TestAddress("comm")
181	)
182
183	// Appoint the moderator
184	testing.SetOriginCaller(adminAddr)
185	testing.SetRealm(testing.NewUserRealm(adminAddr))
186	AdminAddModerator(cross(cur), mod)
187
188	// Add a commenter as a mod
189	testing.SetOriginCaller(mod)
190	testing.SetRealm(testing.NewUserRealm(mod))
191	ModAddCommenter(cross(cur), commenter)
192
193	uassert.True(t, commenterList.Has(commenter.String()))
194
195	// Add the post
196	testing.SetOriginCaller(mod)
197	testing.SetRealm(testing.NewUserRealm(mod))
198	ModAddPost(
199		cross(cur),
200		slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
201	)
202
203	// Make sure the post is present
204	uassert.NotNil(t, b.GetPost(slug))
205
206	// Add the comment
207	testing.SetOriginCaller(commenter)
208	testing.SetRealm(testing.NewUserRealm(commenter))
209	uassert.NotAborts(t, cur, func() {
210		AddComment(cross(cur), slug, "comment")
211	})
212
213	// Delete the comment
214	testing.SetOriginCaller(mod)
215	testing.SetRealm(testing.NewUserRealm(mod))
216	uassert.NotAborts(t, cur, func() {
217		ModDelComment(cross(cur), slug, 0)
218	})
219}