I have 2 requests that needs to be done to set the authentication token , this token is passed to the REST call .
Here is the code that is there in the pre-tests
const siaPostRequest = {
url: âhttps://iam.stage1.bluemix.net/identity/tokenâ,
method: âPOSTâ,
header: {
âAcceptâ: âapplication/jsonâ,
âContent-Typeâ: âapplication/x-www-form-urlencodedâ
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "urn:ibm:params:oauth:grant-type:apikey"},
{key: "apikey", value: "xxx"},
{key: "Accept", value: "application/json"},
{key: "Content-Type", value: "application/x-www-form-urlencoded"}
]
}
};
var getToken = true;
if (!pm.environment.get(âaccessTokenExpiryâ) ||
!pm.environment.get(âcurrentAccessTokenâ)) {
console.log(âToken or expiry date are missingâ)
} else if (pm.environment.get(âaccessTokenExpiryâ) <= (new Date()).getTime()) {
console.log(âToken is expiredâ)
} else {
getToken = false;
console.log(âToken and expiry date are all goodâ);
}
if (getToken === true) {
pm.sendRequest(siaPostRequest, function (err, res) {
console.log(err ? err : res.json());
if (err === null) {
console.log(â***Abt to set the sia access token *******â);
var responseJson = res.json();
pm.environment.set(âsia_access_tokenâ,responseJson.access_token);
console.log('token from env auth1 â + pm.environment.get(âsia_access_tokenâ));
var expiryDate = new Date();
expiryDate.setSeconds(expiryDate.getSeconds() + 55);
pm.environment.set('accessTokenExpiry', expiryDate.getTime());
}
});
const siaDesiredPostRequest = {
url: âhttps://iam.stage1.bluemix.net/identity/tokenâ,
method: âPOSTâ,
header: {
âAcceptâ: âapplication/jsonâ,
âContent-Typeâ: âapplication/x-www-form-urlencodedâ
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "urn:ibm:params:oauth:grant-type:iam-authz"},
{key: "desired_iam_id", value: "crn-crn:v1:bluemix:public:siapoc::a/xxx:instance12345::"},
{key: "Accept", value: "application/json"},
{key: "Content-Type", value: "application/x-www-form-urlencoded"},
{key: "access_token", value: pm.environment.get("sia_access_token")},
]
}
};
pm.sendRequest(siaDesiredPostRequest, function (err, res) {
console.log(err ? err : res.json());
if (err === null) {
console.log(â******ABt to set the SIA desired token *************************â);
var responseJson1 = res.json();
pm.environment.set(âsia_desired_tokenâ,responseJson1.access_token);
console.log(âsia_desired_tokenâ + pm.environment.get(âsia_desired_tokenâ));
}
});
}
Here is the response from the log
******ABt to set the SIA desired token *************************
then
***Abt to set the sia access token *******
So it looks like the order of calling the requests are changed , it need to first then second as the first call output is fed to the second one .So the tests are failing.