Skip to content

Commit

Permalink
fixes flakey tests related to autoRenew (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongranick-okta authored Aug 14, 2020
1 parent 596be9a commit fbf2d26
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
3 changes: 3 additions & 0 deletions jest.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
'moduleNameMapper': {
'^@okta/okta-auth-js$': OktaAuth
},
'setupFiles': [
'<rootDir>/test/support/nodeExceptions.js'
],
'testMatch': [
'**/test/spec/*.{js,ts}'
],
Expand Down
2 changes: 1 addition & 1 deletion lib/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export class TokenManager {

const onTokenExpiredHandler = (key) => {
if (options.autoRenew) {
this.renew(key);
this.renew(key).catch(() => {}); // Renew errors will emit an "error" event
} else {
this.remove(key);
}
Expand Down
54 changes: 37 additions & 17 deletions test/spec/tokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ describe('TokenManager', function() {
});
afterEach(function() {
jest.useRealTimers();
client.tokenManager.clear(); // clear all timeouts
});
it('should register listener for "expired" event', function() {
jest.spyOn(Emitter.prototype, 'on');
Expand Down Expand Up @@ -752,7 +753,6 @@ describe('TokenManager', function() {
});
});


it('does not return the token after tokens were cleared before renew promise was resolved', function() {
var expiresAt = tokens.standardIdTokenParsed.expiresAt;
return oauthUtil.setupFrame({
Expand Down Expand Up @@ -800,7 +800,7 @@ describe('TokenManager', function() {
});
});

it('Emits an "error" event on OAuth failure', function() {
it('Emits an "error" event on OAuth failure', function(done) {
setupSync({
tokenManager: {
autoRenew: true
Expand All @@ -815,11 +815,15 @@ describe('TokenManager', function() {
accessToken: false
};
var errorEventCallback = jest.fn().mockImplementation(function(err) {
util.expectErrorToEqual(err, error);
try {
util.expectErrorToEqual(err, error);
} catch (e) {
done.fail(e);
}
});
client.tokenManager.on('error', errorEventCallback);

return oauthUtil.setupFrame({
oauthUtil.setupFrame({
authClient: client,
autoRenew: true,
willFail: true,
Expand All @@ -839,31 +843,41 @@ describe('TokenManager', function() {
util.expectErrorToEqual(err, error);
oauthUtil.expectTokenStorageToEqual(localStorage, {});
expect(errorEventCallback).toHaveBeenCalled();
})
.then(function() {
done();
})
.catch(function(err) {
done.fail(err);
});
});

it('Emits an "error" event on AuthSdkError', function() {
it('Emits an "error" event on AuthSdkError', function(done) {
setupSync({
tokenManager: {
autoRenew: true
}
});
var errorEventCallback = jest.fn().mockImplementation(function(err) {
util.expectErrorToEqual(err, {
name: 'AuthSdkError',
message: 'The request does not match client configuration',
errorCode: 'INTERNAL',
errorSummary: 'The request does not match client configuration',
errorLink: 'INTERNAL',
errorId: 'INTERNAL',
errorCauses: [],
tokenKey: 'test-idToken',
accessToken: false
});
try {
util.expectErrorToEqual(err, {
name: 'AuthSdkError',
message: 'The request does not match client configuration',
errorCode: 'INTERNAL',
errorSummary: 'The request does not match client configuration',
errorLink: 'INTERNAL',
errorId: 'INTERNAL',
errorCauses: [],
tokenKey: 'test-idToken',
accessToken: false
});
} catch (e) {
done.fail(e);
}
});
client.tokenManager.on('error', errorEventCallback);

return oauthUtil.setupFrame({
oauthUtil.setupFrame({
authClient: client,
autoRenew: true,
willFail: true,
Expand Down Expand Up @@ -895,6 +909,12 @@ describe('TokenManager', function() {
oauthUtil.expectTokenStorageToEqual(localStorage, {});

expect(errorEventCallback).toHaveBeenCalled();
})
.then(function() {
done();
})
.catch(function(err) {
done.fail(err);
});
});

Expand Down
5 changes: 5 additions & 0 deletions test/support/nodeExceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Catch unhandled promise rejections. These should never happen.
// If you see one of these, debug the test and set a breakpoint below.
process.on('unhandledRejection', error => {
console.log('FLAKEY TEST or CODE! unhandledRejection', error);
});
8 changes: 8 additions & 0 deletions test/support/oauthUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ oauthUtil.setup = function(opts) {
authClient = opts.authClient;
} else {
authClient = new OktaAuth({
tokenManager: {
autoRenew: false,
},
pkce: false,
issuer: 'https://auth-js-test.okta.com'
});
Expand Down Expand Up @@ -248,6 +251,11 @@ oauthUtil.setup = function(opts) {
console.error(err); // eslint-disable-line
expect('not to be hit').toBe(true);
}
})
.finally(function() {
if (authClient && authClient._tokenQueue) {
expect(authClient._tokenQueue.queue.length).toBe(0);
}
});
};

Expand Down
5 changes: 4 additions & 1 deletion test/support/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ function setup(options) {
issuer: options.issuer,
transformErrorXHR: options.transformErrorXHR,
headers: options.headers,
ignoreSignature: options.bypassCrypto === true
ignoreSignature: options.bypassCrypto === true,
tokenManager: {
autoRenew: false
}
});

// 3. Initialize status if passed in
Expand Down

0 comments on commit fbf2d26

Please sign in to comment.