@@ -3,8 +3,10 @@ import path from 'node:path';
33
44import { describe , expect , it } from 'vitest' ;
55
6- import { project , request , requestGroup , workspace } from '../../models' ;
6+ import { environment , project , request , requestGroup , workspace } from '../../models' ;
7+ import { EnvironmentKvPairDataType } from '../../models/environment' ;
78import * as importUtil from '../import' ;
9+ import { generateId } from '../misc' ;
810
911/*
1012@vitest -environment jsdom
@@ -161,4 +163,218 @@ describe('importRaw()', () => {
161163 expect ( scanResult [ 0 ] . type ?. id ) . toBe ( 'openapi3' ) ;
162164 expect ( scanResult [ 0 ] . errors . length ) . toBe ( 0 ) ;
163165 } ) ;
166+
167+ it ( 'should import a postman collection variable to a collection base environment' , async ( ) => {
168+ const fixturePath = path . join (
169+ __dirname ,
170+ '..' ,
171+ '__fixtures__' ,
172+ 'postman' ,
173+ 'collection-with-variable-v2_1-input.json' ,
174+ ) ;
175+ const content = fs . readFileSync ( fixturePath , 'utf8' ) . toString ( ) ;
176+
177+ const projectToImportTo = await project . create ( ) ;
178+ const projectId = projectToImportTo . _id ;
179+
180+ const scanResult = await importUtil . scanResources ( [
181+ {
182+ contentStr : content ,
183+ } ,
184+ ] ) ;
185+
186+ expect ( scanResult [ 0 ] . type ?. id ) . toBe ( 'postman' ) ;
187+ expect ( scanResult [ 0 ] . errors . length ) . toBe ( 0 ) ;
188+
189+ await importUtil . importResourcesToProject ( {
190+ projectId : projectToImportTo . _id ,
191+ } ) ;
192+
193+ const projectWorkspaces = await workspace . findByParentId ( projectId ) ;
194+ const importedWorkspaceId = projectWorkspaces [ 0 ] . _id ;
195+ const requestBaseEnvironment = await environment . getByParentId ( importedWorkspaceId ) ;
196+
197+ expect ( requestBaseEnvironment ) . toBeDefined ( ) ;
198+
199+ expect ( requestBaseEnvironment ! . data ) . toMatchObject ( {
200+ from : 'variable' ,
201+ foo : 'bar' ,
202+ } ) ;
203+ } ) ;
204+
205+ it ( 'should merge the json base environment from a postman collection variable when imported inside a workspace' , async ( ) => {
206+ const fixturePath = path . join (
207+ __dirname ,
208+ '..' ,
209+ '__fixtures__' ,
210+ 'postman' ,
211+ 'collection-with-variable-v2_1-input.json' ,
212+ ) ;
213+ const content = fs . readFileSync ( fixturePath , 'utf8' ) . toString ( ) ;
214+
215+ const existingWorkspace = await workspace . create ( ) ;
216+ const workspaceId = existingWorkspace . _id ;
217+ const baseEnvironment = await environment . getOrCreateForParentId ( workspaceId ) ;
218+ await environment . update ( baseEnvironment , {
219+ data : {
220+ existingVar : 'exists' ,
221+ } ,
222+ } ) ;
223+
224+ const scanResult = await importUtil . scanResources ( [
225+ {
226+ contentStr : content ,
227+ } ,
228+ ] ) ;
229+
230+ expect ( scanResult [ 0 ] . type ?. id ) . toBe ( 'postman' ) ;
231+ expect ( scanResult [ 0 ] . errors . length ) . toBe ( 0 ) ;
232+
233+ await importUtil . importResourcesToWorkspace ( {
234+ workspaceId : existingWorkspace . _id ,
235+ } ) ;
236+
237+ const updatedBaseEnvironment = await environment . getByParentId ( workspaceId ) ;
238+
239+ expect ( updatedBaseEnvironment ?. data ) . toMatchObject ( {
240+ existingVar : 'exists' ,
241+ from : 'variable' ,
242+ foo : 'bar' ,
243+ } ) ;
244+ } ) ;
245+
246+ it ( 'should override kv base environment from a postman collection variable when imported inside a workspace' , async ( ) => {
247+ const fixturePath = path . join (
248+ __dirname ,
249+ '..' ,
250+ '__fixtures__' ,
251+ 'postman' ,
252+ 'collection-with-variable-v2_1-input.json' ,
253+ ) ;
254+ const content = fs . readFileSync ( fixturePath , 'utf8' ) . toString ( ) ;
255+
256+ const existingWorkspace = await workspace . create ( ) ;
257+ const workspaceId = existingWorkspace . _id ;
258+ const baseEnvironmentPair = [
259+ {
260+ id : generateId ( 'envPair' ) ,
261+ name : 'from' ,
262+ value : 'baseEnv' ,
263+ type : EnvironmentKvPairDataType . STRING ,
264+ enabled : true ,
265+ } ,
266+ {
267+ id : generateId ( 'envPair' ) ,
268+ name : 'disabledItemKey' ,
269+ value : 'disabledItemValue' ,
270+ type : EnvironmentKvPairDataType . STRING ,
271+ enabled : false ,
272+ } ,
273+ ] ;
274+ const baseEnvironment = await environment . getOrCreateForParentId ( workspaceId ) ;
275+ await environment . update ( baseEnvironment , {
276+ data : {
277+ from : 'baseEnv' ,
278+ } ,
279+ environmentType : environment . EnvironmentType . KVPAIR ,
280+ kvPairData : baseEnvironmentPair ,
281+ } ) ;
282+
283+ const scanResult = await importUtil . scanResources ( [
284+ {
285+ contentStr : content ,
286+ } ,
287+ ] ) ;
288+
289+ expect ( scanResult [ 0 ] . type ?. id ) . toBe ( 'postman' ) ;
290+ expect ( scanResult [ 0 ] . errors . length ) . toBe ( 0 ) ;
291+
292+ await importUtil . importResourcesToWorkspace ( {
293+ workspaceId : existingWorkspace . _id ,
294+ } ) ;
295+
296+ const updatedBaseEnvironment = await environment . getByParentId ( workspaceId ) ;
297+
298+ expect ( updatedBaseEnvironment ?. data ) . toMatchObject ( {
299+ from : 'variable' ,
300+ foo : 'bar' ,
301+ } ) ;
302+ const newKvPairData = updatedBaseEnvironment ?. kvPairData || [ ] ;
303+ expect ( newKvPairData . length ) . toBe ( 3 ) ;
304+ expect ( newKvPairData . filter ( pair => pair . enabled ) . length ) . toBe ( 2 ) ;
305+ expect ( newKvPairData . find ( pair => pair . name === 'from' ) ?. value ) . toBe ( 'variable' ) ;
306+ expect ( newKvPairData . find ( pair => pair . name === 'foo' ) ?. value ) . toBe ( 'bar' ) ;
307+ } ) ;
308+
309+ it ( 'should merge and discard same name variable in kv base environment from a postman collection variable when imported inside a workspace' , async ( ) => {
310+ const fixturePath = path . join (
311+ __dirname ,
312+ '..' ,
313+ '__fixtures__' ,
314+ 'postman' ,
315+ 'collection-with-variable-v2_1-input.json' ,
316+ ) ;
317+ const content = fs . readFileSync ( fixturePath , 'utf8' ) . toString ( ) ;
318+
319+ const existingWorkspace = await workspace . create ( ) ;
320+ const workspaceId = existingWorkspace . _id ;
321+ const baseEnvironmentPair = [
322+ {
323+ id : generateId ( 'envPair' ) ,
324+ name : 'from' ,
325+ value : 'disabledValue' ,
326+ type : EnvironmentKvPairDataType . STRING ,
327+ enabled : false ,
328+ } ,
329+ {
330+ id : generateId ( 'envPair' ) ,
331+ name : 'from' ,
332+ value : 'baseEnv' ,
333+ type : EnvironmentKvPairDataType . STRING ,
334+ enabled : true ,
335+ } ,
336+ {
337+ id : generateId ( 'envPair' ) ,
338+ name : 'disabledItemKey' ,
339+ value : 'disabledItemValue' ,
340+ type : EnvironmentKvPairDataType . STRING ,
341+ enabled : false ,
342+ } ,
343+ ] ;
344+ const baseEnvironment = await environment . getOrCreateForParentId ( workspaceId ) ;
345+ await environment . update ( baseEnvironment , {
346+ data : {
347+ from : 'baseEnv' ,
348+ } ,
349+ environmentType : environment . EnvironmentType . KVPAIR ,
350+ kvPairData : baseEnvironmentPair ,
351+ } ) ;
352+
353+ const scanResult = await importUtil . scanResources ( [
354+ {
355+ contentStr : content ,
356+ } ,
357+ ] ) ;
358+
359+ expect ( scanResult [ 0 ] . type ?. id ) . toBe ( 'postman' ) ;
360+ expect ( scanResult [ 0 ] . errors . length ) . toBe ( 0 ) ;
361+
362+ await importUtil . importResourcesToWorkspace ( {
363+ workspaceId : existingWorkspace . _id ,
364+ overrideBaseEnvironmentData : false ,
365+ } ) ;
366+
367+ const updatedBaseEnvironment = await environment . getByParentId ( workspaceId ) ;
368+
369+ expect ( updatedBaseEnvironment ?. data ) . toMatchObject ( {
370+ from : 'baseEnv' ,
371+ foo : 'bar' ,
372+ } ) ;
373+ const newKvPairData = updatedBaseEnvironment ?. kvPairData || [ ] ;
374+ expect ( newKvPairData . length ) . toBe ( 4 ) ;
375+ expect ( newKvPairData . filter ( pair => pair . enabled ) . length ) . toBe ( 2 ) ;
376+ expect ( newKvPairData . filter ( pair => ! pair . enabled ) . length ) . toBe ( 2 ) ;
377+ expect ( newKvPairData . find ( pair => pair . name === 'from' && pair . enabled ) ?. value ) . toBe ( 'baseEnv' ) ;
378+ expect ( newKvPairData . find ( pair => pair . name === 'foo' ) ?. value ) . toBe ( 'bar' ) ;
379+ } ) ;
164380} ) ;
0 commit comments