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

Return 404 from GET Bindings for Expired Bindings #1355

6 changes: 6 additions & 0 deletions internal/broker/bind_get.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/kyma-project/kyma-environment-broker/internal/storage"
"github.com/pivotal-cf/brokerapi/v8/domain"
@@ -34,6 +35,11 @@ func (b *GetBindingEndpoint) GetBinding(_ context.Context, instanceID, bindingID
return domain.GetBindingSpec{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusNotFound, message)
}

if binding.ExpiresAt.Before(time.Now()) {
message := "Binding expired"
return domain.GetBindingSpec{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusNotFound, message)
}

if err != nil {
b.log.Errorf("GetBinding error: %s", err)
message := fmt.Sprintf("Unexpected error: %s", err)
48 changes: 48 additions & 0 deletions internal/broker/bind_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package broker

import (
"context"
"net/http"
"testing"
"time"

"github.com/kyma-project/kyma-environment-broker/internal"
"github.com/kyma-project/kyma-environment-broker/internal/storage/driver/memory"
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/pivotal-cf/brokerapi/v8/domain/apiresponses"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestGetBinding(t *testing.T) {

t.Run("should return 404 code for the expired binding", func(t *testing.T) {
// given
bindingsMemory := memory.NewBinding()

expiredBinding := &internal.Binding{
ID: "test-binding-id",
InstanceID: "test-instance-id",
ExpiresAt: time.Now().Add(-1 * time.Hour),
}
err := bindingsMemory.Insert(expiredBinding)
require.NoError(t, err)

endpoint := &GetBindingEndpoint{
bindings: bindingsMemory,
log: &logrus.Logger{},
}

// when
_, err = endpoint.GetBinding(context.Background(), "test-instance-id", "test-binding-id", domain.FetchBindingDetails{})

// then
require.NotNil(t, err)
apiErr, ok := err.(*apiresponses.FailureResponse)
require.True(t, ok)
require.Equal(t, http.StatusNotFound, apiErr.ValidatedStatusCode(nil))

errorResponse := apiErr.ErrorResponse().(apiresponses.ErrorResponse)
require.Equal(t, "Binding expired", errorResponse.Description)
})
}