config_test.gno
3.11 Kb ยท 145 lines
1package config
2
3import (
4 "testing"
5
6 "gno.land/p/nt/ownable"
7 "gno.land/p/nt/testutils"
8)
9
10func TestAddBackupOwner(t *testing.T) {
11 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
12 u1 := testutils.TestAddress("u1")
13 u2 := testutils.TestAddress("u2")
14
15 testing.SetOriginCaller(owner)
16 AddBackupOwner(cross, u1)
17 b := BackupOwners()
18 if b[1] != u1.String() {
19 t.Error("failed to add u1 to backupowners")
20 }
21 testing.SetOriginCaller(u1)
22 r := revive(func() {
23 AddBackupOwner(cross, u2)
24 })
25 if r != ownable.ErrUnauthorized {
26 t.Error("failed to catch unauthorized access")
27 }
28
29 testing.SetOriginCaller(owner)
30 RemoveBackupOwner(cross, u1)
31 RemoveBackupOwner(cross, u2)
32}
33
34func TestRemoveBackupOwner(t *testing.T) {
35 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
36 u1 := testutils.TestAddress("u1")
37 u2 := testutils.TestAddress("u2")
38
39 testing.SetOriginCaller(owner)
40 AddBackupOwner(cross, u1)
41
42 testing.SetOriginCaller(u2)
43 r := revive(func() {
44 RemoveBackupOwner(cross, u1)
45 })
46 if r != ownable.ErrUnauthorized {
47 t.Error("failed to catch unauthorized access")
48 }
49
50 testing.SetOriginCaller(owner)
51 RemoveBackupOwner(cross, u1)
52
53 if len(BackupOwners()) != 1 {
54 t.Error("BackupOwners should be length == 1 ")
55 }
56}
57
58func TestClaimOwnership(t *testing.T) {
59 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
60 u1 := testutils.TestAddress("u1")
61
62 if owner != Owner() {
63 t.Errorf("expected: %v, got: %v", owner, Owner())
64 }
65
66 testing.SetOriginCaller(owner)
67 AddBackupOwner(cross, u1)
68
69 testing.SetOriginCaller(u1)
70 ClaimOwnership(cross)
71
72 if u1 != Owner() {
73 t.Errorf("expected: %v, got: %v", owner, Owner())
74 }
75
76 testing.SetOriginCaller(owner)
77 ClaimOwnership(cross)
78}
79
80func TestAddAdmin(t *testing.T) {
81 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
82 u1 := testutils.TestAddress("u1")
83 u2 := testutils.TestAddress("u2")
84
85 testing.SetOriginCaller(owner)
86 AddAdmin(cross, u1)
87 admins := Admins()
88 if admins[1] != u1.String() {
89 t.Error("failed to add u1 to admins group")
90 }
91 testing.SetOriginCaller(u1)
92 r := revive(func() {
93 AddAdmin(cross, u2)
94 })
95 if r != ownable.ErrUnauthorized {
96 t.Error("failed to catch unauthorized access")
97 }
98
99 // cleanup
100 testing.SetOriginCaller(owner)
101 RemoveAdmin(cross, u1)
102}
103
104func TestRemoveAdmin(t *testing.T) {
105 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
106 u1 := testutils.TestAddress("u1")
107 u2 := testutils.TestAddress("u2")
108
109 testing.SetOriginCaller(owner)
110 AddAdmin(cross, u1)
111
112 testing.SetOriginCaller(u2)
113 r := revive(func() {
114 RemoveAdmin(cross, u1)
115 })
116 if r != ownable.ErrUnauthorized {
117 t.Error("failed to catch unauthorized access")
118 }
119
120 testing.SetOriginCaller(owner)
121 RemoveAdmin(cross, u1)
122
123 if len(Admins()) != 1 {
124 t.Error("Admin should be length == 1 ")
125 }
126}
127
128func TestIsAdmin(t *testing.T) {
129 owner := address("g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t")
130 u1 := testutils.TestAddress("u1")
131 u2 := testutils.TestAddress("u2")
132
133 testing.SetOriginCaller(owner)
134 AddAdmin(cross, u1)
135
136 if !IsAdmin(owner) {
137 t.Error("owner should be admin")
138 }
139 if !IsAdmin(u1) {
140 t.Error("u1 should be admin")
141 }
142 if IsAdmin(u2) {
143 t.Error("u2 should not be admin")
144 }
145}