Files
romm/backend/tests/utils/test_auth.py
zurdi 01483dd715 fix: disconnect a session's sockets when it is revoked
A socket joins its user's room, and an admin's the admin room, once at
connect time, so logging out or revoking a user's sessions left their open
sockets receiving notifications and the log stream. Each socket is now
recorded against the login session that opened it, and removing a session
disconnects those sockets on whichever worker holds them.

The main server takes one disconnect handler, so it moves next to the
connect handler and runs the activity cleanup as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 14:23:39 +02:00

36 lines
1.1 KiB
Python

from unittest.mock import AsyncMock
from handler.auth.constants import SESSION_COOKIE_NAME
from utils import auth, json_module
from utils.auth import get_session_from_environ
async def test_prefers_the_session_the_middleware_attached():
session = {"sub": "admin", "session_id": "abc"}
assert await get_session_from_environ({"asgi.scope": {"session": session}}) == (
session
)
async def test_reads_the_cookies_session_and_names_it(mocker):
cache = mocker.patch.object(auth, "async_cache")
cache.get = AsyncMock(return_value=json_module.dumps({"sub": "admin"}))
session = await get_session_from_environ(
{"HTTP_COOKIE": f"{SESSION_COOKIE_NAME}=abc"}
)
cache.get.assert_awaited_once_with("session:abc")
assert session == {"sub": "admin", "session_id": "abc"}
async def test_a_cookie_for_a_missing_session_is_no_session(mocker):
cache = mocker.patch.object(auth, "async_cache")
cache.get = AsyncMock(return_value=None)
assert (
await get_session_from_environ({"HTTP_COOKIE": f"{SESSION_COOKIE_NAME}=abc"})
== {}
)