This repository has been archived by the owner on May 2, 2024. It is now read-only.
forked from adshmh/meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
355 lines (318 loc) · 9.99 KB
/
e2e_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/gojektech/heimdall/httpclient"
"github.com/pokt-foundation/portal-http-db/v2/types"
"github.com/pokt-foundation/relay-meter/api"
timeUtils "github.com/pokt-foundation/utils-go/time"
"github.com/stretchr/testify/suite"
)
/* To run the E2E suite use the command `make test_e2e` from the repository root.
The E2E suite also runs on all Pull Requests to the main or staging branches.
The End-to-End test suite uses a Dockerized reproduction of Relay Meter (Collector & API Server)
and all containers it depends on (Relay Meter Postgres DB, PHD & PHD Postgres DB).
The test verifies this data by verifying it can be accessed from the API server's endpoints. */
// Sets up the suite and runs all the tests.
// TODO: update e2e test to include the new relay-collection logic
func Test_RunSuite_E2E(t *testing.T) {
if testing.Short() {
t.Skip("skipping end to end test")
}
testSuite := new(RelayMeterTestSuite)
suite.Run(t, testSuite)
}
func (ts *RelayMeterTestSuite) Test_RunTests() {
ts.Run("Test_RelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
err error
}{
{
name: "Test return value of /relays endpoint",
date: ts.startOfDay,
err: nil,
},
}
for _, test := range tests {
allRelays, err := get[api.TotalRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays",
id: "",
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
ts.NotEmpty(allRelays.Count.Success)
ts.NotEmpty(allRelays.Count.Failure)
ts.Equal(test.date, allRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), allRelays.To)
}
})
ts.Run("Test_AllAppRelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
err error
}{
{
name: "Test return value of /relays/apps endpoint",
date: ts.startOfDay,
err: nil,
},
}
for _, test := range tests {
allAppsRelays, err := get[[]api.AppRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays/apps",
id: "",
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
for _, appRelays := range allAppsRelays {
ts.Len(appRelays.PublicKey, 37) // Test pub keys have 37 instead of 64 characters
ts.NotEmpty(appRelays.Count.Success)
ts.NotEmpty(appRelays.Count.Failure)
ts.Equal(test.date, appRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), appRelays.To)
}
}
})
ts.Run("Test_AppRelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
appPubKey types.PortalAppPublicKey
err error
}{
{
name: "Test return value of /relays/apps/{APP_PUB_KEY} endpoint",
date: ts.startOfDay,
appPubKey: testRelay.ApplicationPublicKey,
err: nil,
},
}
for _, test := range tests {
appRelays, err := get[api.AppRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays/apps",
id: string(test.appPubKey),
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
ts.Len(appRelays.PublicKey, 37) // Test pub keys have 37 instead of 64 characters
ts.Equal(testRelay.ApplicationPublicKey, appRelays.PublicKey)
ts.NotEmpty(appRelays.Count.Success)
ts.NotEmpty(appRelays.Count.Failure)
ts.Equal(test.date, appRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), appRelays.To)
}
})
ts.Run("Test_UserRelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
userID types.UserID
err error
}{
{
name: "Test return value of /relays/users/{USER_ID} endpoint",
date: ts.startOfDay,
userID: testUserID,
err: nil,
},
}
for _, test := range tests {
userRelays, err := get[api.UserRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays/users",
id: string(test.userID),
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
ts.Len(userRelays.User, 6)
ts.Equal(testUserID, userRelays.User)
ts.Len(userRelays.PublicKeys, 1)
ts.Len(userRelays.PublicKeys[0], 37) // Test pub keys have 37 instead of 64 characters
ts.NotEmpty(userRelays.Count.Success)
ts.NotEmpty(userRelays.Count.Failure)
ts.Equal(test.date, userRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), userRelays.To)
}
})
ts.Run("Test_AllPortalAppRelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
emptyRelaysApp types.PortalAppID
err error
}{
{
name: "Test return value of /relays/endpoints endpoint",
date: ts.startOfDay,
emptyRelaysApp: "test_app_3", // test app 3 has no mock relays
err: nil,
},
}
for _, test := range tests {
allEndpointsRelays, err := get[[]api.PortalAppRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays/endpoints",
id: "",
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
for _, endpointRelays := range allEndpointsRelays {
if endpointRelays.PortalAppID == test.emptyRelaysApp {
continue
}
ts.NotEmpty(endpointRelays.PortalAppID)
ts.Len(endpointRelays.PublicKeys, 1)
ts.Len(endpointRelays.PublicKeys[0], 37) // Test pub keys have 37 instead of 64 characters
ts.NotEmpty(endpointRelays.Count.Success)
ts.NotEmpty(endpointRelays.Count.Failure)
ts.Equal(test.date, endpointRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), endpointRelays.To)
}
}
})
ts.Run("Test_PortalAppRelaysEndpoint", func() {
tests := []struct {
name string
date time.Time
portalAppID types.PortalAppID
err error
}{
{
name: "Test return value of /relays/endpoints/{ENDPOINT_ID} endpoint",
date: ts.startOfDay,
portalAppID: "test_app_1",
err: nil,
},
}
for _, test := range tests {
endpointRelays, err := get[api.PortalAppRelaysResponse](
getOptions{
baseURL: relayMeterBaseURL,
apiKey: relayMeterAPIKey,
path: "v1/relays/endpoints",
id: string(test.portalAppID),
params: ts.dateParams,
httpClient: ts.httpClient,
})
ts.Equal(test.err, err)
ts.Len(endpointRelays.PortalAppID, 10)
ts.Len(endpointRelays.PublicKeys, 1)
ts.Len(endpointRelays.PublicKeys[0], 37) // Test pub keys have 37 instead of 64 characters
ts.NotEmpty(endpointRelays.Count.Success)
ts.NotEmpty(endpointRelays.Count.Failure)
ts.Equal(test.date, endpointRelays.From)
ts.Equal(test.date.AddDate(0, 0, 1), endpointRelays.To)
}
})
}
/* ---------- Relay Meter Test Suite ---------- */
const (
relayMeterBaseURL = "http://localhost:9898"
relayMeterAPIKey = "test_api_key_1234"
testUserID = types.UserID("user_1")
)
var (
ErrResponseNotOK = errors.New("Response not OK")
testRelay = TestRelay{
ApplicationPublicKey: "test_34715cae753e67c75fbb340442e7de8e",
NodePublicKey: "test_node_pub_key_02fbcfbad0777942c1da5425bf0105546e7e7f53fffad9",
Method: "eth_chainId",
Blockchain: "49",
BlockchainSubdomain: "fantom-mainnet",
Origin: "https://app.test1.io",
ElapsedTime: 0.16475,
}
)
type (
RelayMeterTestSuite struct {
suite.Suite
httpClient *httpclient.Client
startOfDay, endOfDay time.Time
dateParams string
}
TestRelay struct {
ApplicationPublicKey types.PortalAppPublicKey `json:"applicationPublicKey"`
NodePublicKey types.PortalAppPublicKey `json:"nodePublicKey"`
Method string `json:"method"`
Blockchain string `json:"blockchain"`
BlockchainSubdomain string `json:"blockchainSubdomain"`
Origin types.PortalAppOrigin `json:"origin"`
ElapsedTime float64 `json:"elapsedTime"`
}
getOptions struct {
baseURL, path, apiKey, id, params string
httpClient *httpclient.Client
}
)
// SetupSuite runs before each test suite run - takes just over 1 minute to complete
func (ts *RelayMeterTestSuite) SetupSuite() {
ts.configureTimePeriod() // Configure time period for test
ts.httpClient = httpclient.NewClient( // HTTP client to test API Server and populate PHD DB
httpclient.WithHTTPTimeout(10*time.Second), httpclient.WithRetryCount(2),
)
<-time.After(1 * time.Second)
}
// Sets the time period vars for the test (00:00.000 to 23:59:59.999 UTC of current day)
func (ts *RelayMeterTestSuite) configureTimePeriod() {
ts.startOfDay = timeUtils.StartOfDay(time.Now().AddDate(0, 0, -1).UTC())
ts.endOfDay = ts.startOfDay.AddDate(0, 0, 1).Add(-time.Millisecond)
ts.dateParams = fmt.Sprintf("?from=%s&to=%s", ts.startOfDay.Format(time.RFC3339), ts.endOfDay.Format(time.RFC3339))
}
// GET test util func
func get[T any](options getOptions) (T, error) {
rawURL := fmt.Sprintf("%s/%s", options.baseURL, options.path)
if options.id != "" {
rawURL = fmt.Sprintf("%s/%s", rawURL, options.id)
}
if options.params != "" {
rawURL = fmt.Sprintf("%s%s", rawURL, options.params)
}
headers := http.Header{}
if options.apiKey != "" {
headers["Authorization"] = []string{options.apiKey}
}
var data T
response, err := options.httpClient.Get(rawURL, headers)
if err != nil {
return data, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return data, fmt.Errorf("%w. %s", ErrResponseNotOK, http.StatusText(response.StatusCode))
}
body, err := io.ReadAll(response.Body)
if err != nil {
return data, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return data, err
}
return data, nil
}