Skip to content

Commit

Permalink
Testes unitários
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoliveiraGN committed Dec 21, 2022
1 parent ecc711f commit 0367907
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const axios = require('axios')
const fs = require('fs')
const Auth = require('../lib/auth')
const constants = require('../lib/constants')

jest.mock('fs')
fs.readFileSync.mockReturnValueOnce('')

const accessTokenPix = {
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
token_type: 'Bearer',
expires_in: 3600,
scope: 'cob.read',
}
const accessTokenBoletos = {
access_token: '1723ad73',
refresh_token: '36accb15',
expires_in: 600,
expire_at: '1656012603684',
token_type: 'Bearer',
}

jest.mock('axios', () => jest.fn().mockResolvedValueOnce({ status: 200, data: accessTokenPix }).mockResolvedValueOnce({ status: 200, data: accessTokenBoletos }))

describe('Auth Tests', () => {
it.each([
{
description: 'Should get Access_Token with pix certificate',
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',
},
expected: { access_token: expect.any(String), token_type: 'Bearer', expires_in: 3600, scope: 'cob.read' },
},
{
description: 'Should get Access_Token without pix certificate [API EMISSÕES]',
options: {
sandbox: false,
client_id: 'Client_Id',
client_secret: 'Client_Secret',
authRoute: { route: '/oauth/token', method: 'post' },
baseUrl: 'https://api.gerencianet.com.br/v1',
},
expected: { access_token: '1723ad73', refresh_token: '36accb15', expires_in: 600, expire_at: '1656012603684', token_type: 'Bearer' },
},
])('TEST $# : $description', async ({ options, expected }) => {
const auth = new Auth(options, constants)
let res = await auth.getAccessToken()
expect(res).toMatchObject(expected)
expect(axios).toHaveBeenCalled()
})
})
129 changes: 129 additions & 0 deletions test/endpoints.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const Auth = require('../lib/auth')
const constants = require('../lib/constants')
const Endpoints = require('../lib/endpoints')
const fs = require('fs')

let 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',
}

jest.spyOn(Endpoints.prototype, 'req').mockImplementationOnce(() => {
return Promise.resolve(pixChargeCreated)
}).mockReturnValueOnce(new Error('FALHA AO LER O CERTIFICADO')).mockReturnValueOnce('')

jest.spyOn(Auth.prototype, 'getAccessToken').mockImplementation(() => {
return Promise.resolve({
access_token: 'RfSfS9AJkLu7jPjOp2IbrI',
token_type: 'Bearer',
expires_in: 3600,
scope: 'cob.read',
})
})

jest.mock('fs')
fs.readFileSync.mockReturnValueOnce('')



let 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.',
}

describe('Endpoints Tests', () => {
let pixEndpoint = {
name: 'pixCreateCharge',
params: { txid: 'dt9BHlyzrb5jrFNAdfEDVpHgiOmDbVq111' },
body: {
calendario: {
expiracao: 3600,
},
valor: {
original: '0.01',
},
chave: 'CHAVEPIX',
},
}
it('TEST 0: Shoud get Access Token', async () => {
let endpoints = new Endpoints(options, constants)
let 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 }) => {
let endpoints = new Endpoints(options, constants)
let 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: undefined,
}
}
])('TEST $# : $description', async ({ name, route, params, expected }) => {
let endpoints = new Endpoints(options, constants)
await endpoints.run(name, params, [])
let res = await endpoints.createRequest(route)
expect(res).toStrictEqual(expected);
})
})

0 comments on commit 0367907

Please sign in to comment.