33
44import aiohttp
55import pytest
6- from aiohttp import ClientError
6+ from aiohttp import BasicAuth , ClientError
7+ from aiohttp .web_request import BaseRequest
78from aresponses import Response , ResponsesMockServer
89
910from python_opensky import (
1516 PositionSource ,
1617 StatesResponse ,
1718)
19+ from python_opensky .exceptions import OpenSkyUnauthenticatedError
1820
1921from . import load_fixture
2022
21- OPENSKY_URL = "python_opensky -network.org"
23+ OPENSKY_URL = "opensky -network.org"
2224
2325
2426async def test_states (
@@ -37,7 +39,7 @@ async def test_states(
3739 )
3840 async with aiohttp .ClientSession () as session :
3941 opensky = OpenSky (session = session )
40- response : StatesResponse = await opensky .states ()
42+ response : StatesResponse = await opensky .get_states ()
4143 assert len (response .states ) == 4
4244 assert response .time == 1683488744
4345 first_aircraft = response .states [0 ]
@@ -62,6 +64,29 @@ async def test_states(
6264 await opensky .close ()
6365
6466
67+ async def test_own_states (
68+ aresponses : ResponsesMockServer ,
69+ ) -> None :
70+ """Test retrieving own states."""
71+ aresponses .add (
72+ OPENSKY_URL ,
73+ "/api/states/own" ,
74+ "GET" ,
75+ aresponses .Response (
76+ status = 200 ,
77+ headers = {"Content-Type" : "application/json" },
78+ text = load_fixture ("states.json" ),
79+ ),
80+ )
81+ async with aiohttp .ClientSession () as session :
82+ opensky = OpenSky (session = session )
83+ opensky .authenticate (BasicAuth (login = "test" , password = "test" ))
84+ response : StatesResponse = await opensky .get_own_states ()
85+ assert len (response .states ) == 4
86+ assert opensky .remaining_credits () == opensky .opensky_credits
87+ await opensky .close ()
88+
89+
6590async def test_states_with_bounding_box (
6691 aresponses : ResponsesMockServer ,
6792) -> None :
@@ -85,7 +110,7 @@ async def test_states_with_bounding_box(
85110 min_longitude = 0 ,
86111 max_longitude = 0 ,
87112 )
88- await opensky .states (bounding_box = bounding_box )
113+ await opensky .get_states (bounding_box = bounding_box )
89114 await opensky .close ()
90115
91116
@@ -105,8 +130,8 @@ async def test_credit_usage(
105130 )
106131 async with aiohttp .ClientSession () as session :
107132 opensky = OpenSky (session = session )
108- await opensky .states ()
109- assert opensky .remaining_credits () == 396
133+ await opensky .get_states ()
134+ assert opensky .remaining_credits () == opensky . opensky_credits - 4
110135 await opensky .close ()
111136
112137
@@ -126,15 +151,15 @@ async def test_new_session(
126151 )
127152 async with OpenSky () as opensky :
128153 assert not opensky .session
129- await opensky .states ()
154+ await opensky .get_states ()
130155 assert opensky .session
131156
132157
133158async def test_timeout (aresponses : ResponsesMockServer ) -> None :
134159 """Test request timeout."""
135160
136161 # Faking a timeout by sleeping
137- async def response_handler (_ : aiohttp . ClientResponse ) -> Response :
162+ async def response_handler (_ : BaseRequest ) -> Response :
138163 """Response handler for this test."""
139164 await asyncio .sleep (2 )
140165 return aresponses .Response (body = "Goodmorning!" )
@@ -149,14 +174,57 @@ async def response_handler(_: aiohttp.ClientResponse) -> Response:
149174 async with aiohttp .ClientSession () as session :
150175 opensky = OpenSky (session = session , request_timeout = 1 )
151176 with pytest .raises (OpenSkyConnectionError ):
152- assert await opensky .states ()
177+ assert await opensky .get_states ()
178+ await opensky .close ()
179+
180+
181+ async def test_auth (aresponses : ResponsesMockServer ) -> None :
182+ """Test request authentication."""
183+
184+ def response_handler (request : BaseRequest ) -> Response :
185+ """Response handler for this test."""
186+ assert request .headers
187+ assert request .headers ["Authorization" ]
188+ assert request .headers ["Authorization" ] == "Basic dGVzdDp0ZXN0"
189+ return aresponses .Response (
190+ status = 200 ,
191+ headers = {"Content-Type" : "application/json" },
192+ text = load_fixture ("states.json" ),
193+ )
194+
195+ aresponses .add (
196+ OPENSKY_URL ,
197+ "/api/states/all" ,
198+ "GET" ,
199+ response_handler ,
200+ )
201+
202+ async with aiohttp .ClientSession () as session :
203+ opensky = OpenSky (session = session )
204+ opensky .authenticate (BasicAuth (login = "test" , password = "test" ))
205+ await opensky .get_states ()
206+ await opensky .close ()
207+
208+
209+ async def test_user_credits () -> None :
210+ """Test authenticated user credits."""
211+ async with aiohttp .ClientSession () as session :
212+ opensky = OpenSky (session = session )
213+ assert opensky .opensky_credits == 400
214+ opensky .authenticate (BasicAuth (login = "test" , password = "test" ))
215+ assert opensky .opensky_credits == 4000
216+ opensky .authenticate (
217+ BasicAuth (login = "test" , password = "test" ),
218+ contributing_user = True ,
219+ )
220+ assert opensky .opensky_credits == 8000
153221 await opensky .close ()
154222
155223
156224async def test_request_error (aresponses : ResponsesMockServer ) -> None :
157225 """Test request error."""
158226
159- async def response_handler (_ : aiohttp . ClientResponse ) -> Response :
227+ async def response_handler (_ : BaseRequest ) -> Response :
160228 """Response handler for this test."""
161229 raise ClientError
162230
@@ -170,7 +238,7 @@ async def response_handler(_: aiohttp.ClientResponse) -> Response:
170238 async with aiohttp .ClientSession () as session :
171239 opensky = OpenSky (session = session )
172240 with pytest .raises (OpenSkyConnectionError ):
173- assert await opensky .states ()
241+ assert await opensky .get_states ()
174242 await opensky .close ()
175243
176244
@@ -192,7 +260,16 @@ async def test_unexpected_server_response(
192260 async with aiohttp .ClientSession () as session :
193261 opensky = OpenSky (session = session )
194262 with pytest .raises (OpenSkyError ):
195- assert await opensky .states ()
263+ assert await opensky .get_states ()
264+ await opensky .close ()
265+
266+
267+ async def test_unauthenticated_own_states () -> None :
268+ """Test unauthenticated access to own states."""
269+ async with aiohttp .ClientSession () as session :
270+ opensky = OpenSky (session = session )
271+ with pytest .raises (OpenSkyUnauthenticatedError ):
272+ assert await opensky .get_own_states ()
196273 await opensky .close ()
197274
198275
0 commit comments