test: add collection authentication tests for basic, bearer, API key, and digest auth

This commit is contained in:
Pragadesh-45 2025-04-22 19:10:28 +05:45
parent 049de84cbb
commit 932d2b77dc

View File

@ -0,0 +1,238 @@
import { describe, it, expect } from '@jest/globals';
import postmanToBruno from '../../../src/postman/postman-to-bruno';
describe('Collection Authentication', () => {
it('should handle basic auth at collection level', () => {
const postmanCollection = {
info: {
name: 'Collection level basic auth',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [],
auth: {
type: 'basic',
basic: [
{
key: 'password',
value: 'testpass',
type: 'string'
},
{
key: 'username',
value: 'testuser',
type: 'string'
}
]
},
event: [
{
listen: 'prerequest',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
},
{
listen: 'test',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
}
]
};
const result = postmanToBruno(postmanCollection);
// console.log('result', JSON.stringify(result, null, 2));
expect(result.root.request.auth).toEqual({
mode: 'basic',
basic: {
username: 'testuser',
password: 'testpass'
},
bearer: null,
awsv4: null,
apikey: null,
oauth2: null,
digest: null
});
});
it('should handle bearer token auth at collection level', () => {
const postmanCollection = {
info: {
name: 'Collection level bearer token',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [],
auth: {
type: 'bearer',
bearer: [
{
key: 'token',
value: 'token',
type: 'string'
}
]
},
event: [
{
listen: 'prerequest',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
},
{
listen: 'test',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
}
]
};
const result = postmanToBruno(postmanCollection);
// console.log('result', JSON.stringify(result, null, 2));
expect(result.root.request.auth).toEqual({
mode: 'bearer',
basic: null,
bearer: {
token: 'token'
},
awsv4: null,
apikey: null,
oauth2: null,
digest: null
});
});
it('should handle API key auth at collection level', () => {
const postmanCollection = {
info: {
name: 'Collection level api key',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [],
auth: {
type: 'apikey',
apikey: [
{
key: 'value',
value: 'apikey',
type: 'string'
},
{
key: 'key',
value: 'apikey',
type: 'string'
}
]
},
event: [
{
listen: 'prerequest',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
},
{
listen: 'test',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
}
]
};
const result = postmanToBruno(postmanCollection);
expect(result.root.request.auth).toEqual({
mode: 'apikey',
basic: null,
bearer: null,
awsv4: null,
apikey: {
key: 'apikey',
value: 'apikey',
placement: 'header'
},
oauth2: null,
digest: null
});
});
it('should handle digest auth at collection level', () => {
const postmanCollection = {
info: {
name: 'Collection level digest auth',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
},
item: [],
auth: {
type: 'digest',
digest: [
{
key: 'password',
value: 'digest auth',
type: 'string'
},
{
key: 'username',
value: 'digest auth',
type: 'string'
},
{
key: 'algorithm',
value: 'MD5',
type: 'string'
}
]
},
event: [
{
listen: 'prerequest',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
},
{
listen: 'test',
script: {
type: 'text/javascript',
packages: {},
exec: ['']
}
}
]
};
const result = postmanToBruno(postmanCollection);
expect(result.root.request.auth).toEqual({
mode: 'digest',
basic: null,
bearer: null,
awsv4: null,
apikey: null,
oauth2: null,
digest: {
username: 'digest auth',
password: 'digest auth'
}
});
});
});