Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

capwallet_test.gno

2.92 Kb · 91 lines
 1package capwallet
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/testutils/v0"
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10var (
11	owner = testutils.TestAddress("owner")
12	percy = testutils.TestAddress("percy")
13	mallory = testutils.TestAddress("mallory")
14)
15
16func TestGrantAndExercise(cur realm, t *testing.T) {
17	testing.SetRealm(testing.NewUserRealm(owner))
18	// Percy may call r/moul/issues.Assign, ≤5 GNOT, 2 times, no expiry.
19	id := Grant(cross(cur), percy, "gno.land/r/moul/issues", "Assign", 5000000, 0, 2, "issue IDs 100-200")
20
21	// Read-only gate other realms use.
22	uassert.True(t, Authorized(id, percy, 5000000))
23	uassert.False(t, Authorized(id, percy, 5000001)) // over ceiling
24	uassert.False(t, Authorized(id, mallory, 1))      // wrong principal
25
26	testing.SetRealm(testing.NewUserRealm(percy))
27	Exercise(cross(cur), id, 3000000)
28	uassert.Equal(t, uint32(1), Get(id).RemainingUses)
29	Exercise(cross(cur), id, 1000000)
30	uassert.Equal(t, uint32(0), Get(id).RemainingUses)
31
32	// Exhausted.
33	uassert.False(t, Authorized(id, percy, 1))
34	uassert.AbortsWithMessage(t, cur, "capability exhausted", func(cur realm) {
35		Exercise(cur, id, 1)
36	})
37}
38
39func TestCeilingEnforced(cur realm, t *testing.T) {
40	testing.SetRealm(testing.NewUserRealm(owner))
41	id := Grant(cross(cur), percy, "r/x", "F", 100, 0, 3, "")
42	testing.SetRealm(testing.NewUserRealm(percy))
43	uassert.AbortsWithMessage(t, cur, "coins 101 exceed capability ceiling 100", func(cur realm) {
44		Exercise(cur, id, 101)
45	})
46}
47
48func TestOnlyPrincipal(cur realm, t *testing.T) {
49	testing.SetRealm(testing.NewUserRealm(owner))
50	id := Grant(cross(cur), percy, "r/x", "F", 100, 0, 3, "")
51	testing.SetRealm(testing.NewUserRealm(mallory))
52	uassert.AbortsWithMessage(t, cur, "caller is not the capability principal", func(cur realm) {
53		Exercise(cur, id, 1)
54	})
55}
56
57func TestRevoke(cur realm, t *testing.T) {
58	testing.SetRealm(testing.NewUserRealm(owner))
59	id := Grant(cross(cur), percy, "r/x", "F", 100, 0, 3, "")
60
61	// Only the granter may revoke.
62	testing.SetRealm(testing.NewUserRealm(mallory))
63	uassert.AbortsWithMessage(t, cur, "only the granter may revoke", func(cur realm) {
64		Revoke(cur, id)
65	})
66
67	testing.SetRealm(testing.NewUserRealm(owner))
68	Revoke(cross(cur), id)
69	uassert.False(t, Authorized(id, percy, 1))
70	testing.SetRealm(testing.NewUserRealm(percy))
71	uassert.AbortsWithMessage(t, cur, "capability revoked", func(cur realm) {
72		Exercise(cur, id, 1)
73	})
74}
75
76func TestExpiry(cur realm, t *testing.T) {
77	testing.SetRealm(testing.NewUserRealm(owner))
78	h := int64(0) // ChainHeight in tests starts at 0; set expiry in the past-ish
79	_ = h
80	// Grant valid only up to height 1.
81	id := Grant(cross(cur), percy, "r/x", "F", 100, 1, 3, "")
82	testing.SkipHeights(5) // now well past height 1
83	uassert.False(t, Authorized(id, percy, 1))
84	testing.SetRealm(testing.NewUserRealm(percy))
85	uassert.AbortsWithMessage(t, cur, "capability expired", func(cur realm) {
86		Exercise(cur, id, 1)
87	})
88
89	uassert.True(t, len(Render("")) > 0)
90	uassert.True(t, len(Render("1")) > 0)
91}