From ab99aac3389a2dc80ad98dfd0f45e1ac35b95e50 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Mon, 3 Jan 2022 17:46:09 +0530 Subject: [PATCH 01/10] add code changes to capture, validate and send ip geolocation --- bin/commands/runs.js | 3 +++ bin/helpers/capabilityHelper.js | 2 ++ bin/helpers/constants.js | 5 +++++ bin/helpers/utils.js | 22 ++++++++++++++++++++++ bin/runner.js | 7 +++++++ 5 files changed, 39 insertions(+) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 2c42cd46..7a1396a5 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -48,6 +48,9 @@ module.exports = function run(args, rawArgs) { // set cypress config filename utils.setCypressConfigFilename(bsConfig, args); + // set cypress geo location + utils.setGeoLocation(bsConfig, args); + // accept the specs list from command line if provided utils.setUserSpecs(bsConfig, args); diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 8c36b8b9..d498e308 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -164,6 +164,8 @@ const validate = (bsConfig, args) => { if( Utils.searchForOption('--async') && ( !Utils.isUndefined(args.async) && bsConfig["connection_settings"]["local"])) reject(Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS); + if (bsConfig.run_settings.userProvidedGeoLocation && bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); + // validate if config file provided exists or not when cypress_config_file provided // validate the cypressProjectDir key otherwise. let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath; diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index f758d963..7437b5ec 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -87,6 +87,10 @@ const validationMessages = { INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference", INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference", INVALID_LOCAL_ASYNC_ARGS: "Cannot run in --async mode when local is set to true. Please run the build after removing --async", + INVALID_GEO_LOCATION: "[BROWSERSTACK_INVALID_COUNTRY_CODE] The country code specified for 'browserstack.geoLocation' is invalid. For list of supported countries, refer to - https://www.browserstack.com/ip-geolocation", + NOT_SUPPORTED_GEO_LOCATION: "The country code you have passed for IP Geolocation is currently not supported. Please refer the link https://www.browserstack.com/ip-geolocation for a list of supported countries.", + NOT_AVAILABLE_GEO_LOCATION: "The country code you have passed for IP Geolocation is not available at the moment. Please try again in a few hours.", + ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details." }; const cliMessages = { @@ -129,6 +133,7 @@ const cliMessages = { CONFIG_DESCRIPTION: "Set configuration values. Separate multiple values with a comma. The values set here override any values set in your configuration file.", REPORTER: "Specify the custom reporter to use", REPORTER_OPTIONS: "Specify reporter options for custom reporter", + CYPRESS_GEO_LOCATION: "Enterprise feature to simulate website and mobile behavior from different locations." }, COMMON: { DISABLE_USAGE_REPORTING: "Disable usage reporting", diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 5affe046..63808b0b 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -272,6 +272,28 @@ exports.setCypressConfigFilename = (bsConfig, args) => { } } +exports.verifyGeoLocationOption = () => { + let glOptionsSet = (this.searchForOption('-gl') || this.searchForOption('--gl')); + let geoHyphenLocationOptionsSet = (this.searchForOption('-geo-location') || this.searchForOption('--geo-location')); + let geoLocationOptionsSet = (this.searchForOption('-geolocation') || this.searchForOption('--geolocation')); + return (glOptionsSet || geoHyphenLocationOptionsSet || geoLocationOptionsSet); +} + +exports.setGeoLocation = (bsConfig, args) => { + let userProvidedGeoLocation = this.verifyGeoLocationOption(); + bsConfig.run_settings.userProvidedGeoLocation = (userProvidedGeoLocation || (!this.isUndefined(bsConfig.run_settings.geolocation))); + + if (userProvidedGeoLocation && !this.isUndefined(args.geolocation)) { + bsConfig.run_settings.geolocation = args.geolocation; + } + + if (this.isUndefined(bsConfig.run_settings.geolocation)){ + bsConfig.run_settings.geolocation = null; + } else { + bsConfig.run_settings.geolocation = bsConfig.run_settings.geolocation.toUpperCase(); + } +} + // specs can be passed from bstack configuration file // specs can be passed via command line args as a string // command line args takes precedence over config diff --git a/bin/runner.js b/bin/runner.js index 97046493..44cad4b4 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -133,6 +133,13 @@ var argv = yargs demand: true, demand: Constants.cliMessages.RUN.CYPRESS_CONFIG_DEMAND }, + 'gl': { + alias: 'geolocation', + describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION, + default: undefined, + type: 'string', + nargs: 1 + }, 'p': { alias: ['parallels', 'parallel'], describe: Constants.cliMessages.RUN.PARALLEL_DESC, From ae9ab92c237b7c51017b8347c3ce30f480ed7889 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Mon, 3 Jan 2022 22:11:38 +0530 Subject: [PATCH 02/10] log error_code in case of invalid geo location --- bin/helpers/utils.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 63808b0b..42e2c564 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -85,6 +85,9 @@ exports.getErrorCodeFromMsg = (errMsg) => { case Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS: errorCode = 'invalid_local_async_args'; break; + case Constants.validationMessages.INVALID_GEO_LOCATION: + errorCode = 'invalid_geo_location'; + break; } if ( errMsg.includes("Please use --config-file .") From 071f6e7a19f4a71cea2fa0cd52983721b659040f Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Wed, 5 Jan 2022 17:18:05 +0530 Subject: [PATCH 03/10] do not allow usage of ip geolocation in conjunction with local mode --- bin/helpers/capabilityHelper.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index d498e308..356bf842 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -164,7 +164,9 @@ const validate = (bsConfig, args) => { if( Utils.searchForOption('--async') && ( !Utils.isUndefined(args.async) && bsConfig["connection_settings"]["local"])) reject(Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS); - if (bsConfig.run_settings.userProvidedGeoLocation && bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); + if (bsConfig.run_settings.userProvidedGeoLocation && !bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); + + if (bsConfig["connection_settings"]["local"] && bsConfig.run_settings.userProvidedGeoLocation) reject(Constants.validationMessages.INVALID_GEO_LOCATION_AND_LOCAL_MODE); // validate if config file provided exists or not when cypress_config_file provided // validate the cypressProjectDir key otherwise. From 7a001dd0c224511f89bb0cd4dfb293215f2dc938 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Wed, 5 Jan 2022 18:28:59 +0530 Subject: [PATCH 04/10] rename a constant and log error --- bin/helpers/capabilityHelper.js | 2 +- bin/helpers/constants.js | 3 ++- bin/helpers/utils.js | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 356bf842..808191ab 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -166,7 +166,7 @@ const validate = (bsConfig, args) => { if (bsConfig.run_settings.userProvidedGeoLocation && !bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); - if (bsConfig["connection_settings"]["local"] && bsConfig.run_settings.userProvidedGeoLocation) reject(Constants.validationMessages.INVALID_GEO_LOCATION_AND_LOCAL_MODE); + if (bsConfig["connection_settings"]["local"] && bsConfig.run_settings.userProvidedGeoLocation) reject(Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE); // validate if config file provided exists or not when cypress_config_file provided // validate the cypressProjectDir key otherwise. diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 7437b5ec..8c6817f8 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -90,7 +90,8 @@ const validationMessages = { INVALID_GEO_LOCATION: "[BROWSERSTACK_INVALID_COUNTRY_CODE] The country code specified for 'browserstack.geoLocation' is invalid. For list of supported countries, refer to - https://www.browserstack.com/ip-geolocation", NOT_SUPPORTED_GEO_LOCATION: "The country code you have passed for IP Geolocation is currently not supported. Please refer the link https://www.browserstack.com/ip-geolocation for a list of supported countries.", NOT_AVAILABLE_GEO_LOCATION: "The country code you have passed for IP Geolocation is not available at the moment. Please try again in a few hours.", - ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details." + ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details.", + NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE: "Cannot run IP Geolocation feature in conjunction with local mode." }; const cliMessages = { diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 42e2c564..d9d6c4f1 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -88,6 +88,9 @@ exports.getErrorCodeFromMsg = (errMsg) => { case Constants.validationMessages.INVALID_GEO_LOCATION: errorCode = 'invalid_geo_location'; break; + case Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE: + errorCode = 'not_allowed_geo_location_and_local_mode'; + break; } if ( errMsg.includes("Please use --config-file .") From 3c1daf00f595dd41d8f3a775c4df3837577689a5 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Mon, 10 Jan 2022 12:57:46 +0530 Subject: [PATCH 05/10] add test cases --- bin/commands/runs.js | 2 +- bin/helpers/utils.js | 10 +- bin/runner.js | 3 +- test/unit/bin/helpers/capabilityHelper.js | 93 +++++++++++++ test/unit/bin/helpers/utils.js | 151 ++++++++++++++++++++++ 5 files changed, 251 insertions(+), 8 deletions(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 7a1396a5..ad67d0cc 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -49,7 +49,7 @@ module.exports = function run(args, rawArgs) { utils.setCypressConfigFilename(bsConfig, args); // set cypress geo location - utils.setGeoLocation(bsConfig, args); + utils.setGeolocation(bsConfig, args); // accept the specs list from command line if provided utils.setUserSpecs(bsConfig, args); diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index d9d6c4f1..0c3dcecc 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -278,18 +278,18 @@ exports.setCypressConfigFilename = (bsConfig, args) => { } } -exports.verifyGeoLocationOption = () => { +exports.verifyGeolocationOption = () => { let glOptionsSet = (this.searchForOption('-gl') || this.searchForOption('--gl')); let geoHyphenLocationOptionsSet = (this.searchForOption('-geo-location') || this.searchForOption('--geo-location')); let geoLocationOptionsSet = (this.searchForOption('-geolocation') || this.searchForOption('--geolocation')); return (glOptionsSet || geoHyphenLocationOptionsSet || geoLocationOptionsSet); } -exports.setGeoLocation = (bsConfig, args) => { - let userProvidedGeoLocation = this.verifyGeoLocationOption(); - bsConfig.run_settings.userProvidedGeoLocation = (userProvidedGeoLocation || (!this.isUndefined(bsConfig.run_settings.geolocation))); +exports.setGeolocation = (bsConfig, args) => { + let userProvidedGeolocation = this.verifyGeolocationOption(); + bsConfig.run_settings.userProvidedGeolocation = (userProvidedGeolocation || (!this.isUndefined(bsConfig.run_settings.geolocation))); - if (userProvidedGeoLocation && !this.isUndefined(args.geolocation)) { + if (userProvidedGeolocation && !this.isUndefined(args.geolocation)) { bsConfig.run_settings.geolocation = args.geolocation; } diff --git a/bin/runner.js b/bin/runner.js index 44cad4b4..275a2c30 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -137,8 +137,7 @@ var argv = yargs alias: 'geolocation', describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION, default: undefined, - type: 'string', - nargs: 1 + type: 'string' }, 'p': { alias: ['parallels', 'parallel'], diff --git a/test/unit/bin/helpers/capabilityHelper.js b/test/unit/bin/helpers/capabilityHelper.js index c7cdd3aa..fc19979d 100644 --- a/test/unit/bin/helpers/capabilityHelper.js +++ b/test/unit/bin/helpers/capabilityHelper.js @@ -1053,5 +1053,98 @@ describe("capabilityHelper.js", () => { }) }); }); + + describe("validate ip geolocation", () => { + beforeEach(() => { + bsConfig = { + auth: {}, + browsers: [ + { + browser: "chrome", + os: "Windows 10", + versions: ["78", "77"], + }, + ], + run_settings: { + cypress_proj_dir: "random path", + cypressConfigFilePath: "random path", + cypressProjectDir: "random path" + }, + connection_settings: {} + }; + }); + + it("should throw an error if both local and geolocation are used", () => { + bsConfig.run_settings.geolocation = "US"; + bsConfig.run_settings.userProvidedGeolocation = true; + bsConfig.connection_settings.local = true; + bsConfig.connection_settings.local_identifier = "some text"; + + return capabilityHelper + .validate(bsConfig, {}) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE); + }); + }); + + it("should throw an error if incorrect format for geolocation code is used (valid country name but incorrect code)", () => { + bsConfig.run_settings.geolocation = "USA"; + bsConfig.run_settings.userProvidedGeolocation = true; + + return capabilityHelper + .validate(bsConfig, {}) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION); + }); + }); + + it("should throw an error if incorrect format for geolocation code is used (random value)", () => { + bsConfig.run_settings.geolocation = "RANDOM"; + bsConfig.run_settings.userProvidedGeolocation = true; + + return capabilityHelper + .validate(bsConfig, {}) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION); + }); + }); + + it("should throw an error if incorrect format for geolocation code is used (special chars)", () => { + bsConfig.run_settings.geolocation = "$USA$!&@*)()"; + bsConfig.run_settings.userProvidedGeolocation = true; + + return capabilityHelper + .validate(bsConfig, {}) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION); + }); + }); + + it("should throw an error if incorrect format for geolocation code is used (small caps)", () => { + bsConfig.run_settings.geolocation = "us"; + bsConfig.run_settings.userProvidedGeolocation = true; + + return capabilityHelper + .validate(bsConfig, {}) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION); + }); + }); + }); }); }); diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index 6aaeed5f..949961c6 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -1808,6 +1808,157 @@ describe('utils', () => { }); }); + describe('verifyGeolocationOption', () => { + let utilsearchForOptionGeolocationStub, userOption, testOption; + + beforeEach(function () { + utilsearchForOptionGeolocationStub = sinon + .stub(utils, 'searchForOption') + .callsFake((...userOption) => { + return userOption == testOption; + }); + }); + + afterEach(function () { + utilsearchForOptionGeolocationStub.restore(); + }); + + it('-gl user option', () => { + testOption = '-gl'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + + it('--gl user option', () => { + testOption = '--gl'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + + it('-geo-location user option', () => { + testOption = '-geo-location'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + + it('--geo-location user option', () => { + testOption = '--geo-location'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + + it('-geolocation user option', () => { + testOption = '-geolocation'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + + it('--geolocation user option', () => { + testOption = '--geolocation'; + expect(utils.verifyGeolocationOption()).to.be.true; + sinon.assert.calledWithExactly( + utilsearchForOptionGeolocationStub, + testOption + ); + }); + }); + + describe('setGeolocation', () => { + let verifyGeolocationOptionStub, + glBool, + args, + bsConfig, + geolocation; + + beforeEach(function () { + verifyGeolocationOptionStub = sinon + .stub(utils, 'verifyGeolocationOption') + .callsFake(() => glBool); + + args = { + geolocation: 'IN', + }; + }); + + afterEach(function () { + sinon.restore(); + }); + + it('has user provided gl flag', () => { + glBool = true; + + bsConfig = { + run_settings: { + geolocation: 'IN', + }, + }; + + utils.setGeolocation(bsConfig, args); + + expect(bsConfig.run_settings.geolocation).to.be.eq( + args.geolocation + ); + expect(bsConfig.run_settings.userProvidedGeolocation).to.be.true; + }); + + it('does not have user provided gl flag, sets the value from bsConfig', () => { + glBool = false; + args = { + geolocation: null + }; + bsConfig = { + run_settings: { + geolocation: 'IN', + }, + }; + + utils.setGeolocation(bsConfig, args); + + expect(bsConfig.run_settings.geolocation).to.not.be.eq( + args.geolocation + ); + expect(bsConfig.run_settings.geolocation).to.be.eq('IN'); + expect(bsConfig.run_settings.userProvidedGeolocation).to.be.true; + }); + + it('does not have user provided gl flag and config value, sets geolocation to be null', () => { + geolocation = 'run_settings_geolocation'; + glBool = false; + args = { + geolocation: null + }; + bsConfig = { + run_settings: { + geolocation: null, + }, + }; + + utils.setGeolocation(bsConfig, args); + + expect(bsConfig.run_settings.geolocation).to.be.eq(null); + expect(bsConfig.run_settings.userProvidedGeolocation).to.be.false; + }); + + afterEach(function () { + verifyGeolocationOptionStub.restore(); + }); + }); + describe('setDefaults', () => { beforeEach(function () { delete process.env.BROWSERSTACK_USERNAME; From 80ff76fae153b1d28cc136c1c410ac43ac121e36 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Mon, 10 Jan 2022 12:59:23 +0530 Subject: [PATCH 06/10] rename userProvidedGeolocation --- bin/helpers/capabilityHelper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 808191ab..974579a8 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -164,9 +164,9 @@ const validate = (bsConfig, args) => { if( Utils.searchForOption('--async') && ( !Utils.isUndefined(args.async) && bsConfig["connection_settings"]["local"])) reject(Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS); - if (bsConfig.run_settings.userProvidedGeoLocation && !bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); + if (bsConfig.run_settings.userProvidedGeolocation && !bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION); - if (bsConfig["connection_settings"]["local"] && bsConfig.run_settings.userProvidedGeoLocation) reject(Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE); + if (bsConfig["connection_settings"]["local"] && bsConfig.run_settings.userProvidedGeolocation) reject(Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE); // validate if config file provided exists or not when cypress_config_file provided // validate the cypressProjectDir key otherwise. From 4730fe31d02064e206fdd5e0deb8c12fd45d4e18 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Tue, 11 Jan 2022 10:36:06 +0530 Subject: [PATCH 07/10] update error message --- bin/commands/runs.js | 3 +++ bin/helpers/constants.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index ad67d0cc..5f176217 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -225,6 +225,9 @@ module.exports = function run(args, rawArgs) { utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData, rawArgs); return; }).catch(async function (err) { + if (err && err.includes('browserstack.geoLocation')) { + err = err.replace(/browserstack.geoLocation/g, 'geolocation'); + } // Build creation failed logger.error(err); // stop the Local instance diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 8c6817f8..74f3924b 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -87,7 +87,7 @@ const validationMessages = { INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference", INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference", INVALID_LOCAL_ASYNC_ARGS: "Cannot run in --async mode when local is set to true. Please run the build after removing --async", - INVALID_GEO_LOCATION: "[BROWSERSTACK_INVALID_COUNTRY_CODE] The country code specified for 'browserstack.geoLocation' is invalid. For list of supported countries, refer to - https://www.browserstack.com/ip-geolocation", + INVALID_GEO_LOCATION: "[BROWSERSTACK_INVALID_COUNTRY_CODE] The country code specified for 'geolocation' is invalid. For list of supported countries, refer to - https://www.browserstack.com/ip-geolocation", NOT_SUPPORTED_GEO_LOCATION: "The country code you have passed for IP Geolocation is currently not supported. Please refer the link https://www.browserstack.com/ip-geolocation for a list of supported countries.", NOT_AVAILABLE_GEO_LOCATION: "The country code you have passed for IP Geolocation is not available at the moment. Please try again in a few hours.", ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details.", From 4075d36fd01feac5d766ce110601a24bf664dddd Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Thu, 13 Jan 2022 18:51:42 +0530 Subject: [PATCH 08/10] update error message in cli --- bin/helpers/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 74f3924b..3a455900 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -91,7 +91,7 @@ const validationMessages = { NOT_SUPPORTED_GEO_LOCATION: "The country code you have passed for IP Geolocation is currently not supported. Please refer the link https://www.browserstack.com/ip-geolocation for a list of supported countries.", NOT_AVAILABLE_GEO_LOCATION: "The country code you have passed for IP Geolocation is not available at the moment. Please try again in a few hours.", ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details.", - NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE: "Cannot run IP Geolocation feature in conjunction with local mode." + NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE: "IP Geolocation feature is not available in conjunction with BrowserStack Local." }; const cliMessages = { From 7fccd87ed60ba33e71338c83021323c496055ece Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Thu, 13 Jan 2022 22:40:22 +0530 Subject: [PATCH 09/10] fix test cases --- test/unit/bin/commands/runs.js | 21 ++++++++++++++++++--- test/unit/bin/helpers/capabilityHelper.js | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index 7f29dc42..2388f53a 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -115,6 +115,7 @@ describe("runs", () => { setBrowsersStub = sandbox.stub(); setConfigStub = sandbox.stub(); setCLIModeStub = sandbox.stub(); + setGeolocationStub = sandbox.stub(); }); afterEach(() => { @@ -154,7 +155,8 @@ describe("runs", () => { setSystemEnvs: setSystemEnvsStub, setBrowsers: setBrowsersStub, setConfig: setConfigStub, - setCLIMode: setCLIModeStub + setCLIMode: setCLIModeStub, + setGeolocation: setGeolocationStub }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub @@ -194,6 +196,7 @@ describe("runs", () => { sinon.assert.calledOnce(getErrorCodeFromMsgStub); sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnce(setUsageReportingFlagStub); + sinon.assert.calledOnce(setGeolocationStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -249,6 +252,7 @@ describe("runs", () => { setBrowsersStub = sandbox.stub(); setConfigStub = sandbox.stub(); setCLIModeStub = sandbox.stub(); + setGeolocationStub = sandbox.stub(); }); afterEach(() => { @@ -290,7 +294,8 @@ describe("runs", () => { setSystemEnvs: setSystemEnvsStub, setBrowsers: setBrowsersStub, setConfig: setConfigStub, - setCLIMode: setCLIModeStub + setCLIMode: setCLIModeStub, + setGeolocation: setGeolocationStub }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub, @@ -348,6 +353,7 @@ describe("runs", () => { sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnce(setDefaultsStub); sinon.assert.calledOnce(setSystemEnvsStub); + sinon.assert.calledOnce(setGeolocationStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -405,6 +411,7 @@ describe("runs", () => { setBrowsersStub = sandbox.stub(); setCLIModeStub = sandbox.stub(); fetchZipSizeStub = sandbox.stub(); + setGeolocationStub = sandbox.stub(); }); afterEach(() => { @@ -448,6 +455,7 @@ describe("runs", () => { setConfig: setConfigStub, setCLIMode: setCLIModeStub, fetchZipSize: fetchZipSizeStub, + setGeolocation: setGeolocationStub, }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub, @@ -507,6 +515,7 @@ describe("runs", () => { sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnce(setDefaultsStub); sinon.assert.calledOnce(setSystemEnvsStub); + sinon.assert.calledOnce(setGeolocationStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -569,6 +578,7 @@ describe("runs", () => { setBrowsersStub = sandbox.stub(); setCLIModeStub = sandbox.stub(); fetchZipSizeStub = sandbox.stub(); + setGeolocationStub = sandbox.stub(); }); afterEach(() => { @@ -613,6 +623,7 @@ describe("runs", () => { setConfig: setConfigStub, setCLIMode: setCLIModeStub, fetchZipSize: fetchZipSizeStub, + setGeolocation: setGeolocationStub, }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub, @@ -683,6 +694,7 @@ describe("runs", () => { sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnce(setDefaultsStub); sinon.assert.calledOnce(setSystemEnvsStub); + sinon.assert.calledOnce(setGeolocationStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, @@ -759,6 +771,7 @@ describe("runs", () => { setCLIModeStub = sandbox.stub(); setProcessHooksStub = sandbox.stub(); fetchZipSizeStub = sandbox.stub(); + setGeolocationStub = sandbox.stub(); }); afterEach(() => { @@ -771,7 +784,7 @@ describe("runs", () => { let errorCode = null; let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`; let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`; - let data = { user_id: 1234, parallels: 10, time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123} + let data = { user_id: 1234, parallels: 10, time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123, test_suite_zip_upload: 1, package_zip_upload: 1} const runs = proxyquire('../../../../bin/commands/runs', { '../helpers/utils': { @@ -811,6 +824,7 @@ describe("runs", () => { setCLIMode: setCLIModeStub, setProcessHooks: setProcessHooksStub, fetchZipSize: fetchZipSizeStub, + setGeolocation: setGeolocationStub, }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub, @@ -898,6 +912,7 @@ describe("runs", () => { sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnce(setDefaultsStub); sinon.assert.calledOnce(setSystemEnvsStub); + sinon.assert.calledOnce(setGeolocationStub); sinon.assert.match( sendUsageReportStub.getCall(0).args, [ diff --git a/test/unit/bin/helpers/capabilityHelper.js b/test/unit/bin/helpers/capabilityHelper.js index 12dd01d5..2c4d2b92 100644 --- a/test/unit/bin/helpers/capabilityHelper.js +++ b/test/unit/bin/helpers/capabilityHelper.js @@ -586,6 +586,7 @@ describe("capabilityHelper.js", () => { cypress_proj_dir: "random path", cypressConfigFilePath: "random path" }, + connection_settings: {local: false} }; }); @@ -952,6 +953,7 @@ describe("capabilityHelper.js", () => { cypressConfigFilePath: "random path", cypressProjectDir: "random path" }, + connection_settings: {local: false} }; }); it("validate cypress json is present", () => { @@ -1081,6 +1083,7 @@ describe("capabilityHelper.js", () => { cypressConfigFilePath: "random path", cypressProjectDir: "random path" }, + connection_settings: {local: false} }; }); From 3c7444ecdccf9e9aac9a972e0acdd843ad370fc7 Mon Sep 17 00:00:00 2001 From: Pranav Jain Date: Fri, 14 Jan 2022 14:15:11 +0530 Subject: [PATCH 10/10] remove fix for unit test --- test/unit/bin/commands/runs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index 2388f53a..3fca403e 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -784,7 +784,7 @@ describe("runs", () => { let errorCode = null; let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`; let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`; - let data = { user_id: 1234, parallels: 10, time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123, test_suite_zip_upload: 1, package_zip_upload: 1} + let data = { user_id: 1234, parallels: 10, time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123} const runs = proxyquire('../../../../bin/commands/runs', { '../helpers/utils': {