proxy_test.gno

3.02 Kb ยท 135 lines
  1package dao
  2
  3import (
  4	"std"
  5	"testing"
  6
  7	"gno.land/p/demo/testutils"
  8	"gno.land/p/demo/urequire"
  9)
 10
 11const v3 = "gno.land/r/gov/dao/v3/impl"
 12const v4 = "gno.land/r/gov/dao/v4/impl"
 13const v5 = "gno.land/r/gov/dao/v5/impl"
 14const v6 = "gno.land/r/gov/dao/v6/impl"
 15
 16const invalid = "gno.land/r/invalid/dao"
 17
 18var (
 19	alice = testutils.TestAddress("alice")
 20)
 21
 22func TestProxy_Functions(cur realm, t *testing.T) {
 23	// initialize tests
 24	UpdateImpl(cross, UpdateRequest{
 25		DAO:         &dummyDao{},
 26		AllowedDAOs: []string{v3},
 27	})
 28
 29	// invalid package cannot add a new dao in charge
 30	testing.SetRealm(std.NewCodeRealm(invalid))
 31	urequire.AbortsWithMessage(t, "permission denied for prev realm: gno.land/r/invalid/dao", func() {
 32		UpdateImpl(cross, UpdateRequest{
 33			DAO: &dummyDao{},
 34		})
 35	})
 36
 37	// dao in charge can add a new dao
 38	testing.SetRealm(std.NewCodeRealm(v3))
 39	urequire.NotPanics(t, func() {
 40		UpdateImpl(cross, UpdateRequest{
 41			DAO: &dummyDao{},
 42		})
 43	})
 44
 45	// v3 that is in charge adds v5 in charge
 46	testing.SetRealm(std.NewCodeRealm(v3))
 47	urequire.NotPanics(t, func() {
 48		UpdateImpl(cross, UpdateRequest{
 49			DAO:         &dummyDao{},
 50			AllowedDAOs: []string{v3, v5},
 51		})
 52	})
 53
 54	// v3 can still do updates
 55	testing.SetRealm(std.NewCodeRealm(v3))
 56	urequire.NotPanics(t, func() {
 57		UpdateImpl(cross, UpdateRequest{
 58			AllowedDAOs: []string{v4},
 59		})
 60	})
 61
 62	// not after removing himself from allowedDAOs list
 63	testing.SetRealm(std.NewCodeRealm(v3))
 64	urequire.AbortsWithMessage(t, "permission denied for prev realm: gno.land/r/gov/dao/v3/impl", func() {
 65		UpdateImpl(cross, UpdateRequest{
 66			AllowedDAOs: []string{v3},
 67		})
 68	})
 69
 70	var pid ProposalID
 71	testing.SetRealm(std.NewUserRealm(alice))
 72	urequire.NotPanics(t, func() {
 73		e := NewSimpleExecutor(
 74			func() error {
 75				return nil
 76			},
 77			"",
 78		)
 79		pid = MustCreateProposal(cross, NewProposalRequest("Proposal Title", "Description", e))
 80	})
 81
 82	p := MustGetProposal(cross, 1000)
 83	if p != nil {
 84		panic("proposal must be nil")
 85	}
 86	p = MustGetProposal(cross, pid)
 87	urequire.Equal(t, "Proposal Title", p.Title())
 88	urequire.Equal(t, p.Author().String(), alice.String())
 89
 90	// need to switch the context back to v4
 91	testing.SetRealm(std.NewCodeRealm(v4))
 92	urequire.Equal(
 93		t,
 94		"Render: gno.land/r/gov/dao/test",
 95		Render("test"),
 96	)
 97
 98	// reset state
 99	testing.SetRealm(std.NewCodeRealm(v4))
100	UpdateImpl(cross, UpdateRequest{
101		DAO:         &dummyDao{},
102		AllowedDAOs: []string{},
103	})
104}
105
106type dummyDao struct {
107}
108
109func (dd *dummyDao) PreCreateProposal(r ProposalRequest) (std.Address, error) {
110	return std.OriginCaller(), nil
111}
112
113func (dd *dummyDao) PostCreateProposal(r ProposalRequest, pid ProposalID) {
114
115}
116
117func (dd *dummyDao) VoteOnProposal(r VoteRequest) error {
118	return nil
119}
120
121func (dd *dummyDao) PreGetProposal(pid ProposalID) error {
122	return nil
123}
124
125func (dd *dummyDao) PostGetProposal(pid ProposalID, p *Proposal) error {
126	return nil
127}
128
129func (dd *dummyDao) PreExecuteProposal(pid ProposalID) (bool, error) {
130	return true, nil
131}
132
133func (dd *dummyDao) Render(pkgpath string, path string) string {
134	return "Render: " + pkgpath + "/" + path
135}