From d145816df75cb6c2a11d6f28956f11d679d78abd Mon Sep 17 00:00:00 2001 From: Richard <42204099+rikroe@users.noreply.github.com> Date: Sat, 3 Aug 2024 21:39:58 +0200 Subject: [PATCH] Update GPS position verification --- bimmer_connected/models.py | 4 ++++ .../eadrax_service_eventposition.json | 2 +- bimmer_connected/tests/test_remote_services.py | 4 ++-- bimmer_connected/tests/test_vehicle.py | 12 ++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bimmer_connected/models.py b/bimmer_connected/models.py index 4a6adaf6..f9506b2e 100644 --- a/bimmer_connected/models.py +++ b/bimmer_connected/models.py @@ -71,6 +71,10 @@ def __post_init__(self): value = getattr(self, field_name) if value is not None and not isinstance(value, (float, int)): raise TypeError(f"'{field_name}' not of type '{Optional[Union[float, int]]}'") + if field_name == "latitude" and not (-90 <= value <= 90): + raise ValueError(f"'latitude' must be between -90 and 90, but got '{value}'") + elif field_name == "longitude" and not (-180 <= value <= 180): + raise ValueError(f"'longitude' must be between -180 and 180, but got '{value}'") def __iter__(self): yield from self.__dict__.values() diff --git a/bimmer_connected/tests/responses/remote_services/eadrax_service_eventposition.json b/bimmer_connected/tests/responses/remote_services/eadrax_service_eventposition.json index c737d608..3d58948f 100644 --- a/bimmer_connected/tests/responses/remote_services/eadrax_service_eventposition.json +++ b/bimmer_connected/tests/responses/remote_services/eadrax_service_eventposition.json @@ -2,7 +2,7 @@ "positionData": { "status": "OK", "position": { - "latitude": 123.456, + "latitude": 12.345, "longitude": 34.5678, "formattedAddress": "some_formatted_address", "heading": 121 diff --git a/bimmer_connected/tests/test_remote_services.py b/bimmer_connected/tests/test_remote_services.py index 494931b8..dd86fbcc 100644 --- a/bimmer_connected/tests/test_remote_services.py +++ b/bimmer_connected/tests/test_remote_services.py @@ -314,12 +314,12 @@ async def test_get_remote_position(bmw_fixture: respx.Router): # Check updated position await vehicle.remote_services.trigger_remote_vehicle_finder() - assert location.location == (123.456, 34.5678) + assert location.location == (12.345, 34.5678) assert location.heading == 121 # Position should still be from vehicle finder after status update await account.get_vehicles() - assert location.location == (123.456, 34.5678) + assert location.location == (12.345, 34.5678) assert location.heading == 121 diff --git a/bimmer_connected/tests/test_vehicle.py b/bimmer_connected/tests/test_vehicle.py index c1f3d53e..9897a94c 100644 --- a/bimmer_connected/tests/test_vehicle.py +++ b/bimmer_connected/tests/test_vehicle.py @@ -284,6 +284,18 @@ def test_gpsposition(): assert pos != "(1, 2)" assert pos[0] == 1 + with pytest.raises(TypeError, match="Either none or all arguments must be 'None'."): + GPSPosition(1, None) + + with pytest.raises(TypeError, match="'longitude' not of type"): + GPSPosition(0, "49.7") + + with pytest.raises(ValueError, match="'latitude' must be between -90 and 90"): + GPSPosition(91, 0) + + with pytest.raises(ValueError, match="'longitude' must be between -180 and 180"): + GPSPosition(90, 181) + @pytest.mark.asyncio async def test_headunit_data(caplog, bmw_fixture: respx.Router):