I connect to Azure Web Apps and I’m assuming its a similar process.
I have two working methods.
- Using form data as the body of the request.
With the following code in the Tests tab to set the bearer token for the next request.
response = pm.response.json();
pm.collectionVariables.set("bearerToken", response.id_token); // used in the authorisation header bearer token
pm.collectionVariables.set("bearerTokenExpiresOn", response.expires_in);
//Step 1: Define the schema
const schema = {
'type': 'object',
'properties': {
'token_type': {
type: 'string'
},
'scope': {
type: 'string'
},
'expires_in': {
type: 'number'
},
'ext_expires_in': {
type: 'number'
},
'access_token': {
type: 'string'
},
'id_token': {
type: 'string'
}
},
required: ['token_type', 'scope', 'expires_in', 'ext_expires_in', 'access_token', 'id_token']
};
//Step 2: Validate response against schema
if(pm.response.code===200)
pm.test('MicrosoftOnline Login response schema is valid', () => {
pm.expect(response).to.have.jsonSchema(schema);
});
The next request just needs to have the bearer token set to the variable. This will then bypass the Microsoft login and take you directly to your API data.
- Use a pre-request script and sendRequest() instead.
I prefer this option as it allows you some control over the token expiry.
pm.test("Check for collectionVariables", function () {
let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
vars.forEach(function (item, index, array) {
console.log(item, index);
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty;
});
if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
pm.sendRequest({
url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/v2.0/token',
method: 'POST',
header: 'Content-Type: application/x-www-form-urlencoded',
body: {
mode: 'urlencoded',
urlencoded: [
{ key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
{ key: "scope", value: pm.collectionVariables.get("scope"), disabled: false },
{ key: "username", value: pm.collectionVariables.get("username"), disabled: false },
{ key: "password", value: pm.collectionVariables.get("password"), disabled: false },
{ key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
{ key: "grant_type", value: "password", disabled: false },
]
}
}, function (err, res) {
if (err) {
console.log(err);
} else {
pm.test("Status code is 200", () => {
pm.expect(res).to.have.status(200);
});
let resJson = res.json();
pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_in);
pm.collectionVariables.set("bearerToken", resJson.id_token);
}
});
}
});
It won’t be exactly the same for you, but hopefully will give you some options to try.
Both of these example are based on the premise that you are testing the application, not the authentication per se (which is hosted by Microsoft).