From 749cadc8d5fae11435ab65ad39728d20ffa0630c Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Sat, 31 Jul 2021 22:54:57 -0500 Subject: [PATCH] Update tests --- test/index.js | 128 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 5 deletions(-) diff --git a/test/index.js b/test/index.js index 8b50c60..935f4ac 100644 --- a/test/index.js +++ b/test/index.js @@ -9,7 +9,6 @@ describe('PitneyBowes.createShipment', function() { this.timeout(5000); beforeEach(function() { - // Clear existing OAuth tokens cache.clear(); }); @@ -28,6 +27,33 @@ describe('PitneyBowes.createShipment', function() { }); }); + it('should return an error for invalid baseUrl', function(done) { + var pitneyBowes = new PitneyBowes({ + api_key: process.env.API_KEY, + api_secret: process.env.API_SECRET + }); + + pitneyBowes.getOAuthToken(function(err, token) { + assert.ifError(err); + + pitneyBowes = new PitneyBowes({ + baseUrl: 'invalid' + }); + + // Update cache + cache.put('invalid/oauth/token', token, token.expiresIn * 1000 / 2); + + pitneyBowes.createShipment({}, {}, function(err, shipment) { + assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/shipments"'); + assert.strictEqual(err.status, undefined); + assert.strictEqual(shipment, undefined); + + done(); + }); + }); + }); + it('should return an error for non 200 status code', function(done) { var pitneyBowes = new PitneyBowes({ api_key: process.env.API_KEY, @@ -134,7 +160,6 @@ describe('PitneyBowes.getOAuthToken', function() { this.timeout(5000); beforeEach(function() { - // Clear existing OAuth tokens cache.clear(); }); @@ -214,7 +239,6 @@ describe('PitneyBowes.rate', function() { this.timeout(5000); beforeEach(function() { - // Clear existing OAuth tokens cache.clear(); }); @@ -233,6 +257,33 @@ describe('PitneyBowes.rate', function() { }); }); + it('should return an error for invalid baseUrl', function(done) { + var pitneyBowes = new PitneyBowes({ + api_key: process.env.API_KEY, + api_secret: process.env.API_SECRET + }); + + pitneyBowes.getOAuthToken(function(err, token) { + assert.ifError(err); + + pitneyBowes = new PitneyBowes({ + baseUrl: 'invalid' + }); + + // Update cache + cache.put('invalid/oauth/token', token, token.expiresIn * 1000 / 2); + + pitneyBowes.rate({}, {}, function(err, shipment) { + assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/rates"'); + assert.strictEqual(err.status, undefined); + assert.strictEqual(shipment, undefined); + + done(); + }); + }); + }); + it('should return an error for non 200 status code', function(done) { var pitneyBowes = new PitneyBowes({ api_key: process.env.API_KEY, @@ -318,7 +369,6 @@ describe('PitneyBowes.tracking', function() { this.timeout(5000); beforeEach(function() { - // Clear existing OAuth tokens cache.clear(); }); @@ -337,6 +387,33 @@ describe('PitneyBowes.tracking', function() { }); }); + it('should return an error for invalid baseUrl', function(done) { + var pitneyBowes = new PitneyBowes({ + api_key: process.env.API_KEY, + api_secret: process.env.API_SECRET + }); + + pitneyBowes.getOAuthToken(function(err, token) { + assert.ifError(err); + + pitneyBowes = new PitneyBowes({ + baseUrl: 'invalid' + }); + + // Update cache + cache.put('invalid/oauth/token', token, token.expiresIn * 1000 / 2); + + pitneyBowes.tracking({ trackingNumber: '4206311892612927005269000081323326' }, function(err, data) { + assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/tracking/4206311892612927005269000081323326?packageIdentifierType=TrackingNumber&carrier=USPS"'); + assert.strictEqual(err.status, undefined); + assert.strictEqual(data, undefined); + + done(); + }); + }); + }); + it('should return an error for non 200 status code', function(done) { var pitneyBowes = new PitneyBowes({ api_key: process.env.API_KEY, @@ -426,7 +503,6 @@ describe('PitneyBowes.validateAddress', function() { this.timeout(5000); beforeEach(function() { - // Clear existing OAuth tokens cache.clear(); }); @@ -460,6 +536,48 @@ describe('PitneyBowes.validateAddress', function() { }); }); + it('should return an error an invalid baseUrl', function(done) { + var pitneyBowes = new PitneyBowes({ + api_key: process.env.API_KEY, + api_secret: process.env.API_SECRET + }); + + pitneyBowes.getOAuthToken(function(err, token) { + assert.ifError(err); + + pitneyBowes = new PitneyBowes({ + baseUrl: 'invalid' + }); + + // Update cache + cache.put('invalid/oauth/token', token, token.expiresIn * 1000 / 2); + + const address = { + addressLines: [ + '1600 Pennsylvania Avenue NW' + ], + cityTown: 'Washington', + stateProvince: 'DC', + postalCode: '20500 ', + countryCode: 'US', + company: 'Pitney Bowes Inc.', + name: 'John Doe', + phone: '203-000-0000', + email: 'john.d@example.com', + residential: false + }; + + pitneyBowes.validateAddress({ address }, function(err, data) { + assert(err); + assert.strictEqual(err.message, 'Invalid URI "invalid/v1/addresses/verify?minimalAddressValidation=false"'); + assert.strictEqual(err.status, undefined); + assert.strictEqual(data, undefined); + + done(); + }); + }); + }); + it('should return an error for non 200 status code', function(done) { var pitneyBowes = new PitneyBowes({ api_key: process.env.API_KEY,