|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from django.utils import timezone |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from rest_framework_jwt.authentication import JSONWebTokenAuthentication |
| 9 | +from rest_framework_jwt.blacklist.models import BlacklistedToken |
| 10 | +from rest_framework_jwt.settings import api_settings |
| 11 | + |
| 12 | +import uuid |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + 'id_setting', ['require', 'include'] |
| 17 | +) |
| 18 | +def test_token_is_blocked_by_id(user, monkeypatch, id_setting): |
| 19 | + monkeypatch.setattr(api_settings, "JWT_TOKEN_ID", id_setting) |
| 20 | + payload = JSONWebTokenAuthentication.jwt_create_payload(user) |
| 21 | + token = JSONWebTokenAuthentication.jwt_encode_payload(payload) |
| 22 | + |
| 23 | + expiration = timezone.now() + timedelta(days=1) |
| 24 | + BlacklistedToken( |
| 25 | + token_id=payload['jti'], |
| 26 | + expires_at=expiration, |
| 27 | + user=user, |
| 28 | + ).save() |
| 29 | + |
| 30 | + assert BlacklistedToken.is_blocked(token, payload) is True |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + 'id_setting', ['require', 'include'] |
| 36 | +) |
| 37 | +def test_refreshed_token_is_blocked_by_original_id(user, call_auth_refresh_endpoint, monkeypatch, id_setting): |
| 38 | + monkeypatch.setattr(api_settings, "JWT_TOKEN_ID", id_setting) |
| 39 | + original_payload = JSONWebTokenAuthentication.jwt_create_payload(user) |
| 40 | + original_token = JSONWebTokenAuthentication.jwt_encode_payload(original_payload) |
| 41 | + |
| 42 | + refresh_response = call_auth_refresh_endpoint(original_token) |
| 43 | + refreshed_token = refresh_response.json()['token'] |
| 44 | + payload = JSONWebTokenAuthentication.jwt_decode_token(refreshed_token) |
| 45 | + |
| 46 | + expiration = timezone.now() + timedelta(days=1) |
| 47 | + BlacklistedToken( |
| 48 | + token_id=original_payload['jti'], |
| 49 | + expires_at=expiration, |
| 50 | + user=user, |
| 51 | + ).save() |
| 52 | + |
| 53 | + assert BlacklistedToken.is_blocked(refreshed_token, payload) is True |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize( |
| 57 | + 'id_setting', ['include', 'off'] |
| 58 | +) |
| 59 | +def test_token_is_blocked_by_value(user, monkeypatch, id_setting): |
| 60 | + monkeypatch.setattr(api_settings, "JWT_TOKEN_ID", id_setting) |
| 61 | + payload = JSONWebTokenAuthentication.jwt_create_payload(user) |
| 62 | + token = JSONWebTokenAuthentication.jwt_encode_payload(payload) |
| 63 | + |
| 64 | + expiration = timezone.now() + timedelta(days=1) |
| 65 | + BlacklistedToken( |
| 66 | + token=token, |
| 67 | + expires_at=expiration, |
| 68 | + user=user, |
| 69 | + ).save() |
| 70 | + |
| 71 | + assert BlacklistedToken.is_blocked(token, payload) is True |
| 72 | + |
| 73 | + |
| 74 | +def test_token_is_not_blocked_by_value_when_ids_required(user, monkeypatch): |
| 75 | + monkeypatch.setattr(api_settings, "JWT_TOKEN_ID", "require") |
| 76 | + payload = JSONWebTokenAuthentication.jwt_create_payload(user) |
| 77 | + token = JSONWebTokenAuthentication.jwt_encode_payload(payload) |
| 78 | + |
| 79 | + expiration = timezone.now() + timedelta(days=1) |
| 80 | + BlacklistedToken( |
| 81 | + token=token, |
| 82 | + expires_at=expiration, |
| 83 | + user=user, |
| 84 | + ).save() |
| 85 | + |
| 86 | + assert BlacklistedToken.is_blocked(token, payload) is False |
| 87 | + |
| 88 | + |
| 89 | +def test_token_is_not_blocked_by_id_when_ids_disabled(user, monkeypatch): |
| 90 | + monkeypatch.setattr(api_settings, "JWT_TOKEN_ID", "off") |
| 91 | + payload = JSONWebTokenAuthentication.jwt_create_payload(user) |
| 92 | + payload['jti'] = uuid.uuid4() |
| 93 | + token = JSONWebTokenAuthentication.jwt_encode_payload(payload) |
| 94 | + |
| 95 | + expiration = timezone.now() + timedelta(days=1) |
| 96 | + BlacklistedToken( |
| 97 | + token_id=payload['jti'], |
| 98 | + expires_at=expiration, |
| 99 | + user=user, |
| 100 | + ).save() |
| 101 | + |
| 102 | + assert BlacklistedToken.is_blocked(token, payload) is False |
0 commit comments