From d4851e7c90163da94e53727053cfdcec4760c444 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Thu, 5 Dec 2024 11:05:25 +0200 Subject: [PATCH] Add test for resubmit flag and fix config retrieval logic Introduce a new integration test to verify handling of the resubmit flag during scan creation for non-existent projects, ensuring successful creation with default config. Additionally, fix the logic to retrieve configuration only when scans are available, preventing potential nil pointer dereferences. --- internal/commands/scan.go | 15 ++++++++++----- test/integration/scan_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/internal/commands/scan.go b/internal/commands/scan.go index d5b8c29f8..163d724b3 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -834,6 +834,7 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use var allScansModel *wrappers.ScansCollectionResponseModel var errorModel *wrappers.ErrorModel var err error + var config []wrappers.Config params := make(map[string]string) params["project-id"] = projectID allScansModel, errorModel, err = scansWrapper.Get(params) @@ -844,12 +845,16 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use if errorModel != nil { return nil, errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message) } - config := allScansModel.Scans[0].Metadata.Configs - engines := allScansModel.Scans[0].Engines - // Check if there are no scan types sent using the flags, and use the latest scan engine types - if userScanTypes == "" { - actualScanTypes = strings.Join(engines, ",") + + if len(allScansModel.Scans) > 0 { + config = allScansModel.Scans[0].Metadata.Configs + engines := allScansModel.Scans[0].Engines + // Check if there are no scan types sent using the flags, and use the latest scan engine types + if userScanTypes == "" { + actualScanTypes = strings.Join(engines, ",") + } } + return config, nil } diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index 99cfa416f..41f922d2f 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -1972,3 +1972,17 @@ func TestCreateAsyncScan_CallExportServiceBeforeScanFinishWithRetry_Success(t *t asserts.Nil(t, err) assert.Assert(t, exportRes != nil, "Export response should not be nil") } + +func TestCreateScanWithResubmitFlag_ProjectNotExist_ScanCreatedSuccessfullyWithDefaultConfig(t *testing.T) { + projectName := GenerateRandomProjectNameForScan() + args := []string{ + scanCommand, "create", + flag(params.ProjectName), projectName, + flag(params.SourcesFlag), Zip, + flag(params.BranchFlag), "main", + flag(params.ScanInfoFormatFlag), printer.FormatJSON, + flag(params.ScanResubmit), + } + err, _ := executeCommand(t, args...) + assert.NilError(t, err) +}