diff options
| author | François Freitag <mail@franek.fr> | 2020-05-18 22:11:46 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-12-28 09:30:16 +0100 |
| commit | b11ec9a69ef626bd7aa22879145e9d74ea38a814 (patch) | |
| tree | c38bd15f3c332cff4f3955e5ca52725395853c2f | |
| parent | 270072c4c231acd72a03534357a4aa59010e76ff (diff) | |
Fixed #32301 -- Made clearsessions raise CommandError when clear_expired() is not implemented.
| -rw-r--r-- | django/contrib/sessions/management/commands/clearsessions.py | 4 | ||||
| -rw-r--r-- | tests/sessions_tests/no_clear_expired.py | 6 | ||||
| -rw-r--r-- | tests/sessions_tests/tests.py | 11 |
3 files changed, 19 insertions, 2 deletions
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') |
