|
| 1 | +// Copyright © 2024 Ory Corp |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package eventstreams |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func ptr(s string) *string { return &s } |
| 14 | + |
| 15 | +func TestStreamConfigValidate(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + base := func() streamConfig { |
| 19 | + return streamConfig{ |
| 20 | + Type: "https", |
| 21 | + HttpsEndpoint: ptr("https://example.com/webhook"), |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + t.Run("accepts a valid active status", func(t *testing.T) { |
| 26 | + c := base() |
| 27 | + c.Status = ptr(StatusActive) |
| 28 | + require.NoError(t, c.Validate()) |
| 29 | + assert.Equal(t, StatusActive, *c.Status) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("accepts a valid paused status", func(t *testing.T) { |
| 33 | + c := base() |
| 34 | + c.Status = ptr(StatusPaused) |
| 35 | + require.NoError(t, c.Validate()) |
| 36 | + assert.Equal(t, StatusPaused, *c.Status) |
| 37 | + }) |
| 38 | + |
| 39 | + t.Run("normalizes an empty status to nil so the server default applies", func(t *testing.T) { |
| 40 | + c := base() |
| 41 | + c.Status = ptr("") |
| 42 | + require.NoError(t, c.Validate()) |
| 43 | + assert.Nil(t, c.Status) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("rejects an unknown status", func(t *testing.T) { |
| 47 | + c := base() |
| 48 | + c.Status = ptr("frozen") |
| 49 | + assert.ErrorContains(t, c.Validate(), "--status") |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("status is optional when unset", func(t *testing.T) { |
| 53 | + c := base() |
| 54 | + require.NoError(t, c.Validate()) |
| 55 | + assert.Nil(t, c.Status) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestStreamConfigToSetBody(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + t.Run("maps all fields including the required type as a pointer", func(t *testing.T) { |
| 63 | + c := streamConfig{ |
| 64 | + Type: "https", |
| 65 | + HttpsEndpoint: ptr("https://example.com/webhook"), |
| 66 | + Status: ptr(StatusPaused), |
| 67 | + } |
| 68 | + body := c.toSetBody() |
| 69 | + require.NotNil(t, body.Type) |
| 70 | + assert.Equal(t, "https", *body.Type) |
| 71 | + require.NotNil(t, body.HttpsEndpoint) |
| 72 | + assert.Equal(t, "https://example.com/webhook", *body.HttpsEndpoint) |
| 73 | + require.NotNil(t, body.Status) |
| 74 | + assert.Equal(t, StatusPaused, *body.Status) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("leaves type nil when unset so the current type is kept", func(t *testing.T) { |
| 78 | + c := streamConfig{Status: ptr(StatusActive)} |
| 79 | + body := c.toSetBody() |
| 80 | + assert.Nil(t, body.Type) |
| 81 | + require.NotNil(t, body.Status) |
| 82 | + assert.Equal(t, StatusActive, *body.Status) |
| 83 | + }) |
| 84 | +} |
0 commit comments