8
8
from api .db .database import get_db
9
9
from api .utils .settings import settings
10
10
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
11
14
12
15
client = TestClient (app )
13
16
@@ -32,7 +35,21 @@ def create_test_token() -> str:
32
35
return jwt .encode (data , settings .SECRET_KEY , algorithm = settings .ALGORITHM )
33
36
34
37
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 ):
36
53
token = create_test_token ()
37
54
38
55
response = client .post (
@@ -41,16 +58,28 @@ def test_send_notification(db_session_mock):
41
58
"title" : "Test Notification" ,
42
59
"message" : "This is a test notification."
43
60
},
44
- headers = { "Authorization" : f"Bearer { token } " } ,
61
+ headers = headers ,
45
62
)
46
63
47
- print (response .json ()) # Debug print
48
64
assert response .status_code == 201
49
65
assert response .json ()["message" ] == "Notification sent successfully"
50
66
assert response .json ()["data" ]["title" ] == "Test Notification"
51
67
assert response .json ()["data" ]["message" ] == "This is a test notification."
52
68
assert response .json ()["data" ]["status" ] == "unread"
53
69
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
+
54
83
def test_get_notification_by_id (db_session_mock ):
55
84
notification = Notification (
56
85
id = "notification_id" ,
0 commit comments