1package guestbook
2
3import (
4 "std"
5 "strings"
6 "testing"
7
8 "gno.land/p/demo/avl"
9 "gno.land/p/demo/ownable"
10)
11
12func TestSign(t *testing.T) {
13 guestbook = avl.Tree{}
14 hasSigned = avl.Tree{}
15
16 std.TestSetRealm(std.NewUserRealm("g1user"))
17 Sign("Hello!")
18
19 std.TestSetRealm(std.NewUserRealm("g1user2"))
20 Sign("Hello2!")
21
22 res := Render("")
23 t.Log(res)
24 if !strings.Contains(res, "> Hello!\n>\n> _Written by g1user ") {
25 t.Error("does not contain first user's message")
26 }
27 if !strings.Contains(res, "> Hello2!\n>\n> _Written by g1user2 ") {
28 t.Error("does not contain second user's message")
29 }
30 if guestbook.Size() != 2 {
31 t.Error("invalid guestbook size")
32 }
33}
34
35func TestSign_FromRealm(t *testing.T) {
36 std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/users"))
37
38 defer func() {
39 rec := recover()
40 if rec == nil {
41 t.Fatal("expected panic")
42 }
43 recString, ok := rec.(string)
44 if !ok {
45 t.Fatal("not a string", rec)
46 } else if recString != errNotAUser {
47 t.Fatal("invalid error", recString)
48 }
49 }()
50 Sign("Hey!")
51}
52
53func TestSign_Double(t *testing.T) {
54 // Should not allow signing twice.
55 guestbook = avl.Tree{}
56 hasSigned = avl.Tree{}
57
58 std.TestSetRealm(std.NewUserRealm("g1user"))
59 Sign("Hello!")
60
61 defer func() {
62 rec := recover()
63 if rec == nil {
64 t.Fatal("expected panic")
65 }
66 recString, ok := rec.(string)
67 if !ok {
68 t.Error("type assertion failed", rec)
69 } else if recString != errAlreadySigned {
70 t.Error("invalid error message", recString)
71 }
72 }()
73
74 Sign("Hello again!")
75}
76
77func TestSign_InvalidMessage(t *testing.T) {
78 // Should not allow control characters in message.
79 guestbook = avl.Tree{}
80 hasSigned = avl.Tree{}
81
82 std.TestSetRealm(std.NewUserRealm("g1user"))
83
84 defer func() {
85 rec := recover()
86 if rec == nil {
87 t.Fatal("expected panic")
88 }
89 recString, ok := rec.(string)
90 if !ok {
91 t.Error("type assertion failed", rec)
92 } else if recString != errInvalidCharacterInMessage {
93 t.Error("invalid error message", recString)
94 }
95 }()
96 Sign("\x00Hello!")
97}
98
99func TestAdminDelete(t *testing.T) {
100 const (
101 userAddr std.Address = "g1user"
102 adminAddr std.Address = "g1admin"
103 )
104
105 guestbook = avl.Tree{}
106 hasSigned = avl.Tree{}
107 owner = ownable.NewWithAddress(adminAddr)
108 signatureID = 0
109
110 std.TestSetRealm(std.NewUserRealm(userAddr))
111
112 const bad = "Very Bad Message! Nyeh heh heh!"
113 Sign(bad)
114
115 if rnd := Render(""); !strings.Contains(rnd, bad) {
116 t.Fatal("render does not contain bad message", rnd)
117 }
118
119 std.TestSetRealm(std.NewUserRealm(adminAddr))
120 AdminDelete(signatureID.String())
121
122 if rnd := Render(""); strings.Contains(rnd, bad) {
123 t.Error("render contains bad message", rnd)
124 }
125 if guestbook.Size() != 0 {
126 t.Error("invalid guestbook size")
127 }
128 if hasSigned.Size() != 1 {
129 t.Error("invalid hasSigned size")
130 }
131}
guestbook_test.gno
2.71 Kb ยท 131 lines