From 0170a97b69f7eff2c893488bec62e6f81d5e8913 Mon Sep 17 00:00:00 2001 From: Matt Everett Date: Mon, 5 Nov 2018 15:20:07 -0800 Subject: [PATCH 01/12] add voidTracking(trackingNumber) method --- README.md | 15 ++++++++++++++- client.js | 31 +++++++++++++++++++++++++++++++ package.json | 6 +++--- test/client.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e70d09a..18023e0 100644 --- a/README.md +++ b/README.md @@ -102,4 +102,17 @@ The Newgistics Shipping API is designed to be low latency and scalable. It offer newgisticsClient.ping(function(err, pong) { console.log(pong); }); -``` \ No newline at end of file +``` + +### newgisticsClient.voidTracking(trackingNumber, callback) + +Void tracking for a package by tracking number + +**Example** + +```javascript +newgisticsClient.voidTracking('myTrackingNumber', function(err) { + console.log(err); +}); +``` + diff --git a/client.js b/client.js index 3a3033f..24c3f5e 100644 --- a/client.js +++ b/client.js @@ -85,6 +85,37 @@ function NewgisticsClient(args) { callback(err, pong); }); }; + + this.voidTracking = function(trackingNumber, callback) { + this.getToken(function(err, token) { + if (err) { + return callback(err); + } + + var req = { + auth: { + bearer: token.access_token + }, + formData: { + trackingId: trackingNumber + }, + method: 'POST', + url: `${opts.shippingapi_url}/v1/packages/trackingId/${trackingNumber}/void` + }; + + request(req, function(err, res) { + if (err) { + return callback(err); + } + + if (res.statusCode !== 200 && res.statusCode !== 404) { + return callback(new Error(res.statusCode)); + } + + callback(); + }); + }); + }; } module.exports = NewgisticsClient; \ No newline at end of file diff --git a/package.json b/package.json index bad409b..c1e62b2 100644 --- a/package.json +++ b/package.json @@ -20,12 +20,12 @@ "main": "client.js", "name": "newgistics-client", "repository": { - "type" : "git", - "url" : "https://github.com/mediocre/newgistics-client.git" + "type": "git", + "url": "https://github.com/mediocre/newgistics-client.git" }, "scripts": { "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test/* && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "test": "mocha --exit --reporter spec test" }, "version": "0.0.4" -} \ No newline at end of file +} diff --git a/test/client.js b/test/client.js index 0b67dae..b66a618 100644 --- a/test/client.js +++ b/test/client.js @@ -4,6 +4,8 @@ const cache = require('memory-cache'); const NewgisticsClient = require('../client'); +var trackingNumber; + describe('NewgisticsClient.createPackage', function() { this.timeout(5000); @@ -114,6 +116,8 @@ describe('NewgisticsClient.createPackage', function() { assert(package.labels[0].labelData); assert(package.pricingInformation); + trackingNumber = package.trackingId; + done(); }); }); @@ -226,6 +230,48 @@ describe('NewgisticsClient.ping', function() { assert(pong); + done(); + }); + }); +}); + +describe('NewgisticsClient.voidTracking', function() { + this.timeout(5000); + + it('should return an error', function(done) { + var newgisticsClient = new NewgisticsClient({ + shippingapi_url: 'invalid' + }); + + newgisticsClient.voidTracking('invalid', function(err) { + assert(err); + + done(); + }); + }); + + it('should not return an error for an invalid tracking number', function(done) { + var newgisticsClient = new NewgisticsClient({ + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET + }); + + newgisticsClient.voidTracking('invalid', function(err) { + assert.ifError(err); + + done(); + }); + }); + + it('should void tracking', function(done) { + var newgisticsClient = new NewgisticsClient({ + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET + }); + + newgisticsClient.voidTracking(trackingNumber, function(err) { + assert.ifError(err); + done(); }); }); From f7085012d03a0c5ab4c5a9fff4d1c565271fde0c Mon Sep 17 00:00:00 2001 From: Matt Everett Date: Mon, 5 Nov 2018 15:43:59 -0800 Subject: [PATCH 02/12] test a non-200, non-404 response --- test/client.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/client.js b/test/client.js index b66a618..22d30c0 100644 --- a/test/client.js +++ b/test/client.js @@ -250,6 +250,19 @@ describe('NewgisticsClient.voidTracking', function() { }); }); + it('should return an error', function(done) { + var newgisticsClient = new NewgisticsClient({ + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET + }); + + newgisticsClient.voidTracking('\n', function(err) { + assert(err); + + done(); + }); + }); + it('should not return an error for an invalid tracking number', function(done) { var newgisticsClient = new NewgisticsClient({ client_id: process.env.NEWGISTICS_CLIENT_ID, From bcdc7e61787b7a261edbabc7f96dbf39dcfea82e Mon Sep 17 00:00:00 2001 From: Matt Everett Date: Mon, 5 Nov 2018 15:52:12 -0800 Subject: [PATCH 03/12] also test failed auth --- test/client.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/client.js b/test/client.js index 22d30c0..f0729fe 100644 --- a/test/client.js +++ b/test/client.js @@ -239,7 +239,12 @@ describe('NewgisticsClient.voidTracking', function() { this.timeout(5000); it('should return an error', function(done) { + // Clear existing token + cache.del('newgistics-client-token'); + var newgisticsClient = new NewgisticsClient({ + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, shippingapi_url: 'invalid' }); From 430e35970436263f7824a2e59c0fce7456254209 Mon Sep 17 00:00:00 2001 From: Matt Everett Date: Mon, 5 Nov 2018 16:15:12 -0800 Subject: [PATCH 04/12] chemistry doggin' it --- test/client.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/client.js b/test/client.js index f0729fe..30f09ac 100644 --- a/test/client.js +++ b/test/client.js @@ -242,6 +242,19 @@ describe('NewgisticsClient.voidTracking', function() { // Clear existing token cache.del('newgistics-client-token'); + var newgisticsClient = new NewgisticsClient({ + shippingapi_url: 'invalid' + }); + + newgisticsClient.voidTracking('invalid', function(err) { + assert(err); + + done(); + }); + }); + + it('should return an error', function(done) { + // Clear existing token var newgisticsClient = new NewgisticsClient({ client_id: process.env.NEWGISTICS_CLIENT_ID, client_secret: process.env.NEWGISTICS_CLIENT_SECRET, From 0de3c0c723ad9a33995e9f6b618b2710e39a0674 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Mon, 5 Nov 2018 18:29:48 -0600 Subject: [PATCH 05/12] Use the method description from Newgistics docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18023e0..86b7e86 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ newgisticsClient.ping(function(err, pong) { ### newgisticsClient.voidTracking(trackingNumber, callback) -Void tracking for a package by tracking number +Void label previously created by tracking number. **Example** From 03e5fdbec68ec84d7d71c6ae153585cbea7ef8ec Mon Sep 17 00:00:00 2001 From: Matt Everett Date: Mon, 5 Nov 2018 16:34:12 -0800 Subject: [PATCH 06/12] keep tests in their own lanes --- test/client.js | 58 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/test/client.js b/test/client.js index 30f09ac..30e638e 100644 --- a/test/client.js +++ b/test/client.js @@ -4,8 +4,6 @@ const cache = require('memory-cache'); const NewgisticsClient = require('../client'); -var trackingNumber; - describe('NewgisticsClient.createPackage', function() { this.timeout(5000); @@ -116,8 +114,6 @@ describe('NewgisticsClient.createPackage', function() { assert(package.labels[0].labelData); assert(package.pricingInformation); - trackingNumber = package.trackingId; - done(); }); }); @@ -300,10 +296,60 @@ describe('NewgisticsClient.voidTracking', function() { client_secret: process.env.NEWGISTICS_CLIENT_SECRET }); - newgisticsClient.voidTracking(trackingNumber, function(err) { + var package = { + classOfService: 'Ground', + clientFacilityId: '8448', + dimensions: { + height: { + measurementValue: '1', + unitOfMeasure: 'Inches' + }, + isRectangular: true, + length: { + measurementValue: '6', + unitOfMeasure: 'Inches' + }, + width: { + measurementValue: '4', + unitOfMeasure: 'Inches' + } + }, + ngsFacilityId: '1361', + pricePackage: true, + returnAddress: { + address1: '4717 Plano Parkway', + address2: 'Suite 130', + city: 'Carrollton', + country: 'US', + isResidential: false, + name: 'A Mediocre Corporation', + postalCode: '75010', + stateOrProvince: 'TX' + }, + shipToAddress: { + address1: '5531 Willis Ave', + city: 'Dallas', + country: 'US', + name: 'Joe User', + postalCode: '75206', + stateOrProvince: 'TX' + }, + weight: { + measurementValue: '0.5', + unitOfMeasure: 'Pounds' + } + }; + + newgisticsClient.createPackage(package, function(err, package) { assert.ifError(err); + assert.notStrictEqual(package.trackingId, null); + assert(package.trackingId.length); - done(); + newgisticsClient.voidTracking(package.trackingId, function(err) { + assert.ifError(err); + + done(); + }); }); }); }); \ No newline at end of file From c421f16cd2e78f0ed503bd43ea673f9af1c87e58 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Mon, 5 Nov 2018 19:17:37 -0600 Subject: [PATCH 07/12] bump version number --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c1e62b2..a9d3e16 100644 --- a/package.json +++ b/package.json @@ -27,5 +27,5 @@ "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test/* && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "test": "mocha --exit --reporter spec test" }, - "version": "0.0.4" -} + "version": "1.0.0" +} \ No newline at end of file From 5c6bbc1657605d3a69a585abe3d06fd690a28388 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Mon, 5 Nov 2018 19:23:09 -0600 Subject: [PATCH 08/12] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 86b7e86..b679b70 100644 --- a/README.md +++ b/README.md @@ -114,5 +114,4 @@ Void label previously created by tracking number. newgisticsClient.voidTracking('myTrackingNumber', function(err) { console.log(err); }); -``` - +``` \ No newline at end of file From cae21665b605faf540bfbf4e9afa611933f4b930 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Fri, 21 Dec 2018 11:23:42 -0600 Subject: [PATCH 09/12] sudo is now deprecated: https://blog.travis-ci.com/2018-11-19-required-linux-infrastructure-migration --- .travis.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index e63eaa5..2c51d49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,7 @@ -after_success: - - npm run coveralls - before_script: - npm i -g eslint - eslint . -dist: trusty - language: node_js node_js: @@ -15,4 +10,5 @@ node_js: notifications: slack: mediocre:nDgxXo7IeSkxifmFL6UIjcFd -sudo: false \ No newline at end of file +script: + - npm run coveralls \ No newline at end of file From 97f7f59f52075916e0ca8fbcc5317c104b340526 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Wed, 9 Jan 2019 23:28:22 -0600 Subject: [PATCH 10/12] better error handling --- client.js | 74 ++++++++++++-------- package.json | 3 +- test/client.js | 184 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 195 insertions(+), 66 deletions(-) diff --git a/client.js b/client.js index 630d9dd..194b760 100644 --- a/client.js +++ b/client.js @@ -1,4 +1,5 @@ const cache = require('memory-cache'); +const createError = require('http-errors'); const request = require('request'); function NewgisticsClient(args) { @@ -41,12 +42,8 @@ function NewgisticsClient(args) { return callback(err); } - if (body && body.error) { - return callback(new Error(body.error.message)); - } - if (res.statusCode !== 200) { - return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`)); + return callback(createError(res.statusCode, body && body.error && body.error.message)); } callback(); @@ -69,20 +66,16 @@ function NewgisticsClient(args) { url: `${opts.shippingapi_url}/v1/packages` }; - request(req, function(err, res, package) { + request(req, function(err, res, body) { if (err) { return callback(err); } - if (package && package.error) { - return callback(new Error(package.error.message)); - } - if (res.statusCode !== 200) { - return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`)); + return callback(createError(res.statusCode, body && body.error && body.error.message)); } - callback(null, package.data); + callback(null, body.data); }); }); }; @@ -107,23 +100,19 @@ function NewgisticsClient(args) { url: `${opts.authapi_url}/connect/token` }; - request(req, function(err, res, token) { + request(req, function(err, res, body) { if (err) { return callback(err); } - if (token && token.error) { - return callback(new Error(token.error)); - } - if (res.statusCode !== 200) { - return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`)); + return callback(createError(res.statusCode, body && body.error)); } // Put the token in memory cache - cache.put('newgistics-client-token', token, token.expires_in / 2); + cache.put('newgistics-client-token', body, body.expires_in / 2); - callback(null, token); + callback(null, body); }); }; @@ -134,43 +123,68 @@ function NewgisticsClient(args) { url: `${opts.shippingapi_url}/ping` }; - request(req, function(err, res, pong) { + request(req, function(err, res, body) { if (err) { return callback(err); } if (res.statusCode !== 200) { - return callback(new Error(`${res.statusCode} ${res.request.method} ${res.request.href} ${res.body}`)); + return callback(createError(res.statusCode, body && body.error && body.error.message)); } - callback(null, pong); + callback(null, body); }); }; - this.voidTracking = function(trackingNumber, callback) { + this.voidPackage = function(packageId, callback) { this.getToken(function(err, token) { if (err) { return callback(err); } - var req = { + const req = { auth: { bearer: token.access_token }, - formData: { - trackingId: trackingNumber + method: 'POST', + url: `${opts.shippingapi_url}/v1/packages/${packageId}/void` + }; + + request(req, function(err, res, body) { + if (err) { + return callback(err); + } + + if (res.statusCode !== 200) { + return callback(createError(res.statusCode, body && body.error && body.error.message)); + } + + callback(); + }); + }); + }; + + this.voidTracking = function(trackingNumber, callback) { + this.getToken(function(err, token) { + if (err) { + return callback(err); + } + + const req = { + auth: { + bearer: token.access_token }, method: 'POST', url: `${opts.shippingapi_url}/v1/packages/trackingId/${trackingNumber}/void` }; - request(req, function(err, res) { + request(req, function(err, res, body) { if (err) { return callback(err); } - if (res.statusCode !== 200 && res.statusCode !== 404) { - return callback(new Error(res.statusCode)); + if (res.statusCode !== 200) { + return callback(createError(res.statusCode, body && body.error && body.error.message)); } callback(); diff --git a/package.json b/package.json index a9d3e16..ce12870 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "http-errors": "~1.7.1", "memory-cache": "~0.2.0", "request": "~2.88.0" }, @@ -28,4 +29,4 @@ "test": "mocha --exit --reporter spec test" }, "version": "1.0.0" -} \ No newline at end of file +} diff --git a/test/client.js b/test/client.js index a73300a..ded68c5 100644 --- a/test/client.js +++ b/test/client.js @@ -33,7 +33,6 @@ describe('NewgisticsClient.closeout', function() { newgisticsClient.closeout('1234', function(err) { assert(err); - assert.strictEqual(err.message, 'invalid_client'); done(); @@ -50,8 +49,8 @@ describe('NewgisticsClient.closeout', function() { newgisticsClient.closeout('1234', function(err) { assert(err); - assert.strictEqual(err.message, 'Merchant configuration not found'); + assert.strictEqual(err.status, 400); done(); }); @@ -67,8 +66,8 @@ describe('NewgisticsClient.closeout', function() { newgisticsClient.closeout('1234', function(err) { assert(err); - - assert.strictEqual(err.message, '500 POST https://httpstat.us/500#/v1/closeout/1234 500 Internal Server Error'); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); done(); }); @@ -116,8 +115,8 @@ describe('NewgisticsClient.createPackage', function() { newgisticsClient.createPackage({}, function(err, package) { assert(err); - assert.strictEqual(err.message, 'invalid_client'); + assert.strictEqual(err.status, 400); assert.strictEqual(package, undefined); done(); @@ -134,7 +133,6 @@ describe('NewgisticsClient.createPackage', function() { newgisticsClient.createPackage({}, function(err, package) { assert(err); - assert.strictEqual(package, undefined); done(); @@ -151,7 +149,6 @@ describe('NewgisticsClient.createPackage', function() { newgisticsClient.createPackage({}, function(err, package) { assert(err); - assert.strictEqual(package, undefined); done(); @@ -168,8 +165,8 @@ describe('NewgisticsClient.createPackage', function() { newgisticsClient.createPackage({}, function(err) { assert(err); - - assert.strictEqual(err.message, '500 POST https://httpstat.us/500#/v1/packages 500 Internal Server Error'); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); done(); }); @@ -255,7 +252,6 @@ describe('NewgisticsClient.getToken', function() { newgisticsClient.getToken(function(err, token) { assert(err); - assert.strictEqual(token, undefined); done(); @@ -269,8 +265,8 @@ describe('NewgisticsClient.getToken', function() { newgisticsClient.getToken(function(err, token) { assert(err); - assert.strictEqual(err.message, 'invalid_client'); + assert.strictEqual(err.status, 400); assert.strictEqual(token, undefined); done(); @@ -287,8 +283,8 @@ describe('NewgisticsClient.getToken', function() { newgisticsClient.getToken(function(err) { assert(err); - - assert.strictEqual(err.message, '500 POST https://httpstat.us/500#/connect/token 500 Internal Server Error'); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); done(); }); @@ -304,7 +300,6 @@ describe('NewgisticsClient.getToken', function() { newgisticsClient.getToken(function(err, token) { assert.ifError(err); - assert(token); assert(token.access_token); assert(token.expires_in); @@ -327,7 +322,6 @@ describe('NewgisticsClient.getToken', function() { newgisticsClient.getToken(function(err, token2) { assert.ifError(err); - assert.deepStrictEqual(token1, token2); done(); @@ -346,7 +340,6 @@ describe('NewgisticsClient.ping', function() { newgisticsClient.ping(function(err, pong) { assert(err); - assert.strictEqual(pong, undefined); done(); @@ -363,8 +356,8 @@ describe('NewgisticsClient.ping', function() { newgisticsClient.ping(function(err) { assert(err); - - assert.strictEqual(err.message, '500 GET https://httpstat.us/500#/ping 500 Internal Server Error'); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); done(); }); @@ -390,12 +383,17 @@ describe('NewgisticsClient.voidTracking', function() { // Clear existing token cache.del('newgistics-client-token'); - var newgisticsClient = new NewgisticsClient({ + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: 'invalid', + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, shippingapi_url: 'invalid' }); - newgisticsClient.voidTracking('invalid', function(err) { + newgisticsClient.voidPackage('invalid', function(err) { assert(err); + assert.strictEqual(err.message, 'invalid_client'); + assert.strictEqual(err.status, 400); done(); }); @@ -403,54 +401,172 @@ describe('NewgisticsClient.voidTracking', function() { it('should return an error', function(done) { // Clear existing token - var newgisticsClient = new NewgisticsClient({ + cache.del('newgistics-client-token'); + + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: 'invalid' + }); + + newgisticsClient.voidPackage('invalid', function(err) { + assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/packages/invalid/void"'); + + done(); + }); + }); + + it('should return an error for an invalid package ID', function(done) { + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: process.env.NEWGISTICS_SHIPPINGAPI_URL + }); + + newgisticsClient.voidPackage('invalid', function(err) { + assert(err); + assert.strictEqual(err.status, 400); + + done(); + }); + }); + + it('should void tracking', function(done) { + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, client_id: process.env.NEWGISTICS_CLIENT_ID, client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: process.env.NEWGISTICS_SHIPPINGAPI_URL + }); + + const package = { + classOfService: 'Ground', + clientFacilityId: process.env.NEWGISTICS_CLIENT_FACILITY_ID, + dimensions: { + height: { + measurementValue: '1', + unitOfMeasure: 'Inches' + }, + isRectangular: true, + length: { + measurementValue: '6', + unitOfMeasure: 'Inches' + }, + width: { + measurementValue: '4', + unitOfMeasure: 'Inches' + } + }, + ngsFacilityId: process.env.NEWGISTICS_FACILITY_ID, + pricePackage: true, + returnAddress: { + address1: '4717 Plano Parkway', + address2: 'Suite 130', + city: 'Carrollton', + country: 'US', + isResidential: false, + name: 'A Mediocre Corporation', + postalCode: '75010', + stateOrProvince: 'TX' + }, + shipToAddress: { + address1: '5531 Willis Ave', + city: 'Dallas', + country: 'US', + name: 'Joe User', + postalCode: '75206', + stateOrProvince: 'TX' + }, + weight: { + measurementValue: '0.5', + unitOfMeasure: 'Pounds' + } + }; + + newgisticsClient.createPackage(package, function(err, package) { + assert.ifError(err); + + newgisticsClient.voidPackage(package.packageId, function(err) { + assert.ifError(err); + + done(); + }); + }); + }); +}); + +describe('NewgisticsClient.voidTracking', function() { + this.timeout(5000); + + it('should return an error', function(done) { + // Clear existing token + cache.del('newgistics-client-token'); + + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: 'invalid', + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, shippingapi_url: 'invalid' }); newgisticsClient.voidTracking('invalid', function(err) { assert(err); + assert.strictEqual(err.message, 'invalid_client'); + assert.strictEqual(err.status, 400); done(); }); }); it('should return an error', function(done) { - var newgisticsClient = new NewgisticsClient({ + // Clear existing token + cache.del('newgistics-client-token'); + + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, client_id: process.env.NEWGISTICS_CLIENT_ID, - client_secret: process.env.NEWGISTICS_CLIENT_SECRET + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: 'invalid' }); - newgisticsClient.voidTracking('\n', function(err) { + newgisticsClient.voidTracking('invalid', function(err) { assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/packages/trackingId/invalid/void"'); done(); }); }); - it('should not return an error for an invalid tracking number', function(done) { - var newgisticsClient = new NewgisticsClient({ + it('should return an error for an invalid tracking number', function(done) { + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, client_id: process.env.NEWGISTICS_CLIENT_ID, - client_secret: process.env.NEWGISTICS_CLIENT_SECRET + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: process.env.NEWGISTICS_SHIPPINGAPI_URL }); newgisticsClient.voidTracking('invalid', function(err) { - assert.ifError(err); + assert(err); + assert.strictEqual(err.status, 404); done(); }); }); it('should void tracking', function(done) { - var newgisticsClient = new NewgisticsClient({ + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, client_id: process.env.NEWGISTICS_CLIENT_ID, - client_secret: process.env.NEWGISTICS_CLIENT_SECRET + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: process.env.NEWGISTICS_SHIPPINGAPI_URL }); - var package = { + const package = { classOfService: 'Ground', - clientFacilityId: '8448', + clientFacilityId: process.env.NEWGISTICS_CLIENT_FACILITY_ID, dimensions: { height: { measurementValue: '1', @@ -466,7 +582,7 @@ describe('NewgisticsClient.voidTracking', function() { unitOfMeasure: 'Inches' } }, - ngsFacilityId: '1361', + ngsFacilityId: process.env.NEWGISTICS_FACILITY_ID, pricePackage: true, returnAddress: { address1: '4717 Plano Parkway', @@ -494,8 +610,6 @@ describe('NewgisticsClient.voidTracking', function() { newgisticsClient.createPackage(package, function(err, package) { assert.ifError(err); - assert.notStrictEqual(package.trackingId, null); - assert(package.trackingId.length); newgisticsClient.voidTracking(package.trackingId, function(err) { assert.ifError(err); From bf2a3bcfad494e45b279ca90397a149dbac456f8 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Wed, 9 Jan 2019 23:52:41 -0600 Subject: [PATCH 11/12] tests --- client.js | 4 +++- test/client.js | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index 194b760..83aa074 100644 --- a/client.js +++ b/client.js @@ -129,7 +129,7 @@ function NewgisticsClient(args) { } if (res.statusCode !== 200) { - return callback(createError(res.statusCode, body && body.error && body.error.message)); + return callback(createError(res.statusCode, res.body)); } callback(null, body); @@ -146,6 +146,7 @@ function NewgisticsClient(args) { auth: { bearer: token.access_token }, + json: {}, method: 'POST', url: `${opts.shippingapi_url}/v1/packages/${packageId}/void` }; @@ -174,6 +175,7 @@ function NewgisticsClient(args) { auth: { bearer: token.access_token }, + json: {}, method: 'POST', url: `${opts.shippingapi_url}/v1/packages/trackingId/${trackingNumber}/void` }; diff --git a/test/client.js b/test/client.js index ded68c5..22fcd21 100644 --- a/test/client.js +++ b/test/client.js @@ -356,7 +356,7 @@ describe('NewgisticsClient.ping', function() { newgisticsClient.ping(function(err) { assert(err); - assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.message, '500 Internal Server Error'); assert.strictEqual(err.status, 500); done(); @@ -376,7 +376,7 @@ describe('NewgisticsClient.ping', function() { }); }); -describe('NewgisticsClient.voidTracking', function() { +describe('NewgisticsClient.voidPackage', function() { this.timeout(5000); it('should return an error', function(done) { @@ -418,6 +418,23 @@ describe('NewgisticsClient.voidTracking', function() { }); }); + it('should return an error for non 200 status code', function(done) { + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: 'https://httpstat.us/500#' + }); + + newgisticsClient.voidPackage('abc', function(err) { + assert(err); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); + + done(); + }); + }); + it('should return an error for an invalid package ID', function(done) { const newgisticsClient = new NewgisticsClient({ authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, @@ -540,6 +557,23 @@ describe('NewgisticsClient.voidTracking', function() { }); }); + it('should return an error for non 200 status code', function(done) { + const newgisticsClient = new NewgisticsClient({ + authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, + client_id: process.env.NEWGISTICS_CLIENT_ID, + client_secret: process.env.NEWGISTICS_CLIENT_SECRET, + shippingapi_url: 'https://httpstat.us/500#' + }); + + newgisticsClient.voidTracking('abc', function(err) { + assert(err); + assert.strictEqual(err.message, 'Internal Server Error'); + assert.strictEqual(err.status, 500); + + done(); + }); + }); + it('should return an error for an invalid tracking number', function(done) { const newgisticsClient = new NewgisticsClient({ authapi_url: process.env.NEWGISTICS_AUTHAPI_URL, From 2ceb64fbb405dd3f3c12a1ebd5f6a49ee64dfd24 Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Thu, 10 Jan 2019 00:05:15 -0600 Subject: [PATCH 12/12] Update client.js --- test/client.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/client.js b/test/client.js index 22fcd21..45d9734 100644 --- a/test/client.js +++ b/test/client.js @@ -446,6 +446,7 @@ describe('NewgisticsClient.voidPackage', function() { newgisticsClient.voidPackage('invalid', function(err) { assert(err); assert.strictEqual(err.status, 400); + assert.strictEqual(err.message, 'Please provide valid PackageId.'); done(); }); @@ -582,9 +583,10 @@ describe('NewgisticsClient.voidTracking', function() { shippingapi_url: process.env.NEWGISTICS_SHIPPINGAPI_URL }); - newgisticsClient.voidTracking('invalid', function(err) { + newgisticsClient.voidTracking('\n', function(err) { assert(err); - assert.strictEqual(err.status, 404); + assert.strictEqual(err.status, 400); + assert.strictEqual(err.message, 'Please provide valid Tracking Id.'); done(); });