Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat: added unit tests for case when project metadata has invalid/mis…
Browse files Browse the repository at this point in the history
…sing hostname
  • Loading branch information
Ayushi Sharma committed Oct 10, 2023
1 parent f446014 commit 038c059
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions internal/server/v1/optimus/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,60 @@ func TestRoutesFindJobSpec(t *testing.T) {
assert.Equal(t, http.StatusNotFound, response.Code)
})

t.Run("should return 422 if project metadata contains invalid optimus host", func(t *testing.T) {
projectRes := &shieldv1beta1.GetProjectResponse{
Project: &shieldv1beta1.Project{
Slug: "test-project",
Metadata: newStruct(t, map[string]interface{}{
"optimus_host": 42,
}),
},
}

shieldClient := new(mocks.ShieldServiceClient)
shieldClient.On("GetProject", mock.Anything, &shieldv1beta1.GetProjectRequest{
Id: projectSlug,
}).Return(projectRes, nil)

defer shieldClient.AssertExpectations(t)

optimusClient := new(optimus.OptimusClientMock)
defer optimusClient.AssertExpectations(t)

response := httptest.NewRecorder()
request := httptest.NewRequest(method, path, nil)
router := chi.NewRouter()
optimus.Routes(shieldClient, optimusClient)(router)
router.ServeHTTP(response, request)
assert.Equal(t, http.StatusUnprocessableEntity, response.Code)
})

t.Run("should return 404 if project metadata doesn't contain optimus host", func(t *testing.T) {
projectRes := &shieldv1beta1.GetProjectResponse{
Project: &shieldv1beta1.Project{
Slug: "test-project",
Metadata: newStruct(t, map[string]interface{}{}),
},
}

shieldClient := new(mocks.ShieldServiceClient)
shieldClient.On("GetProject", mock.Anything, &shieldv1beta1.GetProjectRequest{
Id: projectSlug,
}).Return(projectRes, nil)

defer shieldClient.AssertExpectations(t)

optimusClient := new(optimus.OptimusClientMock)
defer optimusClient.AssertExpectations(t)

response := httptest.NewRecorder()
request := httptest.NewRequest(method, path, nil)
router := chi.NewRouter()
optimus.Routes(shieldClient, optimusClient)(router)
router.ServeHTTP(response, request)
assert.Equal(t, http.StatusNotFound, response.Code)
})

t.Run("should return 500 for internal error", func(t *testing.T) {
clientError := status.Error(codes.Internal, "Internal")

Expand Down Expand Up @@ -230,6 +284,60 @@ func TestRoutesListJobs(t *testing.T) {
assert.JSONEq(t, string(expectedJSON), string(resultJSON))
})

t.Run("should return 422 if project metadata contains invalid optimus host", func(t *testing.T) {
projectRes := &shieldv1beta1.GetProjectResponse{
Project: &shieldv1beta1.Project{
Slug: "test-project",
Metadata: newStruct(t, map[string]interface{}{
"optimus_host": 42,
}),
},
}

shieldClient := new(mocks.ShieldServiceClient)
shieldClient.On("GetProject", mock.Anything, &shieldv1beta1.GetProjectRequest{
Id: projectSlug,
}).Return(projectRes, nil)

defer shieldClient.AssertExpectations(t)

optimusClient := new(optimus.OptimusClientMock)
defer optimusClient.AssertExpectations(t)

response := httptest.NewRecorder()
request := httptest.NewRequest(method, path, nil)
router := chi.NewRouter()
optimus.Routes(shieldClient, optimusClient)(router)
router.ServeHTTP(response, request)
assert.Equal(t, http.StatusUnprocessableEntity, response.Code)
})

t.Run("should return 404 if project metadata doesn't contain optimus host", func(t *testing.T) {
projectRes := &shieldv1beta1.GetProjectResponse{
Project: &shieldv1beta1.Project{
Slug: "test-project",
Metadata: newStruct(t, map[string]interface{}{}),
},
}

shieldClient := new(mocks.ShieldServiceClient)
shieldClient.On("GetProject", mock.Anything, &shieldv1beta1.GetProjectRequest{
Id: projectSlug,
}).Return(projectRes, nil)

defer shieldClient.AssertExpectations(t)

optimusClient := new(optimus.OptimusClientMock)
defer optimusClient.AssertExpectations(t)

response := httptest.NewRecorder()
request := httptest.NewRequest(method, path, nil)
router := chi.NewRouter()
optimus.Routes(shieldClient, optimusClient)(router)
router.ServeHTTP(response, request)
assert.Equal(t, http.StatusNotFound, response.Code)
})

t.Run("should return 500 for internal error", func(t *testing.T) {
clientError := status.Error(codes.Internal, "Internal")

Expand Down

0 comments on commit 038c059

Please sign in to comment.