| 1 |
"""General VeSync tests.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import orjson |
| 5 |
import pytest |
| 6 |
from unittest.mock import patch, MagicMock |
| 7 |
|
| 8 |
from pyvesync import VeSync |
| 9 |
from pyvesync.utils.errors import VeSyncRateLimitError, VeSyncServerError |
| 10 |
|
| 11 |
from pyvesync.const import API_BASE_URL_US |
| 12 |
from pyvesync.utils.errors import ( |
| 13 |
VeSyncAPIStatusCodeError |
| 14 |
) |
| 15 |
import call_json |
| 16 |
from defaults import TestDefaults |
| 17 |
from aiohttp_mocker import AiohttpMockSession |
| 18 |
|
| 19 |
|
| 20 |
DEFAULT_ENDPOINT = '/endpoint' |
| 21 |
DEFAULT_POST_DATA = {'key': 'value'} |
| 22 |
PARAM_ARGS = "endpoint, method, resp_bytes, resp_status" |
| 23 |
|
| 24 |
|
| 25 |
SUCCESS_RESP = call_json.response_body(0, 'Success') |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
RATE_LIMIT_CODE = -11003000 |
| 30 |
RATE_LIMIT_RESP = call_json.response_body(RATE_LIMIT_CODE, "Rate limit exceeded") |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
SERVER_ERROR = -11102000 |
| 35 |
SERVER_ERROR_RESP = call_json.response_body(SERVER_ERROR, "Server error") |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
STATUS_CODE_ERROR = 400 |
| 40 |
STATUS_CODE_RESP = None |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
STATUS_CODE_ERROR = 400 |
| 45 |
STATUS_CODE_RESP = None |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
DEVICE_ERROR_CODE = -11901000 |
| 51 |
DEVICE_ERROR_RESP = call_json.response_body(DEVICE_ERROR_CODE, "Device error") |
| 52 |
|
| 53 |
|
| 54 |
class TestApiFunc: |
| 55 |
"""Test call_api() method.""" |
| 56 |
@pytest.fixture(autouse=True, scope='function') |
| 57 |
def setup(self, caplog): |
| 58 |
"""Fixture to instantiate VeSync object, start logging and start Mock. |
| 59 |
|
| 60 |
Attributes |
| 61 |
---------- |
| 62 |
self.mock_api : Mock |
| 63 |
self.manager : VeSync |
| 64 |
self.caplog : LogCaptureFixture |
| 65 |
|
| 66 |
Yields |
| 67 |
------ |
| 68 |
Class instance with mocked call_api() function and VeSync object |
| 69 |
""" |
| 70 |
self.caplog = caplog |
| 71 |
self.loop = asyncio.new_event_loop() |
| 72 |
self.mock = MagicMock() |
| 73 |
self.manager = VeSync('EMAIL', 'PASSWORD') |
| 74 |
self.manager.enabled = True |
| 75 |
self.manager.auth._token = TestDefaults.token |
| 76 |
self.manager.auth._account_id = TestDefaults.account_id |
| 77 |
yield |
| 78 |
self.mock.stop() |
| •79 |
self.loop.close() |
| 80 |
|
| 81 |
async def run_coro(self, coro): |
| 82 |
"""Run a coroutine in the event loop.""" |
| 83 |
return await coro |
| 84 |
|
| 85 |
def run_in_loop(self, func, *args, **kwargs): |
| 86 |
"""Run a function in the event loop.""" |
| 87 |
return self.loop.run_until_complete(self.run_coro(func(*args, **kwargs))) |
| 88 |
|
| 89 |
@patch("pyvesync.vesync.ClientSession") |
| 90 |
def test_api_success(self, mock): |
| 91 |
"""Test successful api call - returns tuple of response bytes and status code.""" |
| 92 |
mock.return_value.request.return_value = AiohttpMockSession( |
| 93 |
method='post', |
| 94 |
url=API_BASE_URL_US + DEFAULT_ENDPOINT, |
| 95 |
status=200, |
| 96 |
response=orjson.dumps(SUCCESS_RESP), |
| 97 |
) |
| 98 |
resp = self.run_in_loop(self.manager.async_call_api, DEFAULT_ENDPOINT, method="get") |
| 99 |
mock_dict, mock_status = resp |
| 100 |
|
| 101 |
assert SUCCESS_RESP == mock_dict |
| 102 |
assert mock_status == 200 |
| 103 |
|
| 104 |
@patch("pyvesync.vesync.ClientSession") |
| 105 |
def test_api_rate_limit(self, mock): |
| 106 |
"""Test rate limit error - raises `VeSyncRateLimitError` from `VeSync.async_call_api`.""" |
| 107 |
rate_limit_resp = call_json.response_body(RATE_LIMIT_CODE, "Rate limit exceeded") |
| 108 |
mock.return_value.request.return_value = AiohttpMockSession( |
| 109 |
method="post", |
| 110 |
url=API_BASE_URL_US + DEFAULT_ENDPOINT, |
| 111 |
status=200, |
| 112 |
response=orjson.dumps(rate_limit_resp), |
| 113 |
) |
| 114 |
with pytest.raises(VeSyncRateLimitError): |
| 115 |
self.run_in_loop(self.manager.async_call_api, DEFAULT_ENDPOINT, 'post', json_object=DEFAULT_POST_DATA) |
| 116 |
|
| 117 |
@patch("pyvesync.vesync.ClientSession") |
| 118 |
def test_api_server_error(self, mock): |
| 119 |
"""Test server error - raises `VeSyncServerError` from `VeSync.async_call_api`.""" |
| 120 |
mock.return_value.request.return_value = AiohttpMockSession( |
| 121 |
method='post', |
| 122 |
url=API_BASE_URL_US + DEFAULT_ENDPOINT, |
| 123 |
status=200, |
| 124 |
response=orjson.dumps(SERVER_ERROR_RESP), |
| 125 |
) |
| 126 |
with pytest.raises(VeSyncServerError): |
| 127 |
self.run_in_loop(self.manager.async_call_api, DEFAULT_ENDPOINT, 'post', json_object=DEFAULT_POST_DATA) |
| 128 |
|
| 129 |
@patch("pyvesync.vesync.ClientSession") |
| 130 |
def test_api_status_code_error(self, mock): |
| 131 |
"""Test status code error - raises `VeSyncAPIStatusCodeError` from `VeSync.async_call_api`.""" |
| 132 |
mock.return_value.request.return_value = AiohttpMockSession( |
| 133 |
method='get', |
| 134 |
url=API_BASE_URL_US + DEFAULT_ENDPOINT, |
| 135 |
status=404, |
| 136 |
response=None, |
| 137 |
) |
| 138 |
with pytest.raises(VeSyncAPIStatusCodeError): |
| 139 |
self.run_in_loop(self.manager.async_call_api, DEFAULT_ENDPOINT, 'get') |