mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-24 17:03:47 +01:00
Added bruno-cli support for mapping inherited apikey auth from collection
This commit is contained in:
parent
4894ac2754
commit
eda5492363
@ -35,7 +35,7 @@ const prepareRequest = (request, collectionRoot) => {
|
||||
};
|
||||
|
||||
const collectionAuth = get(collectionRoot, 'request.auth');
|
||||
if (collectionAuth && request.auth.mode === 'inherit') {
|
||||
if (collectionAuth && request.auth?.mode === 'inherit') {
|
||||
if (collectionAuth.mode === 'basic') {
|
||||
axiosRequest.auth = {
|
||||
username: get(collectionAuth, 'basic.username'),
|
||||
@ -46,9 +46,22 @@ const prepareRequest = (request, collectionRoot) => {
|
||||
if (collectionAuth.mode === 'bearer') {
|
||||
axiosRequest.headers['Authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`;
|
||||
}
|
||||
|
||||
if (collectionAuth.mode === 'apikey') {
|
||||
if (collectionAuth.apikey?.placement === 'header') {
|
||||
axiosRequest.headers[collectionAuth.apikey?.key] = collectionAuth.apikey?.value;
|
||||
}
|
||||
|
||||
if (request.auth) {
|
||||
if (collectionAuth.apikey?.placement === 'queryparams') {
|
||||
if (axiosRequest.url) {
|
||||
const prefix = (!axiosRequest.url.includes('?')) ? '?' : '&';
|
||||
axiosRequest.url += `${prefix}${collectionAuth.apikey.key}=${collectionAuth.apikey.value}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (request.auth && request.auth.mode !== 'inherit') {
|
||||
if (request.auth.mode === 'basic') {
|
||||
axiosRequest.auth = {
|
||||
username: get(request, 'auth.basic.username'),
|
||||
|
@ -18,4 +18,126 @@ describe('prepare-request: prepareRequest', () => {
|
||||
expect(result.data).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Properly maps inherited auth from collectionRoot', () => {
|
||||
// Initialize Test Fixtures
|
||||
let collectionRoot, request;
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset test fixtures
|
||||
collectionRoot = {
|
||||
request: {
|
||||
auth: {}
|
||||
}
|
||||
};
|
||||
|
||||
request = {
|
||||
url: 'https://www.usebruno.com',
|
||||
auth: {
|
||||
mode: "inherit"
|
||||
},
|
||||
body: {
|
||||
mode: 'json',
|
||||
json: '{\n"test": {{someVar}} // comment\n}'
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('If collection auth is apikey in header', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: "apikey",
|
||||
apikey: {
|
||||
key: "x-api-key",
|
||||
value: "{{apiKey}}",
|
||||
placement: "header"
|
||||
}
|
||||
};
|
||||
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
|
||||
});
|
||||
|
||||
|
||||
it('If collection auth is apikey in header and request has existing headers', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: "apikey",
|
||||
apikey: {
|
||||
key: "x-api-key",
|
||||
value: "{{apiKey}}",
|
||||
placement: "header"
|
||||
}
|
||||
};
|
||||
|
||||
request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }];
|
||||
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
|
||||
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
|
||||
});
|
||||
|
||||
it('If collection auth is apikey in query parameters', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: "apikey",
|
||||
apikey: {
|
||||
key: "apiKey",
|
||||
value: "{{apiKey}}",
|
||||
placement: "queryparams"
|
||||
}
|
||||
};
|
||||
|
||||
const expected = `${request.url}?${collectionRoot.request.auth.apikey.key}=${collectionRoot.request.auth.apikey.value}`;
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
expect(result.url).toEqual(expected);
|
||||
});
|
||||
|
||||
it('If request does not have auth configured', () => {
|
||||
delete request.auth;
|
||||
let result;
|
||||
expect(() => {
|
||||
result = prepareRequest(request, collectionRoot);
|
||||
}).not.toThrow();
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('If collection auth is basic auth', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: 'basic',
|
||||
basic: {
|
||||
username: 'testUser',
|
||||
password: 'testPass123'
|
||||
}
|
||||
};
|
||||
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
const expected = { username: 'testUser', password: 'testPass123' };
|
||||
expect(result.auth).toEqual(expected);
|
||||
});
|
||||
|
||||
it('If collection auth is bearer token', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: 'bearer',
|
||||
bearer: {
|
||||
token: 'token'
|
||||
}
|
||||
};
|
||||
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
|
||||
});
|
||||
|
||||
it('If collection auth is bearer token and request has existing headers', () => {
|
||||
collectionRoot.request.auth = {
|
||||
mode: 'bearer',
|
||||
bearer: {
|
||||
token: 'token'
|
||||
}
|
||||
};
|
||||
|
||||
request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }];
|
||||
|
||||
const result = prepareRequest(request, collectionRoot);
|
||||
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
|
||||
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user