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
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
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))
jaroslaw-pieszka marked this conversation as resolved.
Show resolved Hide resolved

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