Skip to content

Commit ab584fd

Browse files
committed
fix: bug and add test
1 parent e45085a commit ab584fd

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

roborock/web_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ async def request_code(self) -> None:
234234

235235
async def request_code_v4(self) -> None:
236236
"""Request a code using the v4 endpoint."""
237+
if await self.country_code is None or await self.country is None:
238+
_LOGGER.info("No country code or country found, trying old version of request code.")
239+
return await self.request_code()
237240
try:
238241
self._login_limiter.try_acquire("login")
239242
except BucketFullException as ex:
240243
_LOGGER.info(ex.meta_info)
241244
raise RoborockRateLimit("Reached maximum requests for login. Please try again later.") from ex
242245
base_url = await self.base_url
243-
if await self.country_code is None or await self.country is None:
244-
_LOGGER.info("No country code or country found, trying old version of request code.")
245-
return await self.request_code()
246246
header_clientid = self._get_header_client_id()
247247
code_request = PreparedRequest(
248248
base_url,

tests/test_web_api.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,36 @@ async def test_url_cycling(mock_rest) -> None:
170170
)
171171
# Make sure we just have the three we tested for above.
172172
assert len(mock_rest.requests) == 3
173+
174+
175+
async def test_missing_country_login(mock_rest) -> None:
176+
"""Test that we cycle through the URLs correctly."""
177+
mock_rest.clear()
178+
# Make country None, but country code set.
179+
mock_rest.post(
180+
re.compile("https://usiot.roborock.com/api/v1/getUrlByEmail.*"),
181+
status=200,
182+
payload={
183+
"code": 200,
184+
"data": {"url": "https://usiot.roborock.com", "country": None, "countrycode": 1},
185+
"msg": "Success",
186+
},
187+
)
188+
# v4 is not mocked, so it would fail it were called.
189+
mock_rest.post(
190+
re.compile(r"https://.*iot\.roborock\.com/api/v1/loginWithCode.*"),
191+
status=200,
192+
payload={"code": 200, "data": USER_DATA, "msg": "success"},
193+
)
194+
mock_rest.post(
195+
re.compile(r"https://.*iot\.roborock\.com/api/v1/sendEmailCode.*"),
196+
status=200,
197+
payload={"code": 200, "data": None, "msg": "success"},
198+
)
199+
200+
client = RoborockApiClient("test@example.com")
201+
await client.request_code_v4()
202+
ud = await client.code_login_v4(4123)
203+
assert ud is not None
204+
# Ensure we have no surprise REST calls.
205+
assert len(mock_rest.requests) == 3

0 commit comments

Comments
 (0)