Skip to content

Commit 50c6ea9

Browse files
committed
Remove unreachable code path, change 200 to 204 for delete, more tests
1 parent 867d3d3 commit 50c6ea9

File tree

4 files changed

+176
-18
lines changed

4 files changed

+176
-18
lines changed

app/backend/chat_history/cosmosdb.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ async def get_chat_history_session(auth_claims: Dict[str, Any], item_id: str):
113113
if not container:
114114
return jsonify({"error": "Chat history not enabled"}), 400
115115

116-
if not item_id:
117-
return jsonify({"error": "Invalid item ID specifier"}), 400
118-
119116
entra_oid = auth_claims.get("oid")
120117
if not entra_oid:
121118
return jsonify({"error": "User OID not found"}), 401
@@ -148,16 +145,13 @@ async def delete_chat_history_session(auth_claims: Dict[str, Any], item_id: str)
148145
if not container:
149146
return jsonify({"error": "Chat history not enabled"}), 400
150147

151-
if not item_id:
152-
return jsonify({"error": "Invalid path"}), 400
153-
154148
entra_oid = auth_claims.get("oid")
155149
if not entra_oid:
156150
return jsonify({"error": "User OID not found"}), 401
157151

158152
try:
159153
await container.delete_item(item=item_id, partition_key=entra_oid)
160-
return jsonify({}), 200
154+
return jsonify({}), 204
161155
except Exception as error:
162156
return error_response(error, f"/chat_history/items/{item_id}")
163157

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"answers": [
3+
[
4+
"This is a test message"
5+
]
6+
],
7+
"entra_oid": "OID_X",
8+
"id": "123",
9+
"timestamp": 123456789,
10+
"title": "This is a test message"
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"continuation_token": null,
3+
"items": []
4+
}

tests/test_cosmosdb.py

Lines changed: 160 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77

88

99
class MockCosmosDBResultsIterator:
10-
def __init__(self):
11-
self.data = [
12-
[
13-
{
14-
"id": "123",
15-
"entra_oid": "OID_X",
16-
"title": "This is a test message",
17-
"timestamp": 123456789,
18-
"answers": [["This is a test message"]],
19-
}
10+
def __init__(self, empty=False):
11+
if empty:
12+
self.data = []
13+
else:
14+
self.data = [
15+
[
16+
{
17+
"id": "123",
18+
"entra_oid": "OID_X",
19+
"title": "This is a test message",
20+
"timestamp": 123456789,
21+
"answers": [["This is a test message"]],
22+
}
23+
]
2024
]
21-
]
2225

2326
def __aiter__(self):
2427
return self
@@ -141,6 +144,24 @@ def mock_query_items(container_proxy, query, **kwargs):
141144
snapshot.assert_match(json.dumps(result, indent=4), "result.json")
142145

143146

147+
@pytest.mark.asyncio
148+
async def test_chathistory_query_continuation(auth_public_documents_client, monkeypatch, snapshot):
149+
150+
def mock_query_items(container_proxy, query, **kwargs):
151+
return MockCosmosDBResultsIterator(empty=True)
152+
153+
monkeypatch.setattr(ContainerProxy, "query_items", mock_query_items)
154+
155+
response = await auth_public_documents_client.post(
156+
"/chat_history/items",
157+
headers={"Authorization": "Bearer MockToken"},
158+
json={"count": 20},
159+
)
160+
assert response.status_code == 200
161+
result = await response.get_json()
162+
snapshot.assert_match(json.dumps(result, indent=4), "result.json")
163+
164+
144165
@pytest.mark.asyncio
145166
async def test_chathistory_query_error_disabled(client, monkeypatch):
146167

@@ -198,3 +219,131 @@ def mock_query_items(container_proxy, query, **kwargs):
198219
assert (await response.get_json()) == {
199220
"error": "The app encountered an error processing your request.\nIf you are an administrator of the app, view the full error in the logs. See aka.ms/appservice-logs for more information.\nError type: <class 'Exception'>\n"
200221
}
222+
223+
224+
# Tests for getting an individual chat history item
225+
@pytest.mark.asyncio
226+
async def test_chathistory_getitem(auth_public_documents_client, monkeypatch, snapshot):
227+
228+
async def mock_read_item(container_proxy, item, partition_key, **kwargs):
229+
return {
230+
"id": "123",
231+
"entra_oid": "OID_X",
232+
"title": "This is a test message",
233+
"timestamp": 123456789,
234+
"answers": [["This is a test message"]],
235+
}
236+
237+
monkeypatch.setattr(ContainerProxy, "read_item", mock_read_item)
238+
239+
response = await auth_public_documents_client.get(
240+
"/chat_history/items/123",
241+
headers={"Authorization": "Bearer MockToken"},
242+
)
243+
assert response.status_code == 200
244+
result = await response.get_json()
245+
snapshot.assert_match(json.dumps(result, indent=4), "result.json")
246+
247+
248+
# Error handling tests for getting an individual chat history item
249+
@pytest.mark.asyncio
250+
async def test_chathistory_getitem_error_disabled(client, monkeypatch):
251+
252+
response = await client.get(
253+
"/chat_history/items/123",
254+
headers={"Authorization": "BearerMockToken"},
255+
)
256+
assert response.status_code == 400
257+
258+
259+
@pytest.mark.asyncio
260+
async def test_chathistory_getitem_error_container(auth_public_documents_client, monkeypatch):
261+
auth_public_documents_client.app.config["cosmos_history_container"] = None
262+
response = await auth_public_documents_client.get(
263+
"/chat_history/items/123",
264+
headers={"Authorization": "BearerMockToken"},
265+
)
266+
assert response.status_code == 400
267+
268+
269+
@pytest.mark.asyncio
270+
async def test_chathistory_getitem_error_entra(auth_public_documents_client, monkeypatch):
271+
response = await auth_public_documents_client.get(
272+
"/chat_history/items/123",
273+
)
274+
assert response.status_code == 401
275+
276+
277+
@pytest.mark.asyncio
278+
async def test_chathistory_getitem_error_runtime(auth_public_documents_client, monkeypatch):
279+
280+
async def mock_read_item(container_proxy, item, partition_key, **kwargs):
281+
raise Exception("Test Exception")
282+
283+
monkeypatch.setattr(ContainerProxy, "read_item", mock_read_item)
284+
285+
response = await auth_public_documents_client.get(
286+
"/chat_history/items/123",
287+
headers={"Authorization": "Bearer MockToken"},
288+
)
289+
assert response.status_code == 500
290+
291+
292+
# Tests for deleting an individual chat history item
293+
@pytest.mark.asyncio
294+
async def test_chathistory_deleteitem(auth_public_documents_client, monkeypatch):
295+
296+
async def mock_delete_item(container_proxy, item, partition_key, **kwargs):
297+
assert item == "123"
298+
assert partition_key == "OID_X"
299+
300+
monkeypatch.setattr(ContainerProxy, "delete_item", mock_delete_item)
301+
302+
response = await auth_public_documents_client.delete(
303+
"/chat_history/items/123",
304+
headers={"Authorization": "Bearer MockToken"},
305+
)
306+
assert response.status_code == 204
307+
308+
309+
@pytest.mark.asyncio
310+
async def test_chathistory_deleteitem_error_disabled(client, monkeypatch):
311+
312+
response = await client.delete(
313+
"/chat_history/items/123",
314+
headers={"Authorization": "Bearer MockToken"},
315+
)
316+
assert response.status_code == 400
317+
318+
319+
@pytest.mark.asyncio
320+
async def test_chathistory_deleteitem_error_container(auth_public_documents_client, monkeypatch):
321+
auth_public_documents_client.app.config["cosmos_history_container"] = None
322+
response = await auth_public_documents_client.delete(
323+
"/chat_history/items/123",
324+
headers={"Authorization": "Bearer MockToken"},
325+
)
326+
assert response.status_code == 400
327+
328+
329+
@pytest.mark.asyncio
330+
async def test_chathistory_deleteitem_error_entra(auth_public_documents_client, monkeypatch):
331+
response = await auth_public_documents_client.delete(
332+
"/chat_history/items/123",
333+
)
334+
assert response.status_code == 401
335+
336+
337+
@pytest.mark.asyncio
338+
async def test_chathistory_deleteitem_error_runtime(auth_public_documents_client, monkeypatch):
339+
340+
async def mock_delete_item(container_proxy, item, partition_key, **kwargs):
341+
raise Exception("Test Exception")
342+
343+
monkeypatch.setattr(ContainerProxy, "delete_item", mock_delete_item)
344+
345+
response = await auth_public_documents_client.delete(
346+
"/chat_history/items/123",
347+
headers={"Authorization": "Bearer MockToken"},
348+
)
349+
assert response.status_code == 500

0 commit comments

Comments
 (0)