Skip to content

Commit 4cab9e9

Browse files
🐛 Handle non-existing user IDs in read_user_by_id (#1396)
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
1 parent fe3bafc commit 4cab9e9

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

backend/app/api/routes/users.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def read_user_by_id(
170170
status_code=403,
171171
detail="The user doesn't have enough privileges",
172172
)
173+
if user is None:
174+
raise HTTPException(status_code=404, detail="User not found")
173175
return user
174176

175177

backend/tests/api/routes/test_users.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from app.core.config import settings
99
from app.core.security import verify_password
1010
from app.models import User, UserCreate
11+
from tests.utils.user import create_random_user
1112
from tests.utils.utils import random_email, random_lower_string
1213

1314

@@ -56,7 +57,7 @@ def test_create_user_new_email(
5657
assert user.email == created_user["email"]
5758

5859

59-
def test_get_existing_user(
60+
def test_get_existing_user_as_superuser(
6061
client: TestClient, superuser_token_headers: dict[str, str], db: Session
6162
) -> None:
6263
username = random_email()
@@ -75,6 +76,17 @@ def test_get_existing_user(
7576
assert existing_user.email == api_user["email"]
7677

7778

79+
def test_get_non_existing_user_as_superuser(
80+
client: TestClient, superuser_token_headers: dict[str, str]
81+
) -> None:
82+
r = client.get(
83+
f"{settings.API_V1_STR}/users/{uuid.uuid4()}",
84+
headers=superuser_token_headers,
85+
)
86+
assert r.status_code == 404
87+
assert r.json() == {"detail": "User not found"}
88+
89+
7890
def test_get_existing_user_current_user(client: TestClient, db: Session) -> None:
7991
username = random_email()
8092
password = random_lower_string()
@@ -103,10 +115,28 @@ def test_get_existing_user_current_user(client: TestClient, db: Session) -> None
103115

104116

105117
def test_get_existing_user_permissions_error(
106-
client: TestClient, normal_user_token_headers: dict[str, str]
118+
db: Session,
119+
client: TestClient,
120+
normal_user_token_headers: dict[str, str],
107121
) -> None:
122+
user = create_random_user(db)
123+
108124
r = client.get(
109-
f"{settings.API_V1_STR}/users/{uuid.uuid4()}",
125+
f"{settings.API_V1_STR}/users/{user.id}",
126+
headers=normal_user_token_headers,
127+
)
128+
assert r.status_code == 403
129+
assert r.json() == {"detail": "The user doesn't have enough privileges"}
130+
131+
132+
def test_get_non_existing_user_permissions_error(
133+
client: TestClient,
134+
normal_user_token_headers: dict[str, str],
135+
) -> None:
136+
user_id = uuid.uuid4()
137+
138+
r = client.get(
139+
f"{settings.API_V1_STR}/users/{user_id}",
110140
headers=normal_user_token_headers,
111141
)
112142
assert r.status_code == 403

0 commit comments

Comments
 (0)