-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
159 lines (122 loc) · 4.06 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"testing"
"github.com/bjartek/go-with-the-flow/v2/gwtf"
"github.com/onflow/cadence"
)
func TestDeployContract(t *testing.T) {
g := gwtf.NewGoWithTheFlowInMemoryEmulator()
// no voucher on start
deployAndFail(g, t, DeveloperAccount)
// init auditor
authorizeAuditor(g, t)
// auditor creates new voucher for developer account
auditContract(g, t, false, false, 0, 0)
// developer cannot deploy to another account
deployAndFail(g, t, DeveloperAccount2)
deployAndFail(g, t, DeveloperAccount3)
// developer can deploy audited contract
deploy(g, t, DeveloperAccount, false, 0, false)
// developer cannot deploy audited contract twice
deployAndFail(g, t, DeveloperAccount)
}
func TestDeployRecurrentContract(t *testing.T) {
g := gwtf.NewGoWithTheFlowInMemoryEmulator()
// init auditor
authorizeAuditor(g, t)
// auditor adds recurrent voucher for any account
auditContract(g, t, true, true, 0, 0)
// developer can deploy audited contract
deploy(g, t, DeveloperAccount, true, 0, true)
// developer can deploy audited contract again
deploy(g, t, DeveloperAccount2, true, 0, true)
deploy(g, t, DeveloperAccount3, true, 0, true)
// auditor updates voucher to non-recurrent for any account
g.TransactionFromFile(AuditorNewAuditTx).
SignProposeAndPayAs(AuditorAccount).
Argument(cadence.NewOptional(nil)).
StringArgument(TestContractCode).
BooleanArgument(false).
Argument(cadence.NewOptional(cadence.NewUInt64(1))).
Test(t).
AssertSuccess().
AssertEmitEvent(gwtf.NewTestEvent(VoucherCreatedEventName, map[string]interface{}{
"address": "",
"codeHash": TestContractCodeSHA3,
"expiryBlockHeight": "13",
"recurrent": "false",
})).
AssertEmitEvent(gwtf.NewTestEvent(VoucherRemovedEventName, map[string]interface{}{
"key": fmt.Sprintf("any-%s", TestContractCodeSHA3),
"expiryBlockHeight": "",
"recurrent": "true",
}))
// developer deploys and uses voucher
deploy(g, t, DeveloperAccount, false, 13, true)
// developer cannot deploy any more
deployAndFail(g, t, DeveloperAccount)
}
func TestDeleteVoucher(t *testing.T) {
g := gwtf.NewGoWithTheFlowInMemoryEmulator()
// init auditor
authorizeAuditor(g, t)
// auditor adds recurrent voucher
auditContract(g, t, false, true, 0, 0)
// developer can deploy audited contract
deploy(g, t, DeveloperAccount, true, 0, false)
// delete voucher
g.TransactionFromFile(AuditorDeleteAuditTx).
SignProposeAndPayAs(AuditorAccount).
StringArgument(fmt.Sprintf("0x%s-%s", g.Account(DeveloperAccount).Address().String(), TestContractCodeSHA3)).
Test(t).
AssertSuccess().
AssertEmitEvent(gwtf.NewTestEvent(VoucherRemovedEventName, map[string]interface{}{
"key": "0x" + g.Account(DeveloperAccount).Address().String() + "-" + TestContractCodeSHA3,
"expiryBlockHeight": "",
"recurrent": "true",
}))
// developer cannot deploy any more
deployAndFail(g, t, DeveloperAccount)
}
func TestExpiredVouchers(t *testing.T) {
g := gwtf.NewGoWithTheFlowInMemoryEmulator()
// init auditor
authorizeAuditor(g, t)
// auditor adds recurrent voucher for any account
auditContract(g, t, true, true, 2, 10)
// developer can deploy audited contract for 2 blocks
deploy(g, t, DeveloperAccount, true, 10, true)
deploy(g, t, DeveloperAccount2, true, 10, true)
// voucher expired
deployAndFail(g, t, DeveloperAccount3)
}
func TestCleanupExpired(t *testing.T) {
g := gwtf.NewGoWithTheFlowInMemoryEmulator()
// init auditor
authorizeAuditor(g, t)
// auditor adds recurrent voucher for any account
auditContract(g, t, true, true, 1, 9)
// check count
if getVouchersCount(g, t) != 1 {
t.Fail()
}
// cleanup
g.TransactionFromFile(AdminCleanupExpiredVouchersTx).
SignProposeAndPayAsService().
Test(t).
AssertSuccess()
// check count, block offset still valid
if getVouchersCount(g, t) != 1 {
t.Fail()
}
// cleanup
g.TransactionFromFile(AdminCleanupExpiredVouchersTx).
SignProposeAndPayAsService().
Test(t).
AssertSuccess()
// verify cleanup
if getVouchersCount(g, t) != 0 {
t.Fail()
}
}