Skip to content

Commit

Permalink
Add test for resubmit flag and fix config retrieval logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BenAlvo1 committed Dec 5, 2024
1 parent f407a06 commit d4851e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d4851e7

Please sign in to comment.