Repository navigation
Expand file tree
/
Copy pathtest_audit_middleware.py
More file actions
132 lines (101 loc) · 4.2 KB
/
Copy pathtest_audit_middleware.py
File metadata and controls
132 lines (101 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import unittest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from feast.audit.audit_logger import (
AuditEvent,
AuditLogger,
AuditSink,
)
from feast.audit.audit_middleware import AuditLoggingMiddleware
class InMemorySink(AuditSink):
def __init__(self):
self.events: list[AuditEvent] = []
def emit(self, event: AuditEvent) -> None:
self.events.append(event)
def _make_app(audit_logger=None):
"""Create a minimal FastAPI app with audit middleware for testing."""
app = FastAPI()
app.state.audit_logger = audit_logger
app.add_middleware(AuditLoggingMiddleware)
@app.post("/get-online-features")
async def get_online_features():
return {"result": "ok"}
@app.post("/push")
async def push():
return {"result": "ok"}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/mcp")
async def mcp():
return {"jsonrpc": "2.0", "result": "ok", "id": 1}
@app.post("/error-endpoint")
async def error_endpoint():
raise ValueError("test error")
return app
class TestAuditLoggingMiddleware(unittest.TestCase):
def test_logs_http_request(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
resp = client.post("/get-online-features")
self.assertEqual(resp.status_code, 200)
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(len(http_events), 1)
event = http_events[0]
self.assertEqual(event.outcome, "success")
self.assertIn("/get-online-features", event.detail)
self.assertIsNotNone(event.duration_ms)
self.assertGreaterEqual(event.duration_ms, 0)
def test_skips_health_endpoint(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
client.get("/health")
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(len(http_events), 0)
def test_skips_mcp_endpoint(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
client.post("/mcp", json={"jsonrpc": "2.0", "method": "tools/list"})
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(len(http_events), 0)
def test_logs_failure_on_error(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
resp = client.post("/error-endpoint")
self.assertEqual(resp.status_code, 500)
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(len(http_events), 1)
self.assertEqual(http_events[0].outcome, "error")
def test_no_logging_when_audit_logger_is_none(self):
app = _make_app(audit_logger=None)
client = TestClient(app, raise_server_exceptions=False)
resp = client.post("/get-online-features")
self.assertEqual(resp.status_code, 200)
def test_uses_x_request_id_header(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
client.post(
"/get-online-features",
headers={"x-request-id": "custom-id-123"},
)
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(http_events[0].request_id, "custom-id-123")
def test_resource_mapping(self):
sink = InMemorySink()
audit = AuditLogger(sink)
app = _make_app(audit)
client = TestClient(app, raise_server_exceptions=False)
client.post("/push")
http_events = [e for e in sink.events if e.event_type == "http.request"]
self.assertEqual(http_events[0].resource.type, "push_source")
self.assertIn("WRITE_ONLINE", http_events[0].resource.actions)