A source is keyed by a uuid, owned by the organisation or one person, and labelled within that scope. The webhook path is a separate unique column, so renaming either leaves stored events, event types, triggers, groups, mappings and drafts alone. Delivery policies, sealed secrets and group keys use the id.
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
|
)
|
|
|
|
type declaredEvents struct {
|
|
Connectors
|
|
caps map[string]connectors.Capabilities
|
|
}
|
|
|
|
func (d declaredEvents) Capabilities(_ context.Context, connector string) (connectors.Capabilities, bool, error) {
|
|
caps, ok := d.caps[connector]
|
|
return caps, ok, nil
|
|
}
|
|
|
|
var hookSource = uuid.NullUUID{UUID: hookID, Valid: true}
|
|
|
|
type oneSource struct{ connector string }
|
|
|
|
func (o oneSource) Source(_ context.Context, _ string, id uuid.UUID) (store.Source, error) {
|
|
if id != hookID {
|
|
return store.Source{}, store.ErrNotFound
|
|
}
|
|
return store.Source{ID: id, Connector: o.connector}, nil
|
|
}
|
|
|
|
func (o oneSource) SourceByPath(context.Context, string, string) (store.Source, error) {
|
|
return store.Source{}, store.ErrNotFound
|
|
}
|
|
|
|
// TestSavingATriggerChecksItsEventType guards the trigger contract: a trigger
|
|
// on an event type its source's connector doesn't declare is refused when it
|
|
// is saved, because it could never fire. A schedule has no connector and is
|
|
// never checked.
|
|
func TestSavingATriggerChecksItsEventType(t *testing.T) {
|
|
caps := map[string]connectors.Capabilities{
|
|
"pager": {Events: []connectors.Capability{{Name: "incident.opened"}}},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
source uuid.NullUUID
|
|
connector string
|
|
eventType string
|
|
wantAt string
|
|
}{
|
|
{name: "a declared event type is accepted", source: hookSource, connector: "pager", eventType: "incident.opened"},
|
|
{name: "an undeclared event type is refused", source: hookSource, connector: "pager", eventType: "incident.closed", wantAt: "body.event_type"},
|
|
{name: "a connector that never loaded is refused", source: hookSource, connector: "other", eventType: "incident.opened", wantAt: "body.event_type"},
|
|
{name: "an unknown source is refused", source: uuid.NullUUID{UUID: uuid.New(), Valid: true}, connector: "pager", eventType: "incident.opened", wantAt: "body.source_id"},
|
|
{name: "a schedule is exempt", connector: "pager", eventType: scheduleType},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
m := &Module{reader: oneSource{connector: tt.connector}, connectors: declaredEvents{caps: caps}}
|
|
err := m.checkEventType(t.Context(), tt.source, tt.eventType)
|
|
if tt.wantAt == "" {
|
|
if err != nil {
|
|
t.Fatalf("checkEventType: %v", err)
|
|
}
|
|
return
|
|
}
|
|
var refused *huma.ErrorModel
|
|
if !errors.As(err, &refused) || refused.Status != 400 || len(refused.Errors) != 1 || refused.Errors[0].Location != tt.wantAt {
|
|
t.Fatalf("checkEventType error = %#v, want a 400 at %s", err, tt.wantAt)
|
|
}
|
|
})
|
|
}
|
|
}
|