Skip to content

Find, verify, and analyze leaked credentials

[INS-120] Increase test coverage for nested items(scanItem) function (#4648)

Muneeb Ullah Khanf5f43368f99f
diff --git a/pkg/sources/postman/postman_test.go b/pkg/sources/postman/postman_test.go
index 1bb4863bb..fee173bb6 100644
--- a/pkg/sources/postman/postman_test.go
+++ b/pkg/sources/postman/postman_test.go
@@ -163,6 +163,205 @@ func TestSource_ScanCollection(t *testing.T) {
}
}
+func TestSource_ScanCollectionWithFolders(t *testing.T) {
+ ctx := context.Background()
+ s := &Source{
+ DetectorKeywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ keywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ }
+ testCollection := Collection{
+ Info: Info{
+ PostmanID: "col1",
+ Name: "Test Collection with Folders",
+ },
+ Items: []Item{
+ {
+ Name: "Folder 1",
+ Items: []Item{
+ {
+ Uid: "1",
+ Name: "Request 1",
+ Request: Request{
+ URL: URL{
+ Protocol: "https",
+ Host: []string{"example.com"},
+ Path: []string{"api", "endpoint"},
+ Raw: "https://example.com/api/endpoint",
+ },
+ Method: "GET",
+ },
+ },
+ {
+ Uid: "2",
+ Name: "Folder 2",
+ Items: []Item{
+ {
+ Uid: "3",
+ Name: "Request 2",
+ Request: Request{
+ URL: URL{
+ Protocol: "https",
+ Host: []string{"test.com"},
+ Path: []string{"api", "endpoint"},
+ Raw: "https://test.com/api/endpoint",
+ },
+ Method: "POST",
+ Auth: Auth{
+ Type: "bearer",
+ Bearer: []KeyValue{
+ {
+ Key: "token",
+ Value: "abcdef123456",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ expectedChunks := []string{
+ "keyword1:https://example.com/api/endpoint\n",
+ "keyword1:https://test.com/api/endpoint\n",
+ "keyword1:token:abcdef123456\n",
+ }
+
+ chunksChan := make(chan *sources.Chunk, len(expectedChunks))
+ metadata := Metadata{
+ CollectionInfo: testCollection.Info,
+ }
+
+ go s.scanCollection(ctx, chunksChan, metadata, testCollection)
+
+ for _, expectedData := range expectedChunks {
+ chunk := <-chunksChan
+ got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
+ expected := strings.Split(strings.TrimSpace(expectedData), "\n")
+ sort.Strings(got)
+ sort.Strings(expected)
+
+ if !reflect.DeepEqual(got, expected) {
+ t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
+ }
+ }
+}
+
+func TestSource_ScanSingleItem(t *testing.T) {
+ ctx := context.Background()
+ s := &Source{
+ DetectorKeywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ keywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ }
+ testItem := Item{
+ Name: "Request 1",
+ Request: Request{
+ URL: URL{
+ Protocol: "https",
+ Host: []string{"example.com"},
+ Path: []string{"api", "endpoint"},
+ Raw: "https://example.com/api/endpoint",
+ },
+ Method: "GET",
+ },
+ }
+ expectedChunks := []string{
+ "keyword1:https://example.com/api/endpoint\n",
+ }
+ chunksChan := make(chan *sources.Chunk, len(expectedChunks))
+ metadata := Metadata{}
+ s.scanItem(ctx, chunksChan, Collection{}, metadata, testItem, "", map[string]struct{}{})
+ for _, expectedData := range expectedChunks {
+ chunk := <-chunksChan
+ got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
+ expected := strings.Split(strings.TrimSpace(expectedData), "\n")
+ sort.Strings(got)
+ sort.Strings(expected)
+ if !reflect.DeepEqual(got, expected) {
+ t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
+ }
+ }
+}
+
+func TestSource_ScanNestedItems(t *testing.T) {
+ ctx := context.Background()
+ s := &Source{
+ DetectorKeywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ keywords: map[string]struct{}{
+ "keyword1": {},
+ },
+ }
+ testItem := Item{
+ Name: "Folder 1",
+ Items: []Item{
+ {
+ Name: "Request 1",
+ Uid: "1",
+ Request: Request{
+ URL: URL{
+ Protocol: "https",
+ Host: []string{"example.com"},
+ Path: []string{"api", "endpoint"},
+ Raw: "https://example.com/api/endpoint",
+ },
+ Method: "GET",
+ },
+ },
+ {
+ Name: "Folder 2",
+ Uid: "2",
+ Items: []Item{
+ {
+ Uid: "3",
+ Name: "Request 2",
+ Request: Request{
+ URL: URL{
+ Protocol: "https",
+ Host: []string{"test.com"},
+ Path: []string{"api", "endpoint"},
+ Raw: "https://test.com/api/endpoint",
+ },
+ Method: "POST",
+ },
+ },
+ },
+ },
+ },
+ }
+
+ expectedChunks := []string{
+ "keyword1:https://example.com/api/endpoint\n",
+ "keyword1:https://test.com/api/endpoint\n",
+ }
+
+ chunksChan := make(chan *sources.Chunk, len(expectedChunks))
+ metadata := Metadata{}
+ s.scanItem(ctx, chunksChan, Collection{}, metadata, testItem, "", map[string]struct{}{})
+ for _, expectedData := range expectedChunks {
+ chunk := <-chunksChan
+ got := strings.Split(strings.TrimSpace(string(chunk.Data)), "\n")
+ expected := strings.Split(strings.TrimSpace(expectedData), "\n")
+ sort.Strings(got)
+ sort.Strings(expected)
+ if !reflect.DeepEqual(got, expected) {
+ t.Errorf("expected chunk data from collection: \n%sgot: \n%s", expectedData, chunk.Data)
+ }
+ }
+}
+
func TestSource_ScanVariableData(t *testing.T) {
ctx := context.Background()
s := &Source{