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

59 lines
3.0 KiB
Python

"""Toyota credentials require the existing authenticated, origin-checked Galaxy session."""
import http.client
import json
import threading
from opendbc.car.toyota.interface import CarInterface
from opendbc.car.toyota.values import CAR
from openpilot.starpilot.galaxy.access import GalaxyAccessOwner
from openpilot.starpilot.galaxy.server import make_server
from openpilot.starpilot.galaxy.tests.test_toyota_tools import SavedParams
from openpilot.starpilot.galaxy.toyota_tools import ToyotaTools
from types import SimpleNamespace
def test_http_session_origin_and_secret_free_readback(tmp_path):
params = SavedParams(tmp_path)
cp = CarInterface.get_non_essential_params(CAR.TOYOTA_RAV4_PRIME)
cp_raw = cp.to_bytes()
context = SimpleNamespace(sample=lambda: SimpleNamespace(cp=cp, cp_raw=cp_raw, parked=True), close=lambda: None)
tools = ToyotaTools(params, context)
access = GalaxyAccessOwner(tmp_path / 'access')
assert access.configure('password123', lambda: True)
server = make_server(port=0, owner=access, parked=lambda: True, toyota_tools=tools)
worker = threading.Thread(target=server.serve_forever, kwargs={'poll_interval': .01}, daemon=True)
worker.start()
def request(path, payload=None, cookie='', origin=True):
connection = http.client.HTTPConnection('127.0.0.1', server.server_port, timeout=3)
headers = {'Cookie': cookie}
if payload is not None:
headers['Content-Type'] = 'application/json'
headers['Origin'] = f'http://127.0.0.1:{server.server_port}' if origin else 'https://external.invalid'
try:
connection.request('POST' if payload is not None else 'GET', path, json.dumps(payload) if payload is not None else None, headers)
response = connection.getresponse()
return response.status, json.loads(response.read()), dict(response.getheaders())
finally:
connection.close()
try:
assert request('/api/toyota-tools')[0] == 401
code, _, headers = request('/api/auth/login', {'password': 'password123'})
assert code == 200
cookie = headers['Set-Cookie'].split(';', 1)[0]
code, page, _ = request('/api/toyota-tools', cookie=cookie)
assert code == 200 and page['visible'] and page['security']
payload = {'view': page['view'], 'action': 'save_key', 'confirmed': True, 'name': 'My Toyota', 'value': 'ab' * 16}
assert request('/api/toyota-tools/action', payload)[0] == 401
assert request('/api/toyota-tools/action', payload, cookie, origin=False)[0] == 403
assert request('/api/toyota-tools/action', payload, cookie)[0] == 200
code, page, _ = request('/api/toyota-tools', cookie=cookie)
assert code == 200 and page['keys'] == [{'name': 'My Toyota', 'active': False}]
assert 'ab' * 16 not in json.dumps(page)
request('/api/auth/logout', {}, cookie)
assert request('/api/toyota-tools/action', {'view': page['view'], 'action': 'use_key', 'confirmed': True, 'name': 'My Toyota'}, cookie)[0] == 401
finally:
server.shutdown()
worker.join(timeout=2)
server.server_close()