From 69f5089d2047895a9aa6c2949e46b6255ead4ef8 Mon Sep 17 00:00:00 2001 From: roshan Date: Tue, 2 Mar 2021 21:02:44 +0530 Subject: [PATCH 1/7] error shown if username or password not given --- bin/helpers/capabilityHelper.js | 8 ++++++++ bin/helpers/constants.js | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 5d314bdd..8495130d 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -132,6 +132,14 @@ const validate = (bsConfig, args) => { // if parallels specified via arguments validate only arguments if (!Utils.isUndefined(args) && !Utils.isUndefined(args.parallels) && !Utils.isParallelValid(args.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION); + if(!Utils.isUndefined(args)){ + if(!Utils.isUndefined(args.username) && !Utils.isUndefined(args.key)) + reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username and Password")); + else if(!Utils.isUndefined(args.username)) + reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username")); + else if(!Utils.isUndefined(args.key)) + reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Password")); + } // 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 d029440b..9ce79859 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -57,7 +57,8 @@ const validationMessages = { INVALID_CYPRESS_JSON: "cypress.json is not a valid json", INVALID_DEFAULT_AUTH_PARAMS: "Your username and access key are required to run your tests on BrowserStack. Learn more at https://www.browserstack.com/docs/automate/cypress/authentication", LOCAL_NOT_SET: "To test on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing", - INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists." + INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists.", + EMPTY_ARGS: "Empty " }; const cliMessages = { From 8897436ea989278c0a66037dfaa6ba362a58f787 Mon Sep 17 00:00:00 2001 From: roshan Date: Tue, 2 Mar 2021 21:09:55 +0530 Subject: [PATCH 2/7] improved indentation --- 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 8495130d..58a791cf 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -134,11 +134,11 @@ const validate = (bsConfig, args) => { if(!Utils.isUndefined(args)){ if(!Utils.isUndefined(args.username) && !Utils.isUndefined(args.key)) - reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username and Password")); + reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username and Password")); else if(!Utils.isUndefined(args.username)) reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username")); else if(!Utils.isUndefined(args.key)) - reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Password")); + reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Password")); } // validate if config file provided exists or not when cypress_config_file provided // validate the cypressProjectDir key otherwise. From 93b3b31556a28105abe020c49bf71b2d05eb685d Mon Sep 17 00:00:00 2001 From: roshan Date: Tue, 2 Mar 2021 21:29:20 +0530 Subject: [PATCH 3/7] added specs for new validations --- test/unit/bin/helpers/capabilityHelper.js | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/test/unit/bin/helpers/capabilityHelper.js b/test/unit/bin/helpers/capabilityHelper.js index 1ff68018..ba8a5592 100644 --- a/test/unit/bin/helpers/capabilityHelper.js +++ b/test/unit/bin/helpers/capabilityHelper.js @@ -779,6 +779,70 @@ describe("capabilityHelper.js", () => { }); }); + describe("validates username and password" , () =>{ + beforeEach(() => { + //Stub for cypress json validation + sinon.stub(fs, 'existsSync').returns(true); + sinon.stub(fs, 'readFileSync').returns("{}"); + + bsConfig = { + auth: {}, + browsers: [ + { + browser: "chrome", + os: "Windows 10", + versions: ["78", "77"], + }, + ], + run_settings: { + cypress_proj_dir: "random path", + cypressConfigFilePath: "random path" + }, + }; + }); + + afterEach(() => { + fs.existsSync.restore(); + fs.readFileSync.restore(); + }); + + it('throws an error if the user has given username as null', () =>{ + return capabilityHelper + .validate(bsConfig, { username: "" }) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Username")); + }); + }); + + it('throws an error if the user has given password as null', () =>{ + return capabilityHelper + .validate(bsConfig, { key: "" }) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Password")); + }); + }); + + it('throws an error if the user has given username and password both as null', () =>{ + return capabilityHelper + .validate(bsConfig, { + username: "", + key: "" + }) + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Username and Password")); + }); + }); + }); + it("validate bsConfig", () => { let bsConfig = undefined; return capabilityHelper From e6f196bb5f9fac1a43ee0483c1ce11f07ba1227c Mon Sep 17 00:00:00 2001 From: roshan Date: Wed, 3 Mar 2021 14:39:53 +0530 Subject: [PATCH 4/7] checked for response status 401 --- bin/helpers/capabilityHelper.js | 8 -------- bin/helpers/zipUpload.js | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 58a791cf..5d314bdd 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -132,14 +132,6 @@ const validate = (bsConfig, args) => { // if parallels specified via arguments validate only arguments if (!Utils.isUndefined(args) && !Utils.isUndefined(args.parallels) && !Utils.isParallelValid(args.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION); - if(!Utils.isUndefined(args)){ - if(!Utils.isUndefined(args.username) && !Utils.isUndefined(args.key)) - reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username and Password")); - else if(!Utils.isUndefined(args.username)) - reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Username")); - else if(!Utils.isUndefined(args.key)) - reject(Constants.validationMessages.EMPTY_ARGS.replace("", "Password")); - } // 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/zipUpload.js b/bin/helpers/zipUpload.js index 654dd481..a848e89b 100644 --- a/bin/helpers/zipUpload.js +++ b/bin/helpers/zipUpload.js @@ -37,10 +37,19 @@ const uploadCypressZip = (bsConfig, filePath) => { responseData = null } if (resp.statusCode != 200) { - if (responseData && responseData["error"]) { - reject(responseData["error"]); - } else { - reject(Constants.userMessages.ZIP_UPLOADER_NOT_REACHABLE); + if(resp.statusCode == 401){ + if(responseData && responseData["error"]){ + reject(responseData["error"]); + } else { + reject(Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS); + } + } + else{ + if (responseData && responseData["error"]) { + reject(responseData["error"]); + } else { + reject(Constants.userMessages.ZIP_UPLOADER_NOT_REACHABLE); + } } } else { logger.info(`Uploaded tests successfully (${responseData.zip_url})`); From a4b0ff8079690cf9c2c240f8e3b7fd4727fc88cc Mon Sep 17 00:00:00 2001 From: roshan Date: Wed, 3 Mar 2021 14:54:11 +0530 Subject: [PATCH 5/7] added specs for response checks --- test/unit/bin/helpers/capabilityHelper.js | 64 ----------------------- test/unit/bin/helpers/zipUpload.js | 63 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 66 deletions(-) diff --git a/test/unit/bin/helpers/capabilityHelper.js b/test/unit/bin/helpers/capabilityHelper.js index ba8a5592..1ff68018 100644 --- a/test/unit/bin/helpers/capabilityHelper.js +++ b/test/unit/bin/helpers/capabilityHelper.js @@ -779,70 +779,6 @@ describe("capabilityHelper.js", () => { }); }); - describe("validates username and password" , () =>{ - beforeEach(() => { - //Stub for cypress json validation - sinon.stub(fs, 'existsSync').returns(true); - sinon.stub(fs, 'readFileSync').returns("{}"); - - bsConfig = { - auth: {}, - browsers: [ - { - browser: "chrome", - os: "Windows 10", - versions: ["78", "77"], - }, - ], - run_settings: { - cypress_proj_dir: "random path", - cypressConfigFilePath: "random path" - }, - }; - }); - - afterEach(() => { - fs.existsSync.restore(); - fs.readFileSync.restore(); - }); - - it('throws an error if the user has given username as null', () =>{ - return capabilityHelper - .validate(bsConfig, { username: "" }) - .then(function (data) { - chai.assert.fail("Promise error"); - }) - .catch((error) => { - chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Username")); - }); - }); - - it('throws an error if the user has given password as null', () =>{ - return capabilityHelper - .validate(bsConfig, { key: "" }) - .then(function (data) { - chai.assert.fail("Promise error"); - }) - .catch((error) => { - chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Password")); - }); - }); - - it('throws an error if the user has given username and password both as null', () =>{ - return capabilityHelper - .validate(bsConfig, { - username: "", - key: "" - }) - .then(function (data) { - chai.assert.fail("Promise error"); - }) - .catch((error) => { - chai.assert.equal(error, Constants.validationMessages.EMPTY_ARGS.replace("","Username and Password")); - }); - }); - }); - it("validate bsConfig", () => { let bsConfig = undefined; return capabilityHelper diff --git a/test/unit/bin/helpers/zipUpload.js b/test/unit/bin/helpers/zipUpload.js index a2b3e5b5..6d547686 100644 --- a/test/unit/bin/helpers/zipUpload.js +++ b/test/unit/bin/helpers/zipUpload.js @@ -55,7 +55,7 @@ describe("zipUpload", () => { }); }); - it("reject with error (if error present in response) if statusCode != 200", () => { + it("reject with error (if error present in response) if statusCode == 401", () => { let error = "non 200 code"; let requestStub = sandbox @@ -82,7 +82,7 @@ describe("zipUpload", () => { }); }); - it("reject with message if statusCode != 200 and error not in response", () => { + it("reject with message if statusCode == 401 and error not in response", () => { let requestStub = sandbox .stub(request, "post") .yields( @@ -98,6 +98,65 @@ describe("zipUpload", () => { request: { post: requestStub }, }); + return zipUploader + .zipUpload(bsConfig, "./random_file_path") + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + sinon.assert.calledOnce(requestStub); + sinon.assert.calledOnce(getUserAgentStub); + sinon.assert.calledOnce(createReadStreamStub); + chai.assert.equal( + error, + Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS + ); + }); + }); + + it("reject with error (if error present in response) if statusCode != 200 and statusCode != 401", () => { + let error = "non 200 and non 401 code"; + + let requestStub = sandbox + .stub(request, "post") + .yields(null, { statusCode: 404 }, JSON.stringify({ error: error })); + + const zipUploader = proxyquire("../../../../bin/helpers/zipUpload", { + "./utils": { + getUserAgent: getUserAgentStub, + }, + request: { post: requestStub }, + }); + + return zipUploader + .zipUpload(bsConfig, "./random_file_path") + .then(function (data) { + chai.assert.fail("Promise error"); + }) + .catch((error) => { + sinon.assert.calledOnce(requestStub); + sinon.assert.calledOnce(getUserAgentStub); + sinon.assert.calledOnce(createReadStreamStub); + chai.assert.equal(error, "non 200 and non 401 code"); + }); + }); + + it("reject with message if statusCode != 200 and statusCode != 401 and error not in response", () => { + let requestStub = sandbox + .stub(request, "post") + .yields( + null, + { statusCode: 404 }, + JSON.stringify({ message: "random message" }) + ); + + const zipUploader = proxyquire("../../../../bin/helpers/zipUpload", { + "./utils": { + getUserAgent: getUserAgentStub, + }, + request: { post: requestStub }, + }); + return zipUploader .zipUpload(bsConfig, "./random_file_path") .then(function (data) { From ea2977bb1dd4d20efb16881c732fa1d3e2ee1d82 Mon Sep 17 00:00:00 2001 From: roshan Date: Wed, 3 Mar 2021 14:57:22 +0530 Subject: [PATCH 6/7] removed unused constant --- bin/helpers/constants.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 9ce79859..d029440b 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -57,8 +57,7 @@ const validationMessages = { INVALID_CYPRESS_JSON: "cypress.json is not a valid json", INVALID_DEFAULT_AUTH_PARAMS: "Your username and access key are required to run your tests on BrowserStack. Learn more at https://www.browserstack.com/docs/automate/cypress/authentication", LOCAL_NOT_SET: "To test on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing", - INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists.", - EMPTY_ARGS: "Empty " + INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists." }; const cliMessages = { From 4c4c15eeb7b179f1e2497ab125199ee7144040f1 Mon Sep 17 00:00:00 2001 From: roshan Date: Wed, 3 Mar 2021 15:17:56 +0530 Subject: [PATCH 7/7] removed repeating if block --- bin/helpers/zipUpload.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/bin/helpers/zipUpload.js b/bin/helpers/zipUpload.js index a848e89b..320caa16 100644 --- a/bin/helpers/zipUpload.js +++ b/bin/helpers/zipUpload.js @@ -37,16 +37,11 @@ const uploadCypressZip = (bsConfig, filePath) => { responseData = null } if (resp.statusCode != 200) { - if(resp.statusCode == 401){ - if(responseData && responseData["error"]){ - reject(responseData["error"]); - } else { + if(responseData && responseData["error"]){ + reject(responseData["error"]); + } else { + if(resp.statusCode == 401){ reject(Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS); - } - } - else{ - if (responseData && responseData["error"]) { - reject(responseData["error"]); } else { reject(Constants.userMessages.ZIP_UPLOADER_NOT_REACHABLE); }