Skip to content

Node Reject Unauth Flag #162

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 3 commits into from
Jun 27, 2022
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
37 changes: 25 additions & 12 deletions commands/build_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,32 @@ function get_build_info(args) {
);
}
}

request(
constants[env].BUILD_BASE_URL + args.buildId,
{
auth: {
username: username,
password: access_key,
},
let options = {
url: constants[env].BUILD_BASE_URL + args.buildId,
auth: {
username: username,
password: access_key,
},
(err, res, body) => {
if (err) {
reject(err);
};
if ("reject_unauthorized" in args) {
if (
args["reject_unauthorized"] != "false" &&
args["reject_unauthorized"] != "true"
) {
console.log("reject_unauthorized has to boolean");
return;
} else {
if (args["reject_unauthorized"] == "false") {
options["rejectUnauthorized"] = false;
console.log("Setting rejectUnauthorized to false for web requests");
}
}
}

request.get(options, (err, res, body) => {
if (err) {
reject(err);
} else {
if (res.statusCode == "401") {
resolve("Unauthorized");
} else if (JSON.parse(body).status == "success") {
Expand All @@ -60,7 +73,7 @@ function get_build_info(args) {
resolve(JSON.parse(body).message);
}
}
);
});
});
}

Expand Down
15 changes: 15 additions & 0 deletions commands/build_stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ function stop_session(args) {
Username: username,
},
};

if ("reject_unauthorized" in args) {
if (
args["reject_unauthorized"] != "false" &&
args["reject_unauthorized"] != "true"
) {
console.log("reject_unauthorized has to boolean");
return;
} else {
if (args["reject_unauthorized"] == "false") {
options["rejectUnauthorized"] = false;
console.log("Setting rejectUnauthorized to false for web requests");
}
}
}
request.put(options, function (err, resp, body) {
if (err) {
reject(err);
Expand Down
62 changes: 45 additions & 17 deletions commands/generate_reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ var fs = require("fs");
const StreamZip = require("node-stream-zip");
const path = require("path");

function download_artefact(username, access_key, env, test_id, file_path) {
function download_artefact(
username,
access_key,
env,
test_id,
file_path,
rejectUnauthorized
) {
return new Promise(function (resolve, reject) {
let response_code;
if (!fs.existsSync(file_path)) {
Expand All @@ -18,23 +25,26 @@ function download_artefact(username, access_key, env, test_id, file_path) {
file_path = path.join(file_path, "artefacts.zip");
const stream = fs.createWriteStream(file_path);
stream.end();
request(
constants[env].REPORT_URL + test_id,
{
auth: {
username: username,
password: access_key,
},
gzip: true,
timeout: 120000,
let options = {
url: constants[env].REPORT_URL + test_id,
auth: {
username: username,
password: access_key,
},
(err, res, body) => {
if (err) {
reject(err);
}
response_code = res.statusCode;
gzip: true,
timeout: 120000,
};
if (rejectUnauthorized == false) {
options["rejectUnauthorized"] = false;
console.log("Setting rejectUnauthorized to false for web requests");
}

request(options, (err, res, body) => {
if (err) {
reject(err);
}
).pipe(
response_code = res.statusCode;
}).pipe(
fs
.createWriteStream(file_path, {
overwrite: true,
Expand Down Expand Up @@ -132,8 +142,25 @@ function generate_report(args) {
username: username,
access_key: access_key,
},
run_settings: {
reject_unauthorized: true,
},
};

if ("reject_unauthorized" in args) {
if (
args["reject_unauthorized"] != "false" &&
args["reject_unauthorized"] != "true"
) {
console.log("reject_unauthorized has to boolean");
return;
} else {
if (args["reject_unauthorized"] == "false") {
build_payload["run_settings"]["reject_unauthorized"] = false;
console.log("Setting rejectUnauthorized to false for web requests");
}
}
}
build_stats
.get_completed_build_info(build_payload, args["session_id"], env)
.then(function (build_info) {
Expand Down Expand Up @@ -166,7 +193,8 @@ function generate_report(args) {
build_info["data"][i]["browser"],
build_info["data"][i]["version"],
build_info["data"][i]["test_id"]
)
),
build_payload["run_settings"]["reject_unauthorized"]
)
.then(function (resp) {
//Files downloaded
Expand Down
19 changes: 18 additions & 1 deletion commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,25 @@ module.exports = function (args) {
return;
}
}
let rejectUnauthorized = true;
if ("reject_unauthorized" in args) {
if (
args["reject_unauthorized"] != "false" &&
args["reject_unauthorized"] != "true"
) {
console.log("reject_unauthorized has to boolean");
return;
} else {
if (args["reject_unauthorized"] == "false") {
rejectUnauthorized = false;
console.log("Setting rejectUnauthorized to false for web requests");
} else {
rejectUnauthorized = true;
}
}
}
validate_cli
.validate_cli(env)
.validate_cli(env, rejectUnauthorized)
.then(function (resp) {
let cli_flag = false;
for (let i = 0; i < resp["value"].length; i++) {
Expand Down
13 changes: 10 additions & 3 deletions commands/utils/batch/batch_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ const builds = require("../poller/build");
var batchCounter = 0;
var totalBatches = 0;

function run_test(payload, env = "prod") {
function run_test(payload, env = "prod", rejectUnauthorized) {
return new Promise(function (resolve, reject) {
let options = {
url: constants[env].INTEGRATION_BASE_URL + constants.RUN_URL,
body: payload,
};

if (rejectUnauthorized == false) {
options["rejectUnauthorized"] = false;
}
let responseData = null;
request.post(options, function (err, resp, body) {
if (err) {
Expand Down Expand Up @@ -111,7 +113,12 @@ async function run(lt_config, batches, env, i = 0) {
access_key: lt_config["lambdatest_auth"]["access_key"],
type: "cypress",
});
run_test(payload, env)

run_test(
payload,
env,
lt_config.run_settings.reject_unauthorized
)
.then(function (session_id) {
delete_archive(project_file);
delete_archive(file_obj["name"]);
Expand Down
50 changes: 26 additions & 24 deletions commands/utils/poller/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@ const request = require("request");
//https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop/?sessionId=4a7434b9-1905-4aaf-a178-9167acb00c5d
function stop_cypress_session(lt_config, session_id, env) {
return new Promise(function (resolve, reject) {
request(
constants[env].BUILD_STOP_URL + session_id,
{
auth: {
username: lt_config["lambdatest_auth"]["username"],
password: lt_config["lambdatest_auth"]["access_key"],
},
method: "PUT",
let options = {
url: constants[env].BUILD_STOP_URL + session_id,
headers: {
Authorization: "Token " + lt_config["lambdatest_auth"]["access_key"],
Username: lt_config["lambdatest_auth"]["username"],
},
(err, res, body) => {
if (err) {
console.log("Error occured while stopping session", err);
reject(err);
}
if (res.statusCode == "401") {
console.log("Error Occured: Unauthorized access to session-stop");
reject("Unauthorized");
} else if (res.statusCode == "200") {
console.log("Session stopped successfully");
resolve(JSON.parse(body));
} else {
console.log(body);
reject("No response for session stop");
}
method: "PUT",
};
if (lt_config.run_settings.reject_unauthorized == false) {
options["rejectUnauthorized"] = false;
}

request.put(options, (err, res, body) => {
if (err) {
console.log("Error occured while stopping session", err);
reject(err);
}
if (res.statusCode == "401") {
console.log("Error Occured: Unauthorized access to session-stop");
reject("Unauthorized");
} else if (res.statusCode == "200") {
console.log("Session stopped successfully");
resolve(JSON.parse(body));
} else {
console.log(body);
reject("No response for session stop");
}
);
});
});
}

Expand Down
Loading