-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flight_service.py
More file actions
220 lines (181 loc) · 7.15 KB
/
test_flight_service.py
File metadata and controls
220 lines (181 loc) · 7.15 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import pytest
from unittest.mock import MagicMock, patch
from flight_service import app, calculate_bounds, API_KEY
@pytest.fixture(autouse=True)
def disable_api_key(monkeypatch):
"""
Temporarily override API_KEY to None for all tests.
"""
monkeypatch.setattr("flight_service.API_KEY", None)
@pytest.fixture
def client():
app.config["TESTING"] = True
with app.test_client() as client:
yield client
def test_calculate_bounds_basic():
lat, lon, radius = 0, 0, 111
bounds = calculate_bounds(lat, lon, radius)
north, south, west, east = map(float, bounds.split(","))
assert north > south
assert east > west
assert round(north - south, 1) == 2.0
def test_health_check(client):
response = client.get("/health")
assert response.status_code == 200
assert response.json == {"status": "ok"}
def test_index(client):
response = client.get("/")
assert response.status_code == 200
data = response.get_json()
assert "service" in data
assert "/closest-flight" in data["endpoints"]
def test_api_key_valid(client, monkeypatch):
monkeypatch.setattr("flight_service.API_KEY", "secret")
response = client.get(
"/closest-flight?lat=10&lon=20",
headers={"X-API-Key": "secret"}
)
assert response.status_code in (200, 400, 500)
@pytest.mark.parametrize("query", [
"lon=10",
"lat=91&lon=0",
"lat=0&lon=181",
"lat=0&lon=0&radius=0",
"lat=0&lon=0&radius=9999"
])
def test_invalid_parameters(client, query):
response = client.get(f"/closest-flight?{query}")
assert response.status_code == 400
data = response.get_json()
assert "error" in data
class DummyFlight:
def __init__(self, flight_id="ABC123", lat=10.1, lon=20.1):
self.id = flight_id
self.number = "XY123"
self.callsign = "CALL123"
self.icao_24bit = "abcd12"
self.latitude = lat
self.longitude = lon
self.altitude = 10000
self.heading = 250
self.ground_speed = 750
self.vertical_speed = 0
self.aircraft_code = "A320"
self.registration = "REG123"
self.airline_icao = "ICAO"
self.airline_iata = "IATA"
self.origin_airport_iata = "LHR"
self.destination_airport_iata = "CDG"
self.on_ground = False
def get_distance_from(self, other):
return 5.0
def set_flight_details(self, details):
self.origin_airport_name = details.get("origin")
self.destination_airport_name = details.get("destination")
@patch("flight_service.fr_api")
def test_closest_flight_found(mock_api, client):
dummy_flight = DummyFlight()
mock_api.get_flights.return_value = [dummy_flight]
mock_api.get_flight_details.return_value = {"origin": "London", "destination": "Paris"}
response = client.get("/closest-flight?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is True
assert data["flight"]["route"]["origin_name"] == "London"
assert "distance_km" in data
@patch("flight_service.fr_api")
def test_no_flights_found(mock_api, client):
mock_api.get_flights.return_value = []
response = client.get("/closest-flight?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is False
assert "No flights" in data["message"]
@patch("flight_service.fr_api")
def test_no_airborne_flights(mock_api, client):
grounded = DummyFlight()
grounded.on_ground = True
mock_api.get_flights.return_value = [grounded]
response = client.get("/closest-flight?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is False
assert "airborne" in data["message"]
@patch("flight_service.fr_api")
def test_internal_error_handling(mock_api, client):
mock_api.get_flights.side_effect = Exception("Simulated failure")
response = client.get("/closest-flight?lat=10&lon=20")
assert response.status_code == 500
assert "Server error" in response.get_json()["error"]
@patch("flight_service.fr_api")
def test_flights_in_radius_found(mock_api, client):
"""Test that the endpoint returns all flights in the radius."""
dummy_flight1 = DummyFlight(flight_id="ABC123", lat=10.1, lon=20.1)
dummy_flight2 = DummyFlight(flight_id="DEF456", lat=10.2, lon=20.2)
mock_api.get_flights.return_value = [dummy_flight1, dummy_flight2]
mock_api.get_flight_details.return_value = {"origin": "London", "destination": "Paris"}
response = client.get("/flights-in-radius?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is True
assert len(data["flights"]) == 2
assert data["flights"][0]["id"] == "ABC123"
assert data["flights"][1]["id"] == "DEF456"
assert data["flights"][0]["route"]["origin_name"] == "London"
@patch("flight_service.fr_api")
def test_flights_in_radius_empty(mock_api, client):
"""Test that the endpoint handles no flights found."""
mock_api.get_flights.return_value = []
response = client.get("/flights-in-radius?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is False
assert "No flights" in data["message"]
@patch("flight_service.fr_api")
def test_flights_in_radius_grounded(mock_api, client):
"""Test that the endpoint filters out grounded flights."""
grounded = DummyFlight()
grounded.on_ground = True
mock_api.get_flights.return_value = [grounded]
response = client.get("/flights-in-radius?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 200
assert data["found"] is False
assert "No airborne flights" in data["message"]
@patch("flight_service.fr_api")
def test_flights_in_radius_error(mock_api, client):
"""Test that the endpoint handles internal errors."""
mock_api.get_flights.side_effect = Exception("Simulated failure")
response = client.get("/flights-in-radius?lat=10&lon=20")
data = response.get_json()
assert response.status_code == 500
assert "Server error" in data["error"]
@patch("flight_service.fr_api")
def test_flights_in_radius_with_api_key(mock_api, client, monkeypatch):
"""Test that the endpoint respects API key authentication."""
monkeypatch.setattr("flight_service.API_KEY", "secret")
dummy_flight = DummyFlight()
mock_api.get_flights.return_value = [dummy_flight]
response = client.get(
"/flights-in-radius?lat=10&lon=20",
headers={"X-API-Key": "secret"}
)
assert response.status_code in (200, 400, 500)
response = client.get(
"/flights-in-radius?lat=10&lon=20",
headers={"X-API-Key": "wrong"}
)
assert response.status_code == 401
assert response.get_json()["error"] == "Unauthorized"
@pytest.mark.parametrize("query", [
"lon=10",
"lat=91&lon=0",
"lat=0&lon=181",
"lat=0&lon=0&radius=0",
"lat=0&lon=0&radius=9999"
])
def test_flights_in_radius_invalid_parameters(client, query):
"""Test that the endpoint validates parameters."""
response = client.get(f"/flights-in-radius?{query}")
assert response.status_code == 400
assert "error" in response.get_json()