Skip to content

Commit

Permalink
feat: caller can't remove self
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Jan 16, 2023
1 parent 4055858 commit dac208f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/gno.land/r/system/names/names.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (s *Space) removeAdmin(admin std.Address) error {
if len(s.Admins) == 1 {
return errors.New("namespace at least needs one admin")
}
if isCallerAddress(admin) {
return errors.New("cannot remove self")
}
admins := removeAddress(s.Admins, admin)
s.Admins = admins
return nil
Expand All @@ -49,6 +52,9 @@ func (s *Space) addEditor(newEditor std.Address) {
}

func (s *Space) removeEditor(editor std.Address) error {
if isCallerAddress(editor) {
return errors.New("cannot remove self")
}
editors := removeAddress(s.Editors, editor)
s.Editors = editors
return nil
Expand Down
29 changes: 27 additions & 2 deletions examples/gno.land/r/system/names/public_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRegisterNamespace(t *testing.T) {
assertTrue(t, containsAddress(s.Admins, creator))
}

func TestAddAdmins(t *testing.T) {
func TestAdminFuncs(t *testing.T) {
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"))
creator := std.GetOrigCaller()

Expand All @@ -33,7 +33,7 @@ func TestAddAdmins(t *testing.T) {
assertTrue(t, containsAddress(s.Admins, test2))

RemoveAdmin(ns, test1)
assertTrue(t, !containsAddress(s.Admins, test1))
assertFalse(t, containsAddress(s.Admins, test1))
assertTrue(t, containsAddress(s.Admins, test2))

AddEditor(ns, test1)
Expand All @@ -43,8 +43,33 @@ func TestAddAdmins(t *testing.T) {
assertTrue(t, !containsAddress(s.Editors, test1))
}

func TestSpaceMethods(t *testing.T) {
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"))
creator := std.GetOrigCaller()

test1 := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
test2 := std.Address("g1r6casl322p5adkmmjjkjea404e7f6w29y6gzwg")

ns := "test4"
Register(ns)
s := getSpace(ns)

assertTrue(t, s.isAdmin(creator))
assertTrue(t, s.removeAdmin(creator) != nil)

s.addAdmin(test1)
assertTrue(t, s.removeAdmin(test1) == nil)

}

func assertTrue(t *testing.T, b bool) {
if !b {
t.Fail()
}
}

func assertFalse(t *testing.T, b bool) {
if b {
t.Fail()
}
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/system/names/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func containsAddress(addrs []std.Address, addr std.Address) bool {
return false
}

func isCallerAddress(addr std.Address) bool {
if addr == std.GetOrigCaller() {
return true
}
return false
}

func checkErr(err error) {
if err != nil {
panic(err)
Expand Down

0 comments on commit dac208f

Please sign in to comment.