diff --git a/packages/bruno-converters/src/postman/postman-env-to-bruno-env.js b/packages/bruno-converters/src/postman/postman-env-to-bruno-env.js index 52d60b08b..d9183cbfb 100644 --- a/packages/bruno-converters/src/postman/postman-env-to-bruno-env.js +++ b/packages/bruno-converters/src/postman/postman-env-to-bruno-env.js @@ -6,14 +6,14 @@ const isSecret = (type) => { return type === 'secret'; }; -const importPostmanEnvironmentVariables = (brunoEnvironment, values) => { +const importPostmanEnvironmentVariables = (brunoEnvironment, values = []) => { brunoEnvironment.variables = brunoEnvironment.variables || []; - each(values, (i) => { + each(values.filter(i => !(i.key == null && i.value == null)), (i) => { const brunoEnvironmentVariable = { uid: uuid(), - name: i.key.replace(invalidVariableCharacterRegex, '_'), - value: i.value, + name: (i.key ?? '').replace(invalidVariableCharacterRegex, '_'), + value: i.value ?? '', enabled: i.enabled, secret: isSecret(i.type) }; diff --git a/packages/bruno-converters/src/postman/postman-to-bruno.js b/packages/bruno-converters/src/postman/postman-to-bruno.js index bef21b46e..0b1873792 100644 --- a/packages/bruno-converters/src/postman/postman-to-bruno.js +++ b/packages/bruno-converters/src/postman/postman-to-bruno.js @@ -119,10 +119,10 @@ const importScriptsFromEvents = (events, requestObject) => { }; const importCollectionLevelVariables = (variables, requestObject) => { - const vars = variables.map((v) => ({ + const vars = variables.filter(v => !(v.key == null && v.value == null)).map((v) => ({ uid: uuid(), - name: v.key.replace(invalidVariableCharacterRegex, '_'), - value: v.value, + name: (v.key ?? '').replace(invalidVariableCharacterRegex, '_'), + value: v.value ?? '', enabled: true })); diff --git a/packages/bruno-converters/tests/postman/postman-env-to-bruno-env.spec.js b/packages/bruno-converters/tests/postman/postman-env-to-bruno-env.spec.js index 6101548bd..9421d3768 100644 --- a/packages/bruno-converters/tests/postman/postman-env-to-bruno-env.spec.js +++ b/packages/bruno-converters/tests/postman/postman-env-to-bruno-env.spec.js @@ -47,6 +47,66 @@ describe('postmanToBrunoEnvironment Function', () => { expect(brunoEnvironment).toEqual(expectedEnvironment); }); + it('should handle falsy values in environment variables', async () => { + const postmanEnvironment = { + "id": "some-id", + "name": "My Environment", + "values": [ + { + "enabled": true, + "type": "text" + }, + { + "value": "", + "enabled": true, + "type": "text" + }, + { + "key": "", + "enabled": true, + "type": "text" + }, + { + "key": "", + "value": "", + "enabled": true, + "type": "text" + } + ] + }; + + const brunoEnvironment = await postmanToBrunoEnvironment(postmanEnvironment); + + const expectedEnvironment = { + name: 'My Environment', + variables: [ + { + name: '', + value: '', + enabled: true, + secret: false, + uid: "mockeduuidvalue123456", + }, + { + name: '', + value: '', + enabled: true, + secret: false, + uid: "mockeduuidvalue123456", + }, + { + name: '', + value: '', + enabled: true, + secret: false, + uid: "mockeduuidvalue123456", + } + ], + }; + + expect(brunoEnvironment).toEqual(expectedEnvironment); + }); + it.skip('should throw Error when JSON parsing fails', async () => { const invalidBrunoEnvironment = { "id": "some-id", @@ -66,4 +126,23 @@ describe('postmanToBrunoEnvironment Function', () => { 'Unable to parse the postman environment json file' ); }); + + it("should handle empty variables", async () => { + const collectionWithEmptyVars = { + "name": "My Environment", + "values": [] + }; + + const brunoCollection = await postmanToBrunoEnvironment(collectionWithEmptyVars); + expect(brunoCollection.variables).toEqual([]); + }); + + it("should handle undefined variables", async () => { + const collectionWithUndefinedVars = { + "name": "My Environment", + }; + + const brunoCollection = await postmanToBrunoEnvironment(collectionWithUndefinedVars); + expect(brunoCollection.variables).toEqual([]); + }); }); diff --git a/packages/bruno-converters/tests/postman/postman-to-bruno/postman-to-bruno.spec.js b/packages/bruno-converters/tests/postman/postman-to-bruno/postman-to-bruno.spec.js index 3ac79476c..e303ab3d3 100644 --- a/packages/bruno-converters/tests/postman/postman-to-bruno/postman-to-bruno.spec.js +++ b/packages/bruno-converters/tests/postman/postman-to-bruno/postman-to-bruno.spec.js @@ -6,6 +6,73 @@ describe('postman-collection', () => { const brunoCollection = await postmanToBruno(postmanCollection); expect(brunoCollection).toMatchObject(expectedOutput); }); + + it('should handle falsy values in collection variables', async () => { + const collectionWithFalsyVars = { + "info": { + "_postman_id": "7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9", + "name": "collection with falsy vars", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "variable": [ + { + "type": "string" + }, + { + "key": "", + "type": "string" + }, + { + "value": "", + "type": "string" + }, + { + "key": "", + "value": "", + "type": "string" + } + ], + "item": [] + }; + + const brunoCollection = await postmanToBruno(collectionWithFalsyVars); + + expect(brunoCollection.root.request.vars.req).toEqual([ + { + uid: "mockeduuidvalue123456", + name: '', + value: '', + enabled: true + }, + { + uid: "mockeduuidvalue123456", + name: '', + value: '', + enabled: true + }, + { + uid: "mockeduuidvalue123456", + name: '', + value: '', + enabled: true + } + ]); + }); + + it("should handle empty variables", async () => { + const collectionWithEmptyVars = { + "info": { + "_postman_id": "7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9", + "name": "collection with falsy vars", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "variable": [], + "item": [] + }; + + const brunoCollection = await postmanToBruno(collectionWithEmptyVars); + expect(brunoCollection.root.request.vars.req).toEqual([]); + }); }); // Simple Collection (postman)