From b11ec9a69ef626bd7aa22879145e9d74ea38a814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Mon, 18 May 2020 22:11:46 +0200 Subject: [PATCH] Fixed #32301 -- Made clearsessions raise CommandError when clear_expired() is not implemented. --- .../sessions/management/commands/clearsessions.py | 4 ++-- tests/sessions_tests/no_clear_expired.py | 6 ++++++ tests/sessions_tests/tests.py | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/sessions_tests/no_clear_expired.py diff --git a/django/contrib/sessions/management/commands/clearsessions.py b/django/contrib/sessions/management/commands/clearsessions.py index c9d0cef2f0..2d6224775c 100644 --- a/django/contrib/sessions/management/commands/clearsessions.py +++ b/django/contrib/sessions/management/commands/clearsessions.py @@ -1,7 +1,7 @@ from importlib import import_module from django.conf import settings -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError class Command(BaseCommand): @@ -15,7 +15,7 @@ class Command(BaseCommand): try: engine.SessionStore.clear_expired() except NotImplementedError: - self.stderr.write( + raise CommandError( "Session engine '%s' doesn't support clearing expired " "sessions." % settings.SESSION_ENGINE ) diff --git a/tests/sessions_tests/no_clear_expired.py b/tests/sessions_tests/no_clear_expired.py new file mode 100644 index 0000000000..0bdfcab6da --- /dev/null +++ b/tests/sessions_tests/no_clear_expired.py @@ -0,0 +1,6 @@ +from django.contrib.sessions.backends.base import SessionBase + + +class SessionStore(SessionBase): + """Session store without support for clearing expired sessions.""" + pass diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py index 2832fd8970..73d2a13a9f 100644 --- a/tests/sessions_tests/tests.py +++ b/tests/sessions_tests/tests.py @@ -910,3 +910,14 @@ class CookieSessionTests(SessionTestsMixin, SimpleTestCase): @unittest.skip("CookieSession is stored in the client and there is no way to query it.") def test_session_save_does_not_resurrect_session_logged_out_in_other_context(self): pass + + +class ClearSessionsCommandTests(SimpleTestCase): + def test_clearsessions_unsupported(self): + msg = ( + "Session engine 'tests.sessions_tests.no_clear_expired' doesn't " + "support clearing expired sessions." + ) + with self.settings(SESSION_ENGINE='tests.sessions_tests.no_clear_expired'): + with self.assertRaisesMessage(management.CommandError, msg): + management.call_command('clearsessions')