Search Apps Documentation Source Content File Folder Download Copy Actions Download

ownable_test.gno

2.62 Kb · 115 lines
  1package ownable
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/testutils/v0"
  7	"gno.land/p/nt/uassert/v0"
  8	"gno.land/p/nt/urequire/v0"
  9)
 10
 11var (
 12	alice = testutils.TestAddress("alice")
 13	bob   = testutils.TestAddress("bob")
 14)
 15
 16func TestNewWithAddress(cur realm, t *testing.T) {
 17	o := NewWithAddress(alice)
 18
 19	got := o.Owner()
 20	uassert.Equal(t, got, alice)
 21}
 22
 23func TestTransferOwnership(cur realm, t *testing.T) {
 24	testing.SetRealm(testing.NewUserRealm(alice))
 25	o := NewWithAddress(alice)
 26	func(cur realm) {
 27		err := o.TransferOwnership(0, cur, bob)
 28		urequire.NoError(t, err)
 29	}(cross(cur))
 30
 31	got := o.Owner()
 32	uassert.Equal(t, got, bob)
 33}
 34
 35func TestTransferOwnershipUnauthorized(cur realm, t *testing.T) {
 36	o := NewWithAddress(alice)
 37
 38	testing.SetRealm(testing.NewUserRealm(bob))
 39	func(cur realm) {
 40		uassert.ErrorContains(t, o.TransferOwnership(0, cur, bob), ErrUnauthorized.Error())
 41	}(cross(cur))
 42}
 43
 44func TestDropOwnership(cur realm, t *testing.T) {
 45	testing.SetRealm(testing.NewUserRealm(alice))
 46	o := NewWithAddress(alice)
 47	func(cur realm) {
 48		err := o.DropOwnership(0, cur)
 49		urequire.NoError(t, err, "DropOwnership failed")
 50	}(cross(cur))
 51
 52	owner := o.Owner()
 53	uassert.Empty(t, owner, "owner should be empty")
 54}
 55
 56func TestDropOwnershipUnauthorized(cur realm, t *testing.T) {
 57	o := NewWithAddress(alice)
 58
 59	testing.SetRealm(testing.NewUserRealm(bob))
 60	func(cur realm) {
 61		uassert.ErrorContains(t, o.DropOwnership(0, cur), ErrUnauthorized.Error())
 62	}(cross(cur))
 63}
 64
 65func TestOwnedBy(cur realm, t *testing.T) {
 66	o := NewWithAddress(alice)
 67	uassert.True(t, o.OwnedBy(alice))
 68	uassert.False(t, o.OwnedBy(bob))
 69}
 70
 71func TestAssertOwnedBy(cur realm, t *testing.T) {
 72	o := NewWithAddress(alice)
 73
 74	// Should not panic.
 75	o.AssertOwnedBy(alice)
 76
 77	uassert.PanicsWithMessage(t, cur, ErrUnauthorized.Error(), func() {
 78		o.AssertOwnedBy(bob)
 79	})
 80}
 81
 82func TestErrInvalidAddress(cur realm, t *testing.T) {
 83	testing.SetRealm(testing.NewUserRealm(alice))
 84	o := NewWithAddress(alice)
 85	func(cur realm) {
 86		err := o.TransferOwnership(0, cur, "")
 87		uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
 88
 89		err = o.TransferOwnership(0, cur, "10000000001000000000100000000010000000001000000000")
 90		uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
 91	}(cross(cur))
 92}
 93
 94func TestNilReceiver(cur realm, t *testing.T) {
 95	var o *Ownable
 96
 97	owner := o.Owner()
 98	if owner != address("") {
 99		t.Errorf("expected empty address but got %v", owner)
100	}
101
102	isOwner := o.OwnedBy(alice)
103	uassert.False(t, isOwner)
104
105	defer func() {
106		r := recover()
107		if r == nil {
108			t.Error("expected panic but got none")
109		}
110		if r != ErrUnauthorized {
111			t.Errorf("expected ErrUnauthorized but got %v", r)
112		}
113	}()
114	o.AssertOwnedBy(alice)
115}