authorizable_test.gno

2.35 Kb ยท 106 lines
  1package authorizable
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/testutils"
  7	"gno.land/p/nt/uassert"
  8)
  9
 10var (
 11	alice   = testutils.TestAddress("alice")
 12	bob     = testutils.TestAddress("bob")
 13	charlie = testutils.TestAddress("charlie")
 14)
 15
 16func TestNewAuthorizable(t *testing.T) {
 17	testing.SetRealm(testing.NewUserRealm(alice))
 18
 19	a := NewAuthorizable()
 20	got := a.Owner()
 21
 22	if alice != got {
 23		t.Fatalf("Expected %s, got: %s", alice, got)
 24	}
 25}
 26
 27func TestNewAuthorizableWithAddress(t *testing.T) {
 28	a := NewAuthorizableWithAddress(alice)
 29
 30	got := a.Owner()
 31
 32	if alice != got {
 33		t.Fatalf("Expected %s, got: %s", alice, got)
 34	}
 35}
 36
 37func TestOnAuthList(t *testing.T) {
 38	a := NewAuthorizableWithAddress(alice)
 39	testing.SetRealm(testing.NewUserRealm(alice))
 40
 41	if err := a.OnAuthList(); err == ErrNotInAuthList {
 42		t.Fatalf("expected alice to be on the list")
 43	}
 44}
 45
 46func TestNotOnAuthList(t *testing.T) {
 47	a := NewAuthorizableWithAddress(alice)
 48	testing.SetRealm(testing.NewUserRealm(bob))
 49
 50	if err := a.OnAuthList(); err == nil {
 51		t.Fatalf("expected bob to not be on the list")
 52	}
 53}
 54
 55func TestAddToAuthList(t *testing.T) {
 56	a := NewAuthorizableWithAddress(alice)
 57	testing.SetRealm(testing.NewUserRealm(alice))
 58
 59	if err := a.AddToAuthList(bob); err != nil {
 60		t.Fatalf("Expected no error, got %v", err)
 61	}
 62
 63	testing.SetRealm(testing.NewUserRealm(bob))
 64
 65	if err := a.AddToAuthList(bob); err == nil {
 66		t.Fatalf("Expected AddToAuth to error while bob called it, but it didn't")
 67	}
 68}
 69
 70func TestDeleteFromList(t *testing.T) {
 71	a := NewAuthorizableWithAddress(alice)
 72	testing.SetRealm(testing.NewUserRealm(alice))
 73
 74	if err := a.AddToAuthList(bob); err != nil {
 75		t.Fatalf("Expected no error, got %v", err)
 76	}
 77
 78	if err := a.AddToAuthList(charlie); err != nil {
 79		t.Fatalf("Expected no error, got %v", err)
 80	}
 81
 82	testing.SetRealm(testing.NewUserRealm(bob))
 83
 84	// Try an unauthorized deletion
 85	if err := a.DeleteFromAuthList(alice); err == nil {
 86		t.Fatalf("Expected DelFromAuth to error with %v", err)
 87	}
 88
 89	testing.SetRealm(testing.NewUserRealm(alice))
 90
 91	if err := a.DeleteFromAuthList(charlie); err != nil {
 92		t.Fatalf("Expected no error, got %v", err)
 93	}
 94}
 95
 96func TestAssertOnList(t *testing.T) {
 97	testing.SetRealm(testing.NewUserRealm(alice))
 98
 99	a := NewAuthorizableWithAddress(alice)
100
101	testing.SetRealm(testing.NewUserRealm(bob))
102
103	uassert.PanicsWithMessage(t, ErrNotInAuthList.Error(), func() {
104		a.AssertOnAuthList()
105	})
106}