File: test_store.py

package info (click to toggle)
python-aiohasupervisor 0.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: python: 4,666; sh: 37; makefile: 3
file content (319 lines) | stat: -rw-r--r-- 10,056 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
"""Tests for store supervisor client."""

from json import loads

from aioresponses import aioresponses
import pytest
from yarl import URL

from aiohasupervisor import SupervisorClient
from aiohasupervisor.exceptions import (
    AddonNotSupportedArchitectureError,
    AddonNotSupportedHomeAssistantVersionError,
    AddonNotSupportedMachineTypeError,
    SupervisorBadRequestError,
    SupervisorError,
)
from aiohasupervisor.models import StoreAddonUpdate, StoreAddRepository
from aiohasupervisor.models.addons import StoreAddonInstall

from . import load_fixture
from .const import SUPERVISOR_URL


async def test_store_info(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store info API."""
    responses.get(
        f"{SUPERVISOR_URL}/store", status=200, body=load_fixture("store_info.json")
    )
    store = await supervisor_client.store.info()

    assert len(store.repositories) == 6
    assert store.repositories[0].slug == "local"
    assert store.repositories[0].name == "Local add-ons"

    assert len(store.addons) == 88
    assert store.addons[0].slug == "d5369777_music_assistant"
    assert store.addons[0].name == "Music Assistant Server"


async def test_store_addons_list(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addons list API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/addons",
        status=200,
        body=load_fixture("store_addons_list.json"),
    )
    addons = await supervisor_client.store.addons_list()

    assert len(addons) == 88
    assert addons[0].slug == "d5369777_music_assistant"
    assert addons[0].name == "Music Assistant Server"


async def test_store_repositories_list(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store repositories list API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/repositories",
        status=200,
        body=load_fixture("store_repositories_list.json"),
    )
    repositories = await supervisor_client.store.repositories_list()

    assert len(repositories) == 6
    assert repositories[0].slug == "local"
    assert repositories[0].name == "Local add-ons"


async def test_store_addon_info(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addon info API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto",
        status=200,
        body=load_fixture("store_addon_info.json"),
    )
    info = await supervisor_client.store.addon_info("core_mosquitto")

    assert info.name == "Mosquitto broker"
    assert info.slug == "core_mosquitto"
    assert info.signed is True
    assert info.supervisor_api is False
    assert info.supervisor_role == "default"


async def test_store_addon_changelog(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addon info changelog API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/changelog",
        status=200,
        body=load_fixture("store_addon_changelog.txt"),
    )
    changelog = await supervisor_client.store.addon_changelog("core_mosquitto")
    assert changelog.startswith("# Changelog")


async def test_store_addon_documentation(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addon documentation API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/documentation",
        status=200,
        body=load_fixture("store_addon_documentation.txt"),
    )
    documentation = await supervisor_client.store.addon_documentation("core_mosquitto")
    assert documentation.startswith("# Home Assistant Add-on: Mosquitto broker")


@pytest.mark.parametrize(
    ("options", "has_timeout"),
    [
        (None, False),
        (StoreAddonInstall(), False),
        (StoreAddonInstall(background=False), False),
        (StoreAddonInstall(background=True), True),
    ],
)
async def test_store_addon_install(
    responses: aioresponses,
    supervisor_client: SupervisorClient,
    options: StoreAddonInstall | None,
    has_timeout: bool,  # noqa: FBT001
) -> None:
    """Test store addon install API."""
    responses.post(f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install", status=200)

    assert (
        await supervisor_client.store.install_addon("core_mosquitto", options)
    ) is None
    assert (
        bool(
            responses.requests[
                ("POST", URL(f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install"))
            ][0].kwargs["timeout"]
        )
        is has_timeout
    )


@pytest.mark.parametrize(
    ("options", "has_timeout"),
    [
        (None, False),
        (StoreAddonUpdate(), False),
        (StoreAddonUpdate(backup=True), False),
        (StoreAddonUpdate(background=False), False),
        (StoreAddonUpdate(background=True), True),
    ],
)
async def test_store_addon_update(
    responses: aioresponses,
    supervisor_client: SupervisorClient,
    options: StoreAddonUpdate | None,
    has_timeout: bool,  # noqa: FBT001
) -> None:
    """Test store addon update API."""
    responses.post(f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update", status=200)

    assert (
        await supervisor_client.store.update_addon("core_mosquitto", options)
    ) is None
    assert (
        bool(
            responses.requests[
                ("POST", URL(f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update"))
            ][0].kwargs["timeout"]
        )
        is has_timeout
    )


async def test_store_addon_availability(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addon availability API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability", status=200
    )

    assert (await supervisor_client.store.addon_availability("core_mosquitto")) is None


@pytest.mark.parametrize(
    ("error_fixture", "error_key", "exc_type"),
    [
        (
            "store_addon_availability_error_architecture.json",
            "addon_not_supported_architecture_error",
            AddonNotSupportedArchitectureError,
        ),
        (
            "store_addon_availability_error_machine.json",
            "addon_not_supported_machine_type_error",
            AddonNotSupportedMachineTypeError,
        ),
        (
            "store_addon_availability_error_home_assistant.json",
            "addon_not_supported_home_assistant_version_error",
            AddonNotSupportedHomeAssistantVersionError,
        ),
        (
            "store_addon_availability_error_other.json",
            None,
            SupervisorBadRequestError,
        ),
    ],
)
async def test_store_addon_availability_error(
    responses: aioresponses,
    supervisor_client: SupervisorClient,
    error_fixture: str,
    error_key: str | None,
    exc_type: type[SupervisorError],
) -> None:
    """Test store addon availability errors."""
    error_body = load_fixture(error_fixture)
    error_data = loads(error_body)

    def check_availability_error(err: SupervisorError) -> bool:
        assert err.error_key == error_key
        assert err.extra_fields == error_data["extra_fields"]
        return True

    # Availability API
    responses.get(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/availability",
        status=400,
        body=error_body,
    )
    with pytest.raises(
        exc_type, match=error_data["message"], check=check_availability_error
    ):
        await supervisor_client.store.addon_availability("core_mosquitto")

    # Install API
    responses.post(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/install",
        status=400,
        body=error_body,
    )
    with pytest.raises(
        exc_type, match=error_data["message"], check=check_availability_error
    ):
        await supervisor_client.store.install_addon("core_mosquitto")

    # Update API
    responses.post(
        f"{SUPERVISOR_URL}/store/addons/core_mosquitto/update",
        status=400,
        body=error_body,
    )
    with pytest.raises(
        exc_type, match=error_data["message"], check=check_availability_error
    ):
        await supervisor_client.store.update_addon("core_mosquitto")


async def test_store_reload(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store reload API."""
    responses.post(f"{SUPERVISOR_URL}/store/reload", status=200)

    assert (await supervisor_client.store.reload()) is None
    assert responses.requests.keys() == {
        ("POST", URL(f"{SUPERVISOR_URL}/store/reload"))
    }


async def test_store_repository_info(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store repository info API."""
    responses.get(
        f"{SUPERVISOR_URL}/store/repositories/core",
        status=200,
        body=load_fixture("store_repository_info.json"),
    )
    repository = await supervisor_client.store.repository_info("core")
    assert repository.slug == "core"
    assert repository.name == "Official add-ons"
    assert repository.source == "core"


async def test_store_add_repository(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store add repository API."""
    responses.post(f"{SUPERVISOR_URL}/store/repositories", status=200)

    assert (
        await supervisor_client.store.add_repository(
            StoreAddRepository(repository="test")
        )
    ) is None
    assert responses.requests.keys() == {
        ("POST", URL(f"{SUPERVISOR_URL}/store/repositories"))
    }


async def test_store_remove_repository(
    responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
    """Test store addon info API."""
    responses.delete(f"{SUPERVISOR_URL}/store/repositories/test", status=200)

    assert (await supervisor_client.store.remove_repository("test")) is None
    assert responses.requests.keys() == {
        ("DELETE", URL(f"{SUPERVISOR_URL}/store/repositories/test"))
    }