2020-04-29 22:45:00 +08:00
|
|
|
from django.contrib.auth import HASH_SESSION_KEY
|
2014-12-03 20:33:44 +08:00
|
|
|
from django.contrib.auth.middleware import AuthenticationMiddleware
|
2014-04-01 08:16:09 +08:00
|
|
|
from django.contrib.auth.models import User
|
2019-09-27 01:06:35 +08:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2014-04-01 08:16:09 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
2015-09-03 08:50:34 +08:00
|
|
|
class TestAuthenticationMiddleware(TestCase):
|
2018-11-24 10:24:25 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.user = User.objects.create_user('test_user', 'test@example.com', 'test_password')
|
|
|
|
|
2014-04-01 08:16:09 +08:00
|
|
|
def setUp(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.middleware = AuthenticationMiddleware(lambda req: HttpResponse())
|
2015-09-03 08:50:34 +08:00
|
|
|
self.client.force_login(self.user)
|
2014-12-03 20:33:44 +08:00
|
|
|
self.request = HttpRequest()
|
|
|
|
self.request.session = self.client.session
|
|
|
|
|
2015-09-03 08:50:34 +08:00
|
|
|
def test_no_password_change_doesnt_invalidate_session(self):
|
|
|
|
self.request.session = self.client.session
|
2019-09-27 01:06:35 +08:00
|
|
|
self.middleware(self.request)
|
2020-04-29 22:45:00 +08:00
|
|
|
self.assertIsNotNone(self.request.user)
|
|
|
|
self.assertFalse(self.request.user.is_anonymous)
|
|
|
|
|
|
|
|
def test_no_password_change_does_not_invalidate_legacy_session(self):
|
|
|
|
# RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
|
|
|
|
session = self.client.session
|
|
|
|
session[HASH_SESSION_KEY] = self.user._legacy_get_session_auth_hash()
|
|
|
|
session.save()
|
|
|
|
self.request.session = session
|
|
|
|
self.middleware(self.request)
|
2014-12-03 20:33:44 +08:00
|
|
|
self.assertIsNotNone(self.request.user)
|
2016-04-02 19:18:26 +08:00
|
|
|
self.assertFalse(self.request.user.is_anonymous)
|
2014-12-03 20:33:44 +08:00
|
|
|
|
2015-09-03 08:50:34 +08:00
|
|
|
def test_changed_password_invalidates_session(self):
|
|
|
|
# After password change, user should be anonymous
|
2014-12-03 20:33:44 +08:00
|
|
|
self.user.set_password('new_password')
|
|
|
|
self.user.save()
|
2019-09-27 01:06:35 +08:00
|
|
|
self.middleware(self.request)
|
2014-12-03 20:33:44 +08:00
|
|
|
self.assertIsNotNone(self.request.user)
|
2016-04-02 19:18:26 +08:00
|
|
|
self.assertTrue(self.request.user.is_anonymous)
|
2014-12-03 20:33:44 +08:00
|
|
|
# session should be flushed
|
|
|
|
self.assertIsNone(self.request.session.session_key)
|
2019-10-23 14:04:14 +08:00
|
|
|
|
|
|
|
def test_no_session(self):
|
|
|
|
msg = (
|
|
|
|
"The Django authentication middleware requires session middleware "
|
|
|
|
"to be installed. Edit your MIDDLEWARE setting to insert "
|
|
|
|
"'django.contrib.sessions.middleware.SessionMiddleware' before "
|
|
|
|
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
self.middleware(HttpRequest())
|