Skip to content

Commit

Permalink
Merge pull request #158 from ans-group/DeleteApplicationRestrictions-…
Browse files Browse the repository at this point in the history
…functionality

Added DeleteApplicationRestrictions function + testing
  • Loading branch information
PCloughster authored Sep 5, 2024
2 parents a322435 + 953c826 commit b63be8d
Show file tree
Hide file tree
Showing 3 changed files with 97 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 @@ -41,6 +41,7 @@ type AccountService interface {
SetApplicationServices(appID string, req SetServiceRequest) error
GetApplicationRestrictions(appID string) (ApplicationRestriction, error)
SetApplicationRestrictions(appID string, req SetRestrictionRequest) error
DeleteApplicationRestrictions(appID string) error
DeleteApplication(appID string) error
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/service/account/service_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ func (s *Service) setApplicationRestrictionsResponseBody(appID string, req SetRe

return connection.Put[interface{}](s.connection, fmt.Sprintf("/account/v1/applications/%s/ip-restrictions", appID), &req, connection.NotFoundResponseHandler(&ApplicationNotFoundError{ID: appID}))
}

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

return err
}

func (s *Service) deleteApplicationRestrictionsResponseBody(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/ip-restrictions", appID), connection.APIRequestParameters{}, connection.NotFoundResponseHandler(&ApplicationNotFoundError{ID: appID}))
}
83 changes: 82 additions & 1 deletion pkg/service/account/service_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestCreateApplication(t *testing.T) {
id, err := s.CreateApplication(createRequest)

assert.Nil(t, err)
assert.Equal(t, "test-id-123", id)
assert.Equal(t, "test-id-123", id.ID)
})

t.Run("ConnectionError_ReturnsError", func(t *testing.T) {
Expand Down Expand Up @@ -624,6 +624,87 @@ func TestSetApplicationRestrictions(t *testing.T) {
})
}

func TestDeleteApplicationRestriction(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/ip-restrictions", gomock.Eq(connection.APIRequestParameters{})).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: 204,
},
}, nil).Times(1)

err := s.DeleteApplicationRestrictions("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/ip-restrictions", gomock.Any()).Return(&connection.APIResponse{}, errors.New("test error 1")).Times(1)

err := s.DeleteApplicationRestrictions("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.DeleteApplicationRestrictions("")

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/ip-restrictions", gomock.Any()).Return(&connection.APIResponse{
Response: &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: 404,
},
}, nil).Times(1)

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

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

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

0 comments on commit b63be8d

Please sign in to comment.