Skip to content

Commit 3c1daf0

Browse files
committed
add test cases
1 parent 7a001dd commit 3c1daf0

File tree

5 files changed

+251
-8
lines changed

5 files changed

+251
-8
lines changed

bin/commands/runs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = function run(args, rawArgs) {
4949
utils.setCypressConfigFilename(bsConfig, args);
5050

5151
// set cypress geo location
52-
utils.setGeoLocation(bsConfig, args);
52+
utils.setGeolocation(bsConfig, args);
5353

5454
// accept the specs list from command line if provided
5555
utils.setUserSpecs(bsConfig, args);

bin/helpers/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,18 @@ exports.setCypressConfigFilename = (bsConfig, args) => {
278278
}
279279
}
280280

281-
exports.verifyGeoLocationOption = () => {
281+
exports.verifyGeolocationOption = () => {
282282
let glOptionsSet = (this.searchForOption('-gl') || this.searchForOption('--gl'));
283283
let geoHyphenLocationOptionsSet = (this.searchForOption('-geo-location') || this.searchForOption('--geo-location'));
284284
let geoLocationOptionsSet = (this.searchForOption('-geolocation') || this.searchForOption('--geolocation'));
285285
return (glOptionsSet || geoHyphenLocationOptionsSet || geoLocationOptionsSet);
286286
}
287287

288-
exports.setGeoLocation = (bsConfig, args) => {
289-
let userProvidedGeoLocation = this.verifyGeoLocationOption();
290-
bsConfig.run_settings.userProvidedGeoLocation = (userProvidedGeoLocation || (!this.isUndefined(bsConfig.run_settings.geolocation)));
288+
exports.setGeolocation = (bsConfig, args) => {
289+
let userProvidedGeolocation = this.verifyGeolocationOption();
290+
bsConfig.run_settings.userProvidedGeolocation = (userProvidedGeolocation || (!this.isUndefined(bsConfig.run_settings.geolocation)));
291291

292-
if (userProvidedGeoLocation && !this.isUndefined(args.geolocation)) {
292+
if (userProvidedGeolocation && !this.isUndefined(args.geolocation)) {
293293
bsConfig.run_settings.geolocation = args.geolocation;
294294
}
295295

bin/runner.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ var argv = yargs
137137
alias: 'geolocation',
138138
describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION,
139139
default: undefined,
140-
type: 'string',
141-
nargs: 1
140+
type: 'string'
142141
},
143142
'p': {
144143
alias: ['parallels', 'parallel'],

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,5 +1053,98 @@ describe("capabilityHelper.js", () => {
10531053
})
10541054
});
10551055
});
1056+
1057+
describe("validate ip geolocation", () => {
1058+
beforeEach(() => {
1059+
bsConfig = {
1060+
auth: {},
1061+
browsers: [
1062+
{
1063+
browser: "chrome",
1064+
os: "Windows 10",
1065+
versions: ["78", "77"],
1066+
},
1067+
],
1068+
run_settings: {
1069+
cypress_proj_dir: "random path",
1070+
cypressConfigFilePath: "random path",
1071+
cypressProjectDir: "random path"
1072+
},
1073+
connection_settings: {}
1074+
};
1075+
});
1076+
1077+
it("should throw an error if both local and geolocation are used", () => {
1078+
bsConfig.run_settings.geolocation = "US";
1079+
bsConfig.run_settings.userProvidedGeolocation = true;
1080+
bsConfig.connection_settings.local = true;
1081+
bsConfig.connection_settings.local_identifier = "some text";
1082+
1083+
return capabilityHelper
1084+
.validate(bsConfig, {})
1085+
.then(function (data) {
1086+
chai.assert.fail("Promise error");
1087+
})
1088+
.catch((error) => {
1089+
chai.assert.equal(error, Constants.validationMessages.NOT_ALLOWED_GEO_LOCATION_AND_LOCAL_MODE);
1090+
});
1091+
});
1092+
1093+
it("should throw an error if incorrect format for geolocation code is used (valid country name but incorrect code)", () => {
1094+
bsConfig.run_settings.geolocation = "USA";
1095+
bsConfig.run_settings.userProvidedGeolocation = true;
1096+
1097+
return capabilityHelper
1098+
.validate(bsConfig, {})
1099+
.then(function (data) {
1100+
chai.assert.fail("Promise error");
1101+
})
1102+
.catch((error) => {
1103+
chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION);
1104+
});
1105+
});
1106+
1107+
it("should throw an error if incorrect format for geolocation code is used (random value)", () => {
1108+
bsConfig.run_settings.geolocation = "RANDOM";
1109+
bsConfig.run_settings.userProvidedGeolocation = true;
1110+
1111+
return capabilityHelper
1112+
.validate(bsConfig, {})
1113+
.then(function (data) {
1114+
chai.assert.fail("Promise error");
1115+
})
1116+
.catch((error) => {
1117+
chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION);
1118+
});
1119+
});
1120+
1121+
it("should throw an error if incorrect format for geolocation code is used (special chars)", () => {
1122+
bsConfig.run_settings.geolocation = "$USA$!&@*)()";
1123+
bsConfig.run_settings.userProvidedGeolocation = true;
1124+
1125+
return capabilityHelper
1126+
.validate(bsConfig, {})
1127+
.then(function (data) {
1128+
chai.assert.fail("Promise error");
1129+
})
1130+
.catch((error) => {
1131+
chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION);
1132+
});
1133+
});
1134+
1135+
it("should throw an error if incorrect format for geolocation code is used (small caps)", () => {
1136+
bsConfig.run_settings.geolocation = "us";
1137+
bsConfig.run_settings.userProvidedGeolocation = true;
1138+
1139+
return capabilityHelper
1140+
.validate(bsConfig, {})
1141+
.then(function (data) {
1142+
chai.assert.fail("Promise error");
1143+
})
1144+
.catch((error) => {
1145+
chai.assert.equal(error, Constants.validationMessages.INVALID_GEO_LOCATION);
1146+
});
1147+
});
1148+
});
10561149
});
10571150
});

test/unit/bin/helpers/utils.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,157 @@ describe('utils', () => {
18081808
});
18091809
});
18101810

1811+
describe('verifyGeolocationOption', () => {
1812+
let utilsearchForOptionGeolocationStub, userOption, testOption;
1813+
1814+
beforeEach(function () {
1815+
utilsearchForOptionGeolocationStub = sinon
1816+
.stub(utils, 'searchForOption')
1817+
.callsFake((...userOption) => {
1818+
return userOption == testOption;
1819+
});
1820+
});
1821+
1822+
afterEach(function () {
1823+
utilsearchForOptionGeolocationStub.restore();
1824+
});
1825+
1826+
it('-gl user option', () => {
1827+
testOption = '-gl';
1828+
expect(utils.verifyGeolocationOption()).to.be.true;
1829+
sinon.assert.calledWithExactly(
1830+
utilsearchForOptionGeolocationStub,
1831+
testOption
1832+
);
1833+
});
1834+
1835+
it('--gl user option', () => {
1836+
testOption = '--gl';
1837+
expect(utils.verifyGeolocationOption()).to.be.true;
1838+
sinon.assert.calledWithExactly(
1839+
utilsearchForOptionGeolocationStub,
1840+
testOption
1841+
);
1842+
});
1843+
1844+
it('-geo-location user option', () => {
1845+
testOption = '-geo-location';
1846+
expect(utils.verifyGeolocationOption()).to.be.true;
1847+
sinon.assert.calledWithExactly(
1848+
utilsearchForOptionGeolocationStub,
1849+
testOption
1850+
);
1851+
});
1852+
1853+
it('--geo-location user option', () => {
1854+
testOption = '--geo-location';
1855+
expect(utils.verifyGeolocationOption()).to.be.true;
1856+
sinon.assert.calledWithExactly(
1857+
utilsearchForOptionGeolocationStub,
1858+
testOption
1859+
);
1860+
});
1861+
1862+
it('-geolocation user option', () => {
1863+
testOption = '-geolocation';
1864+
expect(utils.verifyGeolocationOption()).to.be.true;
1865+
sinon.assert.calledWithExactly(
1866+
utilsearchForOptionGeolocationStub,
1867+
testOption
1868+
);
1869+
});
1870+
1871+
it('--geolocation user option', () => {
1872+
testOption = '--geolocation';
1873+
expect(utils.verifyGeolocationOption()).to.be.true;
1874+
sinon.assert.calledWithExactly(
1875+
utilsearchForOptionGeolocationStub,
1876+
testOption
1877+
);
1878+
});
1879+
});
1880+
1881+
describe('setGeolocation', () => {
1882+
let verifyGeolocationOptionStub,
1883+
glBool,
1884+
args,
1885+
bsConfig,
1886+
geolocation;
1887+
1888+
beforeEach(function () {
1889+
verifyGeolocationOptionStub = sinon
1890+
.stub(utils, 'verifyGeolocationOption')
1891+
.callsFake(() => glBool);
1892+
1893+
args = {
1894+
geolocation: 'IN',
1895+
};
1896+
});
1897+
1898+
afterEach(function () {
1899+
sinon.restore();
1900+
});
1901+
1902+
it('has user provided gl flag', () => {
1903+
glBool = true;
1904+
1905+
bsConfig = {
1906+
run_settings: {
1907+
geolocation: 'IN',
1908+
},
1909+
};
1910+
1911+
utils.setGeolocation(bsConfig, args);
1912+
1913+
expect(bsConfig.run_settings.geolocation).to.be.eq(
1914+
args.geolocation
1915+
);
1916+
expect(bsConfig.run_settings.userProvidedGeolocation).to.be.true;
1917+
});
1918+
1919+
it('does not have user provided gl flag, sets the value from bsConfig', () => {
1920+
glBool = false;
1921+
args = {
1922+
geolocation: null
1923+
};
1924+
bsConfig = {
1925+
run_settings: {
1926+
geolocation: 'IN',
1927+
},
1928+
};
1929+
1930+
utils.setGeolocation(bsConfig, args);
1931+
1932+
expect(bsConfig.run_settings.geolocation).to.not.be.eq(
1933+
args.geolocation
1934+
);
1935+
expect(bsConfig.run_settings.geolocation).to.be.eq('IN');
1936+
expect(bsConfig.run_settings.userProvidedGeolocation).to.be.true;
1937+
});
1938+
1939+
it('does not have user provided gl flag and config value, sets geolocation to be null', () => {
1940+
geolocation = 'run_settings_geolocation';
1941+
glBool = false;
1942+
args = {
1943+
geolocation: null
1944+
};
1945+
bsConfig = {
1946+
run_settings: {
1947+
geolocation: null,
1948+
},
1949+
};
1950+
1951+
utils.setGeolocation(bsConfig, args);
1952+
1953+
expect(bsConfig.run_settings.geolocation).to.be.eq(null);
1954+
expect(bsConfig.run_settings.userProvidedGeolocation).to.be.false;
1955+
});
1956+
1957+
afterEach(function () {
1958+
verifyGeolocationOptionStub.restore();
1959+
});
1960+
});
1961+
18111962
describe('setDefaults', () => {
18121963
beforeEach(function () {
18131964
delete process.env.BROWSERSTACK_USERNAME;

0 commit comments

Comments
 (0)