My question:
I have a small collection with a few requests. As all requests require a JWT Token I implemented a method in collection pre-request scripts that makes the authorization requests and stores the token as an environment variable.
Along with this method I have a few common test methods like schema validation, etc.
If I run the collection using postman, postman runner or newman on my machine everything works as expected.
If I put the newman execution inside a step in a Jenkins pipeline (Linux machine) I get an error “Unexpected token { in pre-request script”
Details (like screenshots):
/******************************************
GLOBAL FUNCTIONS USED IN ALL TESTS
******************************************/
utils = {
validate_response_code: function(pm, expected_code) {
pm.response.to.have.status(expected_code)
},
validate_response_is_json: function validate_response_is_json(pm)
{
var json_response = null;
try
{
json_response = pm.response.json();
} catch {
json = null;
} finally {
console.log(json_response);
pm.expect(json_response).not.eq(null);
}
},
validate_response_is_not_json: function validate_response_is_not_json(pm)
{
var json_response = null;
try
{
json_response = pm.response.json();
} catch {
json = null;
} finally {
console.log(json_response);
pm.expect(json_response).eq(null);
}
},
validate_schema: function validate_schema(pm, schema)
{
var response_data = pm.response.json();
var Ajv = require("ajv");
var ajv = new Ajv({logger: console}, schema);
pm.expect(ajv.validate(schema, pm.response.json()), JSON.stringify(ajv.errors)).to.be.true;
},
get_error_schema: function get_error_schema()
{
const schema = {
"type": "object",
"properties": {
"timestamp": {
"type": "string"
},
"status": {
"type": "integer"
},
"error": {
"type": "string"
},
"message": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"timestamp",
"status",
"error",
"message",
"path"
]
};
return schema;
}
}
/******************************************
END OF GLOBAL FUNCTIONS USED IN ALL TESTS
******************************************/
// Local use variables
var is_patch_ok = false;
var is_post_ok = false;
var token = null;
// Environment variables
var debug = pm.environment.get("debug");
var host_sts = pm.environment.get("host_sts");
var client_id = pm.environment.get("client_id");
var client_secret = pm.environment.get("client_secret");
var admin_user_id_sts = pm.environment.get("admin_user_id_sts");
var admin_user_password_sts = pm.environment.get("admin_user_password_sts");
// Iteration data variables
// If not set in iteration data, we are executing this in Postman, so we get the value from environment
var user_global_id = pm.iterationData.get("user_global_id");
if (user_global_id === undefined)
{
user_global_id = pm.environment.get("user_global_id");
}
// Calculated variables
var url = host_sts + "/admin/client/" + client_id;
var basic_auth = admin_user_id_sts + ":" + admin_user_password_sts;
var auth_header = "Basic " + CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(basic_auth));
// Variable logs
if (debug == "true")
{
console.log(client_id);
console.log(client_secret);
console.log(admin_user_id_sts);
console.log(admin_user_password_sts);
console.log(basic_auth);
console.log(url);
console.log(auth_header);
}
// START STEP 1 (PATCH) EXECUTION
pm.sendRequest({
url: url,
method: 'PATCH',
header: {
'Authorization': auth_header,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify(
{
userId: user_global_id,
extraClaims: {
sec_mode: "platform",
country: "XXXXXXXXXXXXXXXXXX",
local_realm: "XXXXXXXXXXXXXXXXX"
},
audience: [
"XXXXXXXXXXXXXXXXXXX"
]
}
)
}
}, function (err, res) {
if(res.code === 204)
{
if (debug == "true")
{
console.log("\tPATCH execution succeeded");
}
// Execute Step 2 of authentication once the first request is complete and correct
// Calculated variables
let url = XXXXXXXXXXXXXX;
let basic_auth = client_id + ":" + client_secret;
let auth_header = "Basic " + CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(basic_auth));
if (debug == "true")
{
console.log(basic_auth);
console.log(url);
console.log(auth_header);
}
pm.sendRequest({
url: url,
method: 'POST',
header: {
'Authorization': auth_header,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify(
{
userId: client_id,
extraClaims: {
sec_mode: "platform",
country: "XXXXXXXXXX",
local_realm: "XXXXXXXXXX"
},
audience: [
"XXXXXXXXXXXXXXX"
]
}
)
}
}, function (err, res) {
if(res.code === 200)
{
let response = res.json();
is_post_ok = true;
let token = response.token;
// Save the token to environment for further uses
token = "Bearer " + token;
pm.environment.set("token", token);
if (debug == "true")
{
console.log(token);
}
} else {
console.log("\t\tError executing POST.")
console.log(res);
console.log(err);
}
});
} else {
console.log("\tError executing PATCH.")
console.log(res);
console.log(err);
}
});
I’ve already tried: