Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freshlogic committed Aug 1, 2021
1 parent 10183a5 commit f1c76e5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 120 deletions.
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const crypto = require('crypto');

const cache = require('memory-cache');
const createError = require('http-errors');
const request = require('request');
Expand Down Expand Up @@ -55,13 +53,10 @@ function PitneyBowes(args) {
};

this.getOAuthToken = function(callback) {
// Generate a random cache key
if (!this._oAuthTokenCacheKey) {
this._oAuthTokenCacheKey = crypto.randomBytes(16).toString('hex');
}
const url = `${options.baseUrl.replace('/shippingservices', '')}/oauth/token`;

// Try to get the token from memory cache
const oAuthToken = cache.get(this._oAuthTokenCacheKey);
const oAuthToken = cache.get(url);

if (oAuthToken) {
return callback(null, oAuthToken);
Expand All @@ -76,7 +71,7 @@ function PitneyBowes(args) {
},
json: true,
method: 'POST',
url: `${options.baseUrl.replace('/shippingservices', '')}/oauth/token`
url
};

request(req, function(err, res, body) {
Expand All @@ -89,7 +84,7 @@ function PitneyBowes(args) {
}

// Put the token in memory cache
cache.put(this._oAuthTokenCacheKey, body, body.expiresIn * 1000 / 2);
cache.put(url, body, body.expiresIn * 1000 / 2);

callback(null, body);
});
Expand Down
111 changes: 0 additions & 111 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@ 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) {
assert.ifError(err);

pitneyBowes = new PitneyBowes({
baseUrl: 'invalid'
});

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,
Expand Down Expand Up @@ -257,30 +233,6 @@ 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) {
assert.ifError(err);

pitneyBowes = new PitneyBowes({
baseUrl: 'invalid'
});

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,
Expand Down Expand Up @@ -385,30 +337,6 @@ 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) {
assert.ifError(err);

pitneyBowes = new PitneyBowes({
baseUrl: 'invalid'
});

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,
Expand Down Expand Up @@ -532,45 +460,6 @@ 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) {
assert.ifError(err);

pitneyBowes = new PitneyBowes({
baseUrl: 'invalid'
});

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,
Expand Down

0 comments on commit f1c76e5

Please sign in to comment.