Search Apps Documentation Source Content File Folder Download Copy Actions Download

guestbook_test.gno

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