Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrayson committed Mar 21, 2024
1 parent c2c9179 commit 623651b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/authjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const refreshToken = (vars, callback) => {
const opts = json.request(convertAuthConfig(vars));

request(opts, (err, response, body) => {
if (err) return callback(`Error while fetching new tokens: ${err}`, null);
if (err) return callback(`Error while fetching new token: ${err}`, null);

let tokenResponse = {};
try {
Expand Down
129 changes: 124 additions & 5 deletions test/authjson-spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const { assert } = require('chai');
const { requestVariables, validate } = require('../lib/authjson');
const {assert} = require('chai');
const nock = require('nock');
const integration = require('../lib/authjson');
const {requestVariables, validate} = require('../lib/authjson');
const parser = require('leadconduit-integration').test.types.parser(requestVariables());

const baseApiUrl = 'https://api.example.com';
const baseAuthUrl = 'https://auth.example.com';

describe('Token Authenticated', function () {

describe('Request variables', function () {
describe('Request variables', () => {
it('makes credential_id required', function () {
const variables = requestVariables();
const credentials = variables.filter(variable => variable.name === 'credential_id');
Expand All @@ -12,9 +18,9 @@ describe('Token Authenticated', function () {
});
});

describe('Validate', function () {
describe('Validate', () => {
let vars;
beforeEach(function() {
beforeEach(function () {
vars = {
url: 'https://example.com/deliver',
credential_id: 'abc123',
Expand Down Expand Up @@ -44,4 +50,117 @@ describe('Token Authenticated', function () {
assert.equal(validate(vars), 'authentication URL is required');
});
});

describe('Handle', () => {
const successEvent = {
outcome: 'success',
price: 0,
score: 99
};

afterEach(function() {
nock.cleanAll();
});

it('works in one transaction (as with valid token)', (done) => {
nock(baseApiUrl)
.post('/api/examples/42', {campaign_id: 'zzz123'})
.reply(200, {score: 99});

nock(baseAuthUrl)
.post('/login')
.reply(200);

integration.handle(baseVars(), (err, event) => {
assert.isNull(err);
assert.deepEqual(event, successEvent);

// ensure the auth URL wasn't used
const pending = nock.pendingMocks();
assert.equal(pending.length, 1);
assert.isTrue(pending.includes(`POST ${baseAuthUrl}:443/login`));
done();
});
});

it('refreshes the token when it\'s expired', (done) => {
nock(baseApiUrl)
.post('/api/examples/42', {campaign_id: 'zzz123'})
.reply(401);

nock(baseAuthUrl)
.post('/login', { scope: 'mint' })
.matchHeader('authorization', 'Bearer old-token-xyz-789')
.reply(200, { access_token: 'new-token-abc-123' });

nock(baseApiUrl)
.post('/api/examples/42', {campaign_id: 'zzz123'})
.reply(200, {score: 99});

const vars = baseVars();
integration.handle(vars, (err, event) => {
assert.isNull(err);
assert.deepEqual(event, successEvent);
assert.equal(vars.credential.access_token, 'new-token-abc-123');
assert.equal(nock.pendingMocks().length, 0);
done();
});
});

it('only tries to refresh the token once', (done) => {
const errorEvent = {
outcome: 'error',
reason: 'Unable to authenticate after attempting to refresh token'
};

nock(baseApiUrl)
.post('/api/examples/42', {campaign_id: 'zzz123'})
.reply(401);

nock(baseAuthUrl)
.post('/login', { scope: 'mint' })
.matchHeader('authorization', 'Bearer old-token-xyz-789')
.reply(403, { message: 'nope' });

nock(baseApiUrl)
.post('/api/examples/42', {campaign_id: 'zzz123'})
.reply(401);

const vars = baseVars();
integration.handle(vars, (err, event) => {
assert.isNull(err);
assert.deepEqual(event, errorEvent);
assert.isUndefined(vars.credential.access_token);
assert.equal(nock.pendingMocks().length, 0);
done();
});
});
});
});

const baseVars = (custom = {}) => {
const vars = {
lead: {
email: '[email protected]',
phone_1: '5125557777',
},
credential: {
token: 'a-long-lived-api-token',
access_token: 'old-token-xyz-789'
},
url: 'https://api.example.com/api/examples/42',
method: 'post',
json_property: {
campaign_id: 'zzz123'
},
authentication_url: 'https://auth.example.com/login',
authentication_header: {
Authorization: 'Bearer ACCESS_TOKEN'
},
authentication_token_path: 'access_token',
authentication_property: {
scope: 'mint'
}
};
return parser(Object.assign(vars, custom));
};

0 comments on commit 623651b

Please sign in to comment.