|
| 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