Skip to content

Commit f5db669

Browse files
committed
Add test for AuthenticationMiddleware not being reached (and no user attribute on Request)
1 parent 65b3a64 commit f5db669

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

django_session_jwt/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@
7575
)
7676

7777
MIDDLEWARE = (
78-
'django.middleware.common.CommonMiddleware',
7978
'django_session_jwt.middleware.SessionMiddleware',
79+
# MiddlewareFailTestCase relies on this ordering.
80+
'django.middleware.common.CommonMiddleware',
8081
'django.middleware.csrf.CsrfViewMiddleware',
8182
'django.contrib.auth.middleware.AuthenticationMiddleware',
8283
'django.contrib.messages.middleware.MessageMiddleware',

django_session_jwt/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def test_unauthenicated_view(self):
133133
r.cookies.get(settings.SESSION_COOKIE_NAME).value)
134134
self.assertNotEqual(jwt1['iat'], jwt2['iat'])
135135

136+
def test_middleware_early_return(self):
137+
"""
138+
By passing an incorrect HOST, CommonMiddleware returns early before
139+
AuthenticationMiddleware is reached. We test this to ensure we don't error in
140+
process_response, and end up hiding the real error with a HTTP 500
141+
"""
142+
response = self.client.post('/login/', {'username': 'john', 'password': 'password'},
143+
HTTP_HOST="blahblah.com")
144+
self.assertEqual(response.status_code, 400)
145+
136146

137147
class TestClientTestCase(BaseTestCase):
138148
def test_login(self):

0 commit comments

Comments
 (0)