File: test__init__.py

package info (click to toggle)
python-ovoenergy 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 352 kB
  • sloc: python: 1,519; makefile: 4
file content (333 lines) | stat: -rw-r--r-- 8,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""Tests for the client module."""

from datetime import datetime, timedelta

from aioresponses import aioresponses
import pytest
from syrupy.assertion import SnapshotAssertion

from ovoenergy import OVOEnergy
from ovoenergy.const import AUTH_LOGIN_URL, AUTH_TOKEN_URL, USAGE_DAILY_URL
from ovoenergy.exceptions import (
    OVOEnergyAPINoCookies,
    OVOEnergyAPINotAuthorized,
    OVOEnergyAPINotFound,
    OVOEnergyNoAccount,
)

from . import ACCOUNT, ACCOUNT_BAD, PASSWORD, RESPONSE_JSON_AUTH, USERNAME


@pytest.mark.asyncio
async def test_authorize(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test authorize."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    assert not ovoenergy_client.oauth_expired

    assert ovoenergy_client.oauth is not None

    assert ovoenergy_client.oauth.access_token == snapshot(
        name="authorize_oauth_access_token",
    )
    assert ovoenergy_client.oauth.expires_in == snapshot(
        name="authorize_oauth_expires_in",
    )
    assert ovoenergy_client.oauth.refresh_expires_in == snapshot(
        name="authorize_oauth_refresh_expires_in",
    )
    assert ovoenergy_client.account_id == snapshot(
        name="authorize_account_id",
    )
    assert ovoenergy_client.account_ids == snapshot(
        name="authorize_account_ids",
    )
    assert ovoenergy_client.customer_id == snapshot(
        name="authorize_customer_id",
    )
    assert ovoenergy_client.username == snapshot(
        name="authorize_username",
    )


@pytest.mark.asyncio
async def test_bootstrap(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test bootstrap."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    assert await ovoenergy_client.bootstrap_accounts() == snapshot(
        name="bootstrap_accounts",
    )


@pytest.mark.asyncio
async def test_get_daily_usage(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get daily usage."""
    with pytest.raises(OVOEnergyNoAccount):
        await ovoenergy_client.get_daily_usage("2024-01")

    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    assert await ovoenergy_client.get_daily_usage("2024-01") == snapshot(
        name="daily_usage",
    )


@pytest.mark.asyncio
async def test_get_half_hourly_usage(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get half hourly usage."""
    with pytest.raises(OVOEnergyNoAccount):
        await ovoenergy_client.get_half_hourly_usage("2024-01-01")

    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    assert await ovoenergy_client.get_half_hourly_usage("2024-01-01") == snapshot(
        name="half_hourly_usage",
    )


@pytest.mark.asyncio
async def test_get_footprint(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get footprint."""
    with pytest.raises(OVOEnergyNoAccount):
        await ovoenergy_client.get_footprint()

    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    assert await ovoenergy_client.get_footprint() == snapshot(
        name="footprint",
    )


@pytest.mark.asyncio
async def test_get_carbon_intensity(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test get carbon intensity."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    assert await ovoenergy_client.get_carbon_intensity() == snapshot(
        name="carbon_intensity",
    )


@pytest.mark.asyncio
async def test_bootstrap_custom_account(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
    snapshot: SnapshotAssertion,
) -> None:
    """Test bootstrap custom account."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    ovoenergy_client.custom_account_id = ACCOUNT

    await ovoenergy_client.bootstrap_accounts()

    assert await ovoenergy_client.get_daily_usage("2024-01") == snapshot(
        name="bootstrap_accounts_custom_account",
    )


@pytest.mark.asyncio
async def test_bad_account(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test bad account."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    ovoenergy_client.custom_account_id = ACCOUNT_BAD

    with pytest.raises(OVOEnergyAPINotFound):
        await ovoenergy_client.get_daily_usage("2024-01")


# pylint: disable=protected-access
@pytest.mark.asyncio
async def test_no_cookies(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test no cookies."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    ovoenergy_client._cookies = None

    with pytest.raises(OVOEnergyAPINoCookies):
        await ovoenergy_client.get_daily_usage("2024-01")


# pylint: disable=protected-access
@pytest.mark.asyncio
async def test_no_auth(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test no auth."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    ovoenergy_client._oauth = None

    with pytest.raises(OVOEnergyAPINotAuthorized):
        await ovoenergy_client.get_daily_usage("2024-01")


# pylint: disable=protected-access
@pytest.mark.asyncio
async def test_oauth_expired(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test oauth expired."""
    an_hour_ago = datetime.now() - timedelta(hours=1)

    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    assert ovoenergy_client._oauth is not None

    ovoenergy_client._oauth.expires_at = an_hour_ago

    await ovoenergy_client.get_daily_usage("2024-01")

    ovoenergy_client._oauth.expires_at = an_hour_ago

    mock_aioresponse.clear()
    mock_aioresponse.get(
        AUTH_TOKEN_URL,
        status=403,
        repeat=True,
    )

    with pytest.raises(OVOEnergyAPINotAuthorized):
        await ovoenergy_client.get_daily_usage("2024-01")


@pytest.mark.asyncio
async def test_forbidden(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test forbidden."""
    await ovoenergy_client.authenticate(USERNAME, PASSWORD)

    await ovoenergy_client.bootstrap_accounts()

    mock_aioresponse.clear()
    mock_aioresponse.get(
        f"{USAGE_DAILY_URL}/{ACCOUNT}?date=2024-01",
        status=403,
        repeat=True,
    )

    with pytest.raises(OVOEnergyAPINotAuthorized):
        await ovoenergy_client.get_daily_usage("2024-01")


@pytest.mark.asyncio
async def test_auth_not_found(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test auth endpoint not found."""
    mock_aioresponse.clear()
    mock_aioresponse.post(
        AUTH_LOGIN_URL,
        status=404,
        repeat=True,
    )

    with pytest.raises(OVOEnergyAPINotFound):
        await ovoenergy_client.authenticate(USERNAME, PASSWORD)


@pytest.mark.asyncio
async def test_auth_code_not_found(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test auth endpoint code not found."""
    mock_aioresponse.clear()
    mock_aioresponse.post(
        AUTH_LOGIN_URL,
        status=204,
        repeat=True,
    )

    assert not await ovoenergy_client.authenticate(USERNAME, PASSWORD)


@pytest.mark.asyncio
async def test_auth_code_unknown(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test auth token not found."""
    mock_aioresponse.clear()
    mock_aioresponse.post(
        AUTH_LOGIN_URL,
        status=200,
        payload={"code": "Unknown"},
        repeat=True,
    )

    assert not await ovoenergy_client.authenticate(USERNAME, PASSWORD)


@pytest.mark.asyncio
async def test_auth_token_not_found(
    ovoenergy_client: OVOEnergy,
    mock_aioresponse: aioresponses,
) -> None:
    """Test auth token not found."""
    mock_aioresponse.clear()
    mock_aioresponse.post(
        AUTH_LOGIN_URL,
        payload=RESPONSE_JSON_AUTH,
        status=200,
        repeat=True,
    )
    mock_aioresponse.get(
        AUTH_TOKEN_URL,
        status=404,
        repeat=True,
    )

    with pytest.raises(OVOEnergyAPINotFound):
        await ovoenergy_client.authenticate(USERNAME, PASSWORD)