Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Inherited apikey auth mapping for bruno-cli #3512

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/bruno-cli/src/runner/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const prepareRequest = (item = {}, collection = {}) => {
};

const collectionAuth = get(collection, 'root.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'),
Expand All @@ -47,9 +47,22 @@ const prepareRequest = (item = {}, collection = {}) => {
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 (collectionAuth.apikey?.placement === 'queryparams') {
if (axiosRequest.url) {
const prefix = (!axiosRequest.url.includes('?')) ? '?' : '&';
axiosRequest.url += `${prefix}${collectionAuth.apikey.key}=${collectionAuth.apikey.value}`;
}
}
}
}

if (request.auth) {
if (request.auth && request.auth.mode !== 'inherit') {
if (request.auth.mode === 'basic') {
axiosRequest.auth = {
username: get(request, 'auth.basic.username'),
Expand Down
143 changes: 141 additions & 2 deletions packages/bruno-cli/tests/runner/prepare-request.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { describe, it, expect } = require('@jest/globals');

const { describe, it, expect, beforeEach } = require('@jest/globals');
const prepareRequest = require('../../src/runner/prepare-request');

describe('prepare-request: prepareRequest', () => {
Expand All @@ -22,4 +21,144 @@ describe('prepare-request: prepareRequest', () => {
expect(result.data).toEqual(expected);
});
});

describe('Properly maps inherited auth from collectionRoot', () => {
// Initialize Test Fixtures
let collection, item;

beforeEach(() => {
collection = {
name: 'Test Collection',
root: {
request: {
auth: {}
}
}
};

item = {
name: 'Test Request',
type: 'http-request',
request: {
method: 'GET',
headers: [],
params: [],
url: 'https://usebruno.com',
auth: {
mode: 'inherit'
},
script: {
req: 'console.log("Pre Request")',
res: 'console.log("Post Response")'
}
}
};
});

describe('API Key Authentication', () => {
it('If collection auth is apikey in header', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
}
};

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
});

it('If collection auth is apikey in header and request has existing headers', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
}
};

item.request.headers.push({ name: 'Content-Type', value: 'application/json', enabled: true });
const result = prepareRequest(item, collection);
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', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "queryparams"
}
};

const urlObj = new URL(item.request.url);
urlObj.searchParams.set(collection.root.request.auth.apikey.key, collection.root.request.auth.apikey.value);

const expected = urlObj.toString();
const result = prepareRequest(item, collection);
expect(result.url).toEqual(expected);
});
});

describe('Basic Authentication', () => {
it('If collection auth is basic auth', () => {
collection.root.request.auth = {
mode: 'basic',
basic: {
username: 'testUser',
password: 'testPass123'
}
};

const result = prepareRequest(item, collection);
const expected = { username: 'testUser', password: 'testPass123' };
expect(result.auth).toEqual(expected);
});
});

describe('Bearer Token Authentication', () => {
it('If collection auth is bearer token', () => {
collection.root.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
});

it('If collection auth is bearer token and request has existing headers', () => {
collection.root.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

item.request.headers.push({ name: 'Content-Type', value: 'application/json', enabled: true });

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
});
});

describe('No Authentication', () => {
it('If request does not have auth configured', () => {
delete item.request.auth;
let result;
expect(() => {
result = prepareRequest(item, collection);
}).not.toThrow();
expect(result).toBeDefined();
});
});
});
});
Loading