Skip to content

Commit a751b59

Browse files
committed
refactor: replace hardcoded status codes with fastapi.status constants
1 parent d1df85e commit a751b59

File tree

9 files changed

+126
-74
lines changed

9 files changed

+126
-74
lines changed

backend/app/api/deps.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
4040
)
4141
user = session.get(User, token_data.sub)
4242
if not user:
43-
raise HTTPException(status_code=404, detail="User not found")
43+
raise HTTPException(
44+
status_code=status.HTTP_404_NOT_FOUND,
45+
detail="User not found",
46+
)
4447
if not user.is_active:
45-
raise HTTPException(status_code=400, detail="Inactive user")
48+
raise HTTPException(
49+
status_code=status.HTTP_400_BAD_REQUEST,
50+
detail="Inactive user",
51+
)
4652
return user
4753

4854

@@ -52,6 +58,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
5258
def get_current_active_superuser(current_user: CurrentUser) -> User:
5359
if not current_user.is_superuser:
5460
raise HTTPException(
55-
status_code=403, detail="The user doesn't have enough privileges"
61+
status_code=status.HTTP_403_FORBIDDEN,
62+
detail="The user doesn't have enough privileges",
5663
)
5764
return current_user

backend/app/api/routes/items.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
from typing import Any
33

4-
from fastapi import APIRouter, HTTPException
4+
from fastapi import APIRouter, HTTPException, status
55
from sqlmodel import func, select
66

77
from app.api.deps import CurrentUser, SessionDep
@@ -48,9 +48,15 @@ def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) ->
4848
"""
4949
item = session.get(Item, id)
5050
if not item:
51-
raise HTTPException(status_code=404, detail="Item not found")
51+
raise HTTPException(
52+
status_code=status.HTTP_404_NOT_FOUND,
53+
detail="Item not found",
54+
)
5255
if not current_user.is_superuser and (item.owner_id != current_user.id):
53-
raise HTTPException(status_code=400, detail="Not enough permissions")
56+
raise HTTPException(
57+
status_code=status.HTTP_400_BAD_REQUEST,
58+
detail="Not enough permissions",
59+
)
5460
return item
5561

5662

@@ -81,9 +87,15 @@ def update_item(
8187
"""
8288
item = session.get(Item, id)
8389
if not item:
84-
raise HTTPException(status_code=404, detail="Item not found")
90+
raise HTTPException(
91+
status_code=status.HTTP_404_NOT_FOUND,
92+
detail="Item not found",
93+
)
8594
if not current_user.is_superuser and (item.owner_id != current_user.id):
86-
raise HTTPException(status_code=400, detail="Not enough permissions")
95+
raise HTTPException(
96+
status_code=status.HTTP_400_BAD_REQUEST,
97+
detail="Not enough permissions",
98+
)
8799
update_dict = item_in.model_dump(exclude_unset=True)
88100
item.sqlmodel_update(update_dict)
89101
session.add(item)
@@ -101,9 +113,15 @@ def delete_item(
101113
"""
102114
item = session.get(Item, id)
103115
if not item:
104-
raise HTTPException(status_code=404, detail="Item not found")
116+
raise HTTPException(
117+
status_code=status.HTTP_404_NOT_FOUND,
118+
detail="Item not found",
119+
)
105120
if not current_user.is_superuser and (item.owner_id != current_user.id):
106-
raise HTTPException(status_code=400, detail="Not enough permissions")
121+
raise HTTPException(
122+
status_code=status.HTTP_400_BAD_REQUEST,
123+
detail="Not enough permissions",
124+
)
107125
session.delete(item)
108126
session.commit()
109127
return Message(message="Item deleted successfully")

backend/app/api/routes/login.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22
from typing import Annotated, Any
33

4-
from fastapi import APIRouter, Depends, HTTPException
4+
from fastapi import APIRouter, Depends, HTTPException, status
55
from fastapi.responses import HTMLResponse
66
from fastapi.security import OAuth2PasswordRequestForm
77

@@ -32,9 +32,15 @@ def login_access_token(
3232
session=session, email=form_data.username, password=form_data.password
3333
)
3434
if not user:
35-
raise HTTPException(status_code=400, detail="Incorrect email or password")
35+
raise HTTPException(
36+
status_code=status.HTTP_400_BAD_REQUEST,
37+
detail="Incorrect email or password",
38+
)
3639
elif not user.is_active:
37-
raise HTTPException(status_code=400, detail="Inactive user")
40+
raise HTTPException(
41+
status_code=status.HTTP_400_BAD_REQUEST,
42+
detail="Inactive user",
43+
)
3844
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
3945
return Token(
4046
access_token=security.create_access_token(
@@ -60,7 +66,7 @@ def recover_password(email: str, session: SessionDep) -> Message:
6066

6167
if not user:
6268
raise HTTPException(
63-
status_code=404,
69+
status_code=status.HTTP_404_NOT_FOUND,
6470
detail="The user with this email does not exist in the system.",
6571
)
6672
password_reset_token = generate_password_reset_token(email=email)
@@ -82,15 +88,21 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8288
"""
8389
email = verify_password_reset_token(token=body.token)
8490
if not email:
85-
raise HTTPException(status_code=400, detail="Invalid token")
91+
raise HTTPException(
92+
status_code=status.HTTP_400_BAD_REQUEST,
93+
detail="Invalid token",
94+
)
8695
user = crud.get_user_by_email(session=session, email=email)
8796
if not user:
8897
raise HTTPException(
89-
status_code=404,
98+
status_code=status.HTTP_404_NOT_FOUND,
9099
detail="The user with this email does not exist in the system.",
91100
)
92101
elif not user.is_active:
93-
raise HTTPException(status_code=400, detail="Inactive user")
102+
raise HTTPException(
103+
status_code=status.HTTP_400_BAD_REQUEST,
104+
detail="Inactive user",
105+
)
94106
hashed_password = get_password_hash(password=body.new_password)
95107
user.hashed_password = hashed_password
96108
session.add(user)
@@ -111,7 +123,7 @@ def recover_password_html_content(email: str, session: SessionDep) -> Any:
111123

112124
if not user:
113125
raise HTTPException(
114-
status_code=404,
126+
status_code=status.HTTP_404_NOT_FOUND,
115127
detail="The user with this username does not exist in the system.",
116128
)
117129
password_reset_token = generate_password_reset_token(email=email)

backend/app/api/routes/users.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
from typing import Any
33

4-
from fastapi import APIRouter, Depends, HTTPException
4+
from fastapi import APIRouter, Depends, HTTPException, status
55
from sqlmodel import col, delete, func, select
66

77
from app import crud
@@ -58,7 +58,7 @@ def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
5858
user = crud.get_user_by_email(session=session, email=user_in.email)
5959
if user:
6060
raise HTTPException(
61-
status_code=400,
61+
status_code=status.HTTP_400_BAD_REQUEST,
6262
detail="The user with this email already exists in the system.",
6363
)
6464

@@ -87,7 +87,8 @@ def update_user_me(
8787
existing_user = crud.get_user_by_email(session=session, email=user_in.email)
8888
if existing_user and existing_user.id != current_user.id:
8989
raise HTTPException(
90-
status_code=409, detail="User with this email already exists"
90+
status_code=status.HTTP_409_CONFLICT,
91+
detail="User with this email already exists",
9192
)
9293
user_data = user_in.model_dump(exclude_unset=True)
9394
current_user.sqlmodel_update(user_data)
@@ -105,10 +106,14 @@ def update_password_me(
105106
Update own password.
106107
"""
107108
if not verify_password(body.current_password, current_user.hashed_password):
108-
raise HTTPException(status_code=400, detail="Incorrect password")
109+
raise HTTPException(
110+
status_code=status.HTTP_400_BAD_REQUEST,
111+
detail="Incorrect password",
112+
)
109113
if body.current_password == body.new_password:
110114
raise HTTPException(
111-
status_code=400, detail="New password cannot be the same as the current one"
115+
status_code=status.HTTP_400_BAD_REQUEST,
116+
detail="New password cannot be the same as the current one",
112117
)
113118
hashed_password = get_password_hash(body.new_password)
114119
current_user.hashed_password = hashed_password
@@ -132,7 +137,8 @@ def delete_user_me(session: SessionDep, current_user: CurrentUser) -> Any:
132137
"""
133138
if current_user.is_superuser:
134139
raise HTTPException(
135-
status_code=403, detail="Super users are not allowed to delete themselves"
140+
status_code=status.HTTP_403_FORBIDDEN,
141+
detail="Super users are not allowed to delete themselves",
136142
)
137143
session.delete(current_user)
138144
session.commit()
@@ -147,7 +153,7 @@ def register_user(session: SessionDep, user_in: UserRegister) -> Any:
147153
user = crud.get_user_by_email(session=session, email=user_in.email)
148154
if user:
149155
raise HTTPException(
150-
status_code=400,
156+
status_code=status.HTTP_400_BAD_REQUEST,
151157
detail="The user with this email already exists in the system",
152158
)
153159
user_create = UserCreate.model_validate(user_in)
@@ -167,7 +173,7 @@ def read_user_by_id(
167173
return user
168174
if not current_user.is_superuser:
169175
raise HTTPException(
170-
status_code=403,
176+
status_code=status.HTTP_403_FORBIDDEN,
171177
detail="The user doesn't have enough privileges",
172178
)
173179
return user
@@ -191,14 +197,15 @@ def update_user(
191197
db_user = session.get(User, user_id)
192198
if not db_user:
193199
raise HTTPException(
194-
status_code=404,
200+
status_code=status.HTTP_404_NOT_FOUND,
195201
detail="The user with this id does not exist in the system",
196202
)
197203
if user_in.email:
198204
existing_user = crud.get_user_by_email(session=session, email=user_in.email)
199205
if existing_user and existing_user.id != user_id:
200206
raise HTTPException(
201-
status_code=409, detail="User with this email already exists"
207+
status_code=status.HTTP_409_CONFLICT,
208+
detail="User with this email already exists",
202209
)
203210

204211
db_user = crud.update_user(session=session, db_user=db_user, user_in=user_in)
@@ -214,10 +221,14 @@ def delete_user(
214221
"""
215222
user = session.get(User, user_id)
216223
if not user:
217-
raise HTTPException(status_code=404, detail="User not found")
224+
raise HTTPException(
225+
status_code=status.HTTP_404_NOT_FOUND,
226+
detail="User not found",
227+
)
218228
if user == current_user:
219229
raise HTTPException(
220-
status_code=403, detail="Super users are not allowed to delete themselves"
230+
status_code=status.HTTP_403_FORBIDDEN,
231+
detail="Super users are not allowed to delete themselves",
221232
)
222233
statement = delete(Item).where(col(Item.owner_id) == user_id)
223234
session.exec(statement) # type: ignore

backend/app/api/routes/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import APIRouter, Depends
1+
from fastapi import APIRouter, Depends, status
22
from pydantic.networks import EmailStr
33

44
from app.api.deps import get_current_active_superuser
@@ -11,7 +11,7 @@
1111
@router.post(
1212
"/test-email/",
1313
dependencies=[Depends(get_current_active_superuser)],
14-
status_code=201,
14+
status_code=status.HTTP_201_CREATED,
1515
)
1616
def test_email(email_to: EmailStr) -> Message:
1717
"""

backend/app/tests/api/routes/test_items.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import uuid
22

3+
from fastapi import status
34
from fastapi.testclient import TestClient
45
from sqlmodel import Session
56

@@ -16,7 +17,7 @@ def test_create_item(
1617
headers=superuser_token_headers,
1718
json=data,
1819
)
19-
assert response.status_code == 200
20+
assert response.status_code == status.HTTP_200_OK
2021
content = response.json()
2122
assert content["title"] == data["title"]
2223
assert content["description"] == data["description"]
@@ -32,7 +33,7 @@ def test_read_item(
3233
f"{settings.API_V1_STR}/items/{item.id}",
3334
headers=superuser_token_headers,
3435
)
35-
assert response.status_code == 200
36+
assert response.status_code == status.HTTP_200_OK
3637
content = response.json()
3738
assert content["title"] == item.title
3839
assert content["description"] == item.description
@@ -47,7 +48,7 @@ def test_read_item_not_found(
4748
f"{settings.API_V1_STR}/items/{uuid.uuid4()}",
4849
headers=superuser_token_headers,
4950
)
50-
assert response.status_code == 404
51+
assert response.status_code == status.HTTP_404_NOT_FOUND
5152
content = response.json()
5253
assert content["detail"] == "Item not found"
5354

@@ -60,7 +61,7 @@ def test_read_item_not_enough_permissions(
6061
f"{settings.API_V1_STR}/items/{item.id}",
6162
headers=normal_user_token_headers,
6263
)
63-
assert response.status_code == 400
64+
assert response.status_code == status.HTTP_400_BAD_REQUEST
6465
content = response.json()
6566
assert content["detail"] == "Not enough permissions"
6667

@@ -74,7 +75,7 @@ def test_read_items(
7475
f"{settings.API_V1_STR}/items/",
7576
headers=superuser_token_headers,
7677
)
77-
assert response.status_code == 200
78+
assert response.status_code == status.HTTP_200_OK
7879
content = response.json()
7980
assert len(content["data"]) >= 2
8081

@@ -89,7 +90,7 @@ def test_update_item(
8990
headers=superuser_token_headers,
9091
json=data,
9192
)
92-
assert response.status_code == 200
93+
assert response.status_code == status.HTTP_200_OK
9394
content = response.json()
9495
assert content["title"] == data["title"]
9596
assert content["description"] == data["description"]
@@ -106,7 +107,7 @@ def test_update_item_not_found(
106107
headers=superuser_token_headers,
107108
json=data,
108109
)
109-
assert response.status_code == 404
110+
assert response.status_code == status.HTTP_404_NOT_FOUND
110111
content = response.json()
111112
assert content["detail"] == "Item not found"
112113

@@ -121,7 +122,7 @@ def test_update_item_not_enough_permissions(
121122
headers=normal_user_token_headers,
122123
json=data,
123124
)
124-
assert response.status_code == 400
125+
assert response.status_code == status.HTTP_400_BAD_REQUEST
125126
content = response.json()
126127
assert content["detail"] == "Not enough permissions"
127128

@@ -134,7 +135,7 @@ def test_delete_item(
134135
f"{settings.API_V1_STR}/items/{item.id}",
135136
headers=superuser_token_headers,
136137
)
137-
assert response.status_code == 200
138+
assert response.status_code == status.HTTP_200_OK
138139
content = response.json()
139140
assert content["message"] == "Item deleted successfully"
140141

@@ -146,7 +147,7 @@ def test_delete_item_not_found(
146147
f"{settings.API_V1_STR}/items/{uuid.uuid4()}",
147148
headers=superuser_token_headers,
148149
)
149-
assert response.status_code == 404
150+
assert response.status_code == status.HTTP_404_NOT_FOUND
150151
content = response.json()
151152
assert content["detail"] == "Item not found"
152153

@@ -159,6 +160,6 @@ def test_delete_item_not_enough_permissions(
159160
f"{settings.API_V1_STR}/items/{item.id}",
160161
headers=normal_user_token_headers,
161162
)
162-
assert response.status_code == 400
163+
assert response.status_code == status.HTTP_400_BAD_REQUEST
163164
content = response.json()
164165
assert content["detail"] == "Not enough permissions"

0 commit comments

Comments
 (0)