-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_extract_token_from_header.py
More file actions
42 lines (32 loc) · 1.48 KB
/
test_extract_token_from_header.py
File metadata and controls
42 lines (32 loc) · 1.48 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
import pytest
from flask import Flask
from jwt_helper import TokenError, extract_token_from_header
app = Flask(__name__)
def test_extract_token_valid(sample_token):
"""Test extracting a valid Bearer token."""
with app.test_request_context(headers={"Authorization": f"Bearer {sample_token}"}):
assert extract_token_from_header() == sample_token
def test_extract_token_missing():
"""Test missing Authorization header."""
with app.test_request_context(headers={}):
with pytest.raises(
TokenError, match="Token is missing or improperly formatted"
) as excinfo:
extract_token_from_header()
assert excinfo.value.status_code == 401
def test_extract_token_invalid_format():
"""Test an improperly formatted Authorization header."""
with app.test_request_context(headers={"Authorization": "InvalidTokenFormat"}):
with pytest.raises(
TokenError, match="Token is missing or improperly formatted"
) as excinfo:
extract_token_from_header()
assert excinfo.value.status_code == 401
def test_extract_token_no_bearer(sample_token):
"""Test Authorization header without 'Bearer ' prefix."""
with app.test_request_context(headers={"Authorization": f"Basic {sample_token}"}):
with pytest.raises(
TokenError, match="Token is missing or improperly formatted"
) as excinfo:
extract_token_from_header()
assert excinfo.value.status_code == 401