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

proxy_test.gno

3.69 Kb · 139 lines
  1package dao
  2
  3import (
  4	"chain/runtime/unsafe"
  5	"testing"
  6
  7	"gno.land/p/nt/testutils/v0"
  8	"gno.land/p/nt/urequire/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
 16
 17const (
 18	v0 = "gno.land/r/gov/dao/impl/v0"
 19	v1 = "gno.land/r/gov/dao/v1/impl"
 20	v2 = "gno.land/r/gov/dao/v2/impl"
 21	v3 = "gno.land/r/gov/dao/v3/impl"
 22)
 23
 24const invalid = "gno.land/r/invalid/dao"
 25
 26var alice = testutils.TestAddress("alice")
 27
 28func TestProxy_Functions(cur realm, t *testing.T) {
 29	// initialize tests
 30	UpdateImpl(cross(cur), UpdateRequest{
 31		DAO:         &dummyDao{},
 32		AllowedDAOs: []string{v0},
 33	})
 34
 35	// invalid package cannot add a new dao in charge
 36	testing.SetRealm(testing.NewCodeRealm(invalid))
 37	urequire.AbortsWithMessage(t, cur, "permission denied for prev realm: gno.land/r/invalid/dao", func() {
 38		UpdateImpl(cross(cur), UpdateRequest{
 39			DAO: &dummyDao{},
 40		})
 41	})
 42
 43	// dao in charge can add a new dao
 44	testing.SetRealm(testing.NewCodeRealm(v0))
 45	urequire.NotPanics(t, cur, func() {
 46		UpdateImpl(cross(cur), UpdateRequest{
 47			DAO: &dummyDao{},
 48		})
 49	})
 50
 51	// v0 that is in charge adds v2 in charge
 52	testing.SetRealm(testing.NewCodeRealm(v0))
 53	urequire.NotPanics(t, cur, func() {
 54		UpdateImpl(cross(cur), UpdateRequest{
 55			DAO:         &dummyDao{},
 56			AllowedDAOs: []string{v0, v2},
 57		})
 58	})
 59
 60	// v0 can still do updates
 61	testing.SetRealm(testing.NewCodeRealm(v0))
 62	urequire.NotPanics(t, cur, func() {
 63		UpdateImpl(cross(cur), UpdateRequest{
 64			AllowedDAOs: []string{v1},
 65		})
 66	})
 67
 68	// not after removing himself from allowedDAOs list
 69	testing.SetRealm(testing.NewCodeRealm(v0))
 70	urequire.AbortsWithMessage(t, cur, "permission denied for prev realm: gno.land/r/gov/dao/impl/v0", func() {
 71		UpdateImpl(cross(cur), UpdateRequest{
 72			AllowedDAOs: []string{v0},
 73		})
 74	})
 75
 76	var pid ProposalID
 77	testing.SetRealm(testing.NewUserRealm(alice))
 78	urequire.NotPanics(t, cur, func() {
 79		e := NewSimpleExecutor(0, cur,
 80			func(realm) error {
 81				return nil
 82			},
 83			"",
 84		)
 85		pid = MustCreateProposal(cross(cur), NewProposalRequest("Proposal Title", "Description", e))
 86	})
 87
 88	p, err := GetProposal(1000)
 89	if p != nil || err == nil {
 90		panic("proposal should not exist and should return an error")
 91	}
 92	p = MustGetProposal(pid)
 93	urequire.Equal(t, "Proposal Title", p.Title())
 94	urequire.Equal(t, p.Author().String(), alice.String())
 95
 96	// need to switch the context back to v1
 97	testing.SetRealm(testing.NewCodeRealm(v1))
 98	urequire.Equal(
 99		t,
100		"Render: gno.land/r/gov/dao/test",
101		Render(cross(cur), "test"),
102	)
103
104	// reset state
105	testing.SetRealm(testing.NewCodeRealm(v1))
106	UpdateImpl(cross(cur), UpdateRequest{
107		DAO: &dummyDao{},
108	})
109	// UpdateImpl deliberately ignores an empty AllowedDAOs, so it can no
110	// longer be used to restore the bootstrap-open state — that is the point
111	// of the guard. Reset the package var directly instead; we are in the
112	// same package.
113	allowedDAOs = nil
114}
115
116type dummyDao struct{}
117
118func (dd *dummyDao) PreCreateProposal(_ int, rlm realm, r ProposalRequest) (address, error) {
119	return unsafe.OriginCaller(), nil
120}
121
122func (dd *dummyDao) PostCreateProposal(_ int, rlm realm, r ProposalRequest, pid ProposalID) {
123}
124
125func (dd *dummyDao) VoteOnProposal(_ int, rlm realm, r VoteRequest) error {
126	return nil
127}
128
129func (dd *dummyDao) PreExecuteProposal(_ int, rlm realm, pid ProposalID) (bool, error) {
130	return true, nil
131}
132
133func (dd *dummyDao) ExecuteProposal(_ int, rlm realm, pid ProposalID, e Executor) error {
134	return nil
135}
136
137func (dd *dummyDao) Render(cur realm, pkgpath string, path string) string {
138	return "Render: " + pkgpath + "/" + path
139}