Skip to content

Commit 8f3b5a4

Browse files
authored
Merge pull request #166 from python-astrodynamics/feature/session-state
Fix closing after a failed logout and changing base_url
2 parents 961027f + 50d3098 commit 8f3b5a4

5 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎newsfragments/166.fixed.2.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Setting ``base_url`` now resets the authentication state and the cached predicates, which are specific to the previous host.

‎newsfragments/166.fixed.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`SpaceTrackClient.close() <spacetrack.base.SpaceTrackClient.close>` and :meth:`AsyncSpaceTrackClient.close() <spacetrack.aio.AsyncSpaceTrackClient.close>` now close the underlying HTTPX client even if logging out fails.

‎src/spacetrack/aio.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
302302
async def close(self):
303303
"""Log out of Space-Track (if necessary) and close any open connections."""
304304
self._finalizer.detach()
305-
await self.logout()
306-
await self.client.aclose()
305+
try:
306+
await self.logout()
307+
finally:
308+
await self.client.aclose()
307309

308310

309311
async def _iter_lines_generator(response):

‎src/spacetrack/base.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ def base_url(self):
400400
@base_url.setter
401401
def base_url(self, url):
402402
self.client.base_url = url
403+
# The session cookie and predicate metadata are host-specific.
404+
self._authenticated = False
405+
self._predicates = dict()
403406

404407
def _handle_event(self, event):
405408
if isinstance(event, NormalRequest):
@@ -1089,8 +1092,10 @@ def _cleanup(cls, warn_message):
10891092
def close(self):
10901093
"""Log out of Space-Track (if necessary) and close any open connections."""
10911094
self._finalizer.detach()
1092-
self.logout()
1093-
self.client.close()
1095+
try:
1096+
self.logout()
1097+
finally:
1098+
self.client.close()
10941099

10951100
def __repr__(self):
10961101
r = ReprHelper(self)

‎tests/test_spacetrack.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,22 @@ def test_base_url(httpx2_mock):
504504
assert len(httpx2_mock.get_requests(method="POST", url=login_url)) == 1
505505

506506

507+
def test_base_url_change_resets_session_state(client, httpx2_mock, mock_gp_predicates):
508+
httpx2_mock.add_response(method="POST", url=api_url("ajaxauth/login"), json="")
509+
httpx2_mock.add_response(
510+
method="GET", url=api_url("basicspacedata/query/class/gp"), json={"a": 1}
511+
)
512+
513+
assert client.gp() == {"a": 1}
514+
assert client._authenticated
515+
assert client._predicates
516+
517+
client.base_url = "https://testing.space-track.org"
518+
519+
assert not client._authenticated
520+
assert not client._predicates
521+
522+
507523
def test_raise_for_status(httpx2_mock):
508524
httpx2_mock.add_response(
509525
method="GET",
@@ -781,6 +797,24 @@ def test_unknown_event(client):
781797
client._handle_event(object())
782798

783799

800+
def test_close_when_logout_fails(httpx2_mock):
801+
httpx2_mock.add_response(method="POST", url=api_url("ajaxauth/login"), json="")
802+
httpx2_mock.add_response(
803+
method="GET",
804+
url=api_url("ajaxauth/logout"),
805+
status_code=500,
806+
json={"error": "oops"},
807+
)
808+
809+
client = SpaceTrackClient("identity", "password")
810+
client.authenticate()
811+
812+
with pytest.raises(httpx2.HTTPStatusError):
813+
client.close()
814+
815+
assert client.client.is_closed
816+
817+
784818
def test_implicit_cleanup_warning():
785819
with pytest.warns(ResourceWarning, match="without being closed explicitly"):
786820
SpaceTrackClient("identity", "password")

0 commit comments

Comments
 (0)