Skip to content

Commit

Permalink
refactor: no files for cert/key (#10)
Browse files Browse the repository at this point in the history
* refactor: no files for cert/key

* tests: fix azul config

* build: fix action
  • Loading branch information
crisog authored Aug 24, 2024
1 parent 3b36164 commit 32cf412
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 110 deletions.
17 changes: 3 additions & 14 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,14 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "lts/*"
- name: Write key and certificate to files
env:
AZUL_KEY: ${{ secrets.AZUL_KEY }}
AZUL_CERT: ${{ secrets.AZUL_CERT }}
run: |
echo "${AZUL_KEY}" | base64 --decode > ${HOME}/test.key
echo "${AZUL_CERT}" | base64 --decode > ${HOME}/test.crt
node-version: 'lts/*'
- run: npm ci
- name: Run tests
env:
AUTH1: ${{ secrets.AUTH1 }}
AUTH2: ${{ secrets.AUTH2 }}
MERCHANT_ID: ${{ secrets.MERCHANT_ID }}
AZUL_KEY: ${{ secrets.AZUL_KEY }}
AZUL_CERT: ${{ secrets.AZUL_CERT }}
run: |
export CERTIFICATE_PATH=${HOME}/test.crt
export KEY_PATH=${HOME}/test.key
npm run test
- name: Remove key and certificate
run: |
rm ${HOME}/test.key
rm ${HOME}/test.crt
34 changes: 10 additions & 24 deletions src/azul-api/request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import path from 'path';
import fs from 'fs/promises';
import { request, Agent } from 'undici';
import { capitalizeKeys } from '../utils';
import { Process } from './processes';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

enum AzulURL {
DEV = 'https://pruebas.azul.com.do/webservices/JSON/Default.aspx',
Expand All @@ -17,8 +11,8 @@ export type Config = {
auth1: string;
auth2: string;
merchantId: string;
certificatePath: string;
keyPath: string;
certificate: string;
key: string;
environment?: 'dev' | 'prod';
channel?: string;
};
Expand All @@ -30,17 +24,15 @@ class AzulRequester {
private auth2: string;
private channel: string;
private merchantId: string;
private certificatePath: string;
private keyPath: string;
private certificate: Buffer | undefined;
private certificateKey: Buffer | undefined;
private certificate: string;
private key: string;

constructor(config: Config) {
this.auth1 = config.auth1;
this.auth2 = config.auth2;
this.merchantId = config.merchantId;
this.certificatePath = config.certificatePath;
this.keyPath = config.keyPath;
this.certificate = config.certificate;
this.key = config.key;

if (config.channel === undefined) {
this.channel = 'EC';
Expand Down Expand Up @@ -89,20 +81,14 @@ class AzulRequester {
return (await response.body.json()) as any;
}

private async getCertificates(): Promise<{ cert: Buffer; key: Buffer }> {
if (this.certificate && this.certificateKey) {
return {
cert: this.certificate,
key: this.certificateKey
};
private async getCertificates(): Promise<{ cert: string; key: string }> {
if (!this.certificate || !this.key) {
throw new Error('Missing certificate or key');
}

this.certificate = await fs.readFile(path.resolve(__dirname, this.certificatePath));
this.certificateKey = await fs.readFile(path.resolve(__dirname, this.keyPath));

return {
cert: this.certificate,
key: this.certificateKey
key: this.key
};
}
}
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/hold.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, beforeAll, it } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can hold a payment', () => {
const customOrderId = randomUUID();
let azulOrderId: string;
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AzulAPI from '../../src/azul-api/api';

export const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificate: process.env.AZUL_CERT!,
key: process.env.AZUL_KEY!
});
10 changes: 1 addition & 9 deletions tests/integration/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can post a payment', () => {
it('Can post a hold', async () => {
const customOrderId = randomUUID();
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/refund.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can refund a payment', () => {
it('Can refund a sale', async () => {
const customOrderId = randomUUID();
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/sale.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can make a payment', () => {
it('Can make a payment', async () => {
const result = await azul.payments.sale({
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it, beforeAll } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can search a payment', () => {
const customOrderId = randomUUID();

Expand Down
10 changes: 1 addition & 9 deletions tests/integration/vault.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('DataVault', () => {
let dataVaultToken: string;

Expand Down
10 changes: 1 addition & 9 deletions tests/integration/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it, beforeAll } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can verify a payment', () => {
const customOrderId = randomUUID();

Expand Down
10 changes: 1 addition & 9 deletions tests/integration/void.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomUUID } from 'crypto';
import AzulAPI from '../../src/azul-api/api';
import { azul } from './instance';
import { describe, expect, it, beforeAll } from 'vitest';
import 'dotenv/config';

const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificatePath: process.env.CERTIFICATE_PATH!,
keyPath: process.env.KEY_PATH!
});

describe('Can void a payment', () => {
const customOrderId = randomUUID();
let azulOrderId: string | undefined = undefined;
Expand Down

0 comments on commit 32cf412

Please sign in to comment.