From 72c62e8cfaa6e5c2f439dbdae217b31505372f2d Mon Sep 17 00:00:00 2001 From: Luna-81 Date: Tue, 13 Oct 2026 14:21:54 +0000 Subject: [PATCH] fix: apply household visibility preference to recipes imported from zip (#8557) Co-authored-by: jl1204 Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com> --- mealie/services/recipe/recipe_service.py | 9 ++- .../user_recipe_tests/test_recipe_crud.py | 5 +- .../test_recipe_import_as.py | 68 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tests/integration_tests/user_recipe_tests/test_recipe_import_as.py diff --git a/mealie/services/recipe/recipe_service.py b/mealie/services/recipe/recipe_service.py index ae34df0f7..0383c67fd 100644 --- a/mealie/services/recipe/recipe_service.py +++ b/mealie/services/recipe/recipe_service.py @@ -500,7 +500,14 @@ class RecipeService(RecipeServiceBase): if recipe_dict is None: raise exceptions.UnexpectedNone("No json data found in Zip") - recipe = self.create_one(Recipe(**self.clean_recipe_dict(recipe_dict))) + recipe_data = Recipe(**self.clean_recipe_dict(recipe_dict)) + + if self.household.preferences is not None: + if recipe_data.settings is None: + recipe_data.settings = RecipeSettings() + recipe_data.settings.public = self.household.preferences.recipe_public + + recipe = self.create_one(recipe_data) if recipe and recipe.id: data_service = RecipeDataService(recipe.id) diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_crud.py b/tests/integration_tests/user_recipe_tests/test_recipe_crud.py index 1e5e49a1e..3b9f27f98 100644 --- a/tests/integration_tests/user_recipe_tests/test_recipe_crud.py +++ b/tests/integration_tests/user_recipe_tests/test_recipe_crud.py @@ -535,11 +535,14 @@ def test_create_recipe_from_zip_reimport_preserves_contents(api_client: TestClie "rating", "orgURL", "extras", - "settings", "nutrition", ]: assert imported[field] == original[field], field + imported_settings = imported["settings"].copy() + imported_settings["public"] = original["settings"]["public"] + assert imported_settings == original["settings"] + assert [c["name"] for c in imported["recipeCategory"]] == [category["name"]] assert [t["name"] for t in imported["tags"]] == [tag["name"]] assert [t["name"] for t in imported["tools"]] == [tool["name"]] diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_import_as.py b/tests/integration_tests/user_recipe_tests/test_recipe_import_as.py new file mode 100644 index 000000000..293c6c726 --- /dev/null +++ b/tests/integration_tests/user_recipe_tests/test_recipe_import_as.py @@ -0,0 +1,68 @@ +import json +import zipfile +from io import BytesIO + +from fastapi.testclient import TestClient + +from tests.utils import api_routes +from tests.utils.factories import random_string +from tests.utils.fixture_schemas import TestUser + + +def test_zip_import_applies_household_public_preference(api_client: TestClient, unique_user: TestUser) -> None: + """Recipes imported from a zip should follow the household's recipe_public preference. + + Regression test for https://github.com/mealie-recipes/mealie/issues/7172 + """ + # 1. Make sure the household default is public + response = api_client.get(api_routes.households_preferences, headers=unique_user.token) + assert response.status_code == 200 + prefs = response.json() + prefs["recipePublic"] = True + + response = api_client.put(api_routes.households_preferences, json=prefs, headers=unique_user.token) + assert response.status_code == 200 + + # 2. Create a recipe, then force its `public` setting to False + recipe_name = random_string() + response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token) + assert response.status_code == 201 + slug = response.json() + + recipe = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token).json() + recipe["settings"]["public"] = False + response = api_client.put(api_routes.recipes_slug(slug), json=recipe, headers=unique_user.token) + assert response.status_code == 200 + + # 3. Export it as a zip + response = api_client.post(api_routes.shared_recipes, json={"recipeId": recipe["id"]}, headers=unique_user.token) + assert response.status_code == 201 + token_id = response.json()["id"] + + response = api_client.get(api_routes.recipes_shared_token_id_zip(token_id)) + assert response.status_code == 200 + zip_bytes = BytesIO(response.content) + + # Sanity check: the exported zip really contains a private recipe + with zipfile.ZipFile(zip_bytes) as zip_fp: + json_name = next(n for n in zip_fp.namelist() if n.endswith(".json")) + exported = json.loads(zip_fp.read(json_name)) + assert exported["settings"]["public"] is False + + zip_bytes.seek(0) + + # 4. Import the zip + response = api_client.post( + api_routes.recipes_create_zip, + files={"archive": ("recipe.zip", zip_bytes, "application/zip")}, + headers=unique_user.token, + ) + assert response.status_code == 201 + imported_slug = response.json() + assert imported_slug != slug + + # 5. The imported recipe should be public, following the household preference + imported = api_client.get(api_routes.recipes_slug(imported_slug), headers=unique_user.token).json() + assert imported["settings"]["public"] is True, ( + "Imported recipe should inherit the household's recipe_public=True preference" + )