-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_refresh_token.py
More file actions
43 lines (32 loc) · 1.5 KB
/
test_refresh_token.py
File metadata and controls
43 lines (32 loc) · 1.5 KB
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
import jwt
import pytest
from jwt_helper import JWT_REFRESH_TOKEN_EXPIRY, JWT_SECRET_KEY, generate_refresh_token
@pytest.fixture
def sample_refresh_token(sample_person_id):
"""Provide a sample refresh token for testing"""
return generate_refresh_token(sample_person_id)
def test_refresh_token_type(sample_refresh_token):
"""Ensure generate_refresh_token returns a string"""
assert isinstance(sample_refresh_token, str)
def test_decoded_refresh_token_decoded(sample_person_id, sample_refresh_token):
"""
Ensure the generated refresh token can be decoded and contains the correct payload
- Check if the payload contains the correct person ID
- Check if the token has an expiration time
- Check if the token type is 'refresh'
"""
payload = jwt.decode(sample_refresh_token, JWT_SECRET_KEY, algorithms=["HS256"])
assert payload["person_id"] == sample_person_id
assert "exp" in payload
assert payload["token_type"] == "refresh"
def test_refresh_token_expiration(sample_refresh_token):
"""
Ensure the generated refresh token has a valid expiration time
- Check if the expiration time is greater than 0
- Check if the expiration time is greater than the issued at time
- Check if the token is not expired
"""
payload = jwt.decode(sample_refresh_token, JWT_SECRET_KEY, algorithms=["HS256"])
assert payload["exp"] > 0
assert payload["exp"] > payload["iat"]
assert (payload["exp"] - payload["iat"]) == JWT_REFRESH_TOKEN_EXPIRY.total_seconds()