test: add request authentication tests for basic and bearer auth handling

This commit is contained in:
Pragadesh-45 2025-04-22 20:25:27 +05:45
parent c7e8c07d40
commit 46dab6e474

View File

@ -0,0 +1,134 @@
import { describe, it, expect } from '@jest/globals';
import postmanToBruno from '../../../src/postman/postman-to-bruno';
describe('Request Authentication', () => {
it('should handle basic auth at request level', () => {
const postmanCollection = {
info: {
name: 'Request Auth Collection',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [
{
name: 'Basic Auth Request',
request: {
method: 'GET',
url: 'https://api.example.com/test',
auth: {
type: 'basic',
basic: [
{ key: 'username', value: 'requestuser' },
{ key: 'password', value: 'requestpass' }
]
}
}
}
]
};
const result = postmanToBruno(postmanCollection);
expect(result.items[0].request.auth).toEqual({
mode: 'basic',
basic: {
username: 'requestuser',
password: 'requestpass'
},
bearer: null,
awsv4: null,
apikey: null,
oauth2: null,
digest: null
});
});
it('should inherit folder auth when request has no auth', () => {
const postmanCollection = {
info: {
name: 'Inherit Request Auth Collection',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [
{
name: 'Auth Folder',
auth: {
type: 'bearer',
bearer: [{ key: 'token', value: 'foldertoken' }]
},
item: [
{
name: 'No Auth Request',
request: {
method: 'GET',
url: 'https://api.example.com/test'
}
}
]
}
]
};
const result = postmanToBruno(postmanCollection);
expect(result.items[0].items[0].request.auth).toEqual({
mode: 'bearer',
basic: null,
bearer: {
token: 'foldertoken'
},
awsv4: null,
apikey: null,
oauth2: null,
digest: null
});
});
it('should override folder auth with request auth', () => {
const postmanCollection = {
info: {
name: 'Override Request Auth Collection',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [
{
name: 'Auth Folder',
auth: {
type: 'basic',
basic: [
{ key: 'username', value: 'folderuser' },
{ key: 'password', value: 'folderpass' }
]
},
item: [
{
name: 'Override Auth Request',
request: {
method: 'GET',
url: 'https://api.example.com/test',
auth: {
type: 'bearer',
bearer: [{ key: 'token', value: 'requesttoken' }]
}
}
}
]
}
]
};
const result = postmanToBruno(postmanCollection);
expect(result.items[0].items[0].request.auth).toEqual({
mode: 'bearer',
basic: null,
bearer: {
token: 'requesttoken'
},
awsv4: null,
apikey: null,
oauth2: null,
digest: null
});
});
});