Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miryamfoiferCX committed Dec 4, 2024
1 parent c414781 commit 8eb3c30
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
31 changes: 31 additions & 0 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,23 @@ func TestCreateScan_WhenProjectNotExists_ShouldCreateProjectAndAssignGroup(t *te
assert.Equal(t, strings.Contains(stdoutString, "Updating project groups"), true, "Expected output: %s", "Updating project groups")
}

func TestCreateScan_WhenProjectNotExists_ShouldCreateProjectAndAssociateApplication(t *testing.T) {
file := createOutputFile(t, outputFileName)
defer deleteOutputFile(file)
defer logger.SetOutput(os.Stdout)

baseArgs := []string{"scan", "create", "--project-name", "newProject", "-s", ".", "--branch", "main", "--application-name", mock.ExistingApplication, "--debug"}
execCmdNilAssertion(
t,
baseArgs...,
)
stdoutString, err := util.ReadFileAsString(file.Name())
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
assert.Equal(t, strings.Contains(stdoutString, "application association done successfully"), true, "Expected output: %s", "application association done successfully")
}

func TestScanWorkflowMissingID(t *testing.T) {
err := execCmdNotNilAssertion(t, "scan", "workflow")
assert.Error(t, err, "Please provide a scan ID", err.Error())
Expand Down Expand Up @@ -624,6 +641,20 @@ func TestCreateScan_WhenProjectExists_ShouldIgnoreGroups(t *testing.T) {
}
assert.Equal(t, strings.Contains(stdoutString, noUpdatesForExistingProject), true, "Expected output: %s", noUpdatesForExistingProject)
}

func TestCreateScan_WhenProjectExists_ShouldIgnoreApplication(t *testing.T) {
file := createOutputFile(t, outputFileName)
defer deleteOutputFile(file)
defer logger.SetOutput(os.Stdout)
baseArgs := []string{scanCommand, "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch",
"--debug", "--application-name", "anyApplication"}
execCmdNilAssertion(t, baseArgs...)
stdoutString, err := util.ReadFileAsString(file.Name())
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
assert.Equal(t, strings.Contains(stdoutString, noUpdatesForExistingProject), true, "Expected output: %s", noUpdatesForExistingProject)
}
func TestScanCreateLastSastScanTimeWithInvalidValue(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch", "--sca-exploitable-path", "true", "--sca-last-sast-scan-time", "notaniteger"}
err := execCmdNotNilAssertion(t, baseArgs...)
Expand Down
15 changes: 14 additions & 1 deletion internal/wrappers/mock/application-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ func (a ApplicationsMockWrapper) Get(params map[string]string) (*wrappers.Applic
Name: "MOCK",
Description: "This is a mock application",
Criticality: 2,
ProjectIds: []string{"ProjectID1", "ProjectID2", "MOCK", "test_project", "ID-new-project-name"},
ProjectIds: []string{"ProjectID1", "ProjectID2", "MOCK", "test_project", "ID-new-project-name", "ID-newProject"},
CreatedAt: time.Now(),
}
if params["name"] == ExistingApplication {
mockApplication.Name = ExistingApplication
mockApplication.ID = "ID-newProject"
return &wrappers.ApplicationsResponseModel{
TotalCount: 1,
Applications: []wrappers.Application{mockApplication},
}, nil
}

response := &wrappers.ApplicationsResponseModel{
TotalCount: 1,
Applications: []wrappers.Application{mockApplication},
}

if params["name"] == "anyApplication" {
response.TotalCount = 0
response.Applications = []wrappers.Application{}
}

return response, nil
}
1 change: 1 addition & 0 deletions internal/wrappers/mock/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

const (
ApplicationDoesntExist = "application-doesnt-exist"
ExistingApplication = "application-exists"
NoPermissionApp = "NoPermissionApp"
FakeBadRequest400 = "fake-http-status-bad-request"
FakeUnauthorized401 = "fake-unauthorized-response"
Expand Down
29 changes: 29 additions & 0 deletions test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,35 @@ func TestScanCreate_WhenProjectExists_ShouldNotUpdateGroups(t *testing.T) {
t.Errorf("When project exists, groups before and after scan creation should be equal. Got %v, want %v", groupsAfterScanCreate, groupsBeforeScanCreate)
}

}

func TestScanCreate_WhenProjectExists_ShouldNotUpdateApplication(t *testing.T) {
projectID, projectName := getRootProject(t)
project := showProject(t, projectID)
applicationsBeforeScanCreate := project.ApplicationIds

args := []string{
scanCommand, "create",
flag(params.ProjectName), projectName,
flag(params.SourcesFlag), Zip,
flag(params.ScanTypes), "sast",
flag(params.PresetName), "Checkmarx Default",
flag(params.BranchFlag), "dummy_branch",
flag(params.ApplicationName), "wrong_application",
"--async",
}

err, _ := executeCommand(t, args...)
if err != nil {
assertError(t, err, "running a scan should pass")
}

project = showProject(t, projectID)
applicationsAfterScanCreate := project.ApplicationIds
if !reflect.DeepEqual(applicationsBeforeScanCreate, applicationsAfterScanCreate) {
t.Errorf("When project exists, applications before and after scan creation should be equal. Got %v, want %v", applicationsAfterScanCreate, applicationsBeforeScanCreate)
}

}
func TestScanCreateExploitablePath(t *testing.T) {
_, projectName := getRootProject(t)
Expand Down

0 comments on commit 8eb3c30

Please sign in to comment.