|
| 1 | +import re |
| 2 | + |
1 | 3 | import aiohttp |
| 4 | +from aioresponses.compat import normalize_url |
2 | 5 |
|
3 | 6 | from roborock import HomeData, HomeDataScene, UserData |
4 | | -from roborock.web_api import RoborockApiClient |
| 7 | +from roborock.web_api import IotLoginInfo, RoborockApiClient |
5 | 8 | from tests.mock_data import HOME_DATA_RAW, USER_DATA |
6 | 9 |
|
7 | 10 |
|
@@ -71,3 +74,99 @@ async def test_code_login_v4_flow(mock_rest) -> None: |
71 | 74 | await api.request_code_v4() |
72 | 75 | ud = await api.code_login_v4(4123, "US", 1) |
73 | 76 | assert ud == UserData.from_dict(USER_DATA) |
| 77 | + |
| 78 | + |
| 79 | +async def test_url_cycling(mock_rest) -> None: |
| 80 | + """Test that we cycle through the URLs correctly.""" |
| 81 | + # Clear mock rest so that we can override the patches. |
| 82 | + mock_rest.clear() |
| 83 | + # 1. Mock US URL to return valid status but None for countrycode |
| 84 | + |
| 85 | + mock_rest.post( |
| 86 | + re.compile("https://usiot.roborock.com/api/v1/getUrlByEmail.*"), |
| 87 | + status=200, |
| 88 | + payload={ |
| 89 | + "code": 200, |
| 90 | + "data": {"url": "https://usiot.roborock.com", "country": None, "countrycode": None}, |
| 91 | + "msg": "Success", |
| 92 | + }, |
| 93 | + ) |
| 94 | + |
| 95 | + # 2. Mock EU URL to return a server-side error code |
| 96 | + mock_rest.post( |
| 97 | + re.compile("https://euiot.roborock.com/api/v1/getUrlByEmail.*"), |
| 98 | + status=200, |
| 99 | + payload={ |
| 100 | + "code": 200, |
| 101 | + "data": {"url": "https://euiot.roborock.com", "country": None, "countrycode": None}, |
| 102 | + "msg": "Success", |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + # 3. Mock CN URL to return the correct, valid data |
| 107 | + mock_rest.post( |
| 108 | + re.compile("https://cniot.roborock.com/api/v1/getUrlByEmail.*"), |
| 109 | + status=200, |
| 110 | + payload={ |
| 111 | + "code": 200, |
| 112 | + "data": {"url": "https://cniot.roborock.com", "country": "CN", "countrycode": "86"}, |
| 113 | + "msg": "Success", |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + # The RU URL should not be called, but we can mock it just in case |
| 118 | + # to catch unexpected behavior. |
| 119 | + mock_rest.post(re.compile("https://ruiot.roborock.com/api/v1/getUrlByEmail.*"), status=500) |
| 120 | + |
| 121 | + client = RoborockApiClient("test@example.com") |
| 122 | + result = await client._get_iot_login_info() |
| 123 | + |
| 124 | + assert result is not None |
| 125 | + assert isinstance(result, IotLoginInfo) |
| 126 | + assert result.base_url == "https://cniot.roborock.com" |
| 127 | + assert result.country == "CN" |
| 128 | + assert result.country_code == "86" |
| 129 | + |
| 130 | + assert client._iot_login_info == result |
| 131 | + # Check that all three urls were called. We have to do this kind of weirdly as aioresponses seems to have a bug. |
| 132 | + assert ( |
| 133 | + len( |
| 134 | + mock_rest.requests[ |
| 135 | + ( |
| 136 | + "post", |
| 137 | + normalize_url( |
| 138 | + "https://usiot.roborock.com/api/v1/getUrlByEmail?email=test%2540example.com&needtwostepauth=false" |
| 139 | + ), |
| 140 | + ) |
| 141 | + ] |
| 142 | + ) |
| 143 | + == 1 |
| 144 | + ) |
| 145 | + assert ( |
| 146 | + len( |
| 147 | + mock_rest.requests[ |
| 148 | + ( |
| 149 | + "post", |
| 150 | + normalize_url( |
| 151 | + "https://euiot.roborock.com/api/v1/getUrlByEmail?email=test%2540example.com&needtwostepauth=false" |
| 152 | + ), |
| 153 | + ) |
| 154 | + ] |
| 155 | + ) |
| 156 | + == 1 |
| 157 | + ) |
| 158 | + assert ( |
| 159 | + len( |
| 160 | + mock_rest.requests[ |
| 161 | + ( |
| 162 | + "post", |
| 163 | + normalize_url( |
| 164 | + "https://cniot.roborock.com/api/v1/getUrlByEmail?email=test%2540example.com&needtwostepauth=false" |
| 165 | + ), |
| 166 | + ) |
| 167 | + ] |
| 168 | + ) |
| 169 | + == 1 |
| 170 | + ) |
| 171 | + # Make sure we just have the three we tested for above. |
| 172 | + assert len(mock_rest.requests) == 3 |
0 commit comments