Skip to content

Commit 0367907

Browse files
committed
Testes unitários
1 parent ecc711f commit 0367907

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

Diff for: test/auth.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const axios = require('axios')
2+
const fs = require('fs')
3+
const Auth = require('../lib/auth')
4+
const constants = require('../lib/constants')
5+
6+
jest.mock('fs')
7+
fs.readFileSync.mockReturnValueOnce('')
8+
9+
const accessTokenPix = {
10+
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
11+
token_type: 'Bearer',
12+
expires_in: 3600,
13+
scope: 'cob.read',
14+
}
15+
const accessTokenBoletos = {
16+
access_token: '1723ad73',
17+
refresh_token: '36accb15',
18+
expires_in: 600,
19+
expire_at: '1656012603684',
20+
token_type: 'Bearer',
21+
}
22+
23+
jest.mock('axios', () => jest.fn().mockResolvedValueOnce({ status: 200, data: accessTokenPix }).mockResolvedValueOnce({ status: 200, data: accessTokenBoletos }))
24+
25+
describe('Auth Tests', () => {
26+
it.each([
27+
{
28+
description: 'Should get Access_Token with pix certificate',
29+
options: {
30+
sandbox: false,
31+
client_id: 'Client_Id',
32+
client_secret: 'Client_Secret',
33+
certificate: 'Certificado_Pix',
34+
authRoute: { route: '/oauth/token', method: 'post' },
35+
baseUrl: 'https://api-pix.gerencianet.com.br',
36+
},
37+
expected: { access_token: expect.any(String), token_type: 'Bearer', expires_in: 3600, scope: 'cob.read' },
38+
},
39+
{
40+
description: 'Should get Access_Token without pix certificate [API EMISSÕES]',
41+
options: {
42+
sandbox: false,
43+
client_id: 'Client_Id',
44+
client_secret: 'Client_Secret',
45+
authRoute: { route: '/oauth/token', method: 'post' },
46+
baseUrl: 'https://api.gerencianet.com.br/v1',
47+
},
48+
expected: { access_token: '1723ad73', refresh_token: '36accb15', expires_in: 600, expire_at: '1656012603684', token_type: 'Bearer' },
49+
},
50+
])('TEST $# : $description', async ({ options, expected }) => {
51+
const auth = new Auth(options, constants)
52+
let res = await auth.getAccessToken()
53+
expect(res).toMatchObject(expected)
54+
expect(axios).toHaveBeenCalled()
55+
})
56+
})

Diff for: test/endpoints.test.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
const Auth = require('../lib/auth')
2+
const constants = require('../lib/constants')
3+
const Endpoints = require('../lib/endpoints')
4+
const fs = require('fs')
5+
6+
let options = {
7+
sandbox: false,
8+
client_id: 'Client_Id',
9+
client_secret: 'Client_Secret',
10+
certificate: 'Certificado_Pix',
11+
authRoute: { route: '/oauth/token', method: 'post' },
12+
baseUrl: 'https://api-pix.gerencianet.com.br',
13+
}
14+
15+
jest.spyOn(Endpoints.prototype, 'req').mockImplementationOnce(() => {
16+
return Promise.resolve(pixChargeCreated)
17+
}).mockReturnValueOnce(new Error('FALHA AO LER O CERTIFICADO')).mockReturnValueOnce('')
18+
19+
jest.spyOn(Auth.prototype, 'getAccessToken').mockImplementation(() => {
20+
return Promise.resolve({
21+
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
22+
token_type: 'Bearer',
23+
expires_in: 3600,
24+
scope: 'cob.read',
25+
})
26+
})
27+
28+
jest.mock('fs')
29+
fs.readFileSync.mockReturnValueOnce('')
30+
31+
32+
33+
let pixChargeCreated = {
34+
calendario: {
35+
criacao: '2020-09-09T20:15:00.358Z',
36+
expiracao: 3600,
37+
},
38+
txid: '7978c0c97ea847e78e8849634473c1f1',
39+
revisao: 0,
40+
loc: {
41+
id: 789,
42+
location: 'pix.example.com/qr/v2/9d36b84fc70b478fb95c12729b90ca25',
43+
tipoCob: 'cob',
44+
},
45+
location: 'pix.example.com/qr/v2/9d36b84fc70b478fb95c12729b90ca25',
46+
status: 'ATIVA',
47+
devedor: {
48+
cnpj: '12345678000195',
49+
nome: 'Empresa de Serviços SA',
50+
},
51+
valor: {
52+
original: '567.89',
53+
},
54+
chave: 'a1f4102e-a446-4a57-bcce-6fa48899c1d1',
55+
solicitacaoPagador: 'Informe o número ou identificador do pedido.',
56+
}
57+
58+
describe('Endpoints Tests', () => {
59+
let pixEndpoint = {
60+
name: 'pixCreateCharge',
61+
params: { txid: 'dt9BHlyzrb5jrFNAdfEDVpHgiOmDbVq111' },
62+
body: {
63+
calendario: {
64+
expiracao: 3600,
65+
},
66+
valor: {
67+
original: '0.01',
68+
},
69+
chave: 'CHAVEPIX',
70+
},
71+
}
72+
it('TEST 0: Shoud get Access Token', async () => {
73+
let endpoints = new Endpoints(options, constants)
74+
let res = await endpoints.getAccessToken()
75+
76+
expect(res).toBe('RfSfS9AJkLu7jPjOp2IbrI')
77+
})
78+
79+
it.each([
80+
{
81+
description: 'should create a charge',
82+
body: pixEndpoint,
83+
expected: pixChargeCreated,
84+
},
85+
{
86+
description: 'should throw "FALHA AO LER O CERTIFICADO"',
87+
body: pixEndpoint,
88+
expected: new Error('FALHA AO LER O CERTIFICADO'),
89+
},
90+
])('TEST $# : $description', async ({ body, expected }) => {
91+
let endpoints = new Endpoints(options, constants)
92+
let res = await endpoints.run(body.name, body.params, body.body)
93+
94+
expect(res).toStrictEqual(expected)
95+
})
96+
97+
it.each([
98+
{
99+
description: 'shoud get the request params [createRequest][PIX]',
100+
name: 'listAccountConfig',
101+
route: '/v2/gn/config',
102+
params: [],
103+
expected: {
104+
method: 'get',
105+
url: 'https://api-pix.gerencianet.com.br/v2/gn/config',
106+
headers: expect.anything(),
107+
data: expect.anything(),
108+
httpsAgent: expect.anything(),
109+
}
110+
},
111+
{
112+
description: 'shoud get the request params [listPlans][SUBSCRIPTION]',
113+
name: 'listPlans',
114+
route: '/plans',
115+
params: [],
116+
expected: {
117+
method: 'get',
118+
url: 'https://api.gerencianet.com.br/v1/plans',
119+
headers: expect.anything(),
120+
data: undefined,
121+
}
122+
}
123+
])('TEST $# : $description', async ({ name, route, params, expected }) => {
124+
let endpoints = new Endpoints(options, constants)
125+
await endpoints.run(name, params, [])
126+
let res = await endpoints.createRequest(route)
127+
expect(res).toStrictEqual(expected);
128+
})
129+
})

0 commit comments

Comments
 (0)