Search Apps Documentation Source Content File Folder Download Copy Actions Download

users_test.gno

2.42 Kb ยท 87 lines
 1package users
 2
 3import (
 4	"chain"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils"
 8	"gno.land/p/nt/uassert"
 9	"gno.land/p/nt/urequire"
10
11	susers "gno.land/r/sys/users"
12)
13
14func TestRegister_Valid(t *testing.T) {
15	validUsernames := []string{
16		"abc123",               // letters + digits
17		"abc123_def456",        // mix of letters / digits / underscore
18		"abc_defghi_jklmn123",  // 19 chars
19		"abc_defghi_jklmno456", // 20 chars (max)
20	}
21
22	for _, username := range validUsernames {
23		addr := testutils.TestAddress(username)
24
25		// Simulate a proper user call with the required 1GNOT fee.
26		testing.SetRealm(testing.NewUserRealm(addr))
27		testing.SetOriginCaller(addr)
28		testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", registerPrice)))
29
30		urequire.NotPanics(t, func() {
31			Register(cross, username)
32		})
33	}
34}
35
36func TestRegister_Invalid(t *testing.T) {
37	t.Run("invalid usernames", func(t *testing.T) {
38		testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", registerPrice)))
39		testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bob123")))
40
41		invalidUsernames := []string{
42			"alice",                 // vanity
43			"",                      // empty
44			"    ",                  // empty
45			"123",                   // only numbers (no letters)
46			"abc",                   // only letters (no numbers)
47			"alice&#($)123",         // invalid characters
48			"Alice123",              // uppercase
49			"abcdefghijklmnopqr123", // 21 chars (> max)
50			"toolongusernametoolongusernametoolongusername123", // super long
51		}
52
53		for _, username := range invalidUsernames {
54			uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
55				Register(cross, username)
56			})
57		}
58	})
59
60	t.Run("taken username", func(t *testing.T) {
61		testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", registerPrice)))
62		testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bob123")))
63
64		username := "bob123"
65
66		urequire.NotPanics(t, func() {
67			Register(cross, username)
68		})
69
70		uassert.AbortsWithMessage(t, susers.ErrNameTaken.Error(), func() {
71			Register(cross, username) // already registered
72		})
73	})
74
75	t.Run("invalid payment", func(t *testing.T) {
76		addr := testutils.TestAddress("bob123")
77
78		testing.SetRealm(testing.NewUserRealm(addr))
79		testing.SetOriginCaller(addr)
80
81		testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 12))) // invalid payment amount
82
83		uassert.AbortsWithMessage(t, ErrInvalidPayment.Error(), func() {
84			Register(cross, "alice123")
85		})
86	})
87}