{{ message }}
Repository navigation
Expand file tree
/
Copy pathcambio-v1.test.js
More file actions
289 lines (248 loc) · 9.65 KB
/
Copy pathcambio-v1.test.js
File metadata and controls
289 lines (248 loc) · 9.65 KB
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
import axios from 'axios';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { describe, expect, test } from 'vitest';
dayjs.extend(utc);
dayjs.extend(timezone);
const formatDate = (date, format = 'DD/MM/YYYY') => dayjs(date).format(format);
describe('Cambio v1 (E2E)', () => {
describe('GET /cambio/v1/cotacao/:moeda/:data', () => {
test('Verifica CORS', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-06-27`;
const response = await axios.get(requestUrl);
expect(response.headers['access-control-allow-origin']).toBe('*');
});
test('Utilizando moeda e data válida: USD e 2023-06-27', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-06-27`;
const response = await axios.get(requestUrl);
expect(response.status).toBe(200);
expect(response.data).toEqual({
cotacoes: [
{
paridade_compra: 1,
paridade_venda: 1,
cotacao_compra: 4.7604,
cotacao_venda: 4.761,
data_hora_cotacao: '2023-06-27 10:06:16.012',
tipo_boletim: 'ABERTURA',
},
{
paridade_compra: 1,
paridade_venda: 1,
cotacao_compra: 4.7925,
cotacao_venda: 4.7931,
data_hora_cotacao: '2023-06-27 11:05:16.603',
tipo_boletim: 'INTERMEDIÁRIO',
},
{
paridade_compra: 1,
paridade_venda: 1,
cotacao_compra: 4.8006,
cotacao_venda: 4.8012,
data_hora_cotacao: '2023-06-27 12:03:17.018',
tipo_boletim: 'INTERMEDIÁRIO',
},
{
paridade_compra: 1,
paridade_venda: 1,
cotacao_compra: 4.8054,
cotacao_venda: 4.806,
data_hora_cotacao: '2023-06-27 13:05:03.701',
tipo_boletim: 'INTERMEDIÁRIO',
},
{
paridade_compra: 1,
paridade_venda: 1,
cotacao_compra: 4.7897,
cotacao_venda: 4.7903,
data_hora_cotacao: '2023-06-27 13:05:03.712',
tipo_boletim: 'FECHAMENTO PTAX',
},
],
moeda: 'USD',
data: '2023-06-27',
});
});
test('Utilizando moeda inexistente e data válida: ZZZ e 2023-06-27', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/ZZZ/2023-06-27`;
try {
await axios.get(requestUrl);
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message:
'Tipo de moeda inválida, os tipos disponíveis são: AUD, CAD, CHF, DKK, EUR, GBP, JPY, NOK, SEK, USD',
type: 'currency_error',
name: 'INVALID_CURRENCY_VALUE',
});
}
});
test('Utilizando moeda válida e data com formato inapropriado: USD e 27-06-2023', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/27-06-2023`;
try {
await axios.get(requestUrl);
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message: 'Formato de data inválida, utilize: YYYY-MM-DD',
type: 'format_error',
name: 'DATE_FORMAT_INCORRECT_PATTERN',
});
}
});
test('Utilizando moeda válida e utilizando um dia não útil (fim de semana): USD e 2025-02-16', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2025-02-16`;
const response = await axios.get(requestUrl);
expect(response.status).toBe(200);
expect(response.data.data).toBe('2025-02-14');
expect(response.data.moeda).toBe('USD');
expect(Array.isArray(response.data.cotacoes)).toBe(true);
});
test('Tentando utilizar uma data futura ao dia da consulta: USD', async () => {
const today = new Date();
const fiveYearsLater = dayjs(today).add(5, 'year').format('YYYY-MM-DD');
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/${fiveYearsLater}`;
try {
await axios.get(requestUrl);
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message: 'Datas futuras não são permitidas',
type: 'future_date_error',
name: 'NO_FUTURE_DATE',
});
}
});
test('Datas anteriores ao início da base de dados (1984-11-28): USD e 1984-05-28', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/1984-05-28`;
try {
await axios.get(requestUrl);
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message: 'Dados a partir do dia 1984-11-28',
type: 'data_available_error',
name: 'NO_DATA_AVAILABLE',
});
}
});
test('Não deve permitir acesso a cotação de hoje', async () => {
const today = formatDate(new Date(), 'YYYY-MM-DD');
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/${today}`;
try {
await axios.get(requestUrl);
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message:
'Não é possível consultar a cotação do dia de hoje devido a política de cache',
type: 'today_date_error',
name: 'NO_TODAY_DATE',
});
}
});
test('Não deve aceitar mês 00 (zero): USD e 2023-00-01', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-00-01`;
try {
await axios.get(requestUrl);
expect.fail('Deveria ter lançado um erro');
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message: 'Mês inválido: deve estar entre 01 e 12',
type: 'format_error',
name: 'INVALID_MONTH_VALUE',
});
}
});
test('Não deve aceitar mês 13 ou superior: USD e 2023-13-01', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-13-01`;
try {
await axios.get(requestUrl);
expect.fail('Deveria ter lançado um erro');
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data).toMatchObject({
message: 'Mês inválido: deve estar entre 01 e 12',
type: 'format_error',
name: 'INVALID_MONTH_VALUE',
});
}
});
test('Não deve aceitar dia 00 (zero): USD e 2023-06-00', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-06-00`;
try {
await axios.get(requestUrl);
expect.fail('Deveria ter lançado um erro');
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data.type).toBe('format_error');
expect(response.data.name).toBe('INVALID_DAY_VALUE');
}
});
test('Não deve aceitar dia superior ao máximo do mês: USD e 2023-06-31', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-06-31`;
try {
await axios.get(requestUrl);
expect.fail('Deveria ter lançado um erro');
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data.type).toBe('format_error');
expect(response.data.name).toBe('INVALID_DAY_VALUE');
}
});
test('Não deve aceitar dia 30 em fevereiro: USD e 2023-02-30', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2023-02-30`;
try {
await axios.get(requestUrl);
expect.fail('Deveria ter lançado um erro');
} catch (error) {
const { response } = error;
expect(response.status).toBe(400);
expect(response.data.type).toBe('format_error');
expect(response.data.name).toBe('INVALID_DAY_VALUE');
}
});
test('Deve aceitar 29 de fevereiro em ano bissexto: USD e 2024-02-29', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/cotacao/USD/2024-02-29`;
const response = await axios.get(requestUrl);
expect(response.status).toBe(200);
expect(response.data.moeda).toBe('USD');
// A data retornada pode ser diferente devido à lógica de dias úteis
expect(response.data.data).toBeDefined();
expect(Array.isArray(response.data.cotacoes)).toBe(true);
});
});
describe('GET /cambio/v1/moedas', () => {
test('Verifica CORS', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/moedas`;
const response = await axios.get(requestUrl);
expect(response.headers['access-control-allow-origin']).toBe('*');
});
test('Lista moedas', async () => {
const requestUrl = `${global.SERVER_URL}/api/cambio/v1/moedas`;
const response = await axios.get(requestUrl);
expect(response.status).toBe(200);
expect(Array.isArray(response.data)).toBe(true);
expect(response.data).toEqual(
expect.arrayContaining([
expect.objectContaining({
simbolo: expect.any(String),
nome: expect.any(String),
tipo_moeda: expect.any(String),
}),
])
);
});
});
});
You can’t perform that action at this time.
