-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter.test.ts
226 lines (212 loc) · 5.84 KB
/
adapter.test.ts
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
import { assertError } from '@chainlink/ea-test-helpers'
import { AdapterError, AdapterRequest, Requester } from '@chainlink/ea-bootstrap'
import * as circuitbreakerAllocationAdapter from '../../src/index'
import { dataProviderConfig, mockDataProviderResponses } from './fixtures'
import nock from 'nock'
import { TInputParameters } from '../../src/endpoint'
let oldEnv: NodeJS.ProcessEnv
describe('execute', () => {
const execute = circuitbreakerAllocationAdapter.makeExecute()
beforeAll(async () => {
if (process.env.RECORD) {
nock.recorder.rec()
}
})
afterAll(() => {
process.env = oldEnv
if (process.env.RECORD) {
nock.recorder.play()
}
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
})
describe('valid result method', () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.CACHE_ENABLED = 'false'
for (const source of Object.keys(dataProviderConfig)) {
const { providerUrlEnvVar, providerUrl } = dataProviderConfig[source]
process.env[providerUrlEnvVar] = providerUrl
}
mockDataProviderResponses()
const jobID = '1'
const request = [
{
name: 'should return correct result value with the second source if the first one fail by conection refuse',
input: {
id: jobID,
data: {
primarySource: 'none',
secondarySource: 'coinmarketcap',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'should return correct result value with the second source if the first one fail by 500 error',
input: {
id: jobID,
data: {
primarySource: 'coinpaprika',
secondarySource: 'coinmarketcap',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'should return correct result values',
input: {
id: jobID,
data: {
primarySource: 'coingecko',
secondarySource: 'coinmarketcap',
from: 'ETH',
to: 'USD',
},
},
},
]
request.forEach((req) => {
it(`${req.name}`, async () => {
const resp = await execute(req.input, {})
expect(resp).toMatchSnapshot()
})
})
})
describe('error result method', () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.CACHE_ENABLED = 'false'
for (const source of Object.keys(dataProviderConfig)) {
const { providerUrlEnvVar, providerUrl } = dataProviderConfig[source]
process.env[providerUrlEnvVar] = providerUrl
}
mockDataProviderResponses()
const jobID = '1'
const request = [
{
name: 'should return an error if the 2 sources return connection refuse error',
input: {
id: jobID,
data: {
primarySource: 'coinpaprika',
secondarySource: 'coinpaprika',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'should return an error if the second source return an error',
input: {
id: jobID,
data: {
primarySource: 'wootrade',
secondarySource: 'coinpaprika',
from: 'ETH',
to: 'USD',
},
},
},
]
const expectedProviderStatusCodes = 0
request.forEach((req) => {
it(`${req.name}`, async () => {
try {
await execute(req.input, {})
} catch (error) {
const errorResp = Requester.errored(jobID, error as AdapterError)
if (errorResp && errorResp.error && errorResp.error.message === '') {
errorResp.error.message = errorResp.error.name
}
assertError(
{ expected: expectedProviderStatusCodes, actual: errorResp.providerStatusCode },
errorResp,
jobID,
)
}
})
})
})
describe('validation error', () => {
const jobID = '1'
const requests = [
{
name: 'empty data',
input: { id: jobID, data: {} },
},
{
name: 'empty body',
input: {},
},
{
name: 'primary source attribute not supplied',
input: {
id: jobID,
data: {
firstSource: 'coinmarketcap',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'unsupported primary source',
input: {
id: jobID,
data: {
primarySource: 'none',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'unsupported secondary source',
input: {
id: jobID,
data: {
secondarySource: 'none',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'wrong secondary source name attribute',
input: {
id: jobID,
data: {
secondSource: 'none',
from: 'ETH',
to: 'USD',
},
},
},
{
name: 'empty first and secondary source',
input: {
id: jobID,
data: {
from: 'ETH',
to: 'USD',
},
},
},
{ name: 'allocations not supplied', input: { id: jobID, data: {} } },
{ name: 'base not supplied', input: { id: jobID, data: { quote: 'ARS' } } },
{ name: 'quote not supplied', input: { id: jobID, data: { base: 'BTC' } } },
]
requests.forEach((req) => {
it(`${req.name}`, async () => {
try {
await execute(req.input as unknown as AdapterRequest<TInputParameters>, {})
} catch (error) {
const errorResp = Requester.errored(jobID, error as AdapterError)
assertError({ expected: 400, actual: errorResp.statusCode }, errorResp, jobID)
}
})
})
})
})