-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathaccount_handlers_test.go
197 lines (187 loc) · 6.68 KB
/
account_handlers_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package api
import (
"context"
"fmt"
"testing"
"github.com/tonkeeper/opentonapi/pkg/chainstate"
pkgTesting "github.com/tonkeeper/opentonapi/pkg/testing"
"github.com/tonkeeper/tongo/liteapi"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"github.com/tonkeeper/opentonapi/pkg/addressbook"
"github.com/tonkeeper/opentonapi/pkg/config"
"github.com/tonkeeper/opentonapi/pkg/litestorage"
"github.com/tonkeeper/opentonapi/pkg/oas"
)
func TestHandler_GetRawAccount(t *testing.T) {
tests := []struct {
name string
params oas.GetBlockchainRawAccountParams
wantStatus string
wantAddress string
}{
{
params: oas.GetBlockchainRawAccountParams{AccountID: "EQDendoireMDFMufOUzkqNpFIay83GnjV2tgGMbA64wA3siV"},
wantAddress: "0:de9dda22ade30314cb9f394ce4a8da4521acbcdc69e3576b6018c6c0eb8c00de",
wantStatus: "active",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger, _ := zap.NewDevelopment()
cli, err := liteapi.NewClient(liteapi.FromEnvsOrMainnet())
require.Nil(t, err)
liteStorage, err := litestorage.NewLiteStorage(logger, cli)
require.Nil(t, err)
h, err := NewHandler(logger, WithStorage(liteStorage), WithExecutor(liteStorage))
require.Nil(t, err)
account, err := h.GetBlockchainRawAccount(context.Background(), tt.params)
require.Nil(t, err)
require.Equal(t, tt.wantAddress, account.Address)
require.Equal(t, tt.wantStatus, account.Status)
})
}
}
func TestHandler_GetAccount(t *testing.T) {
tests := []struct {
name string
params oas.GetAccountParams
wantStatus string
wantAddress string
}{
{
params: oas.GetAccountParams{AccountID: "EQDendoireMDFMufOUzkqNpFIay83GnjV2tgGMbA64wA3siV"},
wantAddress: "0:de9dda22ade30314cb9f394ce4a8da4521acbcdc69e3576b6018c6c0eb8c00de",
wantStatus: "active",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger, _ := zap.NewDevelopment()
cli, err := liteapi.NewClient(liteapi.FromEnvsOrMainnet())
require.Nil(t, err)
liteStorage, err := litestorage.NewLiteStorage(logger, cli)
require.Nil(t, err)
h, err := NewHandler(logger, WithStorage(liteStorage), WithExecutor(liteStorage))
require.Nil(t, err)
accountRes, err := h.GetAccount(context.Background(), tt.params)
require.Nil(t, err)
require.Equal(t, tt.wantAddress, accountRes.Address)
require.Equal(t, tt.wantStatus, accountRes.Status)
})
}
}
func TestHandler_GetAccounts(t *testing.T) {
tests := []struct {
name string
params oas.OptGetAccountsReq
wantStatuses map[string]string
wantNames map[string]string
wantBadRequestError string
}{
{
params: oas.OptGetAccountsReq{
Value: oas.GetAccountsReq{
AccountIds: []string{
"-1:3333333333333333333333333333333333333333333333333333333333333333",
"-1:5555555555555555555555555555555555555555555555555555555555555555",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf18",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf17",
},
},
},
wantStatuses: map[string]string{
"-1:3333333333333333333333333333333333333333333333333333333333333333": "active",
"-1:5555555555555555555555555555555555555555555555555555555555555555": "active",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf18": "active",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf17": "nonexist",
},
wantNames: map[string]string{
"-1:3333333333333333333333333333333333333333333333333333333333333333": "Elector Contract",
"-1:5555555555555555555555555555555555555555555555555555555555555555": "Config Contract",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf18": "Getgems Marketplace",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf17": "",
},
},
{
params: oas.OptGetAccountsReq{
Value: oas.GetAccountsReq{
AccountIds: []string{
"-1:3333333333333333333333333333333333333333333333333333333333333333",
"-1:5555555555555555555555555555555555555555555555555555555555555555",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf18",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf17",
"0:a3935861f79daf59a13d6d182e1640210c02f98e3df18fda74b8f5ab141abf16",
},
},
},
wantBadRequestError: "the maximum number of accounts to request at once: 4",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger, _ := zap.NewDevelopment()
cli, err := liteapi.NewClient(liteapi.FromEnvsOrMainnet())
require.Nil(t, err)
liteStorage, err := litestorage.NewLiteStorage(logger, cli)
require.Nil(t, err)
h := &Handler{
addressBook: addressbook.NewAddressBook(logger, config.AddressPath, config.JettonPath, config.CollectionPath),
storage: liteStorage,
state: chainstate.NewChainState(liteStorage),
limits: Limits{
BulkLimits: 4,
},
}
accountRes, err := h.GetAccounts(context.Background(), tt.params)
if len(tt.wantBadRequestError) > 0 {
badRequest, ok := err.(*oas.ErrorStatusCode)
require.True(t, ok)
require.Equal(t, tt.wantBadRequestError, badRequest.Response.Error)
return
}
require.Nil(t, err)
statuses := map[string]string{}
names := map[string]string{}
for _, account := range accountRes.Accounts {
statuses[account.Address] = account.Status
names[account.Address] = account.Name.Value
}
require.Equal(t, tt.wantStatuses, statuses)
require.Equal(t, tt.wantNames, names)
})
}
}
func TestHandler_GetTransactions(t *testing.T) {
tests := []struct {
name string
params oas.GetBlockchainBlockTransactionsParams
filenamePrefix string
}{
{
name: "masterchain block",
params: oas.GetBlockchainBlockTransactionsParams{BlockID: "(-1,8000000000000000,28741341)"},
filenamePrefix: "block-txs-1",
},
{
name: "basechain block",
params: oas.GetBlockchainBlockTransactionsParams{BlockID: "(0,8000000000000000,40834551)"},
filenamePrefix: "block-txs-2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger, _ := zap.NewDevelopment()
cli, err := liteapi.NewClient(liteapi.FromEnvsOrMainnet())
require.Nil(t, err)
liteStorage, err := litestorage.NewLiteStorage(logger, cli)
require.Nil(t, err)
h, err := NewHandler(logger, WithStorage(liteStorage), WithExecutor(liteStorage), WithAddressBook(&mockAddressBook{}))
require.Nil(t, err)
res, err := h.GetBlockchainBlockTransactions(context.Background(), tt.params)
require.Nil(t, err)
fmt.Printf("%v\n", res)
pkgTesting.CompareResults(t, res, tt.filenamePrefix)
})
}
}