My end Goal is to simulate(1000users) a User’s registration, login, grab the token from the successful login’s response and use the token to join a group and remain in the group for certain duration of time. But for now I am using 20 users. I followed this post to make sure that each user registration is carried out by individual VUs without collision. My goal for now is let 20 VUs to register, then the VUs will login with their credentials, after login the script will extract the token from the response and finally the VU will carry this token and join the group and stay in the group for a while for a duration mentioned in the script.
import http from 'k6/http';
import { check, group, sleep, fail } from 'k6';
export let options = {
stages: [{ target: 20, duration: '30s' }],
//thresholds: {
//http_req_duration: ['p(95)<500', 'p(99)<1500'],
//'http_req_duration{name:PublicCrocs}': ['avg<400'],
//'http_req_duration{name:Create}': ['avg<600', 'max<1000'],
//},
};
export default function main() {
let uniqueNumber = __VU * 20 + __ITER; //setting up unique VUs
//This flow is intended that unique VUs each carry an email with same Password
//1) Register
//2) Login with email and Password
//3) Token is grabbed
//4) Joining the group with the token no need user credentials here,
// since the token from the logged in user itself is enough to login
//I am looping here so that I create individual email IDs as usernames
//and a standard pass to register
for (var id = 1; id <= 20; id++) {
const USERNAME = `ithayam_${id}@target.com`[uniqueNumber];
const PASSWORD = 'test123';
const BASE_URL = 'https://target.org';
console.log(` checking for USERNAME ${USERNAME}`);
console.log(` checking for PASSWORD ${PASSWORD}`);
console.log(` checking for BASE_URL ${BASE_URL}`);
// register a new user
let res;
res = http.post(`${BASE_URL}/api/v1/register`,
{
"email": `${USERNAME}`,
"password": `${PASSWORD}`,
"confirm_password": `${PASSWORD}`,
"first_name": "Real",
"last_name": "Original",
"coherenceGoal": "300",
"isUnder16": "false",
"opt_in": "true",
"is_admin": "false",
"inspiration": "false",
"offers": "false",
"birthday": "2000-11-26",
"parental_email": "test@realoriginal.in"
},
{
headers: {
"Content-Type": "application/json",
Accept: "*/*",
Host: "target.org",
Connection: "keep-alive",
},
}
);
console.log(` checking for auth_token after registration ${res.status} ${res.body}`);
check(res, { 'auth_token': (r) => r.status === 200 }); //201 ==> 200, there's no success message so I am using the response token variable to check the success
//Login and grab token
let loginRes;
loginRes= http.post(`${BASE_URL}/api/v1/login`,
{
"email": `${USERNAME}`,
"password": `${PASSWORD}`
},
{
headers: {
Accept: "*/*",
"Content-Type": "application/json",
Host: "target.org",
Connection: "keep-alive",
"User-Agent": "Mozilla/5.0",
},
}
);
console.log(` checking for auth_token after login ${loginRes.status} ${loginRes.body}`);
let authToken = loginRes.json('auth_token');
check(authToken, { 'auth_token': () => authToken !== '' });
return authToken;
function auth(authToken) {
const requestConfigWithTag = (tag) => ({
headers: {
Authorization: `Token ${authToken}`,
},
tags: Object.assign(
{},
{
name: 'AccessToken',
},
tag,
),
});
//Join group 1991 with the token fetched during login
let joinRes = http.post(`${BASE_URL}/api/v1/auth/group/join/1991`, requestConfigWithTag({ name: 'AccessToken' }));
};
}//closing loop
sleep(1);
}
Getting this error, the PASSWORD and BASE_URL variables are coming out well, its the USERNAME’s value thats the issue is in this line const USERNAME =
ithayam_${id}@target.com[uniqueNumber];
but again I used the “uniqueNumber” to make sure that it doesnt have any collision in the future when I scale it. Also I am positive and I checked that removing [uniqueNumber]
gets the right value into ${USERNAME}
I have also attached the following screenshots how the request and response of Registration, Login and Join Group would like when done using CURL for a single user. I intend to do this in a large scale via k6
Registration
Login
Join group 1991(I have used the Token from above and used it in the header)