Search Apps Documentation Source Content File Folder Download Copy Actions Download

admin_test.gno

5.27 Kb · 222 lines
  1package gnoblog
  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	_, found := moderatorList.Get(mod.String())
 95	urequire.True(t, found)
 96
 97	// Remove the moderator
 98	AdminRemoveModerator(cross(cur), mod)
 99
100	// Make sure the moderator is disabled
101	isMod, _ := moderatorList.Get(mod.String())
102	uassert.NotNil(t, isMod)
103
104	uassert.False(t, isMod.(bool))
105}
106
107func TestBlog_AddCommenter(cur realm, t *testing.T) {
108	clearState(t)
109
110	var (
111		mod       = testutils.TestAddress("mod")
112		commenter = testutils.TestAddress("comm")
113		rand      = testutils.TestAddress("rand")
114	)
115
116	// Appoint the moderator
117	testing.SetOriginCaller(adminAddr)
118	testing.SetRealm(testing.NewUserRealm(adminAddr))
119	AdminAddModerator(cross(cur), mod)
120
121	// Add a commenter as a mod
122	testing.SetOriginCaller(mod)
123	testing.SetRealm(testing.NewUserRealm(mod))
124	ModAddCommenter(cross(cur), commenter)
125
126	_, ok := commenterList.Get(commenter.String())
127	uassert.True(t, ok)
128
129	// Make sure a non-mod can't add commenters
130	testing.SetOriginCaller(rand)
131	testing.SetRealm(testing.NewUserRealm(rand))
132
133	uassert.AbortsWithMessage(t, cur, errNotModerator.Error(), func() {
134		ModAddCommenter(cross(cur), testutils.TestAddress("evil"))
135	})
136
137	// Remove a commenter
138	testing.SetOriginCaller(mod)
139	testing.SetRealm(testing.NewUserRealm(mod))
140	ModDelCommenter(cross(cur), commenter)
141
142	active, _ := commenterList.Get(commenter.String())
143	uassert.False(t, active.(bool))
144}
145
146func TestBlog_ManagePost(cur realm, t *testing.T) {
147	clearState(t)
148
149	var (
150		mod  = testutils.TestAddress("mod")
151		slug = "slug"
152	)
153
154	// Appoint the moderator
155	testing.SetOriginCaller(adminAddr)
156	testing.SetRealm(testing.NewUserRealm(adminAddr))
157	AdminAddModerator(cross(cur), mod)
158
159	// Add the post
160	testing.SetOriginCaller(mod)
161	testing.SetRealm(testing.NewUserRealm(mod))
162	ModAddPost(
163		cross(cur),
164		slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
165	)
166
167	// Make sure the post is present
168	uassert.NotNil(t, b.GetPost(slug))
169
170	// Remove the post
171	ModRemovePost(cross(cur), slug)
172	uassert.TypedNil(t, b.GetPost(slug))
173}
174
175func TestBlog_ManageComment(cur realm, t *testing.T) {
176	clearState(t)
177
178	var (
179		slug = "slug"
180
181		mod       = testutils.TestAddress("mod")
182		commenter = testutils.TestAddress("comm")
183	)
184
185	// Appoint the moderator
186	testing.SetOriginCaller(adminAddr)
187	testing.SetRealm(testing.NewUserRealm(adminAddr))
188	AdminAddModerator(cross(cur), mod)
189
190	// Add a commenter as a mod
191	testing.SetOriginCaller(mod)
192	testing.SetRealm(testing.NewUserRealm(mod))
193	ModAddCommenter(cross(cur), commenter)
194
195	_, ok := commenterList.Get(commenter.String())
196	uassert.True(t, ok)
197
198	// Add the post
199	testing.SetOriginCaller(mod)
200	testing.SetRealm(testing.NewUserRealm(mod))
201	ModAddPost(
202		cross(cur),
203		slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
204	)
205
206	// Make sure the post is present
207	uassert.NotNil(t, b.GetPost(slug))
208
209	// Add the comment
210	testing.SetOriginCaller(commenter)
211	testing.SetRealm(testing.NewUserRealm(commenter))
212	uassert.NotAborts(t, cur, func() {
213		AddComment(cross(cur), slug, "comment")
214	})
215
216	// Delete the comment
217	testing.SetOriginCaller(mod)
218	testing.SetRealm(testing.NewUserRealm(mod))
219	uassert.NotAborts(t, cur, func() {
220		ModDelComment(cross(cur), slug, 0)
221	})
222}