Skip to content

Commit 3407c89

Browse files
authored
Merge pull request #1190 from ObiFaith/fix/send-notification
fix: secure send_notification endpoint and ensure correct user association
2 parents 07d2b5d + e7dccd6 commit 3407c89

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

api/v1/routes/notification.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
status_code=status.HTTP_201_CREATED,
2121
)
2222
def send_notification(
23-
notification_data: NotificationCreate, db: Session = Depends(get_db)
23+
notification_data: NotificationCreate,
24+
user: User = Depends(user_service.get_current_user),
25+
db: Session = Depends(get_db)
2426
):
2527
notification = notification_service.send_notification(
2628
title=notification_data.title,
2729
message=notification_data.message,
30+
user=user,
2831
db=db,
2932
)
3033
return success_response(

api/v1/services/notification.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
class NotificationService(Service):
1111

1212
def send_notification(
13-
self, title: str, message: str, db: Session = Depends(get_db)
13+
self, title: str, message: str, user: User, db: Session = Depends(get_db)
1414
):
1515
"""Function to send a notification"""
16-
new_notification = Notification(title=title, message=message, status="unread")
16+
new_notification = Notification(user_id=user.id, title=title, message=message, status="unread")
1717
db.add(new_notification)
1818
db.commit()
1919
db.refresh(new_notification)

tests/v1/notification/test_notifications_service.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from api.db.database import get_db
99
from api.utils.settings import settings
1010
import jwt
11+
from uuid_extensions import uuid7
12+
from api.v1.models.user import User
13+
from api.v1.services.user import user_service
1114

1215
client = TestClient(app)
1316

@@ -32,7 +35,21 @@ def create_test_token() -> str:
3235
return jwt.encode(data, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
3336

3437
def test_send_notification(db_session_mock):
35-
with patch("api.utils.dependencies.get_current_user", return_value=None):
38+
user_id = uuid7()
39+
user = User(
40+
id=user_id,
41+
42+
password=user_service.hash_password("Testpassword@123"),
43+
first_name="Test",
44+
last_name="User",
45+
is_active=False,
46+
created_at=datetime.now(timezone.utc),
47+
updated_at=datetime.now(timezone.utc),
48+
)
49+
access_token = user_service.create_access_token(str(user_id))
50+
headers = {"authorization": f"Bearer {access_token}"}
51+
52+
with patch("api.utils.dependencies.get_current_user", return_value=user):
3653
token = create_test_token()
3754

3855
response = client.post(
@@ -41,16 +58,28 @@ def test_send_notification(db_session_mock):
4158
"title": "Test Notification",
4259
"message": "This is a test notification."
4360
},
44-
headers={"Authorization": f"Bearer {token}"},
61+
headers=headers,
4562
)
4663

47-
print(response.json()) # Debug print
4864
assert response.status_code == 201
4965
assert response.json()["message"] == "Notification sent successfully"
5066
assert response.json()["data"]["title"] == "Test Notification"
5167
assert response.json()["data"]["message"] == "This is a test notification."
5268
assert response.json()["data"]["status"] == "unread"
5369

70+
def test_send_notification_unauthenticated_user(db_session_mock):
71+
with patch("api.utils.dependencies.get_current_user", return_value=None):
72+
response = client.post(
73+
"/api/v1/notifications/send",
74+
json={
75+
"title": "Test Notification",
76+
"message": "This is a test notification."
77+
},
78+
)
79+
80+
assert response.status_code == 401
81+
assert response.json()["message"] == "Not authenticated"
82+
5483
def test_get_notification_by_id(db_session_mock):
5584
notification = Notification(
5685
id="notification_id",

0 commit comments

Comments
 (0)