|
| 1 | +"""Tests for HTTP client error handling — _raise_for_status and _raise_for_ocs_status.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import niquests |
| 7 | +import pytest |
| 8 | + |
| 9 | +from nextcloud_mcp.client import NextcloudError, _raise_for_ocs_status, _raise_for_status |
| 10 | + |
| 11 | + |
| 12 | +def _fake_response(status_code: int, body: dict[str, Any] | str | None = None) -> niquests.Response: |
| 13 | + """Build a minimal niquests.Response with the given status and optional JSON body.""" |
| 14 | + resp = niquests.Response() |
| 15 | + resp.status_code = status_code |
| 16 | + if body is not None: |
| 17 | + text = json.dumps(body) if isinstance(body, dict) else body |
| 18 | + resp._content = text.encode("utf-8") |
| 19 | + resp.headers["Content-Type"] = "application/json" |
| 20 | + return resp |
| 21 | + |
| 22 | + |
| 23 | +def _ocs_error_body(message: str, statuscode: int = 404) -> dict[str, Any]: |
| 24 | + return {"ocs": {"meta": {"status": "failure", "statuscode": statuscode, "message": message}, "data": []}} |
| 25 | + |
| 26 | + |
| 27 | +class TestRaiseForStatus: |
| 28 | + def test_ok_response_does_not_raise(self) -> None: |
| 29 | + _raise_for_status(_fake_response(200)) |
| 30 | + |
| 31 | + def test_404_generic_message(self) -> None: |
| 32 | + with pytest.raises(NextcloudError, match="Not found") as exc_info: |
| 33 | + _raise_for_status(_fake_response(404)) |
| 34 | + assert exc_info.value.status_code == 404 |
| 35 | + |
| 36 | + def test_401_auth_message(self) -> None: |
| 37 | + with pytest.raises(NextcloudError, match="Authentication failed"): |
| 38 | + _raise_for_status(_fake_response(401)) |
| 39 | + |
| 40 | + def test_403_forbidden_message(self) -> None: |
| 41 | + with pytest.raises(NextcloudError, match="Forbidden"): |
| 42 | + _raise_for_status(_fake_response(403)) |
| 43 | + |
| 44 | + def test_409_conflict_message(self) -> None: |
| 45 | + with pytest.raises(NextcloudError, match="Conflict"): |
| 46 | + _raise_for_status(_fake_response(409)) |
| 47 | + |
| 48 | + def test_423_locked_message(self) -> None: |
| 49 | + with pytest.raises(NextcloudError, match="Locked"): |
| 50 | + _raise_for_status(_fake_response(423)) |
| 51 | + |
| 52 | + def test_unmapped_code_shows_http_number(self) -> None: |
| 53 | + with pytest.raises(NextcloudError, match="HTTP 418"): |
| 54 | + _raise_for_status(_fake_response(418)) |
| 55 | + |
| 56 | + def test_context_prefix(self) -> None: |
| 57 | + with pytest.raises(NextcloudError, match=r"Delete 'file\.txt': Not found"): |
| 58 | + _raise_for_status(_fake_response(404), context="Delete 'file.txt'") |
| 59 | + |
| 60 | + |
| 61 | +class TestRaiseForOcsStatus: |
| 62 | + def test_ok_response_does_not_raise(self) -> None: |
| 63 | + _raise_for_ocs_status(_fake_response(200)) |
| 64 | + |
| 65 | + def test_extracts_ocs_meta_message(self) -> None: |
| 66 | + with pytest.raises(NextcloudError, match="User does not exist") as exc_info: |
| 67 | + _raise_for_ocs_status(_fake_response(404, _ocs_error_body("User does not exist"))) |
| 68 | + assert exc_info.value.status_code == 404 |
| 69 | + |
| 70 | + def test_ocs_message_with_context_prefix(self) -> None: |
| 71 | + body = _ocs_error_body("User already exists", 102) |
| 72 | + with pytest.raises(NextcloudError, match=r"OCS POST cloud/users: User already exists") as exc_info: |
| 73 | + _raise_for_ocs_status(_fake_response(400, body), "OCS POST cloud/users") |
| 74 | + assert exc_info.value.status_code == 400 |
| 75 | + |
| 76 | + def test_share_not_found_message(self) -> None: |
| 77 | + body = _ocs_error_body("Wrong share ID, share does not exist") |
| 78 | + with pytest.raises(NextcloudError, match="Wrong share ID, share does not exist"): |
| 79 | + _raise_for_ocs_status(_fake_response(404, body)) |
| 80 | + |
| 81 | + def test_share_wrong_path_message(self) -> None: |
| 82 | + body = _ocs_error_body("Wrong path, file/folder does not exist") |
| 83 | + with pytest.raises(NextcloudError, match="Wrong path"): |
| 84 | + _raise_for_ocs_status(_fake_response(404, body)) |
| 85 | + |
| 86 | + def test_falls_back_when_ocs_message_empty(self) -> None: |
| 87 | + with pytest.raises(NextcloudError, match="HTTP 400"): |
| 88 | + _raise_for_ocs_status(_fake_response(400, _ocs_error_body("", 400))) |
| 89 | + |
| 90 | + def test_falls_back_when_body_not_json(self) -> None: |
| 91 | + with pytest.raises(NextcloudError, match="Not found"): |
| 92 | + _raise_for_ocs_status(_fake_response(404, "<html>404</html>")) |
| 93 | + |
| 94 | + def test_falls_back_when_body_missing_ocs_key(self) -> None: |
| 95 | + with pytest.raises(NextcloudError, match="Not found"): |
| 96 | + _raise_for_ocs_status(_fake_response(404, {"error": "something"})) |
| 97 | + |
| 98 | + def test_falls_back_when_body_missing_meta(self) -> None: |
| 99 | + with pytest.raises(NextcloudError, match="HTTP 400"): |
| 100 | + _raise_for_ocs_status(_fake_response(400, {"ocs": {"data": []}})) |
| 101 | + |
| 102 | + def test_falls_back_when_no_body(self) -> None: |
| 103 | + with pytest.raises(NextcloudError, match="HTTP 500"): |
| 104 | + _raise_for_ocs_status(_fake_response(500)) |
| 105 | + |
| 106 | + def test_preserves_status_code(self) -> None: |
| 107 | + with pytest.raises(NextcloudError) as exc_info: |
| 108 | + _raise_for_ocs_status(_fake_response(400, _ocs_error_body("User already exists", 102))) |
| 109 | + assert exc_info.value.status_code == 400 |
0 commit comments