Skip to content

Commit 539ce9d

Browse files
Japneet Singh ChawlaJapneet Singh Chawla
authored andcommitted
node reject unauthorized flag added
1 parent e5f19d8 commit 539ce9d

File tree

13 files changed

+277
-128
lines changed

13 files changed

+277
-128
lines changed

commands/build_info.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,32 @@ function get_build_info(args) {
3939
);
4040
}
4141
}
42-
43-
request(
44-
constants[env].BUILD_BASE_URL + args.buildId,
45-
{
46-
auth: {
47-
username: username,
48-
password: access_key,
49-
},
42+
let options = {
43+
url: constants[env].BUILD_BASE_URL + args.buildId,
44+
auth: {
45+
username: username,
46+
password: access_key,
5047
},
51-
(err, res, body) => {
52-
if (err) {
53-
reject(err);
48+
};
49+
if ("reject_unauthorized" in args) {
50+
if (
51+
args["reject_unauthorized"] != "false" &&
52+
args["reject_unauthorized"] != "true"
53+
) {
54+
console.log("reject_unauthorized has to boolean");
55+
return;
56+
} else {
57+
if (args["reject_unauthorized"] == "false") {
58+
options["rejectUnauthorized"] = false;
59+
console.log("Setting rejectUnauthorized to false for web requests");
5460
}
61+
}
62+
}
63+
64+
request.get(options, (err, res, body) => {
65+
if (err) {
66+
reject(err);
67+
} else {
5568
if (res.statusCode == "401") {
5669
resolve("Unauthorized");
5770
} else if (JSON.parse(body).status == "success") {
@@ -60,7 +73,7 @@ function get_build_info(args) {
6073
resolve(JSON.parse(body).message);
6174
}
6275
}
63-
);
76+
});
6477
});
6578
}
6679

commands/build_stop.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ function stop_session(args) {
7272
Username: username,
7373
},
7474
};
75+
76+
if ("reject_unauthorized" in args) {
77+
if (
78+
args["reject_unauthorized"] != "false" &&
79+
args["reject_unauthorized"] != "true"
80+
) {
81+
console.log("reject_unauthorized has to boolean");
82+
return;
83+
} else {
84+
if (args["reject_unauthorized"] == "false") {
85+
options["rejectUnauthorized"] = false;
86+
console.log("Setting rejectUnauthorized to false for web requests");
87+
}
88+
}
89+
}
7590
request.put(options, function (err, resp, body) {
7691
if (err) {
7792
reject(err);

commands/generate_reports.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ var fs = require("fs");
77
const StreamZip = require("node-stream-zip");
88
const path = require("path");
99

10-
function download_artefact(username, access_key, env, test_id, file_path) {
10+
function download_artefact(
11+
username,
12+
access_key,
13+
env,
14+
test_id,
15+
file_path,
16+
rejectUnauthorized
17+
) {
1118
return new Promise(function (resolve, reject) {
1219
let response_code;
1320
if (!fs.existsSync(file_path)) {
@@ -18,23 +25,26 @@ function download_artefact(username, access_key, env, test_id, file_path) {
1825
file_path = path.join(file_path, "artefacts.zip");
1926
const stream = fs.createWriteStream(file_path);
2027
stream.end();
21-
request(
22-
constants[env].REPORT_URL + test_id,
23-
{
24-
auth: {
25-
username: username,
26-
password: access_key,
27-
},
28-
gzip: true,
29-
timeout: 120000,
28+
let options = {
29+
url: constants[env].REPORT_URL + test_id,
30+
auth: {
31+
username: username,
32+
password: access_key,
3033
},
31-
(err, res, body) => {
32-
if (err) {
33-
reject(err);
34-
}
35-
response_code = res.statusCode;
34+
gzip: true,
35+
timeout: 120000,
36+
};
37+
if (rejectUnauthorized == false) {
38+
options["rejectUnauthorized"] = false;
39+
console.log("Setting rejectUnauthorized to false for web requests");
40+
}
41+
42+
request(options, (err, res, body) => {
43+
if (err) {
44+
reject(err);
3645
}
37-
).pipe(
46+
response_code = res.statusCode;
47+
}).pipe(
3848
fs
3949
.createWriteStream(file_path, {
4050
overwrite: true,
@@ -132,8 +142,25 @@ function generate_report(args) {
132142
username: username,
133143
access_key: access_key,
134144
},
145+
run_settings: {
146+
reject_unauthorized: true,
147+
},
135148
};
136149

150+
if ("reject_unauthorized" in args) {
151+
if (
152+
args["reject_unauthorized"] != "false" &&
153+
args["reject_unauthorized"] != "true"
154+
) {
155+
console.log("reject_unauthorized has to boolean");
156+
return;
157+
} else {
158+
if (args["reject_unauthorized"] == "false") {
159+
build_payload["run_settings"]["reject_unauthorized"] = false;
160+
console.log("Setting rejectUnauthorized to false for web requests");
161+
}
162+
}
163+
}
137164
build_stats
138165
.get_completed_build_info(build_payload, args["session_id"], env)
139166
.then(function (build_info) {
@@ -166,7 +193,8 @@ function generate_report(args) {
166193
build_info["data"][i]["browser"],
167194
build_info["data"][i]["version"],
168195
build_info["data"][i]["test_id"]
169-
)
196+
),
197+
build_payload["run_settings"]["reject_unauthorized"]
170198
)
171199
.then(function (resp) {
172200
//Files downloaded

commands/run.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,25 @@ module.exports = function (args) {
2828
);
2929
}
3030
}
31+
let rejectUnauthorized = true;
32+
if ("reject_unauthorized" in args) {
33+
if (
34+
args["reject_unauthorized"] != "false" &&
35+
args["reject_unauthorized"] != "true"
36+
) {
37+
console.log("reject_unauthorized has to boolean");
38+
return;
39+
} else {
40+
if (args["reject_unauthorized"] == "false") {
41+
rejectUnauthorized = false;
42+
console.log("Setting rejectUnauthorized to false for web requests");
43+
} else {
44+
rejectUnauthorized = true;
45+
}
46+
}
47+
}
3148
validate_cli
32-
.validate_cli(env)
49+
.validate_cli(env, rejectUnauthorized)
3350
.then(function (resp) {
3451
let cli_flag = false;
3552
for (let i = 0; i < resp["value"].length; i++) {

commands/utils/batch/batch_runner.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const builds = require("../poller/build");
1616
var batchCounter = 0;
1717
var totalBatches = 0;
1818

19-
function run_test(payload, env = "prod") {
19+
function run_test(payload, env = "prod", rejectUnauthorized) {
2020
return new Promise(function (resolve, reject) {
2121
let options = {
2222
url: constants[env].INTEGRATION_BASE_URL + constants.RUN_URL,
2323
body: payload,
2424
};
25-
25+
if (rejectUnauthorized == false) {
26+
options["rejectUnauthorized"] = false;
27+
}
2628
let responseData = null;
2729
request.post(options, function (err, resp, body) {
2830
if (err) {
@@ -111,7 +113,12 @@ async function run(lt_config, batches, env, i = 0) {
111113
access_key: lt_config["lambdatest_auth"]["access_key"],
112114
type: "cypress",
113115
});
114-
run_test(payload, env)
116+
117+
run_test(
118+
payload,
119+
env,
120+
lt_config.run_settings.reject_unauthorized
121+
)
115122
.then(function (session_id) {
116123
delete_archive(project_file);
117124
delete_archive(file_obj["name"]);

commands/utils/poller/build.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@ const request = require("request");
33
//https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop/?sessionId=4a7434b9-1905-4aaf-a178-9167acb00c5d
44
function stop_cypress_session(lt_config, session_id, env) {
55
return new Promise(function (resolve, reject) {
6-
request(
7-
constants[env].BUILD_STOP_URL + session_id,
8-
{
9-
auth: {
10-
username: lt_config["lambdatest_auth"]["username"],
11-
password: lt_config["lambdatest_auth"]["access_key"],
12-
},
13-
method: "PUT",
6+
let options = {
7+
url: constants[env].BUILD_STOP_URL + session_id,
8+
headers: {
9+
Authorization: "Token " + lt_config["lambdatest_auth"]["access_key"],
10+
Username: lt_config["lambdatest_auth"]["username"],
1411
},
15-
(err, res, body) => {
16-
if (err) {
17-
console.log("Error occured while stopping session", err);
18-
reject(err);
19-
}
20-
if (res.statusCode == "401") {
21-
console.log("Error Occured: Unauthorized access to session-stop");
22-
reject("Unauthorized");
23-
} else if (res.statusCode == "200") {
24-
console.log("Session stopped successfully");
25-
resolve(JSON.parse(body));
26-
} else {
27-
console.log(body);
28-
reject("No response for session stop");
29-
}
12+
method: "PUT",
13+
};
14+
if (lt_config.run_settings.reject_unauthorized == false) {
15+
options["rejectUnauthorized"] = false;
16+
}
17+
18+
request.put(options, (err, res, body) => {
19+
if (err) {
20+
console.log("Error occured while stopping session", err);
21+
reject(err);
22+
}
23+
if (res.statusCode == "401") {
24+
console.log("Error Occured: Unauthorized access to session-stop");
25+
reject("Unauthorized");
26+
} else if (res.statusCode == "200") {
27+
console.log("Session stopped successfully");
28+
resolve(JSON.parse(body));
29+
} else {
30+
console.log(body);
31+
reject("No response for session stop");
3032
}
31-
);
33+
});
3234
});
3335
}
3436

0 commit comments

Comments
 (0)