-
Notifications
You must be signed in to change notification settings - Fork 3
/
faucet_test.go
162 lines (129 loc) · 3.41 KB
/
faucet_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
160
161
162
package faucet
import (
"net/http"
"testing"
"github.com/gnolang/faucet/config"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFaucet_NewFaucet(t *testing.T) {
t.Parallel()
t.Run("valid config", func(t *testing.T) {
t.Parallel()
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(config.DefaultConfig()),
)
assert.NotNil(t, f)
assert.NoError(t, err)
})
t.Run("invalid config", func(t *testing.T) {
t.Parallel()
// Create an invalid configuration
invalidCfg := config.DefaultConfig()
invalidCfg.NumAccounts = 0
// Create a faucet instance with the invalid configuration
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(invalidCfg),
)
// Make sure the error was caught
assert.Nil(t, f)
assert.ErrorIs(t, err, config.ErrInvalidNumAccounts)
})
t.Run("with CORS config", func(t *testing.T) {
t.Parallel()
// Example CORS config
corsConfig := config.DefaultCORSConfig()
corsConfig.AllowedOrigins = []string{"gno.land"}
validCfg := config.DefaultConfig()
validCfg.CORSConfig = corsConfig
// Create a valid faucet instance
// with a valid CORS configuration
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(validCfg),
)
assert.NotNil(t, f)
assert.NoError(t, err)
})
t.Run("with handlers", func(t *testing.T) {
t.Parallel()
handlers := []Handler{
{
Pattern: "/hello",
HandlerFunc: func(_ http.ResponseWriter, _ *http.Request) {
// Empty handler
},
},
}
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(config.DefaultConfig()),
WithHandlers(handlers),
)
require.NotNil(t, f)
assert.NoError(t, err)
// Make sure the handler was set
routes := f.mux.Routes()
require.Len(t, routes, len(handlers)+2) // base "/" & "/health" handlers as well
assert.Equal(t, handlers[0].Pattern, routes[2].Pattern)
})
t.Run("with logger", func(t *testing.T) {
t.Parallel()
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(config.DefaultConfig()),
WithLogger(noopLogger),
)
assert.NotNil(t, f)
assert.NoError(t, err)
})
t.Run("with prepare transaction message callback", func(t *testing.T) {
t.Parallel()
var (
cfg = PrepareCfg{
SendAmount: std.NewCoins(std.NewCoin("ugnot", 10)),
FromAddress: crypto.Address{1},
ToAddress: crypto.Address{2},
}
pkgPath = "gno.land/r/demo/example"
pkgFunc = "FundPlayer"
prepareTxMsgFn = func(cfg PrepareCfg) std.Msg {
return vm.MsgCall{
Caller: cfg.FromAddress,
PkgPath: pkgPath,
Func: pkgFunc,
Args: []string{cfg.ToAddress.String()},
Send: cfg.SendAmount,
}
}
)
f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(config.DefaultConfig()),
WithPrepareTxMessageFn(prepareTxMsgFn),
)
require.NotNil(t, f)
require.NoError(t, err)
// Prepare the message
msg := f.prepareTxMsgFn(cfg)
// Validate the message
msgCall, ok := msg.(vm.MsgCall)
require.True(t, ok)
assert.Equal(t, cfg.FromAddress, msgCall.Caller)
assert.Equal(t, pkgPath, msgCall.PkgPath)
assert.Equal(t, pkgFunc, msgCall.Func)
assert.Equal(t, []string{cfg.ToAddress.String()}, msgCall.Args)
assert.Equal(t, cfg.SendAmount, msgCall.Send)
})
}