-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter.test.ts
137 lines (122 loc) · 3.91 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
import { AdapterRequest } from '@chainlink/ea-bootstrap'
import { util } from '@chainlink/ea-bootstrap'
import { server as startServer } from '../../src'
import {
mockSuccessfulResponsesWithCommaSeparatedSources,
mockSuccessfulResponsesWithoutCommaSeparatedSources,
mockSuccessfulResponsesWithSingleSource,
} from './fixtures'
import { setupExternalAdapterTest } from '@chainlink/ea-test-helpers'
import type { SuiteContext } from '@chainlink/ea-test-helpers'
import { SuperTest, Test } from 'supertest'
const setupEnvironment = (adapters: string[]) => {
const env = {} as { [key: string]: string }
for (const a of adapters) {
env[`${a.toUpperCase()}_${util.ENV_ADAPTER_URL}`] = `https://adapters.main.stage.cldev.sh/${a}`
}
return env
}
describe('medianizer', () => {
const context: SuiteContext = {
req: null,
server: startServer,
}
const envVariables = setupEnvironment(['coingecko', 'coinpaprika', 'failing'])
setupExternalAdapterTest(envVariables, context)
describe('successful calls', () => {
const jobID = '1'
it('return success without comma separated sources', async () => {
mockSuccessfulResponsesWithoutCommaSeparatedSources()
const data: AdapterRequest = {
id: jobID,
data: {
sources: ['coingecko', 'coinpaprika'],
from: 'ETH',
to: 'USD',
},
}
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(response.body).toMatchSnapshot()
})
it('returns success with comma separated sources', async () => {
mockSuccessfulResponsesWithCommaSeparatedSources()
const data: AdapterRequest = {
id: jobID,
data: {
sources: 'coingecko,coinpaprika',
from: 'ETH',
to: 'USD',
},
}
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(response.body).toMatchSnapshot()
})
})
describe('erroring calls', () => {
const jobID = '1'
it('returns error if not reaching minAnswers', async () => {
mockSuccessfulResponsesWithSingleSource()
const data: AdapterRequest = {
id: jobID,
data: {
sources: 'coingecko',
from: 'ETH',
to: 'USD',
minAnswers: 2,
},
}
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(500)
expect(response.body).toMatchSnapshot()
})
})
describe('validation error', () => {
const jobID = '2'
it('returns a validation error if the request data is empty', async () => {
const data: AdapterRequest = { id: jobID, data: {} }
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(400)
expect(response.body).toMatchSnapshot()
})
it('returns a validation error if the request contains unsupported sources', async () => {
const data: AdapterRequest = {
id: jobID,
data: {
source: 'NOT_REAL',
from: 'ETH',
to: 'USD',
},
}
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(400)
expect(response.body).toMatchSnapshot()
})
})
})