Локальный audit_log (миграции pg/sqlite), GET /v1/audit, запись на CRUD и async push в auth-portal (source_app=bgp). Co-authored-by: Cursor <[email protected]>
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package audit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"evobgp/internal/store"
|
|
)
|
|
|
|
func TestPortalPusherPushEvent(t *testing.T) {
|
|
var got struct {
|
|
Events []map[string]any `json:"events"`
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != ingestPath {
|
|
t.Fatalf("path=%s", r.URL.Path)
|
|
}
|
|
if r.Header.Get("Authorization") != "Bearer test-secret" {
|
|
t.Fatalf("auth=%q", r.Header.Get("Authorization"))
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&got)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]int{"accepted": 1, "duplicates": 0})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
marked := false
|
|
p := &PortalPusher{
|
|
BaseURL: srv.URL,
|
|
Secret: "test-secret",
|
|
MarkPushed: func(id string) error {
|
|
marked = id == "local-id"
|
|
return nil
|
|
},
|
|
}
|
|
entry := &store.AuditEntry{
|
|
ID: "local-id",
|
|
EventID: "bgp-test-event",
|
|
Action: "bgp.module.create",
|
|
Severity: store.AuditSeverityInfo,
|
|
Summary: "Created module",
|
|
SourceApp: store.AuditSourceAppBGP,
|
|
CreatedAt: time.Now().UTC(),
|
|
TargetType: store.AuditTargetAppResource,
|
|
TargetID: "mod-1",
|
|
}
|
|
if err := p.PushEvent(context.Background(), entry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Events) != 1 {
|
|
t.Fatalf("events=%d", len(got.Events))
|
|
}
|
|
if got.Events[0]["source_app"] != "bgp" {
|
|
t.Fatalf("source_app=%v", got.Events[0]["source_app"])
|
|
}
|
|
if !marked {
|
|
t.Fatal("expected mark pushed")
|
|
}
|
|
}
|