Skip to content

Commit

Permalink
fix: environment variables usage and profile name check in configure …
Browse files Browse the repository at this point in the history
…command
  • Loading branch information
pbheemag committed Feb 21, 2020
1 parent f2adc41 commit 9879b8f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 74 deletions.
27 changes: 11 additions & 16 deletions lib/controllers/authorization-controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = class AuthorizationController {
}

/**
* Generates Authroization URL.
* Generates Authorization URL.
*/
getAuthorizeUrl() {
return this.oauthClient.generateAuthorizeUrl();
Expand Down Expand Up @@ -57,14 +57,20 @@ module.exports = class AuthorizationController {
return callback(messages.ASK_ENV_VARIABLES_ERROR_MESSAGE);
}
if (isNonBlankRefreshToken) {
this._getRefreshTokenAndUpdateConfig(profile, (err, token) => callback(err, err === null ? token : err));
this.oauthClient.refreshToken({ refresh_token: askRefreshToken }, (err, token) => {
if (err) {
return callback(err);
}
callback(null, token.access_token);
});
} else if (isNonBlankAccessToken) {
return callback(null, askAccessToken);
}
} else if (this.oauthClient.isValidToken(AppConfig.getInstance().getToken(profile))) {
callback(null, AppConfig.getInstance().getToken(profile).access_token);
} else {
this._getRefreshTokenAndUpdateConfig(profile, (err, token) => callback(err, err === null ? token : err));
this._getRefreshTokenAndUpdateConfig(profile, AppConfig.getInstance().getToken(profile),
(err, token) => callback(err, err === null ? token : err));
}
}

Expand All @@ -74,8 +80,8 @@ module.exports = class AuthorizationController {
* @param {Function} callback
* @private
*/
_getRefreshTokenAndUpdateConfig(profile, callback) {
this._getRefreshToken(profile, (err, refreshedAccessToken) => {
_getRefreshTokenAndUpdateConfig(profile, token, callback) {
this.oauthClient.refreshToken(token, (err, refreshedAccessToken) => {
if (err) {
return callback(err);
}
Expand All @@ -85,17 +91,6 @@ module.exports = class AuthorizationController {
});
}

/**
* Helper method to call refreshToken.
* @param {String} profile | current profile.
* @param {Function} callback
* @private
*/
_getRefreshToken(profile, callback) {
const token = AppConfig.getInstance().getToken(profile);
this.oauthClient.refreshToken(token, (err, refreshedAccessToken) => callback(err, err == null ? refreshedAccessToken : err));
}

/**
* Helper method to keep listening to LWA response.
* @param {Function} callback
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module.exports.LWA = {
};

module.exports.REGEX_VALIDATIONS = {
PROFILE_NAME: /^[a-zA-Z0-9-_]+$/g
PROFILE_NAME: /(^[a-zA-Z0-9-_]+$)(?!__ENVIRONMENT_ASK_PROFILE__)/g
};

module.exports.COMMAND = {
Expand Down
75 changes: 18 additions & 57 deletions test/unit/controller/authorization-controller/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Controller test - Authorization controller test', () => {
const DEFAULT_SCOPE = CONSTANTS.LWA.DEFAULT_SCOPES;
const authorizePath = CONSTANTS.LWA.DEFAULT_AUTHORIZE_PATH;
const authorizeHost = CONSTANTS.LWA.DEFAULT_AUTHORIZE_HOST;
const TEST_TOKEN = 'testToken';
const TEST_STATE = 'state';
const TEST_PROFILE = 'testProfile';
const TEST_ENVIRONMENT_PROFILE = CONSTANTS.PLACEHOLDER.ENVIRONMENT_VAR.PROFILE_NAME;
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('Controller test - Authorization controller test', () => {
sinon.stub(AppConfig, 'getInstance').returns({
getToken: getTokenStub
});
sinon.stub(httpClient, 'request');
});

describe('# returns valid token', () => {
Expand All @@ -138,7 +140,7 @@ describe('Controller test - Authorization controller test', () => {

it('| non-environment profile, expired access token', (done) => {
// setup
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(1, null, VALID_ACCESS_TOKEN);
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(2, null, VALID_ACCESS_TOKEN);
getTokenStub.withArgs(TEST_PROFILE).returns(TEST_RESPONSE.body);

// call
Expand Down Expand Up @@ -166,13 +168,14 @@ describe('Controller test - Authorization controller test', () => {
it('| environment profile, valid refresh token', (done) => {
// setup
process.env.ASK_REFRESH_TOKEN = TEST_ENV_REFRESH_TOKEN;
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(1, null, VALID_ACCESS_TOKEN);
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(2, null, VALID_ACCESS_TOKEN);
httpClient.request.callsArgWith(3, null, TEST_RESPONSE);

// call
authorizationController.tokenRefreshAndRead(TEST_ENVIRONMENT_PROFILE, (error, accessToken) => {
// verify
expect(error).eq(null);
expect(accessToken).to.deep.eq(VALID_ACCESS_TOKEN);
expect(accessToken).to.deep.eq(TEST_RESPONSE.body.access_token);
done();
});
});
Expand All @@ -191,6 +194,7 @@ describe('Controller test - Authorization controller test', () => {
});

describe('# returns error', () => {

afterEach(() => {
sinon.restore();
delete process.env.ASK_REFRESH_TOKEN;
Expand All @@ -199,7 +203,7 @@ describe('Controller test - Authorization controller test', () => {

it('| non-environment profile, expired access token, _getRefreshTokenAndUpdateConfig fails', (done) => {
// setup
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(1, TEST_ERROR_MESSAGE);
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(2, TEST_ERROR_MESSAGE);
getTokenStub.withArgs(TEST_PROFILE).returns(TEST_RESPONSE.body);

// call
Expand All @@ -211,16 +215,17 @@ describe('Controller test - Authorization controller test', () => {
});
});

it('| environment profile, valid refresh token, _getRefreshTokenAndUpdateConfig fails', (done) => {
it('| environment profile, valid refresh token, refreshing token fails', (done) => {
// setup
process.env.ASK_REFRESH_TOKEN = TEST_ENV_REFRESH_TOKEN;
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(1, TEST_ERROR_MESSAGE);
sinon.stub(AuthorizationController.prototype, '_getRefreshTokenAndUpdateConfig').callsArgWith(2, null, VALID_ACCESS_TOKEN);
httpClient.request.callsArgWith(3, TEST_ERROR_MESSAGE);

// call
authorizationController.tokenRefreshAndRead(TEST_ENVIRONMENT_PROFILE, (error, response) => {
authorizationController.tokenRefreshAndRead(TEST_ENVIRONMENT_PROFILE, (error, accessToken) => {
// verify
expect(error).eq(TEST_ERROR_MESSAGE);
expect(response).to.deep.eq(TEST_ERROR_MESSAGE);
expect(accessToken).eq(undefined);
done();
});
});
Expand Down Expand Up @@ -248,6 +253,7 @@ describe('Controller test - Authorization controller test', () => {
setToken: setTokenStub,
write: writeStub
});
sinon.stub(httpClient, 'request');
});

afterEach(() => {
Expand All @@ -256,10 +262,10 @@ describe('Controller test - Authorization controller test', () => {

it('| returns valid access token and updates config', (done) => {
// setup
sinon.stub(AuthorizationController.prototype, '_getRefreshToken').callsArgWith(1, null, TEST_RESPONSE.body);
httpClient.request.callsArgWith(3, null, TEST_RESPONSE);

// call
authorizationController._getRefreshTokenAndUpdateConfig(TEST_PROFILE, (error, accessToken) => {
authorizationController._getRefreshTokenAndUpdateConfig(TEST_PROFILE, TEST_TOKEN, (error, accessToken) => {
// verify
expect(setTokenStub.args[0][0]).eq(TEST_PROFILE);
expect(setTokenStub.args[0][1]).to.deep.eq(TEST_RESPONSE.body);
Expand All @@ -269,60 +275,15 @@ describe('Controller test - Authorization controller test', () => {
});
});

it('| returns error', (done) => {
// setup
sinon.stub(AuthorizationController.prototype, '_getRefreshToken').callsArgWith(1, TEST_ERROR_MESSAGE);

// call
authorizationController._getRefreshTokenAndUpdateConfig(TEST_PROFILE, (error, response) => {
// verify
expect(error).eq(TEST_ERROR_MESSAGE);
expect(response).eq(undefined);
done();
});
});
});

describe('# test _getRefreshToken', () => {
let getTokenStub;
const authorizationController = new AuthorizationController(TEST_CONFIG);

beforeEach(() => {
sinon.stub(httpClient, 'request');
getTokenStub = sinon.stub();
sinon.stub(AppConfig, 'getInstance').returns({
getToken: getTokenStub
});
});

afterEach(() => {
sinon.restore();
});

it('| returns valid access token', (done) => {
// setup
httpClient.request.callsArgWith(3, null, TEST_RESPONSE);
getTokenStub.withArgs(TEST_PROFILE).returns(TEST_RESPONSE.body);

// call
authorizationController._getRefreshToken(TEST_PROFILE, (error, accessToken) => {
// verify
expect(error).eq(null);
expect(accessToken).to.deep.eq(TEST_RESPONSE.body);
done();
});
});

it('| returns error', (done) => {
// setup
httpClient.request.callsArgWith(3, TEST_ERROR_MESSAGE);
getTokenStub.withArgs(TEST_PROFILE).returns(TEST_RESPONSE.body);

// call
authorizationController._getRefreshToken(TEST_PROFILE, (error, response) => {
authorizationController._getRefreshTokenAndUpdateConfig(TEST_PROFILE, TEST_TOKEN, (error, response) => {
// verify
expect(error).eq(TEST_ERROR_MESSAGE);
expect(response).eq(TEST_ERROR_MESSAGE);
expect(response).eq(undefined);
done();
});
});
Expand Down
5 changes: 5 additions & 0 deletions test/unit/utils/string-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ describe('Utils test - string utility', () => {
testCase: 'input value is a valid profile name',
value: 'askProfile',
expectation: true
},
{
testCase: 'input value is environment profile name',
value: '__ENVIRONMENT_ASK_PROFILE__',
expectation: false
}

].forEach(({ testCase, value, expectation }) => {
Expand Down

0 comments on commit 9879b8f

Please sign in to comment.