mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-11 09:51:30 +00:00
Enhance auth handling in Postman converter
This commit is contained in:
@@ -140,27 +140,24 @@ const importCollectionLevelVariables = (variables, requestObject) => {
|
||||
requestObject.vars.req = vars;
|
||||
};
|
||||
|
||||
export const processAuth = (auth, requestObject, collection = false) => {
|
||||
export const processAuth = (auth, requestObject, isCollection = false) => {
|
||||
// As of 14/05/2025
|
||||
// When collections are set to "No Auth" in Postman, the auth object is null.
|
||||
// When folders and requests are set to "Inherit" in Postman, the auth object is null.
|
||||
// When folders and requests are set to "No Auth" in Postman, the auth object is present.
|
||||
|
||||
// Handle collection-specific "No Auth"
|
||||
if (collection && !auth) return; // Return as requestObject is a collection and has a default mode = none
|
||||
if (isCollection && !auth) return; // Return as requestObject is a collection and has a default mode = none
|
||||
|
||||
// Handle "Inherit Auth" (typically for non-collections when postmanAuth is null)
|
||||
if (!auth) {
|
||||
requestObject.auth.mode = AUTH_TYPES.INHERIT;
|
||||
return;
|
||||
}
|
||||
// Handle folder/request specific "Inherit"
|
||||
if (!auth) return; // Return as requestObject is a folder/request and has a default mode = inherit
|
||||
|
||||
// Handle explicit "No Auth"
|
||||
// Handle folder/request specific "No Auth"
|
||||
if (auth.type === AUTH_TYPES.NOAUTH) {
|
||||
requestObject.auth.mode = 'none';
|
||||
return;
|
||||
requestObject.auth.mode = AUTH_TYPES.NONE; // Set the mode to none
|
||||
return; // No further processing needed
|
||||
}
|
||||
|
||||
|
||||
let authValues = auth[auth.type] ?? [];
|
||||
if (Array.isArray(authValues)) {
|
||||
authValues = convertV21Auth(authValues);
|
||||
@@ -215,7 +212,7 @@ export const processAuth = (auth, requestObject, collection = false) => {
|
||||
};
|
||||
|
||||
const postmanGrantType = findValueUsingKey('grant_type');
|
||||
const targetGrantType = oauth2GrantTypeMaps[postmanGrantType] ?? 'client_credentials'; // Default
|
||||
const targetGrantType = oauth2GrantTypeMaps[postmanGrantType] || 'client_credentials'; // Default
|
||||
|
||||
// Common properties for all OAuth2 grant types
|
||||
const baseOAuth2Config = {
|
||||
@@ -264,6 +261,7 @@ export const processAuth = (auth, requestObject, collection = false) => {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
requestObject.auth.mode = AUTH_TYPES.NONE;
|
||||
console.warn('Unexpected auth.type:', auth.type, '- Mode set, but no specific config generated.');
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -73,6 +73,276 @@ describe('postman-collection', () => {
|
||||
const brunoCollection = await postmanToBruno(collectionWithEmptyVars);
|
||||
expect(brunoCollection.root.request.vars.req).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle collection with auth object having undefined type', async () => {
|
||||
const collectionWithUndefinedAuthType = {
|
||||
'info': {
|
||||
'_postman_id': '7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9',
|
||||
'name': 'collection with undefined auth type',
|
||||
'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
|
||||
},
|
||||
'auth': {
|
||||
'basic': [
|
||||
{ key: 'username', value: 'testuser', type: 'string' },
|
||||
{ key: 'password', value: 'testpass', type: 'string' }
|
||||
]
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'request',
|
||||
'request': {
|
||||
'method': 'GET',
|
||||
'header': [],
|
||||
'url': {
|
||||
'raw': 'https://api.example.com/test',
|
||||
'protocol': 'https',
|
||||
'host': ['api', 'example', 'com'],
|
||||
'path': ['test']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const brunoCollection = await postmanToBruno(collectionWithUndefinedAuthType);
|
||||
|
||||
// Collection level auth should default to 'none'
|
||||
expect(brunoCollection.root.request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
|
||||
// Request should inherit auth mode
|
||||
expect(brunoCollection.items[0].request.auth).toEqual({
|
||||
mode: 'inherit',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle collection with auth object having null type', async () => {
|
||||
const collectionWithNullAuthType = {
|
||||
'info': {
|
||||
'_postman_id': '7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9',
|
||||
'name': 'collection with null auth type',
|
||||
'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
|
||||
},
|
||||
'auth': {
|
||||
'type': null,
|
||||
'bearer': {
|
||||
'token': 'test-token'
|
||||
}
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'request',
|
||||
'request': {
|
||||
'method': 'GET',
|
||||
'header': [],
|
||||
'url': {
|
||||
'raw': 'https://api.example.com/test',
|
||||
'protocol': 'https',
|
||||
'host': ['api', 'example', 'com'],
|
||||
'path': ['test']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const brunoCollection = await postmanToBruno(collectionWithNullAuthType);
|
||||
|
||||
// Collection level auth should default to 'none'
|
||||
expect(brunoCollection.root.request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle collection with auth object having unexpected type value', async () => {
|
||||
const collectionWithUnexpectedAuthType = {
|
||||
'info': {
|
||||
'_postman_id': '7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9',
|
||||
'name': 'collection with unexpected auth type',
|
||||
'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
|
||||
},
|
||||
'auth': {
|
||||
'type': 'unexpected_auth_type',
|
||||
'basic': [
|
||||
{ key: 'username', value: 'testuser', type: 'string' },
|
||||
{ key: 'password', value: 'testpass', type: 'string' }
|
||||
]
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'request',
|
||||
'request': {
|
||||
'method': 'GET',
|
||||
'header': [],
|
||||
'url': {
|
||||
'raw': 'https://api.example.com/test',
|
||||
'protocol': 'https',
|
||||
'host': ['api', 'example', 'com'],
|
||||
'path': ['test']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const brunoCollection = await postmanToBruno(collectionWithUnexpectedAuthType);
|
||||
|
||||
// Collection level auth should default to 'none'
|
||||
expect(brunoCollection.root.request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
|
||||
// Request should inherit auth mode
|
||||
expect(brunoCollection.items[0].request.auth).toEqual({
|
||||
mode: 'inherit',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle request with auth object having undefined type', async () => {
|
||||
const collectionWithRequestUndefinedAuthType = {
|
||||
'info': {
|
||||
'_postman_id': '7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9',
|
||||
'name': 'collection with request undefined auth type',
|
||||
'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'request',
|
||||
'request': {
|
||||
'method': 'GET',
|
||||
'header': [],
|
||||
'url': {
|
||||
'raw': 'https://api.example.com/test',
|
||||
'protocol': 'https',
|
||||
'host': ['api', 'example', 'com'],
|
||||
'path': ['test']
|
||||
},
|
||||
'auth': {
|
||||
'basic': [
|
||||
{ key: 'username', value: 'testuser', type: 'string' },
|
||||
{ key: 'password', value: 'testpass', type: 'string' }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const brunoCollection = await postmanToBruno(collectionWithRequestUndefinedAuthType);
|
||||
|
||||
// Collection level auth should default to 'none'
|
||||
expect(brunoCollection.root.request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
|
||||
// Request auth should default to 'none'
|
||||
expect(brunoCollection.items[0].request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle folder with auth object having unexpected type', async () => {
|
||||
const collectionWithFolderUnexpectedAuthType = {
|
||||
'info': {
|
||||
'_postman_id': '7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9',
|
||||
'name': 'collection with folder unexpected auth type',
|
||||
'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'folder',
|
||||
'auth': {
|
||||
'type': 'unexpected_folder_auth_type',
|
||||
'bearer': {
|
||||
'token': 'folder-token'
|
||||
}
|
||||
},
|
||||
'item': [
|
||||
{
|
||||
'name': 'request',
|
||||
'request': {
|
||||
'method': 'GET',
|
||||
'header': [],
|
||||
'url': {
|
||||
'raw': 'https://api.example.com/test',
|
||||
'protocol': 'https',
|
||||
'host': ['api', 'example', 'com'],
|
||||
'path': ['test']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const brunoCollection = await postmanToBruno(collectionWithFolderUnexpectedAuthType);
|
||||
|
||||
// Folder auth should default to 'none'
|
||||
expect(brunoCollection.items[0].root.request.auth).toEqual({
|
||||
mode: 'none',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
|
||||
// Request should inherit auth mode
|
||||
expect(brunoCollection.items[0].items[0].request.auth).toEqual({
|
||||
mode: 'inherit',
|
||||
basic: null,
|
||||
bearer: null,
|
||||
awsv4: null,
|
||||
apikey: null,
|
||||
oauth2: null,
|
||||
digest: null
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Simple Collection (postman)
|
||||
|
||||
@@ -419,4 +419,98 @@ describe('processAuth', () => {
|
||||
credentialsPlacement: 'body'
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle auth object with undefined type', () => {
|
||||
const auth = {};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle type as null and auth as null', () => {
|
||||
const auth = {
|
||||
type: null,
|
||||
auth: null
|
||||
};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle auth object with undefined type, but basic auth', () => {
|
||||
const auth = {
|
||||
basic: [
|
||||
{ key: 'username', value: 'testuser', type: 'string' },
|
||||
{ key: 'password', value: 'testpass', type: 'string' }
|
||||
]
|
||||
};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle auth object with null type', () => {
|
||||
const auth = {
|
||||
type: null,
|
||||
};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle auth object with empty string type', () => {
|
||||
const auth = {
|
||||
type: null,
|
||||
basic: {
|
||||
username: 'testuser',
|
||||
password: 'testpass'
|
||||
}
|
||||
};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
|
||||
it('should handle auth object with boolean type value', () => {
|
||||
const auth = {
|
||||
type: "unknown_auth_type",
|
||||
unknown_auth_type: {
|
||||
accessKey: 'test-access-key',
|
||||
secretKey: 'test-secret-key'
|
||||
}
|
||||
};
|
||||
processAuth(auth, requestObject);
|
||||
expect(requestObject.auth.mode).toBe('none');
|
||||
expect(requestObject.auth.basic).toBe(null);
|
||||
expect(requestObject.auth.bearer).toBe(null);
|
||||
expect(requestObject.auth.awsv4).toBe(null);
|
||||
expect(requestObject.auth.apikey).toBe(null);
|
||||
expect(requestObject.auth.oauth2).toBe(null);
|
||||
expect(requestObject.auth.digest).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user