Skip to content

Commit

Permalink
Merge pull request #159 from ans-group/DeleteApplicationServices
Browse files Browse the repository at this point in the history
Improved test coverage and implemented DeleteApplicationServices
  • Loading branch information
PCloughster authored Sep 9, 2024
2 parents b63be8d + a78be78 commit b505bb0
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/service/account/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type AccountService interface {
UpdateApplication(appID string, req UpdateApplicationRequest) error
GetApplicationServices(appID string) (ApplicationServiceMapping, error)
SetApplicationServices(appID string, req SetServiceRequest) error
DeleteApplicationServices(appID string) error
GetApplicationRestrictions(appID string) (ApplicationRestriction, error)
SetApplicationRestrictions(appID string, req SetRestrictionRequest) error
DeleteApplicationRestrictions(appID string) error
Expand Down
16 changes: 15 additions & 1 deletion pkg/service/account/service_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *Service) updateApplicationResponseBody(appID string, req UpdateApplicat
return &connection.APIResponseBodyData[Application]{}, fmt.Errorf("invalid application id")
}

return connection.Patch[Application](s.connection, fmt.Sprintf("/account/v1/applications/%s", appID), &req)
return connection.Patch[Application](s.connection, fmt.Sprintf("/account/v1/applications/%s", appID), &req, connection.NotFoundResponseHandler(&ApplicationNotFoundError{ID: appID}))
}

// GetApplicationServices retrieves the services and roles of an application by id
Expand Down Expand Up @@ -109,6 +109,20 @@ func (s *Service) setApplicationServicesResponseBody(appID string, req SetServic
return connection.Put[interface{}](s.connection, fmt.Sprintf("/account/v1/applications/%s/services", appID), &req, connection.NotFoundResponseHandler(&ApplicationNotFoundError{ID: appID}))
}

func (s *Service) DeleteApplicationServices(appID string) error {
_, err := s.deleteApplicationServicesResponseBody(appID)

return err
}

func (s *Service) deleteApplicationServicesResponseBody(appID string) (*connection.APIResponseBodyData[interface{}], error) {
if appID == "" {
return &connection.APIResponseBodyData[interface{}]{}, fmt.Errorf("invalid application id")
}

return connection.Put[interface{}](s.connection, fmt.Sprintf("/account/v1/applications/%s/services", appID), SetServiceRequest{Scopes: []ApplicationServiceScope{}}, connection.NotFoundResponseHandler(&ApplicationNotFoundError{ID: appID}))
}

// DeleteApplication removes an application
func (s *Service) DeleteApplication(appID string) error {
_, err := s.deleteApplicationResponseBody(appID)
Expand Down
164 changes: 164 additions & 0 deletions pkg/service/account/service_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,89 @@ func TestCreateApplication(t *testing.T) {
})
}

func TestUpdateApplication(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

updateRequest := UpdateApplicationRequest{}

c.EXPECT().Patch("/account/v1/applications/test-id-123", gomock.Eq(&updateRequest)).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte("{\"data\":{\"id\":\"test-id-123\"}}"))),
StatusCode: 202,
},
}, nil).Times(1)

err := s.UpdateApplication("test-id-123", updateRequest)

assert.Nil(t, err)
})

t.Run("ConnectionError_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

c.EXPECT().Patch("/account/v1/applications/test-id-123", gomock.Any()).Return(&connection.APIResponse{}, errors.New("test error 1")).Times(1)

err := s.UpdateApplication("test-id-123", UpdateApplicationRequest{})

assert.NotNil(t, err)
assert.Equal(t, "test error 1", err.Error())
})

t.Run("InvalidApplicationID_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

err := s.UpdateApplication("", UpdateApplicationRequest{})

assert.NotNil(t, err)
assert.Equal(t, "invalid application id", err.Error())
})

t.Run("404_ReturnsApplicationNotFoundError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

c.EXPECT().Patch("/account/v1/applications/test-id-123", gomock.Any()).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: 404,
},
}, nil).Times(1)

err := s.UpdateApplication("test-id-123", UpdateApplicationRequest{})

assert.NotNil(t, err)
assert.IsType(t, &ApplicationNotFoundError{}, err)
})
}

func TestGetApplicationServices(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
Expand Down Expand Up @@ -459,6 +542,87 @@ func TestSetApplicationServices(t *testing.T) {
})
}

func TestDeleteApplicationServices(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

c.EXPECT().Put("/account/v1/applications/test-id-123/services", gomock.Eq(SetServiceRequest{Scopes: []ApplicationServiceScope{}})).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte("{\"data\":{\"id\":\"test-id-123\"}}"))),
StatusCode: 202,
},
}, nil).Times(1)

err := s.DeleteApplicationServices("test-id-123")

assert.Nil(t, err)
})

t.Run("ConnectionError_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

c.EXPECT().Put("/account/v1/applications/test-id-123/services", gomock.Any()).Return(&connection.APIResponse{}, errors.New("test error 1")).Times(1)

err := s.DeleteApplicationServices("test-id-123")

assert.NotNil(t, err)
assert.Equal(t, "test error 1", err.Error())
})

t.Run("InvalidApplicationID_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

err := s.DeleteApplicationServices("")

assert.NotNil(t, err)
assert.Equal(t, "invalid application id", err.Error())
})

t.Run("404_ReturnsApplicationNotFoundError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

c := mocks.NewMockConnection(mockCtrl)

s := Service{
connection: c,
}

c.EXPECT().Put("/account/v1/applications/test-id-456/services", gomock.Any()).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: 404,
},
}, nil).Times(1)

err := s.DeleteApplicationServices("test-id-456")

assert.NotNil(t, err)
assert.IsType(t, &ApplicationNotFoundError{}, err)
})
}

func TestGetApplicationRestrictions(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
Expand Down

0 comments on commit b505bb0

Please sign in to comment.