mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Try default tapo credentials for klap and aes (#685)
* Try default tapo credentials for klap and aes * Add tests
This commit is contained in:
@@ -16,6 +16,7 @@ from ..deviceconfig import DeviceConfig
|
||||
from ..exceptions import (
|
||||
SMART_RETRYABLE_ERRORS,
|
||||
SMART_TIMEOUT_ERRORS,
|
||||
AuthenticationException,
|
||||
SmartDeviceException,
|
||||
SmartErrorCode,
|
||||
)
|
||||
@@ -91,6 +92,53 @@ async def test_login(mocker, status_code, error_code, inner_error_code, expectat
|
||||
assert transport._login_token == mock_aes_device.token
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"inner_error_codes, expectation, call_count",
|
||||
[
|
||||
([SmartErrorCode.LOGIN_ERROR, 0, 0, 0], does_not_raise(), 4),
|
||||
(
|
||||
[SmartErrorCode.LOGIN_ERROR, SmartErrorCode.LOGIN_ERROR],
|
||||
pytest.raises(AuthenticationException),
|
||||
3,
|
||||
),
|
||||
(
|
||||
[SmartErrorCode.LOGIN_FAILED_ERROR],
|
||||
pytest.raises(AuthenticationException),
|
||||
1,
|
||||
),
|
||||
],
|
||||
ids=("LOGIN_ERROR-success", "LOGIN_ERROR-LOGIN_ERROR", "LOGIN_FAILED_ERROR"),
|
||||
)
|
||||
async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
|
||||
host = "127.0.0.1"
|
||||
mock_aes_device = MockAesDevice(host, 200, 0, inner_error_codes)
|
||||
post_mock = mocker.patch.object(
|
||||
aiohttp.ClientSession, "post", side_effect=mock_aes_device.post
|
||||
)
|
||||
|
||||
transport = AesTransport(
|
||||
config=DeviceConfig(host, credentials=Credentials("foo", "bar"))
|
||||
)
|
||||
transport._handshake_done = True
|
||||
transport._session_expire_at = time.time() + 86400
|
||||
transport._encryption_session = mock_aes_device.encryption_session
|
||||
|
||||
assert transport._login_token is None
|
||||
|
||||
request = {
|
||||
"method": "get_device_info",
|
||||
"params": None,
|
||||
"request_time_milis": round(time.time() * 1000),
|
||||
"requestID": 1,
|
||||
"terminal_uuid": "foobar",
|
||||
}
|
||||
|
||||
with expectation:
|
||||
await transport.send(json_dumps(request))
|
||||
assert transport._login_token == mock_aes_device.token
|
||||
assert post_mock.call_count == call_count # Login, Handshake, Login
|
||||
|
||||
|
||||
@status_parameters
|
||||
async def test_send(mocker, status_code, error_code, inner_error_code, expectation):
|
||||
host = "127.0.0.1"
|
||||
@@ -166,8 +214,16 @@ class MockAesDevice:
|
||||
self.host = host
|
||||
self.status_code = status_code
|
||||
self.error_code = error_code
|
||||
self.inner_error_code = inner_error_code
|
||||
self._inner_error_code = inner_error_code
|
||||
self.http_client = HttpClient(DeviceConfig(self.host))
|
||||
self.inner_call_count = 0
|
||||
|
||||
@property
|
||||
def inner_error_code(self):
|
||||
if isinstance(self._inner_error_code, list):
|
||||
return self._inner_error_code[self.inner_call_count]
|
||||
else:
|
||||
return self._inner_error_code
|
||||
|
||||
async def post(self, url, params=None, json=None, *_, **__):
|
||||
return await self._post(url, json)
|
||||
@@ -215,8 +271,10 @@ class MockAesDevice:
|
||||
|
||||
async def _return_login_response(self, url, json):
|
||||
result = {"result": {"token": self.token}, "error_code": self.inner_error_code}
|
||||
self.inner_call_count += 1
|
||||
return self._mock_response(self.status_code, result)
|
||||
|
||||
async def _return_send_response(self, url, json):
|
||||
result = {"result": {"method": None}, "error_code": self.inner_error_code}
|
||||
self.inner_call_count += 1
|
||||
return self._mock_response(self.status_code, result)
|
||||
|
@@ -28,6 +28,7 @@ from ..klaptransport import (
|
||||
KlapTransportV2,
|
||||
_sha256,
|
||||
)
|
||||
from ..protocol import DEFAULT_CREDENTIALS, get_default_credentials
|
||||
from ..smartprotocol import SmartProtocol
|
||||
|
||||
DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
|
||||
@@ -241,10 +242,7 @@ def test_encrypt_unicode():
|
||||
(Credentials("foo", "bar"), does_not_raise()),
|
||||
(Credentials(), does_not_raise()),
|
||||
(
|
||||
Credentials(
|
||||
KlapTransport.KASA_SETUP_EMAIL,
|
||||
KlapTransport.KASA_SETUP_PASSWORD,
|
||||
),
|
||||
get_default_credentials(DEFAULT_CREDENTIALS["KASA"]),
|
||||
does_not_raise(),
|
||||
),
|
||||
(
|
||||
|
Reference in New Issue
Block a user