-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
71 lines (62 loc) · 1.92 KB
/
utils.js
File metadata and controls
71 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import http from "k6/http";
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
function load_profile(profile_path) {
if (profile_path === undefined) {
return {};
}
let profile = {};
try{
profile = JSON.parse(open(profile_path));
}
catch(error){
console.error("Invalid profile file.");
throw error;
}
return profile;
}
function get_profile() {
const default_profile = load_profile("default.json");
const profile = load_profile(__ENV.PROFILE);
return Object.assign(default_profile, profile);
}
function getUser(i){
return {
username: `performance__user_${i+1}_browser`,
email: `performance__user_${i+1}_browser@test.com`,
password: `password${i+1}`,
is_active: true,
redirect: true,
};
}
function createUser(i, lms_root_url, course_id){
const user = getUser(i);
const lmsUrl = new URL(`${lms_root_url}/auto_auth`);
lmsUrl.searchParams.append('username', user.username);
lmsUrl.searchParams.append('email', user.email);
lmsUrl.searchParams.append('password', user.password);
lmsUrl.searchParams.append('course_id', course_id);
lmsUrl.searchParams.append('is_active', user.is_active.toString());
lmsUrl.searchParams.append('redirect', "false");
const res = http.get(lmsUrl.toString());
console.info(`Creating user ${user.username}: [${res.status}]`);
return user;
}
function loginUser(i, lms_root_url, lms_login_session_path){
// Proceed with the Login
const user = getUser(i);
const res = http.get(lms_root_url);
const csrfToken = res.cookies.csrftoken[0].value;
const formData = {
email: user.email,
password: user.password,
};
const headers = {
"x-csrftoken": csrfToken,
referer: lms_root_url,
};
const loginRes = http.post(lms_root_url + lms_login_session_path, formData, {
headers: headers,
});
console.debug(`[${loginRes.status}] ${lms_login_session_path}`);
}
export { get_profile, createUser, loginUser, getUser };