2014-12-03 12:33:44 +00:00
|
|
|
from django.contrib.auth.middleware import AuthenticationMiddleware
|
2014-04-01 00:16:09 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
2015-09-03 00:50:34 +00:00
|
|
|
class TestAuthenticationMiddleware(TestCase):
|
2014-04-01 00:16:09 +00:00
|
|
|
def setUp(self):
|
2015-09-03 00:50:34 +00:00
|
|
|
self.user = User.objects.create_user('test_user', 'test@example.com', 'test_password')
|
2014-12-03 12:33:44 +00:00
|
|
|
self.middleware = AuthenticationMiddleware()
|
2015-09-03 00:50:34 +00:00
|
|
|
self.client.force_login(self.user)
|
2014-12-03 12:33:44 +00:00
|
|
|
self.request = HttpRequest()
|
|
|
|
self.request.session = self.client.session
|
|
|
|
|
2015-09-03 00:50:34 +00:00
|
|
|
def test_no_password_change_doesnt_invalidate_session(self):
|
|
|
|
self.request.session = self.client.session
|
2014-12-03 12:33:44 +00:00
|
|
|
self.middleware.process_request(self.request)
|
|
|
|
self.assertIsNotNone(self.request.user)
|
2016-04-02 11:18:26 +00:00
|
|
|
self.assertFalse(self.request.user.is_anonymous)
|
2014-12-03 12:33:44 +00:00
|
|
|
|
2015-09-03 00:50:34 +00:00
|
|
|
def test_changed_password_invalidates_session(self):
|
|
|
|
# After password change, user should be anonymous
|
2014-12-03 12:33:44 +00:00
|
|
|
self.user.set_password('new_password')
|
|
|
|
self.user.save()
|
|
|
|
self.middleware.process_request(self.request)
|
|
|
|
self.assertIsNotNone(self.request.user)
|
2016-04-02 11:18:26 +00:00
|
|
|
self.assertTrue(self.request.user.is_anonymous)
|
2014-12-03 12:33:44 +00:00
|
|
|
# session should be flushed
|
|
|
|
self.assertIsNone(self.request.session.session_key)
|