diff --git a/console/tests/test_novncd.py b/console/tests/test_novncd.py index 778daf89..38c52818 100644 --- a/console/tests/test_novncd.py +++ b/console/tests/test_novncd.py @@ -262,6 +262,21 @@ class NovncdDaemonLogicTestCase(TestCase): self.assertIsNone(tokens.session_user_id(f"{settings.SESSION_COOKIE_NAME}={SessionStore().session_key}")) self.assertIsNone(tokens.session_user_id(None)) + def test_a_session_that_a_password_change_or_a_deactivation_ended_names_no_one(self): + user = get_user_model().objects.create_user("vnc-old-session", password="pw") + for change in ({"password": "new"}, {"is_active": False}): + with self.subTest(change=change): + self.client.force_login(user) + cookie = f"{settings.SESSION_COOKIE_NAME}={self.client.session.session_key}" + if "password" in change: + user.set_password(change["password"]) + else: + user.is_active = False + user.save() + self.assertIsNone(tokens.session_user_id(cookie)) + user.is_active = True + user.save() + def test_a_vnc_session_ends_when_its_user_may_no_longer_open_it(self): tsock, ended = MagicMock(), MagicMock() ended.wait.return_value = False # ACCESS_CHECK passed, the session still open diff --git a/console/tests/test_socketiod.py b/console/tests/test_socketiod.py index 47e5c4e9..791919b9 100644 --- a/console/tests/test_socketiod.py +++ b/console/tests/test_socketiod.py @@ -11,8 +11,7 @@ from unittest.mock import patch from django.conf import settings from django.contrib.auth import get_user_model -from django.contrib.sessions.backends.db import SessionStore -from django.test import TestCase, override_settings +from django.test import Client, TestCase, override_settings from accounts.models import UserInstance from computes.models import Compute @@ -38,10 +37,9 @@ class Base(TestCase): def signed_in(self, user=None): """The environ of a request with a session of user (the owner).""" - session = SessionStore() - session["_auth_user_id"] = str((user or self.user).pk) - session.create() - return {"HTTP_COOKIE": f"{settings.SESSION_COOKIE_NAME}={session.session_key}"} + client = Client() + client.force_login(user or self.user) + return {"HTTP_COOKIE": f"{settings.SESSION_COOKIE_NAME}={client.session.session_key}"} def token(self, user=None, vm=None): vm = vm or self.vm diff --git a/console/tokens.py b/console/tokens.py index 4b396fc5..5066cb83 100644 --- a/console/tokens.py +++ b/console/tokens.py @@ -6,11 +6,11 @@ also wants the session cookie of the token's user (session_user_id). socketiod's token travels in the Socket.IO handshake, not the URL.""" from importlib import import_module +from types import SimpleNamespace from django import db from django.conf import settings -from django.contrib.auth import SESSION_KEY -from django.contrib.auth import get_user_model +from django.contrib.auth import get_user, get_user_model from django.core.signing import BadSignature, SignatureExpired, TimestampSigner from django.http import parse_cookie @@ -55,8 +55,10 @@ def may_still_open(user_id, instance): def session_user_id(cookie_header): """The id (a string) of the user signed in with the session cookie of a - request's Cookie header, or None.""" + request's Cookie header, or None. Django's own check: a session that a + password change or a deactivation ended is no one's.""" session_key = parse_cookie(cookie_header or "").get(settings.SESSION_COOKIE_NAME) if not session_key: return None - return import_module(settings.SESSION_ENGINE).SessionStore(session_key).get(SESSION_KEY) + user = get_user(SimpleNamespace(session=import_module(settings.SESSION_ENGINE).SessionStore(session_key))) + return str(user.pk) if user.is_authenticated else None