| 1 |
"""Tests for the Error endpoint module.""" |
| 2 |
|
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from asusrouter.const import HTTPStatus |
| 8 |
from asusrouter.error import ( |
| 9 |
AsusRouterAccessError, |
| 10 |
AsusRouterLogoutError, |
| 11 |
AsusRouterRequestFormatError, |
| 12 |
) |
| 13 |
from asusrouter.modules.endpoint import error as endpoint_error |
| 14 |
from asusrouter.modules.endpoint.error import AccessError, handle_access_error |
| 15 |
|
| 16 |
|
| 17 |
def test_handle_access_error_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 18 |
"""Test handling of access errors.""" |
| 19 |
|
| 20 |
monkeypatch.setattr( |
| 21 |
endpoint_error, |
| 22 |
"read_json_content", |
| 23 |
Mock(return_value={"error_status": HTTPStatus.OK}), |
| 24 |
) |
| 25 |
|
| 26 |
# Should not raise |
| 27 |
handle_access_error( |
| 28 |
endpoint=None, # type: ignore[arg-type] |
| 29 |
status=None, |
| 30 |
headers=None, |
| 31 |
content=None, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize( |
| 36 |
"bad_status", |
| 37 |
[HTTPStatus.JSON_BAD_FORMAT, HTTPStatus.JSON_BAD_REQUEST], |
| 38 |
) |
| 39 |
def test_handle_access_error_json_format_raises( |
| 40 |
bad_status: HTTPStatus, monkeypatch: pytest.MonkeyPatch |
| 41 |
) -> None: |
| 42 |
"""Test handling of JSON format errors.""" |
| 43 |
|
| 44 |
monkeypatch.setattr( |
| 45 |
endpoint_error, |
| 46 |
"read_json_content", |
| 47 |
Mock(return_value={"error_status": bad_status}), |
| 48 |
) |
| 49 |
|
| 50 |
with pytest.raises(AsusRouterRequestFormatError): |
| 51 |
handle_access_error( |
| 52 |
endpoint=None, # type: ignore[arg-type] |
| 53 |
status=None, |
| 54 |
headers=None, |
| 55 |
content=None, |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def test_handle_access_error_logout_raises( |
| 60 |
monkeypatch: pytest.MonkeyPatch, |
| 61 |
) -> None: |
| 62 |
"""Test handling of logout errors.""" |
| 63 |
|
| 64 |
monkeypatch.setattr( |
| 65 |
endpoint_error, |
| 66 |
"read_json_content", |
| 67 |
Mock(return_value={"error_status": AccessError.LOGOUT}), |
| 68 |
) |
| 69 |
|
| 70 |
with pytest.raises(AsusRouterLogoutError): |
| 71 |
handle_access_error( |
| 72 |
endpoint=None, # type: ignore[arg-type] |
| 73 |
status=None, |
| 74 |
headers=None, |
| 75 |
content=None, |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
def test_handle_access_error_try_again_includes_timeout( |
| 80 |
monkeypatch: pytest.MonkeyPatch, |
| 81 |
) -> None: |
| 82 |
"""Test handling of try again errors.""" |
| 83 |
|
| 84 |
timeout = 42 |
| 85 |
|
| 86 |
monkeypatch.setattr( |
| 87 |
endpoint_error, |
| 88 |
"read_json_content", |
| 89 |
Mock( |
| 90 |
return_value={ |
| 91 |
"error_status": AccessError.TRY_AGAIN, |
| 92 |
"remaining_lock_time": str(timeout), |
| 93 |
} |
| 94 |
), |
| 95 |
) |
| 96 |
|
| 97 |
with pytest.raises(AsusRouterAccessError) as exinfo: |
| 98 |
handle_access_error( |
| 99 |
endpoint=None, # type: ignore[arg-type] |
| 100 |
status=None, |
| 101 |
headers=None, |
| 102 |
content=None, |
| 103 |
) |
| 104 |
|
| 105 |
ex = exinfo.value |
| 106 |
# raised with args: ("Access error", error_enum, attributes) |
| 107 |
assert ex.args[1] == AccessError.TRY_AGAIN |
| 108 |
assert isinstance(ex.args[2], dict) |
| 109 |
assert ex.args[2].get("timeout") == timeout |
| 110 |
|
| 111 |
|
| 112 |
def test_handle_access_error_unknown_raises_access_error( |
| 113 |
monkeypatch: pytest.MonkeyPatch, |
| 114 |
) -> None: |
| 115 |
"""Test handling of unknown errors.""" |
| 116 |
|
| 117 |
monkeypatch.setattr( |
| 118 |
endpoint_error, |
| 119 |
"read_json_content", |
| 120 |
Mock(return_value={"error_status": 999}), |
| 121 |
) |
| 122 |
|
| 123 |
with pytest.raises(AsusRouterAccessError) as exinfo: |
| 124 |
handle_access_error( |
| 125 |
endpoint=None, # type: ignore[arg-type] |
| 126 |
status=None, |
| 127 |
headers=None, |
| 128 |
content=None, |
| 129 |
) |
| 130 |
|
| 131 |
ex = exinfo.value |
| 132 |
assert ex.args[1] == AccessError.UNKNOWN |
| 133 |
assert ex.args[2] == {} |