Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/apirouter/destination_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ func (h *DestinationHandlers) Update(c *gin.Context) {
updatedDestination.Filter = input.Filter
}
if input.DeliveryMetadata != nil {
updatedDestination.DeliveryMetadata = maputil.MergeStringMaps(originalDestination.DeliveryMetadata, input.DeliveryMetadata)
updatedDestination.DeliveryMetadata = input.DeliveryMetadata
}
if input.Metadata != nil {
updatedDestination.Metadata = maputil.MergeStringMaps(originalDestination.Metadata, input.Metadata)
updatedDestination.Metadata = input.Metadata
}

// Always preprocess before updating
Expand Down
185 changes: 185 additions & 0 deletions internal/apirouter/destination_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,191 @@ func TestAPI_Destinations(t *testing.T) {
require.Equal(t, http.StatusUnprocessableEntity, resp.Code)
})

t.Run("filter is replaced not merged", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithFilter(models.Filter{"body": map[string]any{"user_id": "usr_123"}}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"filter": map[string]any{"body": map[string]any{"status": "active"}},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Nil(t, dest.Filter["body"].(map[string]any)["user_id"], "old filter key should not be present")
assert.Equal(t, "active", dest.Filter["body"].(map[string]any)["status"])
})

t.Run("filter cleared with empty object", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithFilter(models.Filter{"body": map[string]any{"user_id": "usr_123"}}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"filter": map[string]any{},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.True(t, dest.Filter == nil || len(dest.Filter) == 0, "filter should be cleared")
})

t.Run("filter unchanged when omitted", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithFilter(models.Filter{"body": map[string]any{"user_id": "usr_123"}}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"topics": []string{"user.created"},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Equal(t, "usr_123", dest.Filter["body"].(map[string]any)["user_id"])
})

t.Run("metadata is replaced not merged", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithMetadata(map[string]string{"env": "production", "team": "platform", "region": "us-east-1"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"metadata": map[string]string{"env": "production", "team": "platform"},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Equal(t, models.Metadata{"env": "production", "team": "platform"}, dest.Metadata)
_, hasRegion := dest.Metadata["region"]
assert.False(t, hasRegion, "region key should have been removed")
})

t.Run("metadata cleared with empty object", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithMetadata(map[string]string{"env": "production"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"metadata": map[string]string{},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.True(t, dest.Metadata == nil || len(dest.Metadata) == 0, "metadata should be cleared")
})

t.Run("metadata unchanged when omitted", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithMetadata(map[string]string{"env": "production"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"topics": []string{"user.created"},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Equal(t, models.Metadata{"env": "production"}, dest.Metadata)
})

t.Run("delivery_metadata is replaced not merged", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithDeliveryMetadata(map[string]string{"source": "outpost", "version": "1.0"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"delivery_metadata": map[string]string{"source": "outpost"},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Equal(t, models.DeliveryMetadata{"source": "outpost"}, dest.DeliveryMetadata)
_, hasVersion := dest.DeliveryMetadata["version"]
assert.False(t, hasVersion, "version key should have been removed")
})

t.Run("delivery_metadata cleared with empty object", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithDeliveryMetadata(map[string]string{"source": "outpost"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"delivery_metadata": map[string]string{},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.True(t, dest.DeliveryMetadata == nil || len(dest.DeliveryMetadata) == 0, "delivery_metadata should be cleared")
})

t.Run("delivery_metadata unchanged when omitted", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
h.tenantStore.CreateDestination(t.Context(), df.Any(
df.WithID("d1"), df.WithTenantID("t1"),
df.WithDeliveryMetadata(map[string]string{"source": "outpost"}),
))

req := h.jsonReq(http.MethodPatch, "/api/v1/tenants/t1/destinations/d1", map[string]any{
"topics": []string{"user.created"},
})
resp := h.do(h.withAPIKey(req))

require.Equal(t, http.StatusOK, resp.Code)

var dest destregistry.DestinationDisplay
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &dest))
assert.Equal(t, models.DeliveryMetadata{"source": "outpost"}, dest.DeliveryMetadata)
})

t.Run("sending same type is allowed", func(t *testing.T) {
h := newAPITest(t)
h.tenantStore.UpsertTenant(t.Context(), tf.Any(tf.WithID("t1")))
Expand Down
2 changes: 1 addition & 1 deletion sdks/outpost-go/.speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ go:
methodArguments: require-security-and-request
modulePath: ""
multipartArrayFormat: legacy
nullableOptionalWrapper: false
nullableOptionalWrapper: true
outputModelSuffix: output
packageName: github.com/hookdeck/outpost/sdks/outpost-go
respectRequiredFields: false
Expand Down
17 changes: 17 additions & 0 deletions spec-sdk-tests/tests/destinations/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/hookdeck/outpost/spec-sdk-tests/tests/destinations

go 1.22

require (
github.com/hookdeck/outpost/sdks/outpost-go v0.0.0
github.com/stretchr/testify v1.11.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spyzhov/ajson v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/hookdeck/outpost/sdks/outpost-go => ../../../sdks/outpost-go
12 changes: 12 additions & 0 deletions spec-sdk-tests/tests/destinations/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spyzhov/ajson v0.8.0 h1:sFXyMbi4Y/BKjrsfkUZHSjA2JM1184enheSjjoT/zCc=
github.com/spyzhov/ajson v0.8.0/go.mod h1:63V+CGM6f1Bu/p4nLIN8885ojBdt88TbLoSFzyqMuVA=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading