I'm encountering an issue where I need to create e2e api tests. The goal of the first test is to obtain a token for an unauthorized user, use that token in the method header for the second test to return a token for an authorized user, and then continue using that second token in subsequent tests. How can I store the token values in variables and pass them through the tests?
As a newcomer to JS, I am now facing a 'ReferenceError: auth_token is not defined'.
const chai = require('chai');
const request = require('request-promise-native');
const mocha = require('mocha');
const config = require('../config');
const assert = chai.assert;
describe('0_auth', () => {
it('should return token for unauthorized user', async () => {
const result = await request({
headers: config.headers,
url: `${config.url}/rest/v1/auth/get-token`,
method: "POST",
json: {
"deviceUuidSource": "DEVICE",
"source" : "KIOSK",
"deviceUuid" : "uniquedeviceuuid"
}
});
assert.isNotNull(result);
assert.property(result, 'token');
var auth_token=result.token;
console.log(auth_token)
}).timeout(15000);
it('should return token for authorized user', async () => {
const result = await request({
headers: Object.assign(config.headers, { 'Authorization': 'Bearer '+auth_token }),
url: `${config.url}/rest/v1/auth/with-password`,
method: "POST",
json: {
"email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1672637a736e6378257e6061265627267b7f7886623c6e6f64">[email protected]</a>",
"password" : "Test123"
}
});
assert.isNotNull(result);
assert.property(result, 'token');
assert.property(result, 'user');
console.log('token:', result.token);
}).timeout(15000);
});
For the following test, I plan to pass the Bearer token to the Authorization field in a different class called config.js
config.headers = {
'User-Agent': 'WOR API Tester', // android
Source: 'MOBILE',
'Accept-Language': 'EN',
Authorization:'Bearer '+auth_token;
};
module.exports = config;