Skip to content

Commit

Permalink
add admin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Nov 22, 2024
1 parent 3e58017 commit 69cfe1c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 28 deletions.
23 changes: 0 additions & 23 deletions examples/gno.land/r/demo/boards/z_0_b_filetest.gno

This file was deleted.

2 changes: 1 addition & 1 deletion examples/gno.land/r/sys/users/administration.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

func init() {
callerWhitelist.Set(std.DerivePkgAddr("gno.land/r/gnoland/users"), struct{}{})
callerWhitelist.Set(std.DerivePkgAddr("gno.land/r/gnoland/users").String(), struct{}{})
}

func AddToWhitelist(addr std.Address) error {
Expand Down
68 changes: 68 additions & 0 deletions examples/gno.land/r/sys/users/administration_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package users

import (
"std"
"testing"

"gno.land/p/demo/uassert"
)

var (
whitelistedCaller = std.DerivePkgAddr("gno.land/r/gnoland/users")
adminRealm = std.NewUserRealm(adminAddr)
alice = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
aliceCodeRealm = std.NewCodeRealm("gno.land/r/alice/package")
)

func TestIsOnWhitelist(t *testing.T) {
t.Run("init_caller_registration", func(t *testing.T) {
t.Parallel()
uassert.True(t, IsOnWhitelist(whitelistedCaller))
})

t.Run("not on whitelist", func(t *testing.T) {
t.Parallel()
uassert.False(t, IsOnWhitelist(aliceCodeRealm.Addr()))
})
}

func TestAddToWhitelist(t *testing.T) {
t.Run("invalid new address", func(t *testing.T) {
t.Parallel()
std.TestSetRealm(adminRealm)

uassert.Error(t, AddToWhitelist(std.Address("invalidaddress")), ErrInvalidAddress.Error())
uassert.Error(t, AddToWhitelist(std.Address("")), ErrInvalidAddress.Error())
uassert.Error(t, AddToWhitelist(std.Address("000000000000000000000000000000000000000000000000")), ErrInvalidAddress.Error())
})

t.Run("already whitelisted", func(t *testing.T) {
t.Parallel()
std.TestSetRealm(adminRealm)

uassert.Error(t, AddToWhitelist(whitelistedCaller), ErrAlreadyWhitelisted.Error())
})

t.Run("valid addition", func(t *testing.T) {
t.Parallel()
std.TestSetRealm(adminRealm)

uassert.NoError(t, AddToWhitelist("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
})
}

func TestDelFromWhitelist(t *testing.T) {
t.Run("not on whitelist", func(t *testing.T) {
t.Parallel()
std.TestSetRealm(adminRealm)

uassert.Error(t, DelFromWhitelist(std.Address("")), ErrDoesNotExist.Error())
})

t.Run("valid deletion", func(t *testing.T) {
t.Parallel()
std.TestSetRealm(adminRealm)

uassert.NoError(t, DelFromWhitelist(whitelistedCaller))
})
}
2 changes: 1 addition & 1 deletion examples/gno.land/r/sys/users/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package users
import "errors"

var (
ErrUnauthorized = errors.New("r/sys/users: not authorized")
ErrUnauthorizedWrite = errors.New("r/sys/users: not authorized to write to store")
ErrAlreadyExists = errors.New("r/sys/users: username already exists")
ErrAlreadyWhitelisted = errors.New("r/sys/users: already whitelisted")
ErrDoesNotExist = errors.New("r/sys/users: does not exist in whitelist")
Expand Down
11 changes: 9 additions & 2 deletions examples/gno.land/r/sys/users/store.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ var (

// WriteUser writes to the sys/users stores
// Only whitelisted callers, such as r/gnoland/users, can call this function
// Writing to the store can be paused by the admin
func WriteUser(user *pusers.User) error {
if pausable.IsPaused() {
return p.ErrPaused
}

if !callerWhitelist.Has(std.PrevRealm().Addr().String()) {
return ErrUnauthorized
if !IsOnWhitelist(std.PrevRealm().Addr()) {
return ErrUnauthorizedWrite
}

username := user.Name
Expand All @@ -41,6 +42,12 @@ func WriteUser(user *pusers.User) error {
return nil
}

// IsOnWhitelist checks if the given address has
// permission to write to the user store
func IsOnWhitelist(addr std.Address) bool {
return callerWhitelist.Has(addr.String())
}

// UserCount returns the number of registered users
func UserCount() int {
return aStore.Size()
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/sys/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func Users(offset, count uint64) []pusers.User {
func UsersByPrefix(prefix string, maxResults int) []string {
// most likely moved to r/gnoland/users to keep this package minimal.
// in that case, should we get a value copy of the uStore available to whitelisted callers?
return avlhelpers.ListByteStringKeysByPrefix(uStore, prefix, maxResults)
return avlhelpers.ListByteStringKeysByPrefix(*uStore, prefix, maxResults)
}

0 comments on commit 69cfe1c

Please sign in to comment.