-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtest_noop_with_token.py
More file actions
106 lines (82 loc) · 3.26 KB
/
test_noop_with_token.py
File metadata and controls
106 lines (82 loc) · 3.26 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Unit tests for functions defined in authentication/noop_with_token.py"""
from fastapi import Request, HTTPException
import pytest
from authentication.noop_with_token import NoopWithTokenAuthDependency
from constants import DEFAULT_USER_NAME, DEFAULT_USER_UID
async def test_noop_with_token_auth_dependency() -> None:
"""Test the NoopWithTokenAuthDependency class with default user ID."""
dependency = NoopWithTokenAuthDependency()
request = Request(
scope={
"type": "http",
"query_string": b"",
"headers": [
(b"authorization", b"Bearer spongebob-token"),
],
},
)
# Call the dependency
user_id, username, skip_userid_check, user_token = await dependency(request)
# Assert the expected values
assert user_id == DEFAULT_USER_UID
assert username == DEFAULT_USER_NAME
assert skip_userid_check is True
assert user_token == "spongebob-token"
async def test_noop_with_token_auth_dependency_custom_user_id() -> None:
"""Test the NoopWithTokenAuthDependency class with custom user ID."""
dependency = NoopWithTokenAuthDependency()
# Create a mock request
request = Request(
scope={
"type": "http",
"query_string": b"user_id=test-user",
"headers": [
(b"authorization", b"Bearer spongebob-token"),
],
},
)
# Call the dependency
user_id, username, skip_userid_check, user_token = await dependency(request)
# Assert the expected values
assert user_id == "test-user"
assert username == DEFAULT_USER_NAME
assert skip_userid_check is True
assert user_token == "spongebob-token"
async def test_noop_with_token_auth_dependency_no_token() -> None:
"""
Test if checks for Authorization header is in place.
Test that NoopWithTokenAuthDependency raises an HTTPException when no
Authorization header is present in the request.
Asserts that the exception has a status code of 400 and the detail message
"No Authorization header found".
"""
dependency = NoopWithTokenAuthDependency()
# Create a mock request without token
request = Request(
scope={
"type": "http",
"query_string": b"",
"headers": [],
},
)
# Assert that an HTTPException is raised when no Authorization header is found
with pytest.raises(HTTPException) as exc_info:
await dependency(request)
assert exc_info.value.status_code == 401
assert exc_info.value.detail == "No Authorization header found"
async def test_noop_with_token_auth_dependency_no_bearer() -> None:
"""Test the NoopWithTokenAuthDependency class with no token."""
dependency = NoopWithTokenAuthDependency()
# Create a mock request without token
request = Request(
scope={
"type": "http",
"query_string": b"",
"headers": [(b"authorization", b"NotBearer anything")],
},
)
# Assert that an HTTPException is raised when no Authorization header is found
with pytest.raises(HTTPException) as exc_info:
await dependency(request)
assert exc_info.value.status_code == 401
assert exc_info.value.detail == "No token found in Authorization header"