@@ -53,6 +53,23 @@ async def test_update_conversation(mock_track, mock_update, mock_auth, client, h
5353 assert res .json ()["data" ]["title" ] == "New Title"
5454
5555
56+
57+ @pytest .mark .asyncio
58+ @patch ("auth.auth_utils.get_authenticated_user_details" )
59+ @patch ("services.history_service.HistoryService.update_conversation" , new_callable = AsyncMock )
60+ async def test_update_conversation_missing_id (mock_update , mock_auth , client , headers , mock_user ):
61+ mock_auth .return_value = mock_user
62+ res = await client .post (
63+ "/update" ,
64+ json = {}, # no conversation_id
65+ headers = {** headers , "Content-Type" : "application/json" }
66+ )
67+ # Since the route handler swallows HTTPException and returns 500
68+ assert res .status_code == 500
69+ assert res .json ()["error" ] == "An internal error has occurred!"
70+ mock_update .assert_not_awaited ()
71+
72+
5673@pytest .mark .asyncio
5774@patch ("auth.auth_utils.get_authenticated_user_details" )
5875@patch ("services.history_service.HistoryService.update_message_feedback" , new_callable = AsyncMock )
@@ -64,6 +81,38 @@ async def test_update_message_feedback(mock_track, mock_update, mock_auth, clien
6481 res = await client .post ("/message_feedback" , json = {"message_id" : "m1" , "message_feedback" : "positive" }, headers = headers )
6582 assert res .status_code == 200
6683
84+ @pytest .mark .asyncio
85+ @patch ("auth.auth_utils.get_authenticated_user_details" )
86+ async def test_update_message_feedback_missing_message_id (mock_auth , client , headers , mock_user ):
87+ mock_auth .return_value = mock_user
88+ res = await client .post ("/message_feedback" , json = {"message_feedback" : "positive" }, headers = headers )
89+ assert res .status_code == 500
90+ assert res .json ()["error" ] == "An internal error has occurred!"
91+
92+
93+ @pytest .mark .asyncio
94+ @patch ("auth.auth_utils.get_authenticated_user_details" )
95+ async def test_update_message_feedback_missing_feedback (mock_auth , client , headers , mock_user ):
96+ mock_auth .return_value = mock_user
97+ res = await client .post ("/message_feedback" , json = {"message_id" : "m1" }, headers = headers )
98+ assert res .status_code == 500
99+ assert res .json ()["error" ] == "An internal error has occurred!"
100+
101+
102+ @pytest .mark .asyncio
103+ @patch ("auth.auth_utils.get_authenticated_user_details" )
104+ @patch ("services.history_service.HistoryService.update_message_feedback" , new_callable = AsyncMock )
105+ async def test_update_message_feedback_not_found (mock_update , mock_auth , client , headers , mock_user ):
106+ mock_auth .return_value = mock_user
107+ mock_update .return_value = False
108+ res = await client .post (
109+ "/message_feedback" ,
110+ json = {"message_id" : "m1" , "message_feedback" : "positive" },
111+ headers = headers
112+ )
113+ assert res .status_code == 500
114+ assert res .json ()["error" ] == "An internal error has occurred!"
115+
67116
68117@pytest .mark .asyncio
69118@patch ("auth.auth_utils.get_authenticated_user_details" )
@@ -82,6 +131,21 @@ async def test_delete_conversation(mock_track, mock_delete, mock_auth, client, h
82131 assert res .status_code == 200
83132
84133
134+ @pytest .mark .asyncio
135+ @patch ("auth.auth_utils.get_authenticated_user_details" )
136+ @patch ("services.history_service.HistoryService.delete_conversation" , new_callable = AsyncMock )
137+ async def test_delete_conversation_not_found (mock_delete , mock_auth , client , headers , mock_user ):
138+ mock_auth .return_value = mock_user
139+ mock_delete .return_value = False
140+ res = await client .request (
141+ "DELETE" , "/delete" ,
142+ content = json .dumps ({"conversation_id" : "c1" }),
143+ headers = {** headers , "Content-Type" : "application/json" }
144+ )
145+ assert res .status_code == 500
146+ assert res .json ()["error" ] == "An internal error has occurred!"
147+
148+
85149@pytest .mark .asyncio
86150@patch ("auth.auth_utils.get_authenticated_user_details" )
87151@patch ("services.history_service.HistoryService.get_conversations" , new_callable = AsyncMock )
@@ -95,6 +159,20 @@ async def test_list_conversations(mock_track, mock_get, mock_auth, client, heade
95159 assert isinstance (res .json (), list )
96160
97161
162+ @pytest .mark .asyncio
163+ @patch ("auth.auth_utils.get_authenticated_user_details" )
164+ @patch ("services.history_service.HistoryService.get_conversations" , new_callable = AsyncMock )
165+ @patch ("common.logging.event_utils.track_event_if_configured" )
166+ async def test_list_conversations_not_found (mock_track , mock_get , mock_auth , client , headers , mock_user ):
167+ mock_auth .return_value = mock_user
168+ mock_get .return_value = None
169+
170+ res = await client .get ("/list" , headers = headers )
171+ assert res .status_code == 404
172+ assert "error" in res .json ()
173+
174+
175+
98176@pytest .mark .asyncio
99177@patch ("auth.auth_utils.get_authenticated_user_details" )
100178@patch ("services.history_service.HistoryService.get_conversation_messages" , new_callable = AsyncMock )
@@ -108,6 +186,20 @@ async def test_get_conversation_messages(mock_track, mock_get, mock_auth, client
108186 assert "messages" in res .json ()
109187
110188
189+ @pytest .mark .asyncio
190+ @patch ("auth.auth_utils.get_authenticated_user_details" )
191+ @patch ("services.history_service.HistoryService.get_conversation_messages" , new_callable = AsyncMock )
192+ @patch ("common.logging.event_utils.track_event_if_configured" )
193+ async def test_get_conversation_messages_not_found (mock_track , mock_get , mock_auth , client , headers , mock_user ):
194+ mock_auth .return_value = mock_user
195+ mock_get .return_value = None
196+
197+ res = await client .post ("/read" , json = {"conversation_id" : "c1" }, headers = headers )
198+ assert res .status_code == 500
199+ assert res .json ()["error" ] == "An internal error has occurred!"
200+
201+
202+
111203@pytest .mark .asyncio
112204@patch ("auth.auth_utils.get_authenticated_user_details" )
113205@patch ("services.history_service.HistoryService.rename_conversation" , new_callable = AsyncMock )
@@ -121,6 +213,25 @@ async def test_rename_conversation(mock_track, mock_rename, mock_auth, client, h
121213 assert res .json ()["title" ] == "new name"
122214
123215
216+ @pytest .mark .asyncio
217+ @patch ("auth.auth_utils.get_authenticated_user_details" )
218+ async def test_rename_conversation_missing_conversation_id (mock_auth , client , headers , mock_user ):
219+ mock_auth .return_value = mock_user
220+ res = await client .post ("/rename" , json = {"title" : "new name" }, headers = headers )
221+ assert res .status_code == 500
222+ assert res .json ()["error" ] == "An internal error has occurred!"
223+
224+
225+ @pytest .mark .asyncio
226+ @patch ("auth.auth_utils.get_authenticated_user_details" )
227+ async def test_rename_conversation_missing_title (mock_auth , client , headers , mock_user ):
228+ mock_auth .return_value = mock_user
229+ res = await client .post ("/rename" , json = {"conversation_id" : "c1" }, headers = headers )
230+ assert res .status_code == 500
231+ assert res .json ()["error" ] == "An internal error has occurred!"
232+
233+
234+
124235@pytest .mark .asyncio
125236@patch ("auth.auth_utils.get_authenticated_user_details" )
126237@patch ("services.history_service.HistoryService.get_conversations" , new_callable = AsyncMock )
@@ -197,4 +308,11 @@ async def test_ensure_cosmos_unknown_error(mock_track, mock_ensure, client):
197308 mock_ensure .side_effect = Exception ("Something went wrong" )
198309 res = await client .get ("/history/ensure" )
199310 assert res .status_code == 500
200- assert res .json ()["error" ] == "CosmosDB is not configured or not working"
311+ assert res .json ()["error" ] == "CosmosDB is not configured or not working"
312+
313+
314+ @pytest .mark .asyncio
315+ @patch ("auth.auth_utils.get_authenticated_user_details" , side_effect = Exception ("auth error" ))
316+ async def test_add_conversation_exception (mock_auth , client , headers ):
317+ res = await client .post ("/generate" , json = {"message" : "hi" }, headers = headers )
318+ assert res .status_code == 500
0 commit comments