-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_utils.py
52 lines (37 loc) · 1.29 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from unittest import TestCase
from datetime import timedelta
from rsconnect.json_web_token import JWTEncoder
from tests.utils import (
JWTDecoder,
has_jwt_structure,
)
class TestJwtUtils(TestCase):
def test_jwt_decoder(self):
secret = b"12345678912345678912345678912345"
encoder = JWTEncoder("issuer", "audience", secret)
token = encoder.new_token({}, timedelta(minutes=5))
self.assertTrue(isinstance(token, str))
decoder = JWTDecoder("audience", secret)
result = decoder.decode_token(token)
self.assertTrue(isinstance(result, dict))
self.assertEqual(result["iss"], "issuer")
self.assertEqual(result["aud"], "audience")
def test_has_jwt_structure(self):
"""
Verify that our jwt structure verification helper works as expected
"""
true_examples = [
"aA1-_.aA1-_.aA1-_",
]
for true_example in true_examples:
self.assertTrue(has_jwt_structure(true_example))
false_examples = [
None,
"",
"aA1-_",
"aA1-_.aA1-_.",
"aA1-_.aA1-_.aA1-_.",
".aA1-_.aA1-_.aA1-_",
]
for false_example in false_examples:
self.assertFalse(has_jwt_structure(false_example))