Search Apps Documentation Source Content File Folder Download Copy Actions Download

proxy_test.gno

3.47 Kb · 135 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	v3 = "gno.land/r/gov/dao/v3/impl"
 19	v4 = "gno.land/r/gov/dao/v4/impl"
 20	v5 = "gno.land/r/gov/dao/v5/impl"
 21	v6 = "gno.land/r/gov/dao/v6/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{v3},
 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(v3))
 45	urequire.NotPanics(t, cur, func() {
 46		UpdateImpl(cross(cur), UpdateRequest{
 47			DAO: &dummyDao{},
 48		})
 49	})
 50
 51	// v3 that is in charge adds v5 in charge
 52	testing.SetRealm(testing.NewCodeRealm(v3))
 53	urequire.NotPanics(t, cur, func() {
 54		UpdateImpl(cross(cur), UpdateRequest{
 55			DAO:         &dummyDao{},
 56			AllowedDAOs: []string{v3, v5},
 57		})
 58	})
 59
 60	// v3 can still do updates
 61	testing.SetRealm(testing.NewCodeRealm(v3))
 62	urequire.NotPanics(t, cur, func() {
 63		UpdateImpl(cross(cur), UpdateRequest{
 64			AllowedDAOs: []string{v4},
 65		})
 66	})
 67
 68	// not after removing himself from allowedDAOs list
 69	testing.SetRealm(testing.NewCodeRealm(v3))
 70	urequire.AbortsWithMessage(t, cur, "permission denied for prev realm: gno.land/r/gov/dao/v3/impl", func() {
 71		UpdateImpl(cross(cur), UpdateRequest{
 72			AllowedDAOs: []string{v3},
 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 v4
 97	testing.SetRealm(testing.NewCodeRealm(v4))
 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(v4))
106	UpdateImpl(cross(cur), UpdateRequest{
107		DAO:         &dummyDao{},
108		AllowedDAOs: []string{},
109	})
110}
111
112type dummyDao struct{}
113
114func (dd *dummyDao) PreCreateProposal(_ int, rlm realm, r ProposalRequest) (address, error) {
115	return unsafe.OriginCaller(), nil
116}
117
118func (dd *dummyDao) PostCreateProposal(_ int, rlm realm, r ProposalRequest, pid ProposalID) {
119}
120
121func (dd *dummyDao) VoteOnProposal(_ int, rlm realm, r VoteRequest) error {
122	return nil
123}
124
125func (dd *dummyDao) PreExecuteProposal(_ int, rlm realm, pid ProposalID) (bool, error) {
126	return true, nil
127}
128
129func (dd *dummyDao) ExecuteProposal(_ int, rlm realm, pid ProposalID, e Executor) error {
130	return nil
131}
132
133func (dd *dummyDao) Render(cur realm, pkgpath string, path string) string {
134	return "Render: " + pkgpath + "/" + path
135}