diff --git a/internal/apirouter/destination_handlers.go b/internal/apirouter/destination_handlers.go index 9926436d..ce01ccab 100644 --- a/internal/apirouter/destination_handlers.go +++ b/internal/apirouter/destination_handlers.go @@ -439,6 +439,10 @@ func (h *DestinationHandlers) handleUpsertDestinationError(c *gin.Context, err e AbortWithError(c, http.StatusBadRequest, NewErrBadRequest(err)) return } + if errors.Is(err, tenantstore.ErrMaxDestinationsPerTenantReached) { + AbortWithError(c, http.StatusBadRequest, NewErrBadRequest(err)) + return + } AbortWithError(c, http.StatusInternalServerError, NewErrInternalServer(err)) } diff --git a/internal/apirouter/destination_handlers_test.go b/internal/apirouter/destination_handlers_test.go index 00f50288..78747552 100644 --- a/internal/apirouter/destination_handlers_test.go +++ b/internal/apirouter/destination_handlers_test.go @@ -11,6 +11,7 @@ import ( "github.com/hookdeck/outpost/internal/models" "github.com/hookdeck/outpost/internal/opevents" "github.com/hookdeck/outpost/internal/tenantstore" + "github.com/hookdeck/outpost/internal/tenantstore/memtenantstore" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -57,6 +58,23 @@ func TestAPI_Destinations(t *testing.T) { require.Equal(t, http.StatusCreated, resp.Code) }) + t.Run("max destinations per tenant returns 400", func(t *testing.T) { + h := newAPITest(t, withTenantStore(memtenantstore.New(memtenantstore.WithMaxDestinationsPerTenant(1)))) + h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1"))) + + req := h.jsonReq(http.MethodPost, "/api/v1/tenants/t1/destinations", validDestination()) + resp := h.do(h.withAPIKey(req)) + require.Equal(t, http.StatusCreated, resp.Code) + + req = h.jsonReq(http.MethodPost, "/api/v1/tenants/t1/destinations", validDestination()) + resp = h.do(h.withAPIKey(req)) + + require.Equal(t, http.StatusBadRequest, resp.Code) + var body map[string]any + require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &body)) + assert.Equal(t, "maximum number of destinations per tenant reached", body["message"]) + }) + t.Run("missing type returns 422", func(t *testing.T) { h := newAPITest(t) h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))