Permalink
Browse files

fix(baseConfig): logoutOnInvalidtoken -> logoutOnInvalidToken

The parameter had a typo. I deprecated the old version and added tests to ensure that both versions work fine.
  • Loading branch information...
doktordirk committed Apr 23, 2018
1 parent 3c51df6 commit 16bb54b379ea46c57fa5a55380da9347b49ac77d
Showing with 96 additions and 2 deletions.
  1. +4 −0 gulpfile.js
  2. +20 −1 src/baseConfig.js
  3. +1 −1 src/fetchClientConfig.js
  4. +71 −0 test/fetchClientConfig.spec.js
@@ -7,6 +7,10 @@ require('require-dir')('node_modules/spoonx-tools/build-plugin/tasks');
// the testing express server can be imported and routes added
var app = require('./node_modules/spoonx-tools/build-plugin/tasks/server').app;
// unauthorized test path
app.all('/unauthorized', function(req, res) {
res.sendStatus(401);
});
// default: all routes, all methods
app.all('*', function(req, res) {
@@ -102,7 +102,7 @@ export class BaseConfig {
// The token name used in the header of API requests that require authentication
authTokenType = 'Bearer';
// Logout when the token is invalidated by the server
logoutOnInvalidtoken = false;
logoutOnInvalidToken = false;
// The the property from which to get the access token after a successful login or signup. Can also be dotted eg "accessTokenProp.accessTokenName"
accessTokenProp = 'access_token';
@@ -331,6 +331,10 @@ export class BaseConfig {
* @deprecated
*/
_tokenPrefix = 'aurelia';
/**
* @deprecated
*/
_logoutOnInvalidtoken = false;
/* deprecated methods and parameters */
/**
@@ -430,6 +434,21 @@ export class BaseConfig {
set _current(_) {
throw new Error('Setter BaseConfig._current has been removed. Use BaseConfig directly instead.');
}
/**
* @param {string} logoutOnInvalidtoken
* @deprecated
*/
set logoutOnInvalidtoken(logoutOnInvalidtoken) {
logger.warn('BaseConfig.logoutOnInvalidtoken is obsolete. Use BaseConfig.logoutOnInvalidToken instead.');
this._logoutOnInvalidtoken = logoutOnInvalidtoken;
this.logoutOnInvalidToken = logoutOnInvalidtoken;
return logoutOnInvalidtoken;
}
get logoutOnInvalidtoken() {
return this._logoutOnInvalidtoken;
}
}
/**
@@ -57,7 +57,7 @@ export class FetchConfig {
return reject(response);
}
// logout when server invalidated the authorization token but the token itself is still valid
if (this.config.httpInterceptor && this.config.logoutOnInvalidtoken && !this.authService.isTokenExpired()) {
if (this.config.httpInterceptor && this.config.logoutOnInvalidToken && !this.authService.isTokenExpired()) {
return reject(this.authService.logout());
}
// resolve unexpected authorization errors (not a managed request or token not expired)
@@ -5,6 +5,15 @@ import {Config} from 'aurelia-api';
import {FetchConfig} from '../src/fetchClientConfig';
import {AuthService} from '../src/authService';
const tokenFuture = {
payload: {
name : 'tokenFuture',
admin: true,
exp : '2460017154'
},
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidG9rZW5GdXR1cmUiLCJhZG1pbiI6dHJ1ZSwiZXhwIjoiMjQ2MDAxNzE1NCJ9.iHXLzWGY5U9WwVT4IVRLuKTf65XpgrA1Qq_Jlynv6bc'
};
function getContainer() {
let container = new Container();
let config = container.get(Config);
@@ -255,6 +264,68 @@ describe('FetchConfig', function() {
done();
});
});
it('Should not logout on invalid token (default)', function(done) {
let client = new HttpClient();
client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
fetchConfig.configure(client);
client.fetch('unauthorized')
.then(response => {
expect(authService.isAuthenticated()).toBe(true);
done();
})
.catch(err => {
expect(true).toBe(false);
done();
});
});
it('Should logout on invalid token (logoutOnInvalidToken = true)', function(done) {
let client = new HttpClient();
client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
authService.config.logoutOnInvalidToken = true;
fetchConfig.configure(client);
client.fetch('unauthorized')
.then(response => {
expect(true).toBe(false);
done();
})
.catch(err => {
expect(authService.isAuthenticated()).toBe(false);
done();
});
});
it('Should logout on invalid token (logoutOnInvalidtoken = true) (deprecated)', function(done) {
let client = new HttpClient();
client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
authService.config.logoutOnInvalidtoken = true;
fetchConfig.configure(client);
client.fetch('unauthorized')
.then(response => {
expect(true).toBe(false);
done();
})
.catch(err => {
expect(authService.isAuthenticated()).toBe(false);
done();
});
});
});
authService.accessToken = null;

0 comments on commit 16bb54b

Please sign in to comment.