Skip to content

Commit

Permalink
Handle detached addons correctly and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 committed Dec 16, 2024
1 parent 30e4253 commit 1930fc3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 55 deletions.
5 changes: 4 additions & 1 deletion supervisor/api/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ def _extract_addon(self, request: web.Request, installed=False) -> AnyAddon:
addon_slug: str = request.match_info.get("addon")

if not (addon := self.sys_addons.get(addon_slug)):
raise APINotFound(f"Addon {addon_slug} does not exist in the store")
raise APINotFound(f"Addon {addon_slug} does not exist")

if installed and not addon.is_installed:
raise APIError(f"Addon {addon_slug} is not installed")

if not installed and addon.is_installed:
if not addon.addon_store:
raise APINotFound(f"Addon {addon_slug} does not exist in the store")
return addon.addon_store

return addon

def _extract_repository(self, request: web.Request) -> Repository:
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async def test_job_manual_cleanup(self) -> None:
resp = await api_client.get(f"/jobs/{test.job_id}")
assert resp.status == 404
result = await resp.json()
assert result["message"] == f"No job found with id {test.job_id}"
assert result["message"] == "Job does not exist"


@pytest.mark.parametrize(
Expand Down
63 changes: 16 additions & 47 deletions tests/api/test_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,25 +264,6 @@ async def test_api_update_mount(
coresys.mounts.save_data.assert_called_once()


async def test_api_update_error_mount_missing(
api_client: TestClient, mount_propagation
):
"""Test update mount API errors when mount does not exist."""
resp = await api_client.put(
"/mounts/backup_test",
json={
"type": "cifs",
"usage": "backup",
"server": "backup.local",
"share": "new_backups",
},
)
assert resp.status == 400
result = await resp.json()
assert result["result"] == "error"
assert result["message"] == "No mount exists with name backup_test"


async def test_api_update_dbus_error_mount_remains(
api_client: TestClient,
all_dbus_services: dict[str, DBusServiceMock],
Expand Down Expand Up @@ -399,20 +380,6 @@ async def test_api_reload_mount(
]


async def test_api_reload_error_mount_missing(
api_client: TestClient, mount_propagation
):
"""Test reload mount API errors when mount does not exist."""
resp = await api_client.post("/mounts/backup_test/reload")
assert resp.status == 400
result = await resp.json()
assert result["result"] == "error"
assert (
result["message"]
== "Cannot reload 'backup_test', no mount exists with that name"
)


async def test_api_delete_mount(
api_client: TestClient,
coresys: CoreSys,
Expand All @@ -435,20 +402,6 @@ async def test_api_delete_mount(
coresys.mounts.save_data.assert_called_once()


async def test_api_delete_error_mount_missing(
api_client: TestClient, mount_propagation
):
"""Test delete mount API errors when mount does not exist."""
resp = await api_client.delete("/mounts/backup_test")
assert resp.status == 400
result = await resp.json()
assert result["result"] == "error"
assert (
result["message"]
== "Cannot remove 'backup_test', no mount exists with that name"
)


async def test_api_create_backup_mount_sets_default(
api_client: TestClient,
coresys: CoreSys,
Expand Down Expand Up @@ -903,3 +856,19 @@ async def test_api_read_only_backup_mount_invalid(
result = await resp.json()
assert result["result"] == "error"
assert "Backup mounts cannot be read only" in result["message"]


@pytest.mark.parametrize(
("method", "url"),
[
("put", "/mounts/bad"),
("delete", "/mounts/bad"),
("post", "/mounts/bad/reload"),
],
)
async def test_mount_not_found(api_client: TestClient, method: str, url: str):
"""Test mount not found error."""
resp = await api_client.request(method, url)
assert resp.status == 404
resp = await resp.json()
assert resp["message"] == "No mount exists with name bad"
9 changes: 3 additions & 6 deletions tests/api/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async def test_api_detached_addon_changelog(
resp = await api_client.get(f"/{resource}/{install_addon_ssh.slug}/changelog")
assert resp.status == 200
result = await resp.text()
assert result == "Addon local_ssh with version latest does not exist in the store"
assert result == "Addon local_ssh does not exist in the store"


@pytest.mark.parametrize("resource", ["store/addons", "addons"])
Expand Down Expand Up @@ -280,7 +280,7 @@ async def test_api_detached_addon_documentation(
resp = await api_client.get(f"/{resource}/{install_addon_ssh.slug}/documentation")
assert resp.status == 200
result = await resp.text()
assert result == "Addon local_ssh with version latest does not exist in the store"
assert result == "Addon local_ssh does not exist in the store"


async def get_message(resp: ClientResponse, json_expected: bool) -> str:
Expand Down Expand Up @@ -315,10 +315,7 @@ async def test_store_addon_not_found(
"""Test store addon not found error."""
resp = await api_client.request(method, url)
assert resp.status == 404
assert (
await get_message(resp, json_expected)
== "Addon bad does not exist in the store"
)
assert await get_message(resp, json_expected) == "Addon bad does not exist"


@pytest.mark.parametrize(
Expand Down

0 comments on commit 1930fc3

Please sign in to comment.