|
3 | 3 |
|
4 | 4 | from django.contrib.auth import get_user_model
|
5 | 5 | from django.test import TestCase
|
| 6 | +import jwt.exceptions |
6 | 7 | from rest_framework_jwt import utils
|
| 8 | +from rest_framework_jwt.settings import api_settings, DEFAULTS |
7 | 9 |
|
8 | 10 | User = get_user_model()
|
9 | 11 |
|
@@ -54,3 +56,71 @@ def test_jwt_response_payload(self):
|
54 | 56 | response_data = utils.jwt_response_payload_handler(token, self.user)
|
55 | 57 |
|
56 | 58 | self.assertEqual(response_data, dict(token=token))
|
| 59 | + |
| 60 | + |
| 61 | +class TestAudience(TestCase): |
| 62 | + def setUp(self): |
| 63 | + api_settings.JWT_AUDIENCE = "my_aud" |
| 64 | + |
| 65 | + self.username = 'jpueblo' |
| 66 | + self.email = 'jpueblo@example.com' |
| 67 | + self.user = User.objects.create_user(self.username, self.email) |
| 68 | + |
| 69 | + return super(TestAudience, self).setUp() |
| 70 | + |
| 71 | + def test_fail_audience_missing(self): |
| 72 | + payload = utils.jwt_payload_handler(self.user) |
| 73 | + token = utils.jwt_encode_handler(payload) |
| 74 | + with self.assertRaises(jwt.exceptions.InvalidAudienceError): |
| 75 | + utils.jwt_decode_handler(token) |
| 76 | + |
| 77 | + def test_fail_audience_wrong(self): |
| 78 | + payload = utils.jwt_payload_handler(self.user) |
| 79 | + payload['aud'] = "my_aud2" |
| 80 | + token = utils.jwt_encode_handler(payload) |
| 81 | + with self.assertRaises(jwt.exceptions.InvalidAudienceError): |
| 82 | + utils.jwt_decode_handler(token) |
| 83 | + |
| 84 | + def test_correct_audience(self): |
| 85 | + payload = utils.jwt_payload_handler(self.user) |
| 86 | + payload['aud'] = "my_aud" |
| 87 | + token = utils.jwt_encode_handler(payload) |
| 88 | + decoded_payload = utils.jwt_decode_handler(token) |
| 89 | + self.assertEqual(decoded_payload, payload) |
| 90 | + |
| 91 | + def tearDown(self): |
| 92 | + api_settings.JWT_AUDIENCE = DEFAULTS['JWT_AUDIENCE'] |
| 93 | + |
| 94 | + |
| 95 | +class TestIssuer(TestCase): |
| 96 | + def setUp(self): |
| 97 | + api_settings.JWT_ISSUER = "example.com" |
| 98 | + |
| 99 | + self.username = 'jpueblo' |
| 100 | + self.email = 'jpueblo@example.com' |
| 101 | + self.user = User.objects.create_user(self.username, self.email) |
| 102 | + |
| 103 | + return super(TestIssuer, self).setUp() |
| 104 | + |
| 105 | + def test_fail_issuer_missing(self): |
| 106 | + payload = utils.jwt_payload_handler(self.user) |
| 107 | + token = utils.jwt_encode_handler(payload) |
| 108 | + with self.assertRaises(jwt.exceptions.InvalidIssuerError): |
| 109 | + utils.jwt_decode_handler(token) |
| 110 | + |
| 111 | + def test_fail_issuer_wrong(self): |
| 112 | + payload = utils.jwt_payload_handler(self.user) |
| 113 | + token = utils.jwt_encode_handler(payload) |
| 114 | + payload['iss'] = "example2.com" |
| 115 | + with self.assertRaises(jwt.exceptions.InvalidIssuerError): |
| 116 | + utils.jwt_decode_handler(token) |
| 117 | + |
| 118 | + def test_correct_issuer(self): |
| 119 | + payload = utils.jwt_payload_handler(self.user) |
| 120 | + payload['iss'] = "example.com" |
| 121 | + token = utils.jwt_encode_handler(payload) |
| 122 | + decoded_payload = utils.jwt_decode_handler(token) |
| 123 | + self.assertEqual(decoded_payload, payload) |
| 124 | + |
| 125 | + def tearDown(self): |
| 126 | + api_settings.JWT_ISSUER = DEFAULTS['JWT_ISSUER'] |
0 commit comments