Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Dec 12, 2024
1 parent 14374bc commit 8c880fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
7 changes: 6 additions & 1 deletion examples/gno.land/p/demo/ownable/ownable.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const OwnershipTransferEvent = "OwnershipTransfer"

// Ownable is meant to be used as a top-level object to make your contract ownable OR
// being embedded in a Gno object to manage per-object ownership.
// Do not export this object, as its methods do not validate the caller.
// WARNING: Do not directly export this object at top-level, as its methods do not validate the caller.
// For this, check out SafeOwnable.
type Ownable struct {
owner std.Address
Expand Down Expand Up @@ -88,6 +88,11 @@ func (o *Ownable) NewSafeOwnable() *SafeOwnable {
}
}

// AssertCallerIsOwner panics if the caller is not the owner
func (so SafeOwnable) AssertCallerIsOwner() {
so.ownable.AssertCallerIsOwner()
}

func (so *SafeOwnable) TransferOwnership(newOwner std.Address) error {
if !so.ownable.CallerIsOwner() {
return ErrUnauthorized
Expand Down
33 changes: 11 additions & 22 deletions examples/gno.land/p/demo/ownable/ownable_test.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ownable

import (
"github.com/gnolang/gno/examples/gno.land/p/demo/urequire"
"std"
"testing"

Expand All @@ -13,24 +14,23 @@ var (
bob = testutils.TestAddress("bob")
)

// embed ownable and test
// test safeownable

func TestNew(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice) // TODO(bug): should not be needed

o := New()
got := o.Owner()
if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
uassert.Equal(t, got, alice)
}

func TestNewWithAddress(t *testing.T) {
o := NewWithAddress(alice)

got := o.Owner()
if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
uassert.Equal(t, got, alice)
}

func TestTransferOwnership(t *testing.T) {
Expand All @@ -39,14 +39,9 @@ func TestTransferOwnership(t *testing.T) {
o := New()

err := o.TransferOwnership(bob)
if err != nil {
t.Fatalf("TransferOwnership failed, %v", err)
}

got := o.Owner()
if bob != got {
t.Fatalf("Expected: %s, got: %s", bob, got)
}
uassert.Equal(t, got, bob)
}

func TestCallerIsOwner(t *testing.T) {
Expand All @@ -58,8 +53,7 @@ func TestCallerIsOwner(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(unauthorizedCaller))
std.TestSetOrigCaller(unauthorizedCaller) // TODO(bug): should not be needed

err := o.CallerIsOwner()
uassert.Error(t, err) // XXX: IsError(..., unauthorizedCaller)
uassert.False(t, o.CallerIsOwner())
}

func TestDropOwnership(t *testing.T) {
Expand All @@ -68,7 +62,7 @@ func TestDropOwnership(t *testing.T) {
o := New()

err := o.DropOwnership()
uassert.NoError(t, err, "DropOwnership failed")
urequire.NoError(t, err, "DropOwnership failed")

owner := o.Owner()
uassert.Empty(t, owner, "owner should be empty")
Expand All @@ -85,13 +79,8 @@ func TestErrUnauthorized(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(bob))
std.TestSetOrigCaller(bob) // TODO(bug): should not be needed

err := o.TransferOwnership(alice)
if err != ErrUnauthorized {
t.Fatalf("Should've been ErrUnauthorized, was %v", err)
}

err = o.DropOwnership()
uassert.ErrorContains(t, err, ErrUnauthorized.Error())
uassert.ErrorContains(t, o.TransferOwnership(alice), ErrUnauthorized.Error())
uassert.ErrorContains(t, o.DropOwnership(), ErrUnauthorized.Error())
}

func TestErrInvalidAddress(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions examples/gno.land/p/demo/ownable/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "gno.land/p/demo/ownable"

func main() {
_ = ownable.New()

}

0 comments on commit 8c880fb

Please sign in to comment.