-
Notifications
You must be signed in to change notification settings - Fork 5
/
endpoints.test.ts
155 lines (144 loc) · 3.79 KB
/
endpoints.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
/* eslint-disable import/extensions */
import fs from 'fs';
import Auth from '../src/auth';
import constants from '../src/constants';
import Endpoints from '../src/endpoints';
const options = {
sandbox: false,
client_id: 'Client_Id',
client_secret: 'Client_Secret',
certificate: 'Certificado_Pix',
authRoute: { route: '/oauth/token', method: 'post' },
baseUrl: 'https://api-pix.gerencianet.com.br',
};
const pixChargeCreated = {
calendario: {
criacao: '2020-09-09T20:15:00.358Z',
expiracao: 3600,
},
txid: '7978c0c97ea847e78e8849634473c1f1',
revisao: 0,
loc: {
id: 789,
location: 'pix.example.com/qr/v2/9d36b84fc70b478fb95c12729b90ca25',
tipoCob: 'cob',
},
location: 'pix.example.com/qr/v2/9d36b84fc70b478fb95c12729b90ca25',
status: 'ATIVA',
devedor: {
cnpj: '12345678000195',
nome: 'Empresa de Serviços SA',
},
valor: {
original: '567.89',
},
chave: 'a1f4102e-a446-4a57-bcce-6fa48899c1d1',
solicitacaoPagador: 'Informe o número ou identificador do pedido.',
};
jest.spyOn(Endpoints.prototype, 'req')
.mockResolvedValueOnce(pixChargeCreated)
.mockImplementationOnce(async () => {
return new Error('FALHA AO LER O CERTIFICADO');
})
.mockResolvedValueOnce('');
jest.spyOn(Auth.prototype, 'getAccessToken').mockImplementation(() => {
return Promise.resolve({
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
token_type: 'Bearer',
expires_in: 3600,
scope: 'cob.read',
});
});
jest.mock('fs');
const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readFileSync.mockReturnValueOnce('');
// eslint-disable-next-line prettier/prettier
jest.mock('axios', () =>jest.fn()
.mockResolvedValueOnce({
status: 200,
data: {
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
token_type: 'Bearer',
expires_in: 3600,
scope: 'cob.read',
},
})
.mockResolvedValueOnce({
status: 200,
data: {
access_token: '1723ad73',
refresh_token: '36accb15',
expires_in: 600,
expire_at: '1656012603684',
token_type: 'Bearer',
},
}),
);
describe('Endpoints Tests', () => {
const pixEndpoint = {
name: 'pixCreateCharge',
params: { txid: 'dt9BHlyzrb5jrFNAdfEDVpHgiOmDbVq111' },
body: {
calendario: {
expiracao: 3600,
},
valor: {
original: '0.01',
},
chave: 'CHAVEPIX',
},
};
it('TEST 0: Shoud get Access Token', async () => {
const endpoints = new Endpoints(options, constants);
const res = await endpoints.getAccessToken();
expect(res).toBe('RfSfS9AJkLu7jPjOp2IbrI');
});
it.each([
{
description: 'should create a charge',
body: pixEndpoint,
expected: pixChargeCreated,
},
{
description: 'should throw "FALHA AO LER O CERTIFICADO"',
body: pixEndpoint,
expected: new Error('FALHA AO LER O CERTIFICADO'),
},
])('TEST $# : $description', async ({ body, expected }) => {
const endpoints = new Endpoints(options, constants);
const res = await endpoints.run(body.name, body.params, body.body);
expect(res).toStrictEqual(expected);
});
it.each([
{
description: 'shoud get the request params [createRequest][PIX]',
name: 'listAccountConfig',
route: '/v2/gn/config',
params: [],
expected: {
method: 'get',
url: 'https://api-pix.gerencianet.com.br/v2/gn/config',
headers: expect.anything(),
data: expect.anything(),
httpsAgent: expect.anything(),
},
},
{
description: 'shoud get the request params [listPlans][SUBSCRIPTION]',
name: 'listPlans',
route: '/plans',
params: [],
expected: {
method: 'get',
url: 'https://api.gerencianet.com.br/v1/plans',
headers: expect.anything(),
data: [],
},
},
])('TEST $# : $description', async ({ name, route, params, expected }) => {
const endpoints = new Endpoints(options, constants);
await endpoints.run(name, params, []);
const res = await endpoints.createRequest(route);
expect(res).toStrictEqual(expected);
});
});