From 17950ecff707e29d8bc3581fb8b950a2e372c6f9 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:16:36 +0100 Subject: [PATCH] all endpoints tested --- src/endpoints/sheet.py | 2 +- src/tests/endpoints/test_sheet.py | 46 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/tests/endpoints/test_sheet.py diff --git a/src/endpoints/sheet.py b/src/endpoints/sheet.py index 4db344c..74cee62 100644 --- a/src/endpoints/sheet.py +++ b/src/endpoints/sheet.py @@ -20,4 +20,4 @@ def archive_sheet(sheet:schemas.SubmitSheet, email = Depends(get_token_or_user_a if not sheet.sheet_name and not sheet.sheet_id: raise HTTPException(status_code=422, detail=f"sheet name or id is required") task = create_sheet_task.delay(sheet.model_dump_json()) - return JSONResponse({"id": task.id}) \ No newline at end of file + return JSONResponse({"id": task.id}, status_code=201) \ No newline at end of file diff --git a/src/tests/endpoints/test_sheet.py b/src/tests/endpoints/test_sheet.py new file mode 100644 index 0000000..1c0838f --- /dev/null +++ b/src/tests/endpoints/test_sheet.py @@ -0,0 +1,46 @@ +import json +from unittest.mock import patch + +from db.schemas import TaskResult + + +def test_sheet_no_auth(client, test_no_auth): + test_no_auth(client.post, "/sheet/archive") + + +@patch("worker.create_sheet_task.delay", return_value=TaskResult(id="123-456-789", status="PENDING", result="")) +def test_sheet_rick(m1, client_with_auth): + + response = client_with_auth.post("/sheet/archive", json={"sheet_id": "123-sheet-id"}) + assert response.status_code == 201 + assert response.json() == {'id': '123-456-789'} + + m1.assert_called_once() + called_val = m1.call_args.args[0] + assert json.loads(called_val) == {"sheet_id": "123-sheet-id", "sheet_name": None, "public": False, "author_id": "rick@example.com", "group_id": None, "tags": [], "columns": {}, "header": 1} + + +def test_sheet_missing_sheet_data(client_with_auth): + r = client_with_auth.post("/sheet/archive", json={}) + assert r.status_code == 422 + assert r.json() == {"detail": "sheet name or id is required"} + + +@patch("worker.create_sheet_task.delay", return_value=TaskResult(id="123-API-789", status="PENDING", result="")) +def test_sheet_api(m1, client): + + response = client.post("/sheet/archive", json={"sheet_name": "456-sheet_name-id"}, headers={"Authorization": "Bearer this_is_the_test_api_token"}) + assert response.status_code == 201 + assert response.json() == {'id': '123-API-789'} + + m1.assert_called_once() + called_val = m1.call_args.args[0] + assert json.loads(called_val) == {"sheet_name": "456-sheet_name-id", "sheet_id": None, "public": False, "author_id": "api-endpoint", "group_id": None, "tags": [], "columns": {}, "header": 1} + + response = client.post("/sheet/archive", json={"sheet_id": "456-sheet-id", "author_id": "custom-author"}, headers={"Authorization": "Bearer this_is_the_test_api_token"}) + assert response.status_code == 201 + assert response.json() == {'id': '123-API-789'} + + assert m1.call_count == 2 + called_val = m1.call_args.args[0] + assert json.loads(called_val) == {"sheet_id": "456-sheet-id", "sheet_name": None, "public": False, "author_id": "custom-author", "group_id": None, "tags": [], "columns": {}, "header": 1}