241e3b5f2d
- Introduced a new `auditlog` package to handle audit logging for user actions, including creation, updates, deletions, and authentication events. - Enhanced existing services (customer, user) to log relevant actions using the new audit logger, capturing details such as actor ID, action type, target type, and success status. - Added middleware to enrich request context with metadata for audit logging, ensuring comprehensive tracking of user interactions. - Integrated Elasticsearch for persistent storage of audit logs, with fallback to file-only logging if Elasticsearch is unavailable. - Updated API documentation to include new audit log endpoints for administrative access. This update significantly improves the system's ability to track and audit user actions, enhancing security and accountability within the application.
36 lines
809 B
Go
36 lines
809 B
Go
package audit
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestEnrichEntryFromContext(t *testing.T) {
|
|
ctx := WithRequestMeta(context.Background(), RequestMeta{
|
|
IP: "127.0.0.1",
|
|
RequestID: "req-1",
|
|
UserAgent: "test-agent",
|
|
})
|
|
ctx = WithActor(ctx, Actor{ID: "actor-1", Type: ActorTypeAdmin})
|
|
|
|
entry := enrichEntry(ctx, Entry{
|
|
Action: ActionAuthLoginSuccess,
|
|
TargetType: TargetTypeAdmin,
|
|
TargetID: "actor-1",
|
|
Success: true,
|
|
})
|
|
|
|
if entry.ActorID != "actor-1" {
|
|
t.Fatalf("expected actor_id actor-1, got %s", entry.ActorID)
|
|
}
|
|
if entry.IP != "127.0.0.1" {
|
|
t.Fatalf("expected ip 127.0.0.1, got %s", entry.IP)
|
|
}
|
|
if entry.RequestID != "req-1" {
|
|
t.Fatalf("expected request_id req-1, got %s", entry.RequestID)
|
|
}
|
|
if entry.At == 0 {
|
|
t.Fatal("expected timestamp to be set")
|
|
}
|
|
}
|