Fixed #32301 -- Made clearsessions raise CommandError when clear_expired() is not implemented.

This commit is contained in:
François Freitag 2020-05-18 22:11:46 +02:00 committed by Mariusz Felisiak
parent 270072c4c2
commit b11ec9a69e
3 changed files with 19 additions and 2 deletions

View File

@ -1,7 +1,7 @@
from importlib import import_module from importlib import import_module
from django.conf import settings from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand): class Command(BaseCommand):
@ -15,7 +15,7 @@ class Command(BaseCommand):
try: try:
engine.SessionStore.clear_expired() engine.SessionStore.clear_expired()
except NotImplementedError: except NotImplementedError:
self.stderr.write( raise CommandError(
"Session engine '%s' doesn't support clearing expired " "Session engine '%s' doesn't support clearing expired "
"sessions." % settings.SESSION_ENGINE "sessions." % settings.SESSION_ENGINE
) )

View File

@ -0,0 +1,6 @@
from django.contrib.sessions.backends.base import SessionBase
class SessionStore(SessionBase):
"""Session store without support for clearing expired sessions."""
pass

View File

@ -910,3 +910,14 @@ class CookieSessionTests(SessionTestsMixin, SimpleTestCase):
@unittest.skip("CookieSession is stored in the client and there is no way to query it.") @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): def test_session_save_does_not_resurrect_session_logged_out_in_other_context(self):
pass 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')