Skip to content

Commit ac5c1f8

Browse files
Merge pull request #79 from browserstack/missing_auth_hash
Missing auth Hash causes exception in browserstack-cypress cli
2 parents daf2106 + 61a1e9b commit ac5c1f8

File tree

9 files changed

+631
-414
lines changed

9 files changed

+631
-414
lines changed

bin/commands/info.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = function info(args) {
1010
let bsConfigPath = utils.getConfigPath(args.cf);
1111

1212
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
13+
// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
14+
utils.setDefaultAuthHash(bsConfig, args);
15+
1316
// accept the username from command line if provided
1417
utils.setUsername(bsConfig, args);
1518

@@ -30,7 +33,7 @@ module.exports = function info(args) {
3033
password: bsConfig.auth.access_key,
3134
},
3235
headers: {
33-
"User-Agent": utils.getUserAgent(),
36+
'User-Agent': utils.getUserAgent(),
3437
},
3538
};
3639

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

5659
if (resp.statusCode == 299) {
5760
messageType = Constants.messageTypes.INFO;
58-
errorCode = "api_deprecated";
61+
errorCode = 'api_deprecated';
5962

6063
if (build) {
6164
message = build.message;
@@ -66,14 +69,14 @@ module.exports = function info(args) {
6669
}
6770
} else if (resp.statusCode != 200) {
6871
messageType = Constants.messageTypes.ERROR;
69-
errorCode = "api_failed_build_info";
72+
errorCode = 'api_failed_build_info';
7073

7174
if (build) {
7275
message = `${
7376
Constants.userMessages.BUILD_INFO_FAILED
7477
} with error: \n${JSON.stringify(build, null, 2)}`;
7578
logger.error(message);
76-
if (build.message === "Unauthorized") errorCode = "api_auth_failed";
79+
if (build.message === 'Unauthorized') errorCode = 'api_auth_failed';
7780
} else {
7881
message = Constants.userMessages.BUILD_INFO_FAILED;
7982
logger.error(message);
@@ -89,7 +92,7 @@ module.exports = function info(args) {
8992
}
9093
}
9194
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
92-
})
95+
});
9396
}).catch(function (err) {
9497
logger.error(err);
9598
utils.setUsageReportingFlag(null, args.disableUsageReporting);

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module.exports = function run(args) {
1717
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1818
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
1919

20+
// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
21+
utils.setDefaultAuthHash(bsConfig,args);
22+
2023
// accept the username from command line or env variable if provided
2124
utils.setUsername(bsConfig, args);
2225

bin/commands/stop.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = function stop(args) {
1010
let bsConfigPath = utils.getConfigPath(args.cf);
1111

1212
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
13+
// setting setDefaultAuthHash to {} if not present and set via env variables or via args.
14+
utils.setDefaultAuthHash(bsConfig, args);
15+
1316
// accept the username from command line if provided
1417
utils.setUsername(bsConfig, args);
1518

@@ -30,7 +33,7 @@ module.exports = function stop(args) {
3033
password: bsConfig.auth.access_key,
3134
},
3235
headers: {
33-
"User-Agent": utils.getUserAgent(),
36+
'User-Agent': utils.getUserAgent(),
3437
},
3538
};
3639

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

4750
logger.info(message);
4851
} else {
49-
let build = null
52+
let build = null;
5053
try {
51-
build = JSON.parse(body)
54+
build = JSON.parse(body);
5255
} catch (error) {
53-
build = null
56+
build = null;
5457
}
5558

5659
if (resp.statusCode == 299) {
5760
messageType = Constants.messageTypes.INFO;
58-
errorCode = "api_deprecated";
61+
errorCode = 'api_deprecated';
5962

6063
if (build) {
6164
message = build.message;
@@ -66,14 +69,14 @@ module.exports = function stop(args) {
6669
}
6770
} else if (resp.statusCode != 200) {
6871
messageType = Constants.messageTypes.ERROR;
69-
errorCode = "api_failed_build_stop";
72+
errorCode = 'api_failed_build_stop';
7073

7174
if (build) {
7275
message = `${
7376
Constants.userMessages.BUILD_STOP_FAILED
7477
} with error: \n${JSON.stringify(build, null, 2)}`;
7578
logger.error(message);
76-
if (build.message === "Unauthorized") errorCode = "api_auth_failed";
79+
if (build.message === 'Unauthorized') errorCode = 'api_auth_failed';
7780
} else {
7881
message = Constants.userMessages.BUILD_STOP_FAILED;
7982
logger.error(message);
@@ -85,7 +88,7 @@ module.exports = function stop(args) {
8588
}
8689
}
8790
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
88-
})
91+
});
8992
}).catch(function (err) {
9093
logger.error(err);
9194
utils.setUsageReportingFlag(null, args.disableUsageReporting);

bin/helpers/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ exports.setParallels = (bsConfig, args) => {
115115
}
116116
};
117117

118+
exports.setDefaultAuthHash = (bsConfig, args) => {
119+
if (
120+
this.isUndefined(bsConfig['auth']) &&
121+
(!this.isUndefined(args.username) ||
122+
!this.isUndefined(process.env.BROWSERSTACK_USERNAME))
123+
) {
124+
bsConfig['auth'] = {};
125+
}
126+
}
127+
118128
exports.setUsername = (bsConfig, args) => {
119129
if (!this.isUndefined(args.username)) {
120130
bsConfig["auth"]["username"] = args.username;

test/unit/bin/commands/info.js

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ describe("buildInfo", () => {
2727
validateBstackJsonStub = sandbox.stub();
2828
getConfigPathStub = sandbox.stub();
2929
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
30+
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
3031
getUserAgentStub = sandbox.stub().returns("random user-agent");
3132
sendUsageReportStub = sandbox.stub().callsFake(function () {
3233
return "end";
3334
});
3435
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
36+
setDefaultAuthHashStub = sandbox.stub();
3537
});
3638

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

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

49-
const info = proxyquire("../../../../bin/commands/info", {
50-
"../helpers/utils": {
51+
const info = proxyquire('../../../../bin/commands/info', {
52+
'../helpers/utils': {
5153
setUsername: setUsernameStub,
5254
setAccessKey: setAccessKeyStub,
5355
validateBstackJson: validateBstackJsonStub,
5456
getErrorCodeFromErr: getErrorCodeFromErrStub,
5557
sendUsageReport: sendUsageReportStub,
5658
setUsageReportingFlag: setUsageReportingFlagStub,
59+
setCypressConfigFilename: setCypressConfigFilenameStub,
5760
getUserAgent: getUserAgentStub,
58-
getConfigPath: getConfigPathStub
61+
getConfigPath: getConfigPathStub,
62+
setDefaultAuthHash: setDefaultAuthHashStub
5963
},
60-
request: { get: requestStub },
64+
request: {get: requestStub},
6165
});
6266

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

85-
const info = proxyquire("../../../../bin/commands/info", {
86-
"../helpers/utils": {
89+
const info = proxyquire('../../../../bin/commands/info', {
90+
'../helpers/utils': {
8791
setUsername: setUsernameStub,
8892
setAccessKey: setAccessKeyStub,
8993
validateBstackJson: validateBstackJsonStub,
9094
getErrorCodeFromErr: getErrorCodeFromErrStub,
9195
sendUsageReport: sendUsageReportStub,
9296
setUsageReportingFlag: setUsageReportingFlagStub,
97+
setCypressConfigFilename: setCypressConfigFilenameStub,
9398
getUserAgent: getUserAgentStub,
94-
getConfigPath: getConfigPathStub
99+
getConfigPath: getConfigPathStub,
100+
setDefaultAuthHash: setDefaultAuthHashStub
95101
},
96-
request: { get: requestStub },
102+
request: {get: requestStub},
97103
});
98104

99105
validateBstackJsonStub.returns(Promise.resolve(bsConfig));
@@ -119,11 +125,13 @@ describe("buildInfo", () => {
119125
validateBstackJsonStub = sandbox.stub();
120126
getConfigPathStub = sandbox.stub();
121127
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
128+
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
122129
getUserAgentStub = sandbox.stub().returns("random user-agent");
123130
sendUsageReportStub = sandbox.stub().callsFake(function () {
124131
return "end";
125132
});
126133
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
134+
setDefaultAuthHashStub = sandbox.stub();
127135
});
128136

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

143-
const info = proxyquire("../../../../bin/commands/info", {
144-
"../helpers/utils": {
151+
const info = proxyquire('../../../../bin/commands/info', {
152+
'../helpers/utils': {
145153
setUsername: setUsernameStub,
146154
setAccessKey: setAccessKeyStub,
147155
validateBstackJson: validateBstackJsonStub,
148156
getErrorCodeFromErr: getErrorCodeFromErrStub,
149157
sendUsageReport: sendUsageReportStub,
150158
setUsageReportingFlag: setUsageReportingFlagStub,
159+
setCypressConfigFilename: setCypressConfigFilenameStub,
151160
getUserAgent: getUserAgentStub,
152-
getConfigPath: getConfigPathStub
161+
getConfigPath: getConfigPathStub,
162+
setDefaultAuthHash: setDefaultAuthHashStub
153163
},
154-
request: { get: requestStub },
164+
request: {get: requestStub},
155165
});
156166

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

185-
const info = proxyquire("../../../../bin/commands/info", {
186-
"../helpers/utils": {
195+
const info = proxyquire('../../../../bin/commands/info', {
196+
'../helpers/utils': {
187197
setUsername: setUsernameStub,
188198
setAccessKey: setAccessKeyStub,
189199
validateBstackJson: validateBstackJsonStub,
190200
getErrorCodeFromErr: getErrorCodeFromErrStub,
191201
sendUsageReport: sendUsageReportStub,
192202
setUsageReportingFlag: setUsageReportingFlagStub,
203+
setCypressConfigFilename: setCypressConfigFilenameStub,
193204
getUserAgent: getUserAgentStub,
194-
getConfigPath: getConfigPathStub
205+
getConfigPath: getConfigPathStub,
206+
setDefaultAuthHash: setDefaultAuthHashStub
195207
},
196-
request: { get: requestStub },
208+
request: {get: requestStub},
197209
});
198210

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

222-
const info = proxyquire("../../../../bin/commands/info", {
223-
"../helpers/utils": {
234+
const info = proxyquire('../../../../bin/commands/info', {
235+
'../helpers/utils': {
224236
setUsername: setUsernameStub,
225237
setAccessKey: setAccessKeyStub,
226238
validateBstackJson: validateBstackJsonStub,
227239
getErrorCodeFromErr: getErrorCodeFromErrStub,
228240
sendUsageReport: sendUsageReportStub,
229241
setUsageReportingFlag: setUsageReportingFlagStub,
242+
setCypressConfigFilename: setCypressConfigFilenameStub,
230243
getUserAgent: getUserAgentStub,
231-
getConfigPath: getConfigPathStub
244+
getConfigPath: getConfigPathStub,
245+
setDefaultAuthHash: setDefaultAuthHashStub
232246
},
233-
request: { get: requestStub },
247+
request: {get: requestStub},
234248
});
235249

236250
validateBstackJsonStub.returns(Promise.resolve(bsConfig));
@@ -258,11 +272,13 @@ describe("buildInfo", () => {
258272
validateBstackJsonStub = sandbox.stub();
259273
getConfigPathStub = sandbox.stub();
260274
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
275+
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
261276
getUserAgentStub = sandbox.stub().returns("random user-agent");
262277
sendUsageReportStub = sandbox.stub().callsFake(function () {
263278
return "end";
264279
});
265280
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
281+
setDefaultAuthHashStub = sandbox.stub();
266282
});
267283

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

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

280-
const info = proxyquire("../../../../bin/commands/info", {
281-
"../helpers/utils": {
296+
const info = proxyquire('../../../../bin/commands/info', {
297+
'../helpers/utils': {
282298
setUsername: setUsernameStub,
283299
setAccessKey: setAccessKeyStub,
284300
validateBstackJson: validateBstackJsonStub,
285301
getErrorCodeFromErr: getErrorCodeFromErrStub,
286302
sendUsageReport: sendUsageReportStub,
287303
setUsageReportingFlag: setUsageReportingFlagStub,
304+
setCypressConfigFilename: setCypressConfigFilenameStub,
288305
getUserAgent: getUserAgentStub,
289-
getConfigPath: getConfigPathStub
306+
getConfigPath: getConfigPathStub,
307+
setDefaultAuthHash: setDefaultAuthHashStub
290308
},
291-
request: { get: requestStub },
309+
request: {get: requestStub},
292310
});
293311

294312
validateBstackJsonStub.returns(Promise.resolve(bsConfig));
@@ -315,10 +333,12 @@ describe("buildInfo", () => {
315333
getConfigPathStub = sandbox.stub();
316334
validateBstackJsonStub = sandbox.stub();
317335
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
336+
setCypressConfigFilenameStub = sandbox.stub().returns(undefined);
318337
sendUsageReportStub = sandbox.stub().callsFake(function () {
319338
return "end";
320339
});
321340
getErrorCodeFromErrStub = sandbox.stub().returns("random-error");
341+
setDefaultAuthHashStub = sandbox.stub();
322342
});
323343

324344
afterEach(() => {
@@ -327,15 +347,17 @@ describe("buildInfo", () => {
327347
});
328348

329349
it("send usage report if validateBstackJson fails", () => {
330-
const info = proxyquire("../../../../bin/commands/info", {
331-
"../helpers/utils": {
350+
const info = proxyquire('../../../../bin/commands/info', {
351+
'../helpers/utils': {
332352
setUsername: setUsernameStub,
333353
setAccessKey: setAccessKeyStub,
334354
validateBstackJson: validateBstackJsonStub,
335355
getErrorCodeFromErr: getErrorCodeFromErrStub,
336356
sendUsageReport: sendUsageReportStub,
337357
setUsageReportingFlag: setUsageReportingFlagStub,
338-
getConfigPath: getConfigPathStub
358+
setCypressConfigFilename: setCypressConfigFilenameStub,
359+
getConfigPath: getConfigPathStub,
360+
setDefaultAuthHash: setDefaultAuthHashStub
339361
},
340362
});
341363

test/unit/bin/commands/init.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ describe("init", () => {
7878
};
7979

8080
loggerStub = sandbox.stub(logger, 'error');
81+
cypressConfigFileStub = sandbox.stub(utils, 'setCypressConfigFilename');
8182
usageStub = sandbox.stub(utils, 'sendUsageReport');
8283

8384
expect(get_path(args)).to.be.undefined;
8485
sinon.assert.calledOnce(loggerStub);
86+
sinon.assert.calledOnce(cypressConfigFileStub);
8587
sinon.assert.calledOnce(usageStub);
8688
});
8789

0 commit comments

Comments
 (0)