Skip to content

Commit 7949303

Browse files
Changing all status codes from raw numbers to using fastapi.status
1 parent 7a6107f commit 7949303

File tree

8 files changed

+108
-70
lines changed

8 files changed

+108
-70
lines changed

backend/app/api/deps.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ 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, detail="User not found"
45+
)
4446
if not user.is_active:
45-
raise HTTPException(status_code=400, detail="Inactive user")
47+
raise HTTPException(
48+
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
49+
)
4650
return user
4751

4852

@@ -52,6 +56,7 @@ def get_current_user(session: SessionDep, token: TokenDep) -> User:
5256
def get_current_active_superuser(current_user: CurrentUser) -> User:
5357
if not current_user.is_superuser:
5458
raise HTTPException(
55-
status_code=403, detail="The user doesn't have enough privileges"
59+
status_code=status.HTTP_403_FORBIDDEN,
60+
detail="The user doesn't have enough privileges",
5661
)
5762
return current_user

backend/app/api/routes/items.py

Lines changed: 19 additions & 7 deletions
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,13 @@ 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, detail="Item not found"
53+
)
5254
if not current_user.is_superuser and (item.owner_id != current_user.id):
53-
raise HTTPException(status_code=400, detail="Not enough permissions")
55+
raise HTTPException(
56+
status_code=status.HTTP_400_BAD_REQUEST, detail="Not enough permissions"
57+
)
5458
return item
5559

5660

@@ -81,9 +85,13 @@ def update_item(
8185
"""
8286
item = session.get(Item, id)
8387
if not item:
84-
raise HTTPException(status_code=404, detail="Item not found")
88+
raise HTTPException(
89+
status_code=status.HTTP_404_NOT_FOUND, detail="Item not found"
90+
)
8591
if not current_user.is_superuser and (item.owner_id != current_user.id):
86-
raise HTTPException(status_code=400, detail="Not enough permissions")
92+
raise HTTPException(
93+
status_code=status.HTTP_400_BAD_REQUEST, detail="Not enough permissions"
94+
)
8795
update_dict = item_in.model_dump(exclude_unset=True)
8896
item.sqlmodel_update(update_dict)
8997
session.add(item)
@@ -101,9 +109,13 @@ def delete_item(
101109
"""
102110
item = session.get(Item, id)
103111
if not item:
104-
raise HTTPException(status_code=404, detail="Item not found")
112+
raise HTTPException(
113+
status_code=status.HTTP_404_NOT_FOUND, detail="Item not found"
114+
)
105115
if not current_user.is_superuser and (item.owner_id != current_user.id):
106-
raise HTTPException(status_code=400, detail="Not enough permissions")
116+
raise HTTPException(
117+
status_code=status.HTTP_400_BAD_REQUEST, detail="Not enough permissions"
118+
)
107119
session.delete(item)
108120
session.commit()
109121
return Message(message="Item deleted successfully")

backend/app/api/routes/login.py

Lines changed: 17 additions & 8 deletions
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,14 @@ 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, detail="Inactive user"
42+
)
3843
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
3944
return Token(
4045
access_token=security.create_access_token(
@@ -60,7 +65,7 @@ def recover_password(email: str, session: SessionDep) -> Message:
6065

6166
if not user:
6267
raise HTTPException(
63-
status_code=404,
68+
status_code=status.HTTP_404_NOT_FOUND,
6469
detail="The user with this email does not exist in the system.",
6570
)
6671
password_reset_token = generate_password_reset_token(email=email)
@@ -82,15 +87,19 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8287
"""
8388
email = verify_password_reset_token(token=body.token)
8489
if not email:
85-
raise HTTPException(status_code=400, detail="Invalid token")
90+
raise HTTPException(
91+
status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid token"
92+
)
8693
user = crud.get_user_by_email(session=session, email=email)
8794
if not user:
8895
raise HTTPException(
89-
status_code=404,
96+
status_code=status.HTTP_404_NOT_FOUND,
9097
detail="The user with this email does not exist in the system.",
9198
)
9299
elif not user.is_active:
93-
raise HTTPException(status_code=400, detail="Inactive user")
100+
raise HTTPException(
101+
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
102+
)
94103
hashed_password = get_password_hash(password=body.new_password)
95104
user.hashed_password = hashed_password
96105
session.add(user)
@@ -111,7 +120,7 @@ def recover_password_html_content(email: str, session: SessionDep) -> Any:
111120

112121
if not user:
113122
raise HTTPException(
114-
status_code=404,
123+
status_code=status.HTTP_404_NOT_FOUND,
115124
detail="The user with this username does not exist in the system.",
116125
)
117126
password_reset_token = generate_password_reset_token(email=email)

backend/app/api/routes/users.py

Lines changed: 21 additions & 12 deletions
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,13 @@ 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, detail="Incorrect password"
111+
)
109112
if body.current_password == body.new_password:
110113
raise HTTPException(
111-
status_code=400, detail="New password cannot be the same as the current one"
114+
status_code=status.HTTP_400_BAD_REQUEST,
115+
detail="New password cannot be the same as the current one",
112116
)
113117
hashed_password = get_password_hash(body.new_password)
114118
current_user.hashed_password = hashed_password
@@ -132,7 +136,8 @@ def delete_user_me(session: SessionDep, current_user: CurrentUser) -> Any:
132136
"""
133137
if current_user.is_superuser:
134138
raise HTTPException(
135-
status_code=403, detail="Super users are not allowed to delete themselves"
139+
status_code=status.HTTP_403_FORBIDDEN,
140+
detail="Super users are not allowed to delete themselves",
136141
)
137142
statement = delete(Item).where(col(Item.owner_id) == current_user.id)
138143
session.exec(statement) # type: ignore
@@ -149,7 +154,7 @@ def register_user(session: SessionDep, user_in: UserRegister) -> Any:
149154
user = crud.get_user_by_email(session=session, email=user_in.email)
150155
if user:
151156
raise HTTPException(
152-
status_code=400,
157+
status_code=status.HTTP_400_BAD_REQUEST,
153158
detail="The user with this email already exists in the system",
154159
)
155160
user_create = UserCreate.model_validate(user_in)
@@ -169,7 +174,7 @@ def read_user_by_id(
169174
return user
170175
if not current_user.is_superuser:
171176
raise HTTPException(
172-
status_code=403,
177+
status_code=status.HTTP_403_FORBIDDEN,
173178
detail="The user doesn't have enough privileges",
174179
)
175180
return user
@@ -193,14 +198,15 @@ def update_user(
193198
db_user = session.get(User, user_id)
194199
if not db_user:
195200
raise HTTPException(
196-
status_code=404,
201+
status_code=status.HTTP_404_NOT_FOUND,
197202
detail="The user with this id does not exist in the system",
198203
)
199204
if user_in.email:
200205
existing_user = crud.get_user_by_email(session=session, email=user_in.email)
201206
if existing_user and existing_user.id != user_id:
202207
raise HTTPException(
203-
status_code=409, detail="User with this email already exists"
208+
status_code=status.HTTP_409_CONFLICT,
209+
detail="User with this email already exists",
204210
)
205211

206212
db_user = crud.update_user(session=session, db_user=db_user, user_in=user_in)
@@ -216,10 +222,13 @@ def delete_user(
216222
"""
217223
user = session.get(User, user_id)
218224
if not user:
219-
raise HTTPException(status_code=404, detail="User not found")
225+
raise HTTPException(
226+
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
227+
)
220228
if user == current_user:
221229
raise HTTPException(
222-
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",
223232
)
224233
statement = delete(Item).where(col(Item.owner_id) == user_id)
225234
session.exec(statement) # type: ignore

backend/app/api/routes/utils.py

Lines changed: 2 additions & 2 deletions
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

Lines changed: 12 additions & 11 deletions
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)