forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccounts_test.go
176 lines (142 loc) · 5.17 KB
/
accounts_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
package rest
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
mocktestify "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/access/mock"
"github.com/onflow/flow-go/engine/access/rest/middleware"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)
const expandableFieldKeys = "keys"
const expandableFieldContracts = "contracts"
func accountURL(t *testing.T, address string, height string) string {
u, err := url.ParseRequestURI(fmt.Sprintf("/v1/accounts/%s", address))
require.NoError(t, err)
q := u.Query()
if height != "" {
q.Add("block_height", height)
}
u.RawQuery = q.Encode()
return u.String()
}
func TestGetAccount(t *testing.T) {
backend := &mock.API{}
t.Run("get by address at latest sealed block", func(t *testing.T) {
account := accountFixture(t)
var height uint64 = 100
block := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(height))
req := getAccountRequest(t, account, sealedHeightQueryParam, expandableFieldKeys, expandableFieldContracts)
backend.Mock.
On("GetLatestBlockHeader", mocktestify.Anything, true).
Return(block, flow.BlockStatusSealed, nil)
backend.Mock.
On("GetAccountAtBlockHeight", mocktestify.Anything, account.Address, height).
Return(account, nil)
expected := expectedExpandedResponse(account)
assertOKResponse(t, req, expected, backend)
mocktestify.AssertExpectationsForObjects(t, backend)
})
t.Run("get by address at latest finalized block", func(t *testing.T) {
var height uint64 = 100
block := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(height))
account := accountFixture(t)
req := getAccountRequest(t, account, finalHeightQueryParam, expandableFieldKeys, expandableFieldContracts)
backend.Mock.
On("GetLatestBlockHeader", mocktestify.Anything, false).
Return(block, flow.BlockStatusFinalized, nil)
backend.Mock.
On("GetAccountAtBlockHeight", mocktestify.Anything, account.Address, height).
Return(account, nil)
expected := expectedExpandedResponse(account)
assertOKResponse(t, req, expected, backend)
mocktestify.AssertExpectationsForObjects(t, backend)
})
t.Run("get by address at height", func(t *testing.T) {
var height uint64 = 1337
account := accountFixture(t)
req := getAccountRequest(t, account, fmt.Sprintf("%d", height), expandableFieldKeys, expandableFieldContracts)
backend.Mock.
On("GetAccountAtBlockHeight", mocktestify.Anything, account.Address, height).
Return(account, nil)
expected := expectedExpandedResponse(account)
assertOKResponse(t, req, expected, backend)
mocktestify.AssertExpectationsForObjects(t, backend)
})
t.Run("get by address at height condensed", func(t *testing.T) {
var height uint64 = 1337
account := accountFixture(t)
req := getAccountRequest(t, account, fmt.Sprintf("%d", height))
backend.Mock.
On("GetAccountAtBlockHeight", mocktestify.Anything, account.Address, height).
Return(account, nil)
expected := expectedCondensedResponse(account)
assertOKResponse(t, req, expected, backend)
mocktestify.AssertExpectationsForObjects(t, backend)
})
t.Run("get invalid", func(t *testing.T) {
tests := []struct {
url string
out string
}{
{accountURL(t, "123", ""), `{"code":400, "message":"invalid address"}`},
{accountURL(t, unittest.AddressFixture().String(), "foo"), `{"code":400, "message":"invalid height format"}`},
}
for i, test := range tests {
req, _ := http.NewRequest("GET", test.url, nil)
rr, err := executeRequest(req, backend)
assert.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, rr.Code)
assert.JSONEq(t, test.out, rr.Body.String(), fmt.Sprintf("test #%d failed: %v", i, test))
}
})
}
func expectedExpandedResponse(account *flow.Account) string {
return fmt.Sprintf(`{
"address":"%s",
"balance":"100",
"keys":[
{
"index":"0",
"public_key":"%s",
"signing_algorithm":"ECDSA_P256",
"hashing_algorithm":"SHA3_256",
"sequence_number":"0",
"weight":"1000",
"revoked":false
}
],
"_links":{"_self":"/v1/accounts/%s" },
"_expandable": {},
"contracts": {"contract1":"Y29udHJhY3Qx", "contract2":"Y29udHJhY3Qy"}
}`, account.Address, account.Keys[0].PublicKey.String(), account.Address)
}
func expectedCondensedResponse(account *flow.Account) string {
return fmt.Sprintf(`{
"address":"%s",
"balance":"100",
"_links":{"_self":"/v1/accounts/%s" },
"_expandable":{"contracts":"contracts", "keys":"keys"}
}`, account.Address, account.Address)
}
func getAccountRequest(t *testing.T, account *flow.Account, height string, expandFields ...string) *http.Request {
req, err := http.NewRequest("GET", accountURL(t, account.Address.String(), height), nil)
if len(expandFields) > 0 {
fieldParam := strings.Join(expandFields, ",")
q := req.URL.Query()
q.Add(middleware.ExpandQueryParam, fieldParam)
req.URL.RawQuery = q.Encode()
}
require.NoError(t, err)
return req
}
func accountFixture(t *testing.T) *flow.Account {
account, err := unittest.AccountFixture()
require.NoError(t, err)
return account
}