Skip to content

Commit c6f7434

Browse files
authored
Adapt connect hydration tests to session-aware fixtures
1 parent f9ab15a commit c6f7434

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎tests/units/test_app.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,12 +4906,13 @@ async def event_namespace_with_processor_mock(
49064906
app._session_token_manager = SessionTokenManager("test", secrets=(b"s" * 32,))
49074907
event_namespace = EventNamespace("/event", app)
49084908
event_namespace.emit = AsyncMock()
4909+
token_manager = event_namespace._token_manager
49094910
yield event_namespace
49104911
# The token manager is backed by redis when one is configured; drop the
49114912
# tokens these tests link so they do not show up in another test's
49124913
# enumeration of the shared instance. Awaited rather than run in a fresh
49134914
# loop via asyncio.run: the redis client is bound to the test's loop.
4914-
await event_namespace._token_manager.disconnect_all()
4915+
await token_manager.disconnect_all()
49154916

49164917

49174918
def _connect_environ(token: str) -> dict[str, Any]:
@@ -5335,7 +5336,7 @@ def fake_compile_app(*_args: Any, **_kwargs: Any) -> bool:
53355336

53365337
@pytest.mark.asyncio
53375338
async def test_on_connect_processes_boot_event_from_auth(
5338-
event_namespace: EventNamespace,
5339+
event_namespace_with_processor_mock: EventNamespace,
53395340
):
53405341
"""The hydrate event carried in the socket.io CONNECT packet is processed on connect.
53415342
@@ -5344,8 +5345,9 @@ async def test_on_connect_processes_boot_event_from_auth(
53445345
first; a connect without a boot event still does.
53455346
53465347
Args:
5347-
event_namespace: The event namespace.
5348+
event_namespace_with_processor_mock: The event namespace with session support.
53485349
"""
5350+
event_namespace = event_namespace_with_processor_mock
53495351
event_namespace._token_manager = Mock()
53505352
event_namespace._token_manager.link_token_to_sid = AsyncMock(return_value=None)
53515353
event_namespace.on_event = AsyncMock()
@@ -5359,7 +5361,7 @@ async def test_on_connect_processes_boot_event_from_auth(
53595361
"new_sid", {"QUERY_STRING": "token=abc"}, {"event": boot_event}
53605362
)
53615363
event_namespace._token_manager.link_token_to_sid.assert_awaited_once_with(
5362-
"abc", "new_sid"
5364+
"abc", "new_sid", token_factory=unittest.mock.ANY
53635365
)
53645366
event_namespace.on_event.assert_awaited_once_with("new_sid", boot_event)
53655367
modify_state.assert_not_called()
@@ -5377,13 +5379,14 @@ async def test_on_connect_processes_boot_event_from_auth(
53775379

53785380
@pytest.mark.asyncio
53795381
async def test_on_connect_unlinks_token_when_boot_event_fails(
5380-
event_namespace: EventNamespace,
5382+
event_namespace_with_processor_mock: EventNamespace,
53815383
):
53825384
"""A boot event that fails to process drops the sid/token link before refusing the connect.
53835385
53845386
Args:
5385-
event_namespace: The event namespace.
5387+
event_namespace_with_processor_mock: The event namespace with session support.
53865388
"""
5389+
event_namespace = event_namespace_with_processor_mock
53875390
event_namespace._token_manager = Mock()
53885391
event_namespace._token_manager.link_token_to_sid = AsyncMock(return_value=None)
53895392
event_namespace._token_manager.disconnect_token = AsyncMock()
@@ -5399,6 +5402,8 @@ async def test_on_connect_unlinks_token_when_boot_event_fails(
53995402
)
54005403
# The connection-scoped router data cached for the refused sid goes too.
54015404
assert "new_sid" not in event_namespace._static_router_data
5405+
assert "new_sid" not in event_namespace._sessions
5406+
assert "new_sid" not in event_namespace._session_refresh_at
54025407

54035408

54045409
def test_call_app_wraps_with_otel_asgi_middleware():

0 commit comments

Comments
 (0)