-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_verify_token.py
More file actions
54 lines (41 loc) · 1.71 KB
/
test_verify_token.py
File metadata and controls
54 lines (41 loc) · 1.71 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
44
45
46
47
48
49
50
51
52
53
54
from datetime import datetime, timedelta
import jwt
import pytest
from flask import Flask
from jwt_helper import JWT_SECRET_KEY, TokenError, verify_token
app = Flask(__name__)
def test_verify_valid_access_token():
"""Test verifying a valid access token."""
access_token = jwt.encode(
{"token_type": "access"}, JWT_SECRET_KEY, algorithm="HS256"
)
decoded = verify_token(access_token, "access")
assert decoded["token_type"] == "access"
def test_verify_valid_refresh_token():
"""Test verifying a valid refresh token."""
refresh_token = jwt.encode(
{"token_type": "refresh"}, JWT_SECRET_KEY, algorithm="HS256"
)
decoded = verify_token(refresh_token, "refresh")
assert decoded["token_type"] == "refresh"
def test_verify_token_invalid_type():
"""Test verifying a token with an incorrect type."""
token = jwt.encode({"token_type": "invalid"}, JWT_SECRET_KEY, algorithm="HS256")
with pytest.raises(TokenError, match="Invalid token") as excinfo:
verify_token(token, "access")
assert excinfo.value.status_code == 401
def test_verify_expired_token():
"""Test verifying an expired token."""
expired_token = jwt.encode(
{"token_type": "access", "exp": datetime.now() - timedelta(seconds=1)},
JWT_SECRET_KEY,
algorithm="HS256",
)
with pytest.raises(TokenError, match="Token has expired") as excinfo:
verify_token(expired_token, "access")
assert excinfo.value.status_code == 401
def test_verify_invalid_token():
"""Test verifying an invalid token."""
with pytest.raises(TokenError, match="Invalid token") as excinfo:
verify_token("invalid_token", "access")
assert excinfo.value.status_code == 401