Skip to content

Commit

Permalink
Merge pull request #1360 from openmeterio/notification-api
Browse files Browse the repository at this point in the history
test: add integration test suite for Notification API
  • Loading branch information
chrisgacsal authored Aug 14, 2024
2 parents feeff70 + f7081a8 commit 3342ba9
Show file tree
Hide file tree
Showing 11 changed files with 1,349 additions and 11 deletions.
17 changes: 17 additions & 0 deletions ci/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ func postgres() *dagger.Service {
WithExposedPort(5432).
AsService()
}

const (
SvixJWTSingingSecret = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MjI5NzYyNzMsImV4cCI6MjAzODMzNjI3MywibmJmIjoxNzIyOTc2MjczLCJpc3MiOiJzdml4LXNlcnZlciIsInN1YiI6Im9yZ18yM3JiOFlkR3FNVDBxSXpwZ0d3ZFhmSGlyTXUifQ.PomP6JWRI62W5N4GtNdJm2h635Q5F54eij0J3BU-_Ds"
)

func svix() *dagger.Service {
return dag.Container().
From(fmt.Sprintf("svix/svix-server:%s", svixVersion)).
WithEnvVariable("WAIT_FOR", "true").
WithEnvVariable("SVIX_QUEUE_TYPE", "memory").
WithEnvVariable("SVIX_CACHE_TYPE", "memory").
WithEnvVariable("SVIX_DB_DSN", "postgres://postgres:postgres@postgres:5432/postgres?sslmode=disable").
WithEnvVariable("SVIX_JWT_SECRET", SvixJWTSingingSecret).
WithServiceBinding("postgres", postgres()).
WithExposedPort(8071).
AsService()
}
3 changes: 3 additions & 0 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (m *Ci) Test() *dagger.Container {
WithSource(m.Source).
Container().
WithServiceBinding("postgres", postgres()).
WithServiceBinding("svix", svix()).
WithEnvVariable("POSTGRES_HOST", "postgres").
WithEnvVariable("SVIX_HOST", "svix").
WithEnvVariable("SVIX_JWT_SECRET", SvixJWTSingingSecret).
WithExec([]string{"go", "test", "-tags", "musl", "-v", "./..."})
}

Expand Down
1 change: 1 addition & 0 deletions ci/versions_pinned.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
clickhouseVersion = "24.5.5.78"
redisVersion = "7.0.12"
postgresVersion = "14.9"
svixVersion = "v1.29"

// TODO: add update mechanism for versions below

Expand Down
24 changes: 13 additions & 11 deletions internal/notification/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ func (c *EventHandlerConfig) Validate() error {
return fmt.Errorf("webhook is required")
}

if c.Logger == nil {
c.Logger = slog.Default()
}

if c.ReconcileInterval == 0 {
c.ReconcileInterval = DefaultReconcileInterval
}

return nil
}

Expand Down Expand Up @@ -248,9 +240,19 @@ func NewEventHandler(config EventHandlerConfig) (EventHandler, error) {
return nil, err
}

if config.ReconcileInterval == 0 {
config.ReconcileInterval = DefaultReconcileInterval
}

if config.Logger == nil {
config.Logger = slog.Default()
}

return &handler{
repo: config.Repository,
webhook: config.Webhook,
stopCh: make(chan struct{}),
repo: config.Repository,
webhook: config.Webhook,
reconcileInterval: config.ReconcileInterval,
logger: config.Logger,
stopCh: make(chan struct{}),
}, nil
}
168 changes: 168 additions & 0 deletions test/notification/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package notification

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/openmeterio/openmeter/internal/notification"
notificationwebhook "github.com/openmeterio/openmeter/internal/notification/webhook"
"github.com/openmeterio/openmeter/pkg/models"
)

func NewCreateChannelInput(name string) notification.CreateChannelInput {
return notification.CreateChannelInput{
NamespacedModel: models.NamespacedModel{
Namespace: TestNamespace,
},
Type: notification.ChannelTypeWebhook,
Name: name,
Disabled: false,
Config: notification.ChannelConfig{
ChannelConfigMeta: notification.ChannelConfigMeta{
Type: notification.ChannelTypeWebhook,
},
WebHook: notification.WebHookChannelConfig{
CustomHeaders: map[string]interface{}{
"X-TEST-HEADER": "NotificationChannelTest",
},
URL: TestWebhookURL,
SigningSecret: TestSigningSecret,
},
},
}
}

type ChannelTestSuite struct {
Env TestEnv
}

func (s *ChannelTestSuite) TestCreate(ctx context.Context, t *testing.T) {
service := s.Env.Notification()

createIn := NewCreateChannelInput("NotificationCreateChannel")

channel, err := service.CreateChannel(ctx, createIn)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel, "Channel must not be nil")
assert.NotEmpty(t, channel.ID, "Channel ID must not be empty")
assert.Equal(t, createIn.Disabled, channel.Disabled, "Channel must not be disabled")
assert.Equal(t, createIn.Type, channel.Type, "Channel type must be the same")
assert.EqualValues(t, createIn.Config, channel.Config, "Channel config must be the same")
}

func (s *ChannelTestSuite) TestList(ctx context.Context, t *testing.T) {
service := s.Env.Notification()

createIn1 := NewCreateChannelInput("NotificationListChannel1")
channel1, err := service.CreateChannel(ctx, createIn1)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel1, "Channel must not be nil")

createIn2 := NewCreateChannelInput("NotificationListChannel2")
channel2, err := service.CreateChannel(ctx, createIn2)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel2, "Channel must not be nil")

list, err := service.ListChannels(ctx, notification.ListChannelsInput{
Namespaces: []string{
createIn1.Namespace,
createIn2.Namespace,
},
Channels: []string{
channel1.ID,
channel2.ID,
},
OrderBy: "id",
IncludeDisabled: false,
})
require.NoError(t, err, "Listing channels must not return error")
assert.NotEmpty(t, list.Items, "List of channels must not be empty")

expectedList := []notification.Channel{
*channel1,
*channel2,
}

assert.EqualValues(t, expectedList, list.Items, "Unexpected items returned by listing channels")
}

func (s *ChannelTestSuite) TestUpdate(ctx context.Context, t *testing.T) {
service := s.Env.Notification()

createIn := NewCreateChannelInput("NotificationUpdateChannel1")

channel, err := service.CreateChannel(ctx, createIn)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel, "Channel must not be nil")

secret, err := notificationwebhook.NewSigningSecretWithDefaultSize()
require.NoError(t, err, "Generating new signing secret must not return an error")

updateIn := notification.UpdateChannelInput{
NamespacedModel: channel.NamespacedModel,
Type: channel.Type,
Name: "NotificationUpdateChannel2",
Disabled: true,
Config: notification.ChannelConfig{
ChannelConfigMeta: channel.Config.ChannelConfigMeta,
WebHook: notification.WebHookChannelConfig{
CustomHeaders: map[string]interface{}{
"X-TEST-HEADER": "NotificationUpdateChannel2",
},
URL: "http://example.com/update",
SigningSecret: secret,
},
},
ID: channel.ID,
}

channel2, err := service.UpdateChannel(ctx, updateIn)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel2, "Channel must not be nil")

assert.Equal(t, updateIn.Disabled, channel2.Disabled, "Channel must not be disabled")
assert.Equal(t, updateIn.Type, channel2.Type, "Channel type must be the same")
assert.EqualValues(t, updateIn.Config, channel2.Config, "Channel config must be the same")
}

func (s *ChannelTestSuite) TestDelete(ctx context.Context, t *testing.T) {
service := s.Env.Notification()

createIn := NewCreateChannelInput("NotificationDeleteChannel1")

channel, err := service.CreateChannel(ctx, createIn)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel, "Channel must not be nil")

err = service.DeleteChannel(ctx, notification.DeleteChannelInput{
Namespace: channel.Namespace,
ID: channel.ID,
})
require.NoError(t, err, "Deleting channel must not return error")
}

func (s *ChannelTestSuite) TestGet(ctx context.Context, t *testing.T) {
service := s.Env.Notification()

createIn := NewCreateChannelInput("NotificationGetChannel1")

channel, err := service.CreateChannel(ctx, createIn)
require.NoError(t, err, "Creating channel must not return error")
require.NotNil(t, channel, "Channel must not be nil")

channel2, err := service.GetChannel(ctx, notification.GetChannelInput{
Namespace: channel.Namespace,
ID: channel.ID,
})
require.NoError(t, err, "Deleting channel must not return error")
require.NotNil(t, channel2, "Channel must not be nil")
assert.NotEmpty(t, channel2.ID, "Channel ID must not be empty")
assert.Equal(t, channel.Namespace, channel2.Namespace, "Channel namespace must be equal")
assert.Equal(t, channel.ID, channel2.ID, "Channel ID must be equal")
assert.Equal(t, channel.Disabled, channel2.Disabled, "Channel disabled must not be equal")
assert.Equal(t, channel.Type, channel2.Type, "Channel type must be the same")
assert.EqualValues(t, channel.Config, channel2.Config, "Channel config must be the same")
}
Loading

0 comments on commit 3342ba9

Please sign in to comment.