|
1 | 1 | import asyncio |
2 | | -from datetime import datetime, timezone |
| 2 | +import json |
3 | 3 | from unittest.mock import patch |
4 | 4 |
|
5 | 5 | import aiohttp |
6 | 6 | import pytest |
7 | 7 | from aresponses import ResponsesMockServer |
8 | 8 |
|
9 | 9 | from custom_components.eau_agur.api import AgurApiClient |
10 | | -from custom_components.eau_agur.api.exceptions import AgurApiConnectionError, AgurApiError |
| 10 | +from custom_components.eau_agur.api.exceptions import ( |
| 11 | + AgurApiConnectionError, |
| 12 | + AgurApiError, |
| 13 | + AgurApiInvalidSessionError, |
| 14 | + AgurApiUnauthorizedError, |
| 15 | +) |
11 | 16 |
|
12 | 17 | HOST_PATTERN = "example.com" |
13 | 18 |
|
@@ -52,7 +57,7 @@ async def test_http_error400(aresponses): |
52 | 57 | HOST_PATTERN, |
53 | 58 | "/", |
54 | 59 | "GET", |
55 | | - aresponses.Response(text="Bad request!", status=404), |
| 60 | + aresponses.Response(text="Bad request!", status=400), |
56 | 61 | ) |
57 | 62 |
|
58 | 63 | async with aiohttp.ClientSession() as session: |
@@ -119,6 +124,56 @@ async def test_post_login(aresponses: ResponsesMockServer): |
119 | 124 | await client.login("dupond.toto@mycompany.com", "myP@ssw0rd!") |
120 | 125 |
|
121 | 126 |
|
| 127 | +@pytest.mark.asyncio |
| 128 | +async def test_post_login_invalid_session(aresponses: ResponsesMockServer): |
| 129 | + """Test requesting consumption data.""" |
| 130 | + aresponses.add( |
| 131 | + host_pattern=HOST_PATTERN, |
| 132 | + path_pattern="/webapi/Utilisateur/authentification", |
| 133 | + method_pattern="POST", |
| 134 | + response=aresponses.Response( |
| 135 | + status=400, |
| 136 | + body=json.dumps( |
| 137 | + { |
| 138 | + "severity": "Security", |
| 139 | + "message": "Session Inconnue. Veuillez rafraîchir votre page et vous reconnecter.", |
| 140 | + "according": "W/FRONT", |
| 141 | + "refLog": "LogTicket-250705-0745-c8692f2c-485a-48c3-9daa-ded89ad5d246", |
| 142 | + } |
| 143 | + ), |
| 144 | + ), |
| 145 | + ) |
| 146 | + async with aiohttp.ClientSession() as session: |
| 147 | + client = AgurApiClient(HOST_PATTERN, session=session) |
| 148 | + with pytest.raises(AgurApiInvalidSessionError): |
| 149 | + await client.login("dupond.toto@mycompany.com", "myP@ssw0rd!") |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.asyncio |
| 153 | +async def test_post_login_invalid_credentials(aresponses: ResponsesMockServer): |
| 154 | + """Test requesting consumption data.""" |
| 155 | + aresponses.add( |
| 156 | + host_pattern=HOST_PATTERN, |
| 157 | + path_pattern="/webapi/Utilisateur/authentification", |
| 158 | + method_pattern="POST", |
| 159 | + response=aresponses.Response( |
| 160 | + status=401, |
| 161 | + body=json.dumps( |
| 162 | + { |
| 163 | + "severity": "Security", |
| 164 | + "message": "Par sécurité au bout de 5 essais infructueux votre compte sera bloqué. Il vous reste 5 essais.", |
| 165 | + "according": "W/FRONT", |
| 166 | + "refLog": "LogTicket-250705-0923-98d60ea9-2882-42fe-ab06-43475363c192", |
| 167 | + } |
| 168 | + ), |
| 169 | + ), |
| 170 | + ) |
| 171 | + async with aiohttp.ClientSession() as session: |
| 172 | + client = AgurApiClient(HOST_PATTERN, session=session) |
| 173 | + with pytest.raises(AgurApiUnauthorizedError): |
| 174 | + await client.login("dupond.toto@mycompany.com", "myP@ssw0rd!") |
| 175 | + |
| 176 | + |
122 | 177 | @pytest.mark.asyncio |
123 | 178 | async def test_post_generate_temporary_token(aresponses: ResponsesMockServer): |
124 | 179 | """Test requesting generation of a temporary token.""" |
@@ -177,44 +232,3 @@ async def test_get_last_invoice(aresponses: ResponsesMockServer): |
177 | 232 | client = AgurApiClient(HOST_PATTERN, session=session) |
178 | 233 | value = await client.get_last_invoice("12345") |
179 | 234 | assert value == 30.0 |
180 | | - |
181 | | - |
182 | | -@pytest.mark.asyncio |
183 | | -async def test_is_token_expired_none(): |
184 | | - """Test is_token_expired when token_expires_at is None.""" |
185 | | - async with aiohttp.ClientSession() as session: |
186 | | - client = AgurApiClient(HOST_PATTERN, session=session) |
187 | | - # By default, _token_expires_at is None |
188 | | - assert client.is_token_expired() is True |
189 | | - |
190 | | - |
191 | | -@pytest.mark.asyncio |
192 | | -async def test_is_token_expired_past(): |
193 | | - """Test is_token_expired when token is expired.""" |
194 | | - async with aiohttp.ClientSession() as session: |
195 | | - client = AgurApiClient(HOST_PATTERN, session=session) |
196 | | - # Set token expiration to a past datetime |
197 | | - past_time = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
198 | | - client._token_expires_at = past_time |
199 | | - |
200 | | - # Mock current time to be after expiration |
201 | | - current_time = datetime(2023, 1, 1, 13, 0, 0, tzinfo=timezone.utc) |
202 | | - with patch("custom_components.eau_agur.api.agur_api_client.datetime") as mock_datetime: |
203 | | - mock_datetime.now.return_value = current_time |
204 | | - assert client.is_token_expired() is True |
205 | | - |
206 | | - |
207 | | -@pytest.mark.asyncio |
208 | | -async def test_is_token_expired_future(): |
209 | | - """Test is_token_expired when token is not expired.""" |
210 | | - async with aiohttp.ClientSession() as session: |
211 | | - client = AgurApiClient(HOST_PATTERN, session=session) |
212 | | - # Set token expiration to a future datetime |
213 | | - future_time = datetime(2023, 1, 1, 14, 0, 0, tzinfo=timezone.utc) |
214 | | - client._token_expires_at = future_time |
215 | | - |
216 | | - # Mock current time to be before expiration |
217 | | - current_time = datetime(2023, 1, 1, 13, 0, 0, tzinfo=timezone.utc) |
218 | | - with patch("custom_components.eau_agur.api.agur_api_client.datetime") as mock_datetime: |
219 | | - mock_datetime.now.return_value = current_time |
220 | | - assert client.is_token_expired() is False |
|
0 commit comments