proxy_test.gno
3.04 Kb ยท 133 lines
1package dao
2
3import (
4 "chain/runtime"
5 "testing"
6
7 "gno.land/p/nt/testutils"
8 "gno.land/p/nt/urequire"
9)
10
11const (
12 v3 = "gno.land/r/gov/dao/v3/impl"
13 v4 = "gno.land/r/gov/dao/v4/impl"
14 v5 = "gno.land/r/gov/dao/v5/impl"
15 v6 = "gno.land/r/gov/dao/v6/impl"
16)
17
18const invalid = "gno.land/r/invalid/dao"
19
20var alice = testutils.TestAddress("alice")
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(testing.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(testing.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(testing.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(testing.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(testing.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(testing.NewUserRealm(alice))
72 urequire.NotPanics(t, func() {
73 e := NewSimpleExecutor(
74 func(realm) 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(testing.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(testing.NewCodeRealm(v4))
100 UpdateImpl(cross, UpdateRequest{
101 DAO: &dummyDao{},
102 AllowedDAOs: []string{},
103 })
104}
105
106type dummyDao struct{}
107
108func (dd *dummyDao) PreCreateProposal(r ProposalRequest) (address, error) {
109 return runtime.OriginCaller(), nil
110}
111
112func (dd *dummyDao) PostCreateProposal(r ProposalRequest, pid ProposalID) {
113}
114
115func (dd *dummyDao) VoteOnProposal(r VoteRequest) error {
116 return nil
117}
118
119func (dd *dummyDao) PreGetProposal(pid ProposalID) error {
120 return nil
121}
122
123func (dd *dummyDao) PostGetProposal(pid ProposalID, p *Proposal) error {
124 return nil
125}
126
127func (dd *dummyDao) PreExecuteProposal(pid ProposalID) (bool, error) {
128 return true, nil
129}
130
131func (dd *dummyDao) Render(pkgpath string, path string) string {
132 return "Render: " + pkgpath + "/" + path
133}