Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Get Application by Name with Exact Match and Update Permissions Log Message (AST-36823) #669

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func runCreateProjectCommand(
return getAppErr
}
if application == nil {
return errors.Errorf(applicationErrors.ApplicationDoesntExist)
return errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
}
applicationID = []string{application.ID}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestProjectCreate_ExistingApplication_CreateProjectUnderApplicationSuccessf

func TestProjectCreate_ExistingApplicationWithNoPermission_FailToCreateProject(t *testing.T) {
err := execCmdNotNilAssertion(t, "project", "create", "--project-name", "test_project", "--application-name", mock.NoPermissionApp)
assert.Assert(t, err.Error() == applicationErrors.ApplicationNoPermission)
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestProjectCreate_OnReceivingHttpBadRequestStatusCode_FailedToCreateScan(t *testing.T) {
Expand Down
19 changes: 16 additions & 3 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ func setupScanTypeProjectAndConfig(
return getAppErr
}
if application == nil {
return errors.Errorf(applicationErrors.ApplicationDoesntExist)
return errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
}
applicationID = []string{application.ID}
}
Expand Down Expand Up @@ -878,16 +878,29 @@ func getApplication(applicationName string, applicationsWrapper wrappers.Applica
params["name"] = applicationName
resp, err := applicationsWrapper.Get(params)
if err != nil {

return nil, err
}
if resp.Applications != nil && len(resp.Applications) > 0 {
application := resp.Applications[0]
return &application, nil
application := verifyApplicationNameExactMatch(applicationName, resp)

return application, nil
}
}
return nil, nil
}

func verifyApplicationNameExactMatch(applicationName string, resp *wrappers.ApplicationsResponseModel) *wrappers.Application {
var application *wrappers.Application
for i := range resp.Applications {
if resp.Applications[i].Name == applicationName {
application = &resp.Applications[i]
break
}
}
return application
}

func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, userScanTypes string) (
[]wrappers.Config,
error,
Expand Down
11 changes: 8 additions & 3 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,14 @@ func TestScanCreate_ExistingApplicationAndProject_CreateProjectUnderApplicationS
execCmdNilAssertion(t, "scan", "create", "--project-name", "MOCK", "--application-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch")
}

func TestScanCreate_ApplicationNameIsNotExactMatch_FailedToCreateScan(t *testing.T) {
err := execCmdNotNilAssertion(t, "scan", "create", "--project-name", "MOCK", "--application-name", "MOC", "-s", dummyRepo, "-b", "dummy_branch")
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestScanCreate_ExistingProjectAndApplicationWithNoPermission_FailedToCreateScan(t *testing.T) {
err := execCmdNotNilAssertion(t, "scan", "create", "--project-name", "MOCK", "--application-name", mock.ApplicationDoesntExist, "-s", dummyRepo, "-b", "dummy_branch")
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExist)
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestScanCreate_ExistingApplication_CreateNewProjectUnderApplicationSuccessfully(t *testing.T) {
Expand All @@ -139,7 +144,7 @@ func TestScanCreate_ExistingApplication_CreateNewProjectUnderApplicationSuccessf

func TestScanCreate_ExistingApplicationWithNoPermission_FailedToCreateScan(t *testing.T) {
err := execCmdNotNilAssertion(t, "scan", "create", "--project-name", "NewProject", "--application-name", mock.NoPermissionApp, "-s", dummyRepo, "-b", "dummy_branch")
assert.Assert(t, err.Error() == applicationErrors.ApplicationNoPermission)
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestScanCreate_OnReceivingHttpBadRequestStatusCode_FailedToCreateScan(t *testing.T) {
Expand All @@ -154,7 +159,7 @@ func TestScanCreate_OnReceivingHttpInternalServerErrorStatusCode_FailedToCreateS

func TestCreateScanInsideApplicationProjectExistNoPermissions(t *testing.T) {
err := execCmdNotNilAssertion(t, "scan", "create", "--project-name", "MOCK", "--application-name", mock.NoPermissionApp, "-s", dummyRepo, "-b", "dummy_branch")
assert.Assert(t, err.Error() == applicationErrors.ApplicationNoPermission)
assert.Assert(t, err.Error() == applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestCreateScanSourceDirectory(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/errors/application-errors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package applicationerrors

const (
ApplicationDoesntExist = "Provided application does not exist"
ApplicationNoPermission = "User have no permission to the application"
ApplicationDoesntExistOrNoPermission = "Provided application does not exist or user has no permission to the application"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/wrappers/application-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (a *ApplicationsHTTPWrapper) Get(params map[string]string) (*ApplicationsRe
}
return nil, nil
case http.StatusForbidden:
return nil, errors.Errorf(applicationErrors.ApplicationNoPermission)
return nil, errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
case http.StatusOK:
model := ApplicationsResponseModel{}
err = decoder.Decode(&model)
Expand Down
4 changes: 2 additions & 2 deletions internal/wrappers/mock/application-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type ApplicationsMockWrapper struct{}

func (a ApplicationsMockWrapper) Get(params map[string]string) (*wrappers.ApplicationsResponseModel, error) {
if params["name"] == NoPermissionApp {
return nil, errors.Errorf(applicationErrors.ApplicationNoPermission)
return nil, errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
}
if params["name"] == ApplicationDoesntExist {
return nil, errors.Errorf(applicationErrors.ApplicationDoesntExist)
return nil, errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
}
if params["name"] == FakeHTTPStatusBadRequest {
return nil, errors.Errorf(applicationErrors.FailedToGetApplication)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestProjectCreate_ApplicationDoesntExist_FailAndReturnErrorMessage(t *testi
flag(params.ApplicationName), "application-that-doesnt-exist",
)

assertError(t, err, applicationErrors.ApplicationDoesntExist)
assertError(t, err, applicationErrors.ApplicationDoesntExistOrNoPermission)
}

func TestProjectCreate_ApplicationExists_CreateProjectSuccessfully(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestScanCreate_ApplicationDoesntExist_FailScanWithError(t *testing.T) {
}

err, _ := executeCommand(t, args...)
assertError(t, err, applicationErrors.ApplicationDoesntExist)
assertError(t, err, applicationErrors.ApplicationDoesntExistOrNoPermission)
}

// Create scans from current dir, zip and url and perform assertions in executeScanAssertions
Expand Down
Loading