config_test.gno

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