Files
StarPilot/openpilot/starpilot/galaxy/tests/test_sentry_events_http.py
T
firestar5683 80fbb4f6db Feature registration
Register vehicle controls, planner modes, UI actions and Galaxy services after their providers exist. Wire the shared runtime entrypoints and cover their combined behavior.

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Daniel Koepping <elkoled@gmail.com>
Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: DimitriChrysafis <Dimitri.chrysafis@gmail.com>
Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com>
Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com>
Co-authored-by: Kacper Rączy <gfw.kra@gmail.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
Co-authored-by: stef <19478336+stefpi@users.noreply.github.com>
Co-authored-by: whoisdomi <dcantu83@gmail.com>

Co-authored-by: Intelli <contact@intelli.software>
Co-authored-by: haraschax <6804392+haraschax@users.noreply.github.com>
Co-authored-by: prabhaavp <prabhaav@msu.edu>
2026-10-14 20:02:39 -05:00

93 lines
3.5 KiB
Python

"""Motion metadata requires a live local session before and after storage I/O."""
import http.client
import json
from pathlib import Path
import tempfile
import threading
import unittest
from openpilot.starpilot.galaxy.access import GalaxyAccessOwner
from openpilot.starpilot.galaxy.sentry_events import SentryEvents
from openpilot.starpilot.galaxy.server import make_server
from openpilot.starpilot.sentry_mode.storage import EventStore
class SentryEventsHTTPTest(unittest.TestCase):
def test_auth_before_read_and_recheck_after_logout(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
store = EventStore(root / "events")
receipt = store.record("alarm", 123, permitted=lambda: True)
access = GalaxyAccessOwner(root / "access")
calls = []
entered, release = threading.Event(), threading.Event()
release.set()
class PausedEvents:
def snapshot(self):
calls.append(True)
result = SentryEvents(store).snapshot()
entered.set()
release.wait(2)
return result
server = make_server(port=0, owner=access, sentry_events=PausedEvents())
worker = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": .01}, daemon=True)
worker.start()
def request(path, *, cookie=None, payload=None):
# Exercise the password-backed forwarded path.
headers = {"Forwarded": "for=203.0.113.8", **({"Cookie": cookie} if cookie else {})}
if payload is not None:
headers.update({"Content-Type": "application/json", "Origin": f"http://127.0.0.1:{server.server_port}"})
connection = http.client.HTTPConnection("127.0.0.1", server.server_port, timeout=3)
try:
connection.request("GET" if payload is None else "POST", path,
None if payload is None else json.dumps(payload), headers)
response = connection.getresponse()
return response.status, response.read(), dict(response.getheaders())
finally:
connection.close()
try:
route = "/api/sentry/events"
self.assertEqual(request(route)[0], 503)
access.configure("password123", lambda: True)
self.assertEqual(request(route)[0], 401)
self.assertEqual(calls, [])
status, _, headers = request("/api/auth/login", payload={"password": "password123"})
self.assertEqual(status, 200)
cookie = headers["Set-Cookie"].split(";", 1)[0]
status, body, headers = request(route, cookie=cookie)
self.assertEqual(status, 200)
self.assertEqual(headers["Cache-Control"], "no-store")
result = json.loads(body)
self.assertEqual(result["events"][0]["eventId"], receipt.event_id)
self.assertNotIn(b"sessionId", body)
self.assertNotIn(b"monoTimeNs", body)
self.assertNotIn(str(root).encode(), body)
release.clear()
entered.clear()
responses = []
pending = threading.Thread(target=lambda: responses.append(request(route, cookie=cookie)))
pending.start()
try:
self.assertTrue(entered.wait(1))
self.assertEqual(request("/api/auth/logout", cookie=cookie, payload={})[0], 200)
finally:
release.set()
pending.join(2)
self.assertEqual(responses[0][0], 401)
self.assertNotIn(receipt.event_id.encode(), responses[0][1])
finally:
release.set()
server.shutdown()
worker.join(2)
server.server_close()
if __name__ == "__main__":
unittest.main()