Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

authorizable_test.gno

2.93 Kb · 107 lines
  1package authorizable
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/ownable/v0"
  7	"gno.land/p/nt/testutils/v0"
  8	"gno.land/p/nt/uassert/v0"
  9)
 10
 11// cur is a zero-value realm used as a placeholder when forwarding to
 12// uassert/urequire dispatch helpers that gained an `rlm realm` param.
 13// These tests pass `func()` callbacks (no crossing inside the callback),
 14// so rlm is ignored — a nil realm here is safe.
 15var cur realm
 16var (
 17	alice   = testutils.TestAddress("alice")
 18	bob     = testutils.TestAddress("bob")
 19	charlie = testutils.TestAddress("charlie")
 20)
 21
 22func TestNew(cur realm, t *testing.T) {
 23	testing.SetRealm(testing.NewUserRealm(alice))
 24
 25	a := New(ownable.NewWithAddress(alice))
 26	got := a.Owner()
 27
 28	if alice != got {
 29		t.Fatalf("Expected %s, got: %s", alice, got)
 30	}
 31}
 32
 33func TestOnAuthList(cur realm, t *testing.T) {
 34	a := New(ownable.NewWithAddress(alice))
 35	testing.SetRealm(testing.NewUserRealm(alice))
 36
 37	// After SetRealm, cur is the test-frame HIV with addr=alice;
 38	// OnAuthList reports on rlm.Address() (i.e. cur.Address()).
 39	if err := a.OnAuthList(0, cur); err == ErrNotInAuthList {
 40		t.Fatalf("expected alice to be on the list")
 41	}
 42}
 43
 44func TestNotOnAuthList(cur realm, t *testing.T) {
 45	a := New(ownable.NewWithAddress(alice))
 46	testing.SetRealm(testing.NewUserRealm(bob))
 47
 48	if err := a.OnAuthList(0, cur); err == nil {
 49		t.Fatalf("expected bob to not be on the list")
 50	}
 51}
 52
 53func TestAddToAuthList(cur realm, t *testing.T) {
 54	a := New(ownable.NewWithAddress(alice))
 55
 56	testing.SetRealm(testing.NewUserRealm(alice))
 57	var err error
 58	func(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))
 59	if err != nil {
 60		t.Fatalf("Expected no error, got %v", err)
 61	}
 62
 63	testing.SetRealm(testing.NewUserRealm(bob))
 64	func(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))
 65	if err == nil {
 66		t.Fatalf("Expected AddToAuth to error while bob called it, but it didn't")
 67	}
 68}
 69
 70func TestDeleteFromList(cur realm, t *testing.T) {
 71	a := New(ownable.NewWithAddress(alice))
 72
 73	testing.SetRealm(testing.NewUserRealm(alice))
 74	var err error
 75	func(cur realm) { err = a.AddToAuthList(0, cur, bob) }(cross(cur))
 76	if err != nil {
 77		t.Fatalf("Expected no error, got %v", err)
 78	}
 79
 80	func(cur realm) { err = a.AddToAuthList(0, cur, charlie) }(cross(cur))
 81	if err != nil {
 82		t.Fatalf("Expected no error, got %v", err)
 83	}
 84
 85	// Try an unauthorized deletion (bob is not the superuser).
 86	testing.SetRealm(testing.NewUserRealm(bob))
 87	func(cur realm) { err = a.DeleteFromAuthList(0, cur, alice) }(cross(cur))
 88	if err == nil {
 89		t.Fatalf("Expected DelFromAuth to error with %v", err)
 90	}
 91
 92	testing.SetRealm(testing.NewUserRealm(alice))
 93	func(cur realm) { err = a.DeleteFromAuthList(0, cur, charlie) }(cross(cur))
 94	if err != nil {
 95		t.Fatalf("Expected no error, got %v", err)
 96	}
 97}
 98
 99func TestAssertOnList(cur realm, t *testing.T) {
100	a := New(ownable.NewWithAddress(alice))
101
102	testing.SetRealm(testing.NewUserRealm(bob))
103
104	uassert.PanicsWithMessage(t, cur, ErrNotInAuthList.Error(), func() {
105		a.AssertOnAuthList(0, cur)
106	})
107}