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.