Files
StarPilot/openpilot/starpilot/galaxy/tests/test_camera_snapshot_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

78 lines
3.2 KiB
Python

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.camera_snapshot import SnapshotDenied
from openpilot.starpilot.galaxy.server import make_server
class CameraSnapshotHTTPTest(unittest.TestCase):
def test_origin_session_parked_and_after_capture_revocation(self):
with tempfile.TemporaryDirectory() as directory:
access = GalaxyAccessOwner(Path(directory).resolve() / 'access')
access.configure('password123', lambda: True)
parked = True
calls = []
revoke = False
class Reader:
def capture(self, camera, *, permitted):
nonlocal parked
if not permitted():
raise SnapshotDenied
calls.append(camera)
if revoke == "parked":
parked = False
elif revoke == "logout":
self_status, _, _ = request('/api/auth/logout', cookie=cookie, payload={})
if self_status != 200:
raise AssertionError("logout failed")
return b'\xff\xd8\xff\xd9'
server = make_server(port=0, owner=access, parked=lambda: parked, camera_snapshot=Reader())
worker = threading.Thread(target=server.serve_forever, kwargs={'poll_interval': .01}, daemon=True)
worker.start()
def request(path, *, cookie=None, origin=None, payload=None):
connection = http.client.HTTPConnection('127.0.0.1', server.server_port, timeout=3)
headers = {'Forwarded': 'for=203.0.113.8', 'Content-Type': 'application/json',
'Origin': origin or f'http://127.0.0.1:{server.server_port}'}
if cookie:
headers['Cookie'] = cookie
try:
connection.request('POST', path, json.dumps({'camera': 'cabin'} if payload is None else payload), headers)
response = connection.getresponse()
return response.status, response.read(), dict(response.getheaders())
finally:
connection.close()
try:
route = '/api/cameras/snapshot'
self.assertEqual(request(route)[0], 401)
status, _, headers = request('/api/auth/login', payload={'password': 'password123'})
self.assertEqual(status, 200)
cookie = headers['Set-Cookie'].split(';', 1)[0]
self.assertEqual(request(route, cookie=cookie, origin='https://elsewhere.invalid')[0], 403)
parked = False
self.assertEqual(request(route, cookie=cookie)[0], 409)
self.assertEqual(calls, [])
parked = True
status, body, headers = request(route, cookie=cookie)
self.assertEqual(status, 200)
self.assertEqual(headers['Content-Type'], 'image/jpeg')
self.assertEqual(headers['Cache-Control'], 'no-store')
self.assertEqual(body, b'\xff\xd8\xff\xd9')
revoke = "parked"
status, body, _ = request(route, cookie=cookie)
self.assertEqual(status, 409)
self.assertNotIn(b'\xff\xd8', body)
parked = True
revoke = "logout"
status, body, _ = request(route, cookie=cookie)
self.assertEqual(status, 401)
self.assertNotIn(b'\xff\xd8', body)
finally:
server.shutdown()
server.server_close()
worker.join(2)