Skip to content

Commit 27f3629

Browse files
[INS-120] Increase test coverage for nested items(scanItem) function (#4648)
1 parent e7e7797 commit 27f3629

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

pkg/sources/postman/postman_test.go

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,205 @@ func TestSource_ScanCollection(t *testing.T) {
163163
}
164164
}
165165

166+
func TestSource_ScanCollectionWithFolders(t *testing.T) {
167+
ctx := context.Background()
168+
s := &Source{
169+
DetectorKeywords: map[string]struct{}{
170+
"keyword1": {},
171+
},
172+
keywords: map[string]struct{}{
173+
"keyword1": {},
174+
},
175+
}
176+
testCollection := Collection{
177+
Info: Info{
178+
PostmanID: "col1",
179+
Name: "Test Collection with Folders",
180+
},
181+
Items: []Item{
182+
{
183+
Name: "Folder 1",
184+
Items: []Item{
185+
{
186+
Uid: "1",
187+
Name: "Request 1",
188+
Request: Request{
189+
URL: URL{
190+
Protocol: "https",
191+
Host: []string{"example.com"},
192+
Path: []string{"api", "endpoint"},
193+
Raw: "https://example.com/api/endpoint",
194+
},
195+
Method: "GET",
196+
},
197+
},
198+
{
199+
Uid: "2",
200+
Name: "Folder 2",
201+
Items: []Item{
202+
{
203+
Uid: "3",
204+
Name: "Request 2",
205+
Request: Request{
206+
URL: URL{
207+
Protocol: "https",
208+
Host: []string{"test.com"},
209+
Path: []string{"api", "endpoint"},
210+
Raw: "https://test.com/api/endpoint",
211+
},
212+
Method: "POST",
213+
Auth: Auth{
214+
Type: "bearer",
215+
Bearer: []KeyValue{
216+
{
217+
Key: "token",
218+
Value: "abcdef123456",
219+
},
220+
},
221+
},
222+
},
223+
},
224+
},
225+
},
226+
},
227+
},
228+
},
229+
}
230+
231+
expectedChunks := []string{
232+
"keyword1:https://example.com/api/endpoint\n",
233+
"keyword1:https://test.com/api/endpoint\n",
234+
"keyword1:token:abcdef123456\n",
235+
}
236+
237+
chunksChan := make(chan *sources.Chunk, len(expectedChunks))
238+
metadata := Metadata{
239+
CollectionInfo: testCollection.Info,
240+
}
241+
242+
go s.scanCollection(ctx, chunksChan, metadata, testCollection)
243+
244+
for _, expectedData := range expectedChunks {
245+
chunk := <-chunksChan
246+
got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
247+
expected := strings.Split(strings.TrimSpace(expectedData), "\n")
248+
sort.Strings(got)
249+
sort.Strings(expected)
250+
251+
if !reflect.DeepEqual(got, expected) {
252+
t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
253+
}
254+
}
255+
}
256+
257+
func TestSource_ScanSingleItem(t *testing.T) {
258+
ctx := context.Background()
259+
s := &Source{
260+
DetectorKeywords: map[string]struct{}{
261+
"keyword1": {},
262+
},
263+
keywords: map[string]struct{}{
264+
"keyword1": {},
265+
},
266+
}
267+
testItem := Item{
268+
Name: "Request 1",
269+
Request: Request{
270+
URL: URL{
271+
Protocol: "https",
272+
Host: []string{"example.com"},
273+
Path: []string{"api", "endpoint"},
274+
Raw: "https://example.com/api/endpoint",
275+
},
276+
Method: "GET",
277+
},
278+
}
279+
expectedChunks := []string{
280+
"keyword1:https://example.com/api/endpoint\n",
281+
}
282+
chunksChan := make(chan *sources.Chunk, len(expectedChunks))
283+
metadata := Metadata{}
284+
s.scanItem(ctx, chunksChan, Collection{}, metadata, testItem, "", map[string]struct{}{})
285+
for _, expectedData := range expectedChunks {
286+
chunk := <-chunksChan
287+
got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
288+
expected := strings.Split(strings.TrimSpace(expectedData), "\n")
289+
sort.Strings(got)
290+
sort.Strings(expected)
291+
if !reflect.DeepEqual(got, expected) {
292+
t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
293+
}
294+
}
295+
}
296+
297+
func TestSource_ScanNestedItems(t *testing.T) {
298+
ctx := context.Background()
299+
s := &Source{
300+
DetectorKeywords: map[string]struct{}{
301+
"keyword1": {},
302+
},
303+
keywords: map[string]struct{}{
304+
"keyword1": {},
305+
},
306+
}
307+
testItem := Item{
308+
Name: "Folder 1",
309+
Items: []Item{
310+
{
311+
Name: "Request 1",
312+
Uid: "1",
313+
Request: Request{
314+
URL: URL{
315+
Protocol: "https",
316+
Host: []string{"example.com"},
317+
Path: []string{"api", "endpoint"},
318+
Raw: "https://example.com/api/endpoint",
319+
},
320+
Method: "GET",
321+
},
322+
},
323+
{
324+
Name: "Folder 2",
325+
Uid: "2",
326+
Items: []Item{
327+
{
328+
Uid: "3",
329+
Name: "Request 2",
330+
Request: Request{
331+
URL: URL{
332+
Protocol: "https",
333+
Host: []string{"test.com"},
334+
Path: []string{"api", "endpoint"},
335+
Raw: "https://test.com/api/endpoint",
336+
},
337+
Method: "POST",
338+
},
339+
},
340+
},
341+
},
342+
},
343+
}
344+
345+
expectedChunks := []string{
346+
"keyword1:https://example.com/api/endpoint\n",
347+
"keyword1:https://test.com/api/endpoint\n",
348+
}
349+
350+
chunksChan := make(chan *sources.Chunk, len(expectedChunks))
351+
metadata := Metadata{}
352+
s.scanItem(ctx, chunksChan, Collection{}, metadata, testItem, "", map[string]struct{}{})
353+
for _, expectedData := range expectedChunks {
354+
chunk := <-chunksChan
355+
got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
356+
expected := strings.Split(strings.TrimSpace(expectedData), "\n")
357+
sort.Strings(got)
358+
sort.Strings(expected)
359+
if !reflect.DeepEqual(got, expected) {
360+
t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
361+
}
362+
}
363+
}
364+
166365
func TestSource_ScanVariableData(t *testing.T) {
167366
ctx := context.Background()
168367
s := &Source{

0 commit comments

Comments
 (0)