Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

policy_test.gno

2.85 Kb · 79 lines
 1package gns
 2
 3import (
 4	"testing"
 5)
 6
 7func TestOperatorDelegation(cur realm, t *testing.T) {
 8	reset()
 9	doRegister(cur, "alice", alice, 100000)
10
11	// alice grants bob record management only (not transfer)
12	testing.SetRealm(testing.NewUserRealm(alice))
13	SetOperator(cross(cur), "alice", bob, Permissions{ManageRecords: true})
14
15	// bob can set a text record
16	testing.SetRealm(testing.NewUserRealm(bob))
17	SetText(cross(cur), "alice", "url", "https://bob-managed.example")
18	v, ok := Text("alice", "url")
19	isTrue(t, ok, "operator set record")
20	eqStr(t, "https://bob-managed.example", v, "operator record value")
21
22	// invariant: operator without Transfer cannot transfer
23	n := getRaw("alice")
24	errIs(t, authorize(bob, n, PermTransfer), errUnauthorized, "record operator lacks transfer")
25	noErr(t, authorize(bob, n, PermManageRecords), "record operator has records perm")
26}
27
28func TestRemoveOperator(cur realm, t *testing.T) {
29	reset()
30	doRegister(cur, "alice", alice, 100000)
31	testing.SetRealm(testing.NewUserRealm(alice))
32	SetOperator(cross(cur), "alice", bob, Permissions{ManageRecords: true})
33	n := getRaw("alice")
34	eqInt(t, 1, int64(n.OperatorCount), "one operator")
35
36	testing.SetRealm(testing.NewUserRealm(alice))
37	RemoveOperator(cross(cur), "alice", bob)
38	n = getRaw("alice")
39	eqInt(t, 0, int64(n.OperatorCount), "operator removed")
40	errIs(t, authorize(bob, n, PermManageRecords), errUnauthorized, "removed operator loses perm")
41}
42
43func TestTransferHappy(cur realm, t *testing.T) {
44	reset()
45	doRegister(cur, "alice", alice, 100000)
46	r0 := getRaw("alice").Revision
47
48	testing.SetRealm(testing.NewUserRealm(alice))
49	Transfer(cross(cur), "alice", bob, true, false)
50
51	owner, ok := OwnerOf("alice")
52	isTrue(t, ok, "still exists")
53	eqStr(t, bob.String(), owner, "owner now bob")
54	isTrue(t, getRaw("alice").Revision > r0, "revision bumped on transfer (invariant 12 side-effect)")
55}
56
57func TestAuthorizeOrder(cur realm, t *testing.T) {
58	reset()
59	doRegister(cur, "alice", alice, 100000)
60	n := getRaw("alice")
61
62	// direct owner (active) authorized
63	noErr(t, authorize(alice, n, PermManageRecords), "owner authorized")
64	// unrelated user rejected
65	errIs(t, authorize(carol, n, PermManageRecords), errUnauthorized, "stranger rejected")
66	// admin has NO routine authority over user names (invariant 18)
67	errIs(t, authorize(admin, n, PermTransfer), errUnauthorized, "admin cannot transfer user name")
68}
69
70func TestNonTransferablePolicyPure(t *testing.T) {
71	reset()
72	// A name whose policy forbids owner transfer: the Transfer owner-branch
73	// checks OwnerCanTransfer and rejects with policy_locked. We assert the
74	// policy flag drives that decision.
75	n := newTestName("locked", alice, ControlPolicy{OwnerCanTransfer: false})
76	isFalse(t, n.ControlPolicy.OwnerCanTransfer, "policy forbids transfer")
77	// and no operator/parent path grants transfer either
78	errIs(t, authorize(bob, n, PermTransfer), errUnauthorized, "no delegated transfer")
79}