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