Files
archie/modules/events/source_save_test.go
T
sam 30aa351eb6 feat(events,drafts): give sources an immutable id and a scope (#2127, #2027)
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.
2026-11-24 02:43:51 +11:00

54 lines
1.4 KiB
Go

package events
import (
"testing"
"filippo.io/age"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/events/internal/store"
)
// TestSavedSourceSecretIsSetOnlyForHMAC guards the NOT NULL hmac_secret_sealed
// column: a nil secret is written as NULL and fails the save, so token and none
// sources must carry an empty, non-nil secret and only HMAC a sealed one.
func TestSavedSourceSecretIsSetOnlyForHMAC(t *testing.T) {
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
sealer, err := seal.New(id.String(), nil)
if err != nil {
t.Fatal(err)
}
m := &Module{orgID: "org", sealer: sealer}
tests := []struct {
name string
verify string
wantSecret bool
}{
{"none", store.None, false},
{"token", store.Token, false},
{"hmac", store.HMAC, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
set := SourceSettings{ //nolint:gosec // fixture secret to be sealed
Connector: "c", Verification: tt.verify, HMACHeader: "X-Signature",
HMACSecret: "not-a-real-secret",
}
src, _, err := m.sourceToSave(set, store.Source{ID: hookID}, nil)
if err != nil {
t.Fatal(err)
}
if src.HMACSecretSealed == nil {
t.Fatal("secret is nil, which the store writes as NULL")
}
if got := len(src.HMACSecretSealed) > 0; got != tt.wantSecret {
t.Fatalf("secret set = %v, want %v", got, tt.wantSecret)
}
})
}
}