Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added unit test cases for Autorefresh
  • Loading branch information
sthangavel committed Oct 21, 2019
commit 334017cc230ee16846b84116ceed6e1f54b2ff5f
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Follow the instructions below to use the library :
* `sandbox` - for authorizing in sandbox.
* `production` - for authorizing in production.
* `redirectUri` - redirectUri on your app to get the `authorizationCode` from Intuit Servers. Required
* `autoRefresh` - by default, autoRefresh is disabled i.e `false`. To enable provide `true`. If set to true, authclient would refresh the tokens every `autoRefreshInterval` specified so clients do not have to manually call refresh after the first `createToken` call. Optional.
* `autoRefresh` - by default, autoRefresh is disabled i.e `false`. To enable provide `true`. If set to true, authclient would refresh the tokens every `autoRefreshInterval` specified so clients do not have to manually call refresh after the first successful `createToken` call. Optional.
* `autoRefreshInterval` - only if `autoRefresh` is set to true, config will use `autoRefreshInterval`. It is set in seconds. If user does not specify a value but has set `autoRefresh` as true, it would default to 55 minutes. Use `stopAutoRefresh` method on OAuthClient anytime to stop auto Refresh. Optional.
* `logging` - by default, logging is disabled i.e `false`. To enable provide`true`. Optional.

Expand Down
2 changes: 1 addition & 1 deletion src/OAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function OAuthClient(config) {
this.clientId = config.clientId;
this.clientSecret = config.clientSecret;
this.redirectUri = config.redirectUri;
this.autoRefresh = config.autoRefresh;
this.autoRefresh = config.autoRefresh || false;
// If autoRefreshInterval is not choosen by user, defaults to 3300 seconds (55 minutes) since tokens expire after 60 minutes.
this.autoRefreshInterval = config.autoRefreshInterval || 3300;
this.token = new Token(config.token);
Expand Down
196 changes: 196 additions & 0 deletions test/OAuthClientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,205 @@ const oauthClient = new OAuthClientTest({
logging: false,
});

const oauthClientWithAutoRefresh = new OAuthClientTest({
clientId: 'clientID',
clientSecret: 'clientSecret',
environment: 'sandbox',
redirectUri: 'http://localhost:8000/callback',
autoRefresh: true,
logging: false,
});

const oauthClientWithAutoRefreshAndInterval = new OAuthClientTest({
clientId: 'clientID',
clientSecret: 'clientSecret',
environment: 'sandbox',
redirectUri: 'http://localhost:8000/callback',
autoRefresh: true,
autoRefreshInterval: 3,
logging: false,
});

const { expect } = chai;
chai.use(chaiAsPromised);

describe('Tests for AutoRefresh', () => {
it('Checks if autoRefresh configs are set right', () => {
expect(oauthClient.autoRefresh).to.equal(false);
expect(oauthClient.autoRefreshInterval).to.equal(3300);
expect(oauthClientWithAutoRefresh.autoRefresh).to.equal(true);
expect(oauthClientWithAutoRefresh.autoRefreshInterval).to.equal(3300);
expect(oauthClientWithAutoRefreshAndInterval.autoRefresh).to.equal(true);
expect(oauthClientWithAutoRefreshAndInterval.autoRefreshInterval).to.equal(3);
});

// Test AutoRefresh when set
describe('AutoRefresh Happy path tests', () => {
before(() => {
nock('https://oauth.platform.intuit.com').persist()
.post('/oauth2/v1/tokens/bearer')
.reply(200, expectedTokenResponse, {
'content-type': 'application/json',
'content-length': '1636',
connection: 'close',
server: 'nginx',
intuit_tid: '12345-123-1234-12345',
'cache-control': 'no-cache, no-store',
pragma: 'no-cache',
});
});

it('Test if setIntervalHandle is set', () => {
const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';
oauthClient.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);
expect(oauthClient.autoRefreshHandle).to.equal(undefined);
});

oauthClientWithAutoRefresh.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);
expect(oauthClientWithAutoRefresh.autoRefreshHandle).to.not.equal(undefined);
});

oauthClientWithAutoRefreshAndInterval.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);
expect(oauthClientWithAutoRefreshAndInterval.autoRefreshHandle).to.not.equal(undefined);
});
});
});

// Test if setInterval calls refreshToken
describe('Test if setInterval calls refreshToken', () => {
before(() => {
this.clock = sinon.useFakeTimers();
nock('https://oauth.platform.intuit.com').persist()
.post('/oauth2/v1/tokens/bearer')
.reply(200, expectedTokenResponse, {
'content-type': 'application/json',
'content-length': '1636',
connection: 'close',
server: 'nginx',
intuit_tid: '12345-123-1234-12345',
'cache-control': 'no-cache, no-store',
pragma: 'no-cache',
});
});

after(() => {
this.clock.restore();
});

it('Verify setInterval fires at autoRefreshInterval with default 55 minutes', () => {
var refreshCallSpy = sinon.spy(oauthClientWithAutoRefresh, "refresh");

const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';
oauthClientWithAutoRefresh.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);

// // At time 0, we don't expect the function to have been called.
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock 50 minutes
this.clock.tick(50 * 60 * 1000);
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock again (6 minutes)
this.clock.tick(6 * 60 * 1000);
expect(refreshCallSpy.calledOnce).to.be.true;

// Advance clock again (55 minutes)
this.clock.tick(55 * 60 * 1000);
expect(refreshCallSpy.calledTwice).to.be.true;
});
});

it('Verify setInterval fires at autoRefreshInterval with userSet minutes', () => {
var refreshCallSpy = sinon.spy(oauthClientWithAutoRefreshAndInterval, "refresh");

const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';
oauthClientWithAutoRefreshAndInterval.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);

// // At time 0, we don't expect the function to have been called.
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock 3 seconds
this.clock.tick(3 * 1000);
expect(refreshCallSpy.calledOnce).to.be.true;

// Advance clock again (3 seconds)
this.clock.tick(3 * 1000);
expect(refreshCallSpy.calledTwice).to.be.true;
});
});

it('Test stopAutoRefresh for default values', () => {
var refreshCallSpy = sinon.spy(oauthClientWithAutoRefresh, "refresh");
const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';

oauthClientWithAutoRefresh.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);

//Stop auto fresh
oauthClientWithAutoRefresh.stopAutoRefresh()

// // At time 0, we don't expect the function to have been called.
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock 50 minutes
this.clock.tick(50 * 60 * 1000);
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock again (6 minutes)
this.clock.tick(6 * 60 * 1000);
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock again (55 minutes)
this.clock.tick(55 * 60 * 1000);
expect(refreshCallSpy.calledOnce).to.not.be.true;
});
});

it('Test stopAutoRefresh for user set values', () => {
var refreshCallSpy = sinon.spy(oauthClientWithAutoRefreshAndInterval, "refresh");
const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';

oauthClientWithAutoRefreshAndInterval.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);

//Stop auto fresh
oauthClientWithAutoRefreshAndInterval.stopAutoRefresh()

// // At time 0, we don't expect the function to have been called.
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock 3 seconds
this.clock.tick(3 * 1000);
expect(refreshCallSpy.calledOnce).to.not.be.true;

// Advance clock again (3 seconds)
this.clock.tick(3 * 1000);
expect(refreshCallSpy.calledTwice).to.not.be.true;
});
});

});
});

describe('Tests for OAuthClient', () => {
it('Creates a new access token instance', () => {
const accessToken = oauthClient.getToken();
Expand Down