| 1 |
"""Test VeSync login method.""" |
| 2 |
|
| 3 |
from unittest.mock import MagicMock |
| 4 |
import pytest |
| 5 |
import orjson |
| 6 |
|
| 7 |
from pyvesync.utils.errors import VeSyncLoginError |
| 8 |
from pyvesync import const |
| 9 |
from pyvesync.utils.helpers import Helpers |
| 10 |
from pyvesync.models.vesync_models import ( |
| 11 |
ResponseLoginModel, |
| 12 |
RequestLoginTokenModel, |
| 13 |
RequestGetTokenModel, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
from aiohttp_mocker import AiohttpMockSession |
| 18 |
from base_test_cases import TestApiFunc, TestBase |
| 19 |
import call_json |
| 20 |
from defaults import TestDefaults |
| 21 |
|
| 22 |
|
| 23 |
TOKEN_ERROR_CODE = -11001000 |
| 24 |
TOKEN_ERROR_RESP = call_json.response_body(TOKEN_ERROR_CODE, "Token expired") |
| 25 |
|
| 26 |
US_BASE_URL = const.API_BASE_URL_US |
| 27 |
EU_BASE_URL = const.API_BASE_URL_EU |
| 28 |
|
| 29 |
DEFAULT_ENDPOINT = '/endpoint' |
| 30 |
|
| 31 |
|
| 32 |
INVALID_PASSWORD_ERROR = -11201000 |
| 33 |
INVALID_PASSWORD_RESP = {'code': INVALID_PASSWORD_ERROR, 'msg': 'success'} |
| 34 |
|
| 35 |
|
| 36 |
LOGIN_RET_BODY = { |
| 37 |
"traceId": TestDefaults.trace_id, |
| 38 |
"code": 0, |
| 39 |
"msg": "", |
| 40 |
"stacktrace": None, |
| 41 |
"result": { |
| 42 |
"accountID": TestDefaults.account_id, |
| 43 |
"avatarIcon": "", |
| 44 |
"acceptLanguage": "en", |
| 45 |
"gdprStatus": True, |
| 46 |
"nickName": "nick", |
| 47 |
"userType": "1", |
| 48 |
"token": TestDefaults.token, |
| 49 |
"countryCode": TestDefaults.country_code, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
LOGIN_REQUESTS = call_json.LoginRequests |
| 54 |
LOGIN_RESPONSES = call_json.LoginResponses |
| 55 |
LOGIN_ENDPOINT = '/globalPlatform/api/accountAuth/v1/authByPWDOrOTM' |
| 56 |
LOGIN_TOKEN_ENDPOINT = '/user/api/accountManage/v1/loginByAuthorizeCode4Vesync' |
| 57 |
|
| 58 |
VARIABLE_FIELDS = { |
| 59 |
'traceId': TestDefaults.trace_id, |
| 60 |
'appId': TestDefaults.app_id, |
| 61 |
'terminalId': TestDefaults.terminal_id, |
| 62 |
} |
| 63 |
|
| 64 |
request_login_token_model = RequestLoginTokenModel( |
| 65 |
authorizeCode=TestDefaults.authorization_code, |
| 66 |
method='loginByAuthorizeCode4Vesync', |
| 67 |
userCountryCode=TestDefaults.country_code, |
| 68 |
traceId=TestDefaults.trace_id, |
| 69 |
terminalId=TestDefaults.terminal_id, |
| 70 |
) |
| 71 |
request_get_token_model = RequestGetTokenModel( |
| 72 |
email=TestDefaults.email, |
| 73 |
password=TestDefaults.password, |
| 74 |
method='authByPWDOrOTM', |
| 75 |
userCountryCode=TestDefaults.country_code, |
| 76 |
traceId=TestDefaults.trace_id, |
| 77 |
terminalId=TestDefaults.terminal_id, |
| 78 |
appID=TestDefaults.app_id, |
| 79 |
) |
| 80 |
response_get_token_model = ResponseLoginModel.from_dict(LOGIN_RESPONSES.GET_TOKEN_RESPONSE_SUCCESS) |
| 81 |
|
| 82 |
response_login_token_model = ResponseLoginModel.from_dict(LOGIN_RESPONSES.LOGIN_RESPONSE_SUCCESS) |
| 83 |
|
| 84 |
|
| 85 |
class TestLoginModels(TestBase): |
| 86 |
"""Test VeSync login response models.""" |
| 87 |
|
| 88 |
def test_login_success(self): |
| 89 |
"""Test login response model.""" |
| 90 |
self.mock_api.side_effect = [ |
| 91 |
(response_get_token_model.to_dict(), 200), |
| 92 |
(response_login_token_model.to_dict(), 200) |
| 93 |
] |
| 94 |
self.run_in_loop(self.manager.login) |
| 95 |
assert self.mock_api.call_count == 2 |
| 96 |
assert request_get_token_model.email == TestDefaults.email |
| 97 |
assert request_get_token_model.password == Helpers.hash_password(TestDefaults.password) |
| 98 |
assert self.manager.token == TestDefaults.token |
| 99 |
assert self.manager.account_id == TestDefaults.account_id |
| 100 |
call_list = self.mock_api.call_args_list |
| 101 |
assert call_list[0].args[0] == LOGIN_ENDPOINT |
| 102 |
assert call_list[0].args[1] == 'post' |
| 103 |
assert call_list[1].args[0] == LOGIN_TOKEN_ENDPOINT |
| 104 |
assert call_list[1].args[1] == 'post' |
| 105 |
get_token_kwargs = call_list[0].kwargs['json_object'] |
| 106 |
get_token_kwargs.appID = TestDefaults.app_id |
| 107 |
get_token_kwargs.terminalId = TestDefaults.terminal_id |
| 108 |
get_token_kwargs.traceId = TestDefaults.trace_id |
| 109 |
assert get_token_kwargs == request_get_token_model |
| 110 |
login_token_kwargs = call_list[1].kwargs['json_object'] |
| 111 |
login_token_kwargs.terminalId = TestDefaults.terminal_id |
| 112 |
login_token_kwargs.traceId = TestDefaults.trace_id |
| 113 |
assert login_token_kwargs == request_login_token_model |
| 114 |
|
| 115 |
|
| 116 |
class TestLogin(TestApiFunc): |
| 117 |
"""Test VeSync login class.""" |
| 118 |
def test_invalid_password(self): |
| 119 |
"""Test login with invalid user/password.""" |
| 120 |
self.mock_api.return_value.request.return_value = AiohttpMockSession( |
| 121 |
method='post', |
| 122 |
url=US_BASE_URL + self.default_endpoint, |
| 123 |
status=200, |
| 124 |
response=orjson.dumps(INVALID_PASSWORD_RESP), |
| 125 |
) |
| 126 |
with pytest.raises(VeSyncLoginError): |
| 127 |
self.run_in_loop(self.manager.login) |
| 128 |
|
| 129 |
def _build_token_request(self): |
| 130 |
request_get_token_model = RequestGetTokenModel( |
| 131 |
email=TestDefaults.email, |
| 132 |
password=TestDefaults.password, |
| 133 |
method='authByPWDOrOTM', |
| 134 |
userCountryCode=TestDefaults.country_code, |
| 135 |
traceId=TestDefaults.trace_id, |
| 136 |
terminalId=TestDefaults.terminal_id, |
| 137 |
appID=TestDefaults.app_id, |
| 138 |
) |
| 139 |
return request_get_token_model |
| 140 |
|
| 141 |
def test_token_expired(self): |
| 142 |
"""Test login with expired token.""" |
| 143 |
token_error_response = orjson.dumps(TOKEN_ERROR_RESP) |
| 144 |
get_token_endpoint = LOGIN_ENDPOINT |
| 145 |
login_token_endpoint = LOGIN_TOKEN_ENDPOINT |
| 146 |
final_response = call_json.DeviceList.device_list_response() |
| 147 |
first_final_endpoint = '/cloud/v1/deviceManaged/devices' |
| 148 |
get_token_response = orjson.dumps(LOGIN_RESPONSES.GET_TOKEN_RESPONSE_SUCCESS) |
| 149 |
login_token_response = orjson.dumps(LOGIN_RESPONSES.LOGIN_RESPONSE_SUCCESS) |
| 150 |
self.mock_api.return_value = MagicMock() |
| 151 |
self.mock_api.return_value.request.side_effect = [ |
| 152 |
AiohttpMockSession( |
| 153 |
method='post', |
| 154 |
url=US_BASE_URL + first_final_endpoint, |
| 155 |
status=200, |
| 156 |
response=token_error_response, |
| 157 |
), |
| 158 |
AiohttpMockSession( |
| 159 |
method='post', |
| 160 |
url=US_BASE_URL + get_token_endpoint, |
| 161 |
status=200, |
| 162 |
response=get_token_response, |
| 163 |
), |
| 164 |
AiohttpMockSession( |
| 165 |
method='post', |
| 166 |
url=US_BASE_URL + login_token_endpoint, |
| 167 |
status=200, |
| 168 |
response=login_token_response, |
| 169 |
), |
| 170 |
AiohttpMockSession( |
| 171 |
method='post', |
| 172 |
url=US_BASE_URL + first_final_endpoint, |
| 173 |
status=200, |
| 174 |
response=orjson.dumps(final_response), |
| 175 |
), |
| 176 |
] |
| 177 |
self.run_in_loop(self.manager.get_devices) |
| 178 |
assert len(self.mock_api.mock_calls) == 5 |
| 179 |
call_list = self.mock_api.mock_calls |
| 180 |
assert call_list[1].args[0] == 'post' |
| 181 |
assert call_list[1].kwargs['url'] == US_BASE_URL + first_final_endpoint |
| 182 |
assert call_list[2].args[0] == 'post' |
| 183 |
assert call_list[2].kwargs['url'] == US_BASE_URL + get_token_endpoint |
| 184 |
assert call_list[3].args[0] == 'post' |
| 185 |
assert call_list[3].kwargs['url'] == US_BASE_URL + login_token_endpoint |
| 186 |
assert call_list[4].args[0] == 'post' |
| 187 |
assert call_list[4].kwargs['url'] == US_BASE_URL + first_final_endpoint |