A mapping belongs to an event type and is checked against its inferred schema on save. Each event a mapping resolves is counted once in mapping_matches (migration 0008), read back as match_count and last_matched_at. Bindings carry an optional CEL filter over the mapping's parameters, checked on save. The per-source unique indexes are dropped: every armed binding whose mapping matches the capture's event type and whose filter admits it dispatches once, claimed in the per-binding ledger before its task is enqueued. The State Store gains RecordMappingMatch and the new mapping and binding fields.
371 lines
13 KiB
Go
371 lines
13 KiB
Go
package webui
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
// mappingTestServer builds a Server with both Mappings and Captures backed
|
|
// by the same concrete *store.Store, so preview tests can insert a real
|
|
// capture and resolve a mapping against it end to end.
|
|
func mappingTestServer(t *testing.T) *Server {
|
|
t.Helper()
|
|
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = s.Close() })
|
|
eda := edastore.OpenTest(t)
|
|
return &Server{
|
|
Store: s,
|
|
Log: slog.New(slog.DiscardHandler),
|
|
Mappings: eda,
|
|
Captures: eda,
|
|
EventTypes: testEventTypes(),
|
|
}
|
|
}
|
|
|
|
// testEventTypes holds one event type, et-1, whose schema has every path the
|
|
// mapping tests write.
|
|
func testEventTypes() *memEventTypes {
|
|
return &memEventTypes{types: []eventtype.EventType{{
|
|
ID: "et-1", Source: "sentry", Name: "issue",
|
|
Schema: map[string]eventtype.ValueType{
|
|
"a": eventtype.TypeString, "b": eventtype.TypeBool,
|
|
"issue": eventtype.TypeObject, "issue.title": eventtype.TypeString,
|
|
},
|
|
}}}
|
|
}
|
|
|
|
// A mapping belongs to an event type and is checked against its schema.
|
|
func TestHandleMappingSaveChecksEventTypeSchema(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
eventType string
|
|
path string
|
|
fieldType mapping.FieldType
|
|
want int
|
|
}{
|
|
{"fits the schema", "et-1", "issue.title", mapping.TypeString, http.StatusCreated},
|
|
{"no event type", "", "issue.title", mapping.TypeString, http.StatusBadRequest},
|
|
{"unknown event type", "et-9", "issue.title", mapping.TypeString, http.StatusBadRequest},
|
|
{"path the type does not have", "et-1", "sender.login", mapping.TypeString, http.StatusBadRequest},
|
|
{"type differs from the schema", "et-1", "issue.title", mapping.TypeNumber, http.StatusBadRequest},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
req := map[string]any{
|
|
"name": "m",
|
|
"event_type_id": tt.eventType,
|
|
"fields": []mapping.Field{{Name: "f", Path: tt.path, Type: tt.fieldType}},
|
|
}
|
|
if w := doJSON(t, srv, http.MethodPost, "/api/mappings", req); w.Code != tt.want {
|
|
t.Fatalf("create status = %d, want %d; body = %s", w.Code, tt.want, w.Body.String())
|
|
}
|
|
ok := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "base", "event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
var created mapping.Mapping
|
|
_ = json.Unmarshal(ok.Body.Bytes(), &created)
|
|
want := tt.want
|
|
if want == http.StatusCreated {
|
|
want = http.StatusOK
|
|
}
|
|
if w := doJSON(t, srv, http.MethodPatch, "/api/mappings/"+created.ID, req); w.Code != want {
|
|
t.Fatalf("update status = %d, want %d; body = %s", w.Code, want, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// doJSON issues a request carrying the browser mutation headers
|
|
// (Content-Type, X-Archie-CSRF) every write handler requires via
|
|
// authorizeTaskMutation, so GET/POST/PATCH/DELETE calls in this package's
|
|
// tests exercise the handler logic rather than the CSRF gate. Use
|
|
// doJSONNoCSRF to test the gate itself.
|
|
func doJSON(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
return doJSONWithHeaders(t, srv, method, path, body, true)
|
|
}
|
|
|
|
// doJSONNoCSRF is doJSON without the X-Archie-CSRF header, for asserting
|
|
// that a mutation handler refuses an unmarked cross-origin-shaped request.
|
|
func doJSONNoCSRF(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
return doJSONWithHeaders(t, srv, method, path, body, false)
|
|
}
|
|
|
|
func doJSONWithHeaders(t *testing.T, srv *Server, method, path string, body any, csrf bool) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var r *http.Request
|
|
if body != nil {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
t.Fatalf("marshal request body: %v", err)
|
|
}
|
|
r = httptest.NewRequestWithContext(t.Context(), method, path, bytes.NewReader(b))
|
|
} else {
|
|
r = httptest.NewRequestWithContext(t.Context(), method, path, nil)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if csrf {
|
|
r.Header.Set("X-Archie-CSRF", "1")
|
|
}
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
func TestHandleMappingCreateAndGet(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "sentry issue opened",
|
|
"source_hint": "sentry",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
},
|
|
})
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("create status = %d, want %d; body = %s", w.Code, http.StatusCreated, w.Body.String())
|
|
}
|
|
var created mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("unmarshal created mapping: %v", err)
|
|
}
|
|
if created.ID == "" || created.Name != "sentry issue opened" {
|
|
t.Fatalf("created mapping = %+v", created)
|
|
}
|
|
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("get status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var got mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("unmarshal got mapping: %v", err)
|
|
}
|
|
if got.ID != created.ID || len(got.Fields) != 1 {
|
|
t.Fatalf("got mapping = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingCreateRejectsInvalidMapping(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{"name": "", "fields": []mapping.Field{}})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusBadRequest, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingGetMissingReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings/999", nil)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingsList(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
for range 3 {
|
|
doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "m",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
}
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings", nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var body struct {
|
|
Mappings []mapping.Mapping `json:"mappings"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if len(body.Mappings) != 3 {
|
|
t.Fatalf("mappings = %d, want 3", len(body.Mappings))
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingUpdate(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "orig",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
var created mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
|
|
|
w = doJSON(t, srv, http.MethodPatch, "/api/mappings/"+created.ID, map[string]any{
|
|
"name": "renamed",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "b", Path: "b", Type: mapping.TypeBool}},
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("update status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
var got mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &got)
|
|
if got.Name != "renamed" || len(got.Fields) != 1 || got.Fields[0].Name != "b" {
|
|
t.Fatalf("updated mapping = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingUpdateMissingReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPatch, "/api/mappings/999", map[string]any{
|
|
"name": "x",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusNotFound, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingDelete(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "m",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
var created mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
|
|
|
w = doJSON(t, srv, http.MethodDelete, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("delete status = %d, want %d; body = %s", w.Code, http.StatusNoContent, w.Body.String())
|
|
}
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("get after delete status = %d, want %d", w.Code, http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingPreviewResolvesAgainstARealCapture(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
capID, err := srv.Captures.InsertCapture(t.Context(), store.CapturedEvent{
|
|
Source: "sentry",
|
|
Body: `{"issue":{"title":"bug found"},"count":3}`,
|
|
}, 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("InsertCapture: %v", err)
|
|
}
|
|
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings/preview", map[string]any{
|
|
"capture_id": capID,
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
{Name: "missing", Path: "nope", Type: mapping.TypeString, Required: true},
|
|
},
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("preview status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var body struct {
|
|
Values map[string]any `json:"values"`
|
|
Failures []mapping.Failure `json:"failures"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if body.Values["title"] != "bug found" {
|
|
t.Fatalf("values = %+v", body.Values)
|
|
}
|
|
if len(body.Failures) != 1 || body.Failures[0].FieldName != "missing" || body.Failures[0].Reason != mapping.ReasonMissing {
|
|
t.Fatalf("failures = %+v", body.Failures)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingPreviewUnknownCaptureReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings/preview", map[string]any{
|
|
"capture_id": "rabsent00000000",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusNotFound, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestMappingCaptureScanWindowUsesConfiguredCaptureMaxEvents(t *testing.T) {
|
|
if got := mappingCaptureScanWindow(20000); got != 20000 {
|
|
t.Errorf("mappingCaptureScanWindow(20000) = %d, want 20000", got)
|
|
}
|
|
}
|
|
|
|
func TestMappingCaptureScanWindowFallsBackWhenUnconfigured(t *testing.T) {
|
|
if got := mappingCaptureScanWindow(0); got != captureScanWindowDefault {
|
|
t.Errorf("mappingCaptureScanWindow(0) = %d, want default %d", got, captureScanWindowDefault)
|
|
}
|
|
if got := mappingCaptureScanWindow(-1); got != captureScanWindowDefault {
|
|
t.Errorf("mappingCaptureScanWindow(-1) = %d, want default %d", got, captureScanWindowDefault)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingsUnavailableWhenNotConfigured(t *testing.T) {
|
|
srv := &Server{Store: nil, Log: slog.New(slog.DiscardHandler)}
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings", nil)
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusServiceUnavailable)
|
|
}
|
|
}
|
|
|
|
// TestHandleMappingMutationsRequireCSRFHeader mirrors
|
|
// TestHandleBindingMutationsRequireCSRFHeader: every mapping write handler
|
|
// must enforce the same browser mutation contract as handleTaskAction
|
|
// (docs/prds/dynamic-curators.md), not just bindings.
|
|
func TestHandleMappingMutationsRequireCSRFHeader(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
newMapping := map[string]any{
|
|
"name": "sentry issue opened",
|
|
"source_hint": "sentry",
|
|
"event_type_id": "et-1",
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
},
|
|
}
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", newMapping)
|
|
var created mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("unmarshal created mapping: %v; body = %s", err, w.Body.String())
|
|
}
|
|
id := created.ID
|
|
|
|
tests := []struct {
|
|
name, method, path string
|
|
body any
|
|
}{
|
|
{"create", http.MethodPost, "/api/mappings", newMapping},
|
|
{"update", http.MethodPatch, "/api/mappings/" + id, newMapping},
|
|
{"delete", http.MethodDelete, "/api/mappings/" + id, nil},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
w := doJSONNoCSRF(t, srv, tc.method, tc.path, tc.body)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d without a CSRF header; body = %s", w.Code, http.StatusForbidden, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|