Skip to content

Commit 6bb875c

Browse files
committed
fix(*): update the test cases
1 parent 6731b1d commit 6bb875c

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

backend/app/tests/api/test_deps.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import MagicMock
2+
13
import pytest
24
from sqlmodel import Session
35
from fastapi import HTTPException
@@ -14,6 +16,13 @@
1416
from app.tests.utils.test_data import create_test_api_key
1517

1618

19+
def _mock_request(cookies: dict | None = None) -> MagicMock:
20+
"""Create a mock Request object with optional cookies."""
21+
request = MagicMock()
22+
request.cookies = cookies or {}
23+
return request
24+
25+
1726
class TestGetAuthContext:
1827
"""Test suite for get_auth_context function"""
1928

@@ -22,6 +31,7 @@ def test_get_auth_context_with_valid_api_key(
2231
) -> None:
2332
"""Test successful authentication with valid API key"""
2433
auth_context = get_auth_context(
34+
request=_mock_request(),
2535
session=db,
2636
token=None,
2737
api_key=user_api_key.key,
@@ -33,25 +43,27 @@ def test_get_auth_context_with_valid_api_key(
3343
assert auth_context.organization == user_api_key.organization
3444

3545
def test_get_auth_context_with_invalid_api_key(self, db: Session) -> None:
36-
"""Test authentication fails with invalid API key"""
46+
"""Test authentication fails with invalid API key when no other auth is provided"""
3747
invalid_api_key = "ApiKey InvalidKeyThatDoesNotExist123456789"
3848

3949
with pytest.raises(HTTPException) as exc_info:
4050
get_auth_context(
51+
request=_mock_request(),
4152
session=db,
4253
token=None,
4354
api_key=invalid_api_key,
4455
)
4556

4657
assert exc_info.value.status_code == 401
47-
assert exc_info.value.detail == "Invalid API Key"
58+
assert exc_info.value.detail == "Invalid Authorization format"
4859

4960
def test_get_auth_context_with_valid_token(
5061
self, db: Session, normal_user_token_headers: dict[str, str]
5162
) -> None:
5263
"""Test successful authentication with valid token"""
5364
token = normal_user_token_headers["Authorization"].replace("Bearer ", "")
5465
auth_context = get_auth_context(
66+
request=_mock_request(),
5567
session=db,
5668
token=token,
5769
api_key=None,
@@ -67,6 +79,7 @@ def test_get_auth_context_with_invalid_token(self, db: Session) -> None:
6779

6880
with pytest.raises(HTTPException) as exc_info:
6981
get_auth_context(
82+
request=_mock_request(),
7083
session=db,
7184
token=invalid_token,
7285
api_key=None,
@@ -78,6 +91,7 @@ def test_get_auth_context_with_no_credentials(self, db: Session) -> None:
7891
"""Test authentication fails when neither API key nor token is provided"""
7992
with pytest.raises(HTTPException) as exc_info:
8093
get_auth_context(
94+
request=_mock_request(),
8195
session=db,
8296
token=None,
8397
api_key=None,
@@ -98,6 +112,7 @@ def test_get_auth_context_with_inactive_user_via_api_key(self, db: Session) -> N
98112

99113
with pytest.raises(HTTPException) as exc_info:
100114
get_auth_context(
115+
request=_mock_request(),
101116
session=db,
102117
token=None,
103118
api_key=api_key.key,
@@ -122,6 +137,7 @@ def test_get_auth_context_with_inactive_user_via_token(
122137

123138
with pytest.raises(HTTPException) as exc_info:
124139
get_auth_context(
140+
request=_mock_request(),
125141
session=db,
126142
token=token,
127143
api_key=None,
@@ -142,6 +158,7 @@ def test_get_auth_context_with_inactive_organization(
142158

143159
with pytest.raises(HTTPException) as exc_info:
144160
get_auth_context(
161+
request=_mock_request(),
145162
session=db,
146163
token=None,
147164
api_key=user_api_key.key,
@@ -162,6 +179,7 @@ def test_get_auth_context_with_inactive_project(
162179

163180
with pytest.raises(HTTPException) as exc_info:
164181
get_auth_context(
182+
request=_mock_request(),
165183
session=db,
166184
token=None,
167185
api_key=user_api_key.key,

backend/app/tests/api/test_permissions.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import MagicMock
2+
13
import pytest
24
from fastapi import HTTPException
35
from sqlmodel import Session
@@ -8,6 +10,13 @@
810
from app.tests.utils.test_data import create_test_api_key
911

1012

13+
def _mock_request() -> MagicMock:
14+
"""Create a mock Request object with empty cookies."""
15+
request = MagicMock()
16+
request.cookies = {}
17+
return request
18+
19+
1120
class TestHasPermission:
1221
"""Test suite for has_permission function"""
1322

@@ -21,7 +30,10 @@ def test_superuser_permission_with_superuser(self, db: Session) -> None:
2130
db.refresh(user)
2231

2332
auth_context = get_auth_context(
24-
session=db, token=None, api_key=api_key_response.key
33+
request=_mock_request(),
34+
session=db,
35+
token=None,
36+
api_key=api_key_response.key,
2537
)
2638

2739
result = has_permission(auth_context, Permission.SUPERUSER, db)
@@ -33,7 +45,10 @@ def test_superuser_permission_with_regular_user(self, db: Session) -> None:
3345
api_key_response = create_test_api_key(db)
3446

3547
auth_context = get_auth_context(
36-
session=db, token=None, api_key=api_key_response.key
48+
request=_mock_request(),
49+
session=db,
50+
token=None,
51+
api_key=api_key_response.key,
3752
)
3853

3954
result = has_permission(auth_context, Permission.SUPERUSER, db)
@@ -47,7 +62,10 @@ def test_require_organization_permission_with_organization(
4762
api_key_response = create_test_api_key(db)
4863

4964
auth_context = get_auth_context(
50-
session=db, token=None, api_key=api_key_response.key
65+
request=_mock_request(),
66+
session=db,
67+
token=None,
68+
api_key=api_key_response.key,
5169
)
5270

5371
result = has_permission(auth_context, Permission.REQUIRE_ORGANIZATION, db)
@@ -61,7 +79,10 @@ def test_require_organization_permission_without_organization(
6179
api_key_response = create_test_api_key(db)
6280

6381
auth_context = get_auth_context(
64-
session=db, token=None, api_key=api_key_response.key
82+
request=_mock_request(),
83+
session=db,
84+
token=None,
85+
api_key=api_key_response.key,
6586
)
6687

6788
auth_context.organization = None
@@ -75,7 +96,10 @@ def test_require_project_permission_with_project(self, db: Session) -> None:
7596
api_key_response = create_test_api_key(db)
7697

7798
auth_context = get_auth_context(
78-
session=db, token=None, api_key=api_key_response.key
99+
request=_mock_request(),
100+
session=db,
101+
token=None,
102+
api_key=api_key_response.key,
79103
)
80104

81105
result = has_permission(auth_context, Permission.REQUIRE_PROJECT, db)
@@ -87,7 +111,10 @@ def test_require_project_permission_without_project(self, db: Session) -> None:
87111
api_key_response = create_test_api_key(db)
88112

89113
auth_context = get_auth_context(
90-
session=db, token=None, api_key=api_key_response.key
114+
request=_mock_request(),
115+
session=db,
116+
token=None,
117+
api_key=api_key_response.key,
91118
)
92119

93120
auth_context.project = None
@@ -115,7 +142,10 @@ def test_permission_checker_passes_with_valid_permission(self, db: Session) -> N
115142
db.commit()
116143
db.refresh(user)
117144
auth_context = get_auth_context(
118-
session=db, token=None, api_key=api_key_response.key
145+
request=_mock_request(),
146+
session=db,
147+
token=None,
148+
api_key=api_key_response.key,
119149
)
120150

121151
permission_checker = require_permission(Permission.SUPERUSER)
@@ -127,7 +157,10 @@ def test_permission_checker_raises_403_without_permission(
127157
"""Test that permission checker raises HTTPException with 403 when user lacks permission"""
128158
api_key_response = create_test_api_key(db)
129159
auth_context = get_auth_context(
130-
session=db, token=None, api_key=api_key_response.key
160+
request=_mock_request(),
161+
session=db,
162+
token=None,
163+
api_key=api_key_response.key,
131164
)
132165

133166
permission_checker = require_permission(Permission.SUPERUSER)

0 commit comments

Comments
 (0)