|
10 | 10 | from rest_framework_simplejwt.models import TokenUser |
11 | 11 | from rest_framework_simplejwt.settings import api_settings |
12 | 12 | from rest_framework_simplejwt.tokens import AccessToken, SlidingToken |
| 13 | +from rest_framework_simplejwt.utils import get_md5_hash_password |
13 | 14 |
|
14 | 15 | from .utils import override_api_settings |
15 | 16 |
|
@@ -160,6 +161,45 @@ def test_get_user(self): |
160 | 161 | # Otherwise, should return correct user |
161 | 162 | self.assertEqual(self.backend.get_user(payload).id, u.id) |
162 | 163 |
|
| 164 | + @override_api_settings( |
| 165 | + CHECK_REVOKE_TOKEN=True, REVOKE_TOKEN_CLAIM="revoke_token_claim" |
| 166 | + ) |
| 167 | + def test_get_user_with_check_revoke_token(self): |
| 168 | + payload = {"some_other_id": "foo"} |
| 169 | + |
| 170 | + # Should raise error if no recognizable user identification |
| 171 | + with self.assertRaises(InvalidToken): |
| 172 | + self.backend.get_user(payload) |
| 173 | + |
| 174 | + payload[api_settings.USER_ID_CLAIM] = 42 |
| 175 | + |
| 176 | + # Should raise exception if user not found |
| 177 | + with self.assertRaises(AuthenticationFailed): |
| 178 | + self.backend.get_user(payload) |
| 179 | + |
| 180 | + u = User.objects.create_user(username="markhamill") |
| 181 | + u.is_active = False |
| 182 | + u.save() |
| 183 | + |
| 184 | + payload[api_settings.USER_ID_CLAIM] = getattr(u, api_settings.USER_ID_FIELD) |
| 185 | + |
| 186 | + # Should raise exception if user is inactive |
| 187 | + with self.assertRaises(AuthenticationFailed): |
| 188 | + self.backend.get_user(payload) |
| 189 | + |
| 190 | + u.is_active = True |
| 191 | + u.save() |
| 192 | + |
| 193 | + # Should raise exception if hash password is different |
| 194 | + with self.assertRaises(AuthenticationFailed): |
| 195 | + self.backend.get_user(payload) |
| 196 | + |
| 197 | + if api_settings.CHECK_REVOKE_TOKEN: |
| 198 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = get_md5_hash_password(u.password) |
| 199 | + |
| 200 | + # Otherwise, should return correct user |
| 201 | + self.assertEqual(self.backend.get_user(payload).id, u.id) |
| 202 | + |
163 | 203 |
|
164 | 204 | class TestJWTStatelessUserAuthentication(TestCase): |
165 | 205 | def setUp(self): |
|
0 commit comments