Skip to content

Missing auth Hash causes exception in browserstack-cypress cli #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions bin/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function info(args) {
let bsConfigPath = utils.getConfigPath(args.cf);

return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
utils.setDefaultAuthHash(bsConfig, args);

// accept the username from command line if provided
utils.setUsername(bsConfig, args);

Expand All @@ -30,7 +33,7 @@ module.exports = function info(args) {
password: bsConfig.auth.access_key,
},
headers: {
"User-Agent": utils.getUserAgent(),
'User-Agent': utils.getUserAgent(),
},
};

Expand All @@ -55,7 +58,7 @@ module.exports = function info(args) {

if (resp.statusCode == 299) {
messageType = Constants.messageTypes.INFO;
errorCode = "api_deprecated";
errorCode = 'api_deprecated';

if (build) {
message = build.message;
Expand All @@ -66,14 +69,14 @@ module.exports = function info(args) {
}
} else if (resp.statusCode != 200) {
messageType = Constants.messageTypes.ERROR;
errorCode = "api_failed_build_info";
errorCode = 'api_failed_build_info';

if (build) {
message = `${
Constants.userMessages.BUILD_INFO_FAILED
} with error: \n${JSON.stringify(build, null, 2)}`;
logger.error(message);
if (build.message === "Unauthorized") errorCode = "api_auth_failed";
if (build.message === 'Unauthorized') errorCode = 'api_auth_failed';
} else {
message = Constants.userMessages.BUILD_INFO_FAILED;
logger.error(message);
Expand All @@ -89,7 +92,7 @@ module.exports = function info(args) {
}
}
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
})
});
}).catch(function (err) {
logger.error(err);
utils.setUsageReportingFlag(null, args.disableUsageReporting);
Expand Down
3 changes: 3 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = function run(args) {
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
utils.setDefaultAuthHash(bsConfig,args);

// accept the username from command line or env variable if provided
utils.setUsername(bsConfig, args);

Expand Down
19 changes: 11 additions & 8 deletions bin/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function stop(args) {
let bsConfigPath = utils.getConfigPath(args.cf);

return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
utils.setDefaultAuthHash(bsConfig, args);

// accept the username from command line if provided
utils.setUsername(bsConfig, args);

Expand All @@ -30,7 +33,7 @@ module.exports = function stop(args) {
password: bsConfig.auth.access_key,
},
headers: {
"User-Agent": utils.getUserAgent(),
'User-Agent': utils.getUserAgent(),
},
};

Expand All @@ -46,16 +49,16 @@ module.exports = function stop(args) {

logger.info(message);
} else {
let build = null
let build = null;
try {
build = JSON.parse(body)
build = JSON.parse(body);
} catch (error) {
build = null
build = null;
}

if (resp.statusCode == 299) {
messageType = Constants.messageTypes.INFO;
errorCode = "api_deprecated";
errorCode = 'api_deprecated';

if (build) {
message = build.message;
Expand All @@ -66,14 +69,14 @@ module.exports = function stop(args) {
}
} else if (resp.statusCode != 200) {
messageType = Constants.messageTypes.ERROR;
errorCode = "api_failed_build_stop";
errorCode = 'api_failed_build_stop';

if (build) {
message = `${
Constants.userMessages.BUILD_STOP_FAILED
} with error: \n${JSON.stringify(build, null, 2)}`;
logger.error(message);
if (build.message === "Unauthorized") errorCode = "api_auth_failed";
if (build.message === 'Unauthorized') errorCode = 'api_auth_failed';
} else {
message = Constants.userMessages.BUILD_STOP_FAILED;
logger.error(message);
Expand All @@ -85,7 +88,7 @@ module.exports = function stop(args) {
}
}
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
})
});
}).catch(function (err) {
logger.error(err);
utils.setUsageReportingFlag(null, args.disableUsageReporting);
Expand Down
10 changes: 10 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ exports.setParallels = (bsConfig, args) => {
}
};

exports.setDefaultAuthHash = (bsConfig, args) => {
if (
this.isUndefined(bsConfig['auth']) &&
(!this.isUndefined(args.username) ||
!this.isUndefined(process.env.BROWSERSTACK_USERNAME))
) {
bsConfig['auth'] = {};
}
}

exports.setUsername = (bsConfig, args) => {
if (!this.isUndefined(args.username)) {
bsConfig["auth"]["username"] = args.username;
Expand Down
76 changes: 49 additions & 27 deletions test/unit/bin/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ describe("buildInfo", () => {
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
setDefaultAuthHashStub = sandbox.stub();
});

afterEach(() => {
Expand All @@ -46,18 +48,20 @@ describe("buildInfo", () => {

let requestStub = sandbox.stub(request, "get").yields(null, { statusCode: 299 }, null);

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand All @@ -82,18 +86,20 @@ describe("buildInfo", () => {
.stub(request, "get")
.yields(null, { statusCode: 299 }, JSON.stringify(body));

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand All @@ -119,11 +125,13 @@ describe("buildInfo", () => {
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
setDefaultAuthHashStub = sandbox.stub();
});

afterEach(() => {
Expand All @@ -140,18 +148,20 @@ describe("buildInfo", () => {
.stub(request, "get")
.yields(null, { statusCode: 400 }, null);

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand Down Expand Up @@ -182,18 +192,20 @@ describe("buildInfo", () => {
.stub(request, "get")
.yields(null, { statusCode: 401 }, JSON.stringify(body_with_message));

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand All @@ -219,18 +231,20 @@ describe("buildInfo", () => {
.stub(request, "get")
.yields(null, { statusCode: 402 }, JSON.stringify(body));

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand Down Expand Up @@ -258,11 +272,13 @@ describe("buildInfo", () => {
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
setDefaultAuthHashStub = sandbox.stub();
});

afterEach(() => {
Expand All @@ -277,18 +293,20 @@ describe("buildInfo", () => {

let requestStub = sandbox.stub(request, "get").yields(null, { statusCode: 200 }, JSON.stringify(body));

const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
request: { get: requestStub },
request: {get: requestStub},
});

validateBstackJsonStub.returns(Promise.resolve(bsConfig));
Expand All @@ -315,10 +333,12 @@ describe("buildInfo", () => {
getConfigPathStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
setDefaultAuthHashStub = sandbox.stub();
});

afterEach(() => {
Expand All @@ -327,15 +347,17 @@ describe("buildInfo", () => {
});

it("send usage report if validateBstackJson fails", () => {
const info = proxyquire("../../../../bin/commands/info", {
"../helpers/utils": {
const info = proxyquire('../../../../bin/commands/info', {
'../helpers/utils': {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
validateBstackJson: validateBstackJsonStub,
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub
setCypressConfigFilename: setCypressConfigFilenameStub,
getConfigPath: getConfigPathStub,
setDefaultAuthHash: setDefaultAuthHashStub
},
});

Expand Down
2 changes: 2 additions & 0 deletions test/unit/bin/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ describe("init", () => {
};

loggerStub = sandbox.stub(logger, 'error');
cypressConfigFileStub = sandbox.stub(utils, 'setCypressConfigFilename');
usageStub = sandbox.stub(utils, 'sendUsageReport');

expect(get_path(args)).to.be.undefined;
sinon.assert.calledOnce(loggerStub);
sinon.assert.calledOnce(cypressConfigFileStub);
sinon.assert.calledOnce(usageStub);
});

Expand Down
Loading