Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: node-fetch CVE-2022-0235 and reduce tech debt #214

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
format *.spec.js files
eboureau committed Feb 7, 2022
commit f2e903efae1c0ab4056443f66b359cbc441eb6e2
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
"build-win": "babel lib -d .build/lib && babel index.js --out-file .build/index.js && copy package.json .build && copy README.md .build",
"test": "jest",
"lint": "eslint lib/**/*.js test/**/*.js --cache",
"format": "prettier --write ./test/**/*.js ./lib/*.js ./lib/**/*.js",
"format": "prettier --write ./test/**/*.js ./test/**/*.spec.js ./lib/*.js ./lib/**/*.js",
"publish": "npm publish ./.build"
},
"keywords": [
11 changes: 4 additions & 7 deletions test/accounts.spec.js
Original file line number Diff line number Diff line change
@@ -14,19 +14,16 @@ describe('Avatax Accounts', () => {
const client = new Avatax(loadCreds());

describe('Getting accounts by id', () => {

beforeEach(() => {
nock(baseUrl)
.get(`/api/v2/accounts/${account}`)
.reply(200, accountResponse);
});

it('should return account by id', () => {
return client.getAccount({ id: account })
.then(res => {
expect(res).toEqual(accountResponse);
});
return client.getAccount({ id: account }).then(res => {
expect(res).toEqual(accountResponse);
});
});
});
});

127 changes: 67 additions & 60 deletions test/address.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Avatax from '../lib/AvaTaxClient';
import nock from 'nock';
import resolveAddressResponse from './fixtures/address_response';
@@ -12,75 +11,83 @@ const environment = 'dev';
const machineName = 'mbp';

describe('Address integration test', () => {

const client = new Avatax({ appName, appVersion, environment, machineName })
.withSecurity({ username, password });

it('should resolve address', () => {
const client = new Avatax({
appName,
appVersion,
environment,
machineName,
}).withSecurity({ username, password });

const address = {
line1: '1510 Foster Circle',
line2: 'Algonquin',
line3: '',
city: 'Illinois',
region: 'IL',
postalCode: '60102',
country: 'US',
textCase: 'mixed'
};
it('should resolve address', () => {
const address = {
line1: '1510 Foster Circle',
line2: 'Algonquin',
line3: '',
city: 'Illinois',
region: 'IL',
postalCode: '60102',
country: 'US',
textCase: 'mixed',
};

return client.resolveAddress(address).then(res => {
expect(res.validatedAddresses).toBeDefined();
expect(res.validatedAddresses[0]).toBeDefined();
expect(res.validatedAddresses[0].latitude).toBeDefined();
expect(res.validatedAddresses[0].latitude).toEqual(42.144481999999996);
expect(res.coordinates).toBeDefined();
expect(res.coordinates.longitude).toBeDefined();
expect(res.coordinates.longitude).toEqual(-88.320204);
expect(res.address.line1).toBeDefined();
expect(res.address.line1).toEqual('1510 Foster Circle');
});
return client.resolveAddress(address).then(res => {
expect(res.validatedAddresses).toBeDefined();
expect(res.validatedAddresses[0]).toBeDefined();
expect(res.validatedAddresses[0].latitude).toBeDefined();
expect(res.validatedAddresses[0].latitude).toEqual(42.144481999999996);
expect(res.coordinates).toBeDefined();
expect(res.coordinates.longitude).toBeDefined();
expect(res.coordinates.longitude).toEqual(-88.320204);
expect(res.address.line1).toBeDefined();
expect(res.address.line1).toEqual('1510 Foster Circle');
});
});

it('should list cross border sections', () => {
return client.listCrossBorderSections().then(res => {
expect(res).toBeDefined();
});
it('should list cross border sections', () => {
return client.listCrossBorderSections().then(res => {
expect(res).toBeDefined();
});
});
});

describe('Address resolve Tests', () => {
beforeEach(() => {
nock(baseUrl).get(`/api/v2/addresses/resolve?line1=1510%20Foster%20Circle&line2=Algonquin&city=Illinois&region=IL&postalCode=60102&country=US&textCase=mixed`)
.reply(200, resolveAddressResponse);
})

const client = new Avatax({ appName, appVersion, environment, machineName })
.withSecurity({ username, password });
beforeEach(() => {
nock(baseUrl)
.get(
`/api/v2/addresses/resolve?line1=1510%20Foster%20Circle&line2=Algonquin&city=Illinois&region=IL&postalCode=60102&country=US&textCase=mixed`,
)
.reply(200, resolveAddressResponse);
});

it('should resolve address', () => {
const client = new Avatax({
appName,
appVersion,
environment,
machineName,
}).withSecurity({ username, password });

const address = {
line1: '1510 Foster Circle',
line2: 'Algonquin',
line3: '',
city: 'Illinois',
region: 'IL',
postalCode: '60102',
country: 'US',
textCase: 'mixed'
};
it('should resolve address', () => {
const address = {
line1: '1510 Foster Circle',
line2: 'Algonquin',
line3: '',
city: 'Illinois',
region: 'IL',
postalCode: '60102',
country: 'US',
textCase: 'mixed',
};

return client.resolveAddress(address).then(res => {
expect(res.validatedAddresses).toBeDefined();
expect(res.validatedAddresses[0]).toBeDefined();
expect(res.validatedAddresses[0].latitude).toBeDefined();
expect(res.validatedAddresses[0].latitude).toEqual(42.144481999999996);
expect(res.coordinates).toBeDefined();
expect(res.coordinates.longitude).toBeDefined();
expect(res.coordinates.longitude).toEqual(-88.320204);
expect(res.address.line1).toBeDefined();
expect(res.address.line1).toEqual('1510 Foster Circle');
});
return client.resolveAddress(address).then(res => {
expect(res.validatedAddresses).toBeDefined();
expect(res.validatedAddresses[0]).toBeDefined();
expect(res.validatedAddresses[0].latitude).toBeDefined();
expect(res.validatedAddresses[0].latitude).toEqual(42.144481999999996);
expect(res.coordinates).toBeDefined();
expect(res.coordinates.longitude).toBeDefined();
expect(res.coordinates.longitude).toEqual(-88.320204);
expect(res.address.line1).toBeDefined();
expect(res.address.line1).toEqual('1510 Foster Circle');
});
});
});
4 changes: 1 addition & 3 deletions test/basic_auth.spec.js
Original file line number Diff line number Diff line change
@@ -6,15 +6,13 @@
import { createBasicAuthHeader } from '../lib/utils/basic_auth';

describe('BasicAuth Header', () => {

it('should create a basic auth header', () => {
const account = '12345';
const licenseKey = '54321';
const expectedHeader = 'Basic MTIzNDU6NTQzMjE=';

const basicAuthHeader = createBasicAuthHeader(account, licenseKey);

expect(basicAuthHeader).toBe(expectedHeader);
});
});

106 changes: 54 additions & 52 deletions test/batch.spec.js
Original file line number Diff line number Diff line change
@@ -17,73 +17,75 @@ let id = 5321678;
const baseUrl = 'https://sandbox-rest.avatax.com';

describe('Batch Full Integration Tests', () => {
const clientCreds = loadCreds();
const client = new Avatax(clientCreds).withSecurity(clientCreds);
const clientCreds = loadCreds();
const client = new Avatax(clientCreds).withSecurity(clientCreds);

describe('Create Batch', () => {

it('should create a new batch', () => {
return client.createBatches({companyId, model: batchCreateRequest}).then(res => {

expect(res[0]).toBeDefined();
expect(res[0].status).toEqual("Waiting");
expect(res[0].type).toEqual("TransactionImport");
expect(res[0].companyId).toEqual(companyId);
});
describe('Create Batch', () => {
it('should create a new batch', () => {
return client
.createBatches({ companyId, model: batchCreateRequest })
.then(res => {
expect(res[0]).toBeDefined();
expect(res[0].status).toEqual('Waiting');
expect(res[0].type).toEqual('TransactionImport');
expect(res[0].companyId).toEqual(companyId);
});
});
});

describe('Download Batch', () => {

it.skip('should download the specified batch', () =>{
return client.downloadBatch({companyId, batchId, id}).then(res => {
expect(res).toBeDefined();
expect(res.headers.get("transfer-encoding")).toEqual("chunked");
});
});
describe('Download Batch', () => {
it.skip('should download the specified batch', () => {
return client.downloadBatch({ companyId, batchId, id }).then(res => {
expect(res).toBeDefined();
expect(res.headers.get('transfer-encoding')).toEqual('chunked');
});
});
});
});


/*nock unit tests for creating and downloading a batch*/
describe('Batch Unit Tests', () => {
const clientCreds = loadCreds();
const client = new Avatax(clientCreds).withSecurity(clientCreds);
const clientCreds = loadCreds();
const client = new Avatax(clientCreds).withSecurity(clientCreds);

afterEach(() => {
nock.cleanAll();
});
afterEach(() => {
nock.cleanAll();
});

/*Unit test for creating a new batch*/
describe('Creating new batches', () => {
beforeEach(() => {
nock(baseUrl)
.post(`/api/v2/companies/${companyId}/batches`, batchCreateRequest)
.reply(201, batchCreateResponse);
});

it('should create a new batchNock', () =>{
return client.createBatches({companyId, model: batchCreateRequest})
.then(actualResponse => {
expect(actualResponse).toEqual(batchCreateResponse);
});
});
/*Unit test for creating a new batch*/
describe('Creating new batches', () => {
beforeEach(() => {
nock(baseUrl)
.post(`/api/v2/companies/${companyId}/batches`, batchCreateRequest)
.reply(201, batchCreateResponse);
});

/* top passes, this does not */
/*Unit test for downloading a batch*/
describe('Downloading a batch', () => {
beforeEach(() => {
nock(baseUrl)
.get(`/api/v2/companies/${companyId}/batches/${batchId}/files/${id}/attachment`)
.reply(201, batchDownloadResponse);
it('should create a new batchNock', () => {
return client
.createBatches({ companyId, model: batchCreateRequest })
.then(actualResponse => {
expect(actualResponse).toEqual(batchCreateResponse);
});
});
});

/* top passes, this does not */
/*Unit test for downloading a batch*/
describe('Downloading a batch', () => {
beforeEach(() => {
nock(baseUrl)
.get(
`/api/v2/companies/${companyId}/batches/${batchId}/files/${id}/attachment`,
)
.reply(201, batchDownloadResponse);
});

it('should download the specified batchNock', () =>{
return client.downloadBatch({companyId, batchId, id})
.then(actualResponse => {
expect(actualResponse).toEqual(batchDownloadResponse);
});
it('should download the specified batchNock', () => {
return client
.downloadBatch({ companyId, batchId, id })
.then(actualResponse => {
expect(actualResponse).toEqual(batchDownloadResponse);
});
});
});
});
39 changes: 21 additions & 18 deletions test/client.spec.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import Avatax from '../lib/AvaTaxClient';

describe('Avatax Client', () => {

it('should configure client account and license key', () => {
// arrange
const accountId = '12345';
@@ -16,8 +15,12 @@ describe('Avatax Client', () => {
const environment = 'sandbox';
const machineName = 'mbp';

const client = new Avatax({ appName, appVersion, environment, machineName })
.withSecurity({ accountId, licenseKey });
const client = new Avatax({
appName,
appVersion,
environment,
machineName,
}).withSecurity({ accountId, licenseKey });

// assert
// expect(client.account).toBe(accountId);
@@ -26,24 +29,24 @@ describe('Avatax Client', () => {
});

it('should handle various environment settings', () => {

const testCases = [
{ environment:'sandbox', expected: 'https://sandbox-rest.avatax.com' },
{ environment:'production', expected: 'https://rest.avatax.com' },
{ environment: 'sandbox', expected: 'https://sandbox-rest.avatax.com' },
{ environment: 'production', expected: 'https://rest.avatax.com' },
{ environment: undefined, expected: 'https://rest.avatax.com' },
{ environment:'http://specific-url' , expected: 'http://specific-url' },
{ environment:'https://specific-https-url' , expected: 'https://specific-https-url' },
]
testCases.forEach(({ environment, expected })=>{

const client = new Avatax({
appName:'myapp',
appVersion:'1.0',
{ environment: 'http://specific-url', expected: 'http://specific-url' },
{
environment: 'https://specific-https-url',
expected: 'https://specific-https-url',
},
];
testCases.forEach(({ environment, expected }) => {
const client = new Avatax({
appName: 'myapp',
appVersion: '1.0',
machineName: 'test-run',
environment
})
environment,
});
expect(client.baseUrl).toBe(expected);
})
});
});
});

4 changes: 1 addition & 3 deletions test/companies.spec.js
Original file line number Diff line number Diff line change
@@ -74,9 +74,7 @@ describe('Company Unit Tests', () => {

describe('Listing companies for account', () => {
beforeEach(() => {
nock(baseUrl)
.get(`/api/v2/companies`)
.reply(200, companiesListResponse);
nock(baseUrl).get(`/api/v2/companies`).reply(200, companiesListResponse);
});

it('should return list of companies', () => {
35 changes: 19 additions & 16 deletions test/transactions.spec.js
Original file line number Diff line number Diff line change
@@ -19,34 +19,37 @@ describe.skip('Avatax Transactions', () => {
const environment = 'sandbox';
const machineName = 'mbp';

const client = new Avatax({ appName, appVersion, environment, machineName })
.withSecurity({ username, password });
const client = new Avatax({
appName,
appVersion,
environment,
machineName,
}).withSecurity({ username, password });

describe('Listing transactions by company', () => {
beforeEach(() => {
nock(baseUrl).get(`/api/v2/companies/${companyCode}/transactions`)
nock(baseUrl)
.get(`/api/v2/companies/${companyCode}/transactions`)
.reply(200, transactionsListResponse);
})
});

it('should resolve address', () => {
const address = {
city: 'irvine',
postalCode: '92615',
region: 'ca',
country: 'us'
country: 'us',
};

return client.resolveAddress(address)
.then(res => {
console.log(res);
});
return client.resolveAddress(address).then(res => {
console.log(res);
});
});

it('should list transactions by company code', () => {
return client.listTransactions({ companyCode })
.then(actualResponse => {
expect(actualResponse).toEqual(transactionsListResponse);
})
return client.listTransactions({ companyCode }).then(actualResponse => {
expect(actualResponse).toEqual(transactionsListResponse);
});
});
});

@@ -58,11 +61,11 @@ describe.skip('Avatax Transactions', () => {
});

it('should create a new transaction', () => {
return client.createTransaction(transactionRequest)
return client
.createTransaction(transactionRequest)
.then(actualResponse => {
expect(actualResponse).toEqual(transactionResponse);
});
});
});
});

30 changes: 14 additions & 16 deletions test/transactions_create_void_adjust.spec.js
Original file line number Diff line number Diff line change
@@ -22,8 +22,6 @@ const baseUrl = 'https://sandbox-rest.avatax.com';
describe('Transaction Full Integration Tests', () => {
const clientCreds = loadCreds();
const client = new Avatax(clientCreds).withSecurity(clientCreds);



describe('Create Transaction', () => {
it('should create a new transaction', () => {
@@ -56,7 +54,7 @@ describe('Transaction Full Integration Tests', () => {
.adjustTransaction({
companyCode,
transactionCode,
model: adjustTransactionRequest
model: adjustTransactionRequest,
})
.then(res => {
expect(res).toBeDefined();
@@ -76,7 +74,7 @@ describe('Transaction Full Integration Tests', () => {
.voidTransaction({
companyCode,
transactionCode,
model: voidTransactionRequest
model: voidTransactionRequest,
})
.then(res => {
expect(res.totalAmount).toBeGreaterThanOrEqual(0);
@@ -112,17 +110,17 @@ describe('Transactions Unit Tests', () => {
});

/*Case for adjusting existing transaction
*
* Need to grab transactionCode from newly created transaction above
* transactionCode = actualResponse.code;
* EDIT - its hard coded in so we can cheat a little
* */
*
* Need to grab transactionCode from newly created transaction above
* transactionCode = actualResponse.code;
* EDIT - its hard coded in so we can cheat a little
* */
describe('Adjusting existing transactions', () => {
beforeEach(() => {
nock(baseUrl)
.post(
`/api/v2/companies/${companyCode}/transactions/${transactionCode}/adjust`,
adjustTransactionRequest
adjustTransactionRequest,
)
.reply(200, adjustTransactionResponse);
});
@@ -132,7 +130,7 @@ describe('Transactions Unit Tests', () => {
.adjustTransaction({
companyCode,
transactionCode,
model: adjustTransactionRequest
model: adjustTransactionRequest,
})
.then(actualResponse => {
expect(actualResponse).toEqual(adjustTransactionResponse);
@@ -141,15 +139,15 @@ describe('Transactions Unit Tests', () => {
});

/* Code for voiding an existing transaction
*
* Use same transactionCode from before
* */
*
* Use same transactionCode from before
* */
describe('Voiding existing transactions', () => {
beforeEach(() => {
nock(baseUrl)
.post(
`/api/v2/companies/${companyCode}/transactions/${transactionCode}/void`,
voidTransactionRequest
voidTransactionRequest,
)
.reply(200, voidTransactionResponse);
});
@@ -159,7 +157,7 @@ describe('Transactions Unit Tests', () => {
.voidTransaction({
companyCode,
transactionCode,
model: voidTransactionRequest
model: voidTransactionRequest,
})
.then(actualResponse => {
expect(actualResponse).toEqual(voidTransactionResponse);