fix: error when trying to change recipe image (#5771)

Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
Mario Džoić
2025-08-16 10:41:46 +02:00
committed by GitHub
parent 6cbc308d83
commit c41a4a52ed
7 changed files with 115 additions and 26 deletions

View File

@@ -7,6 +7,7 @@ from fastapi.testclient import TestClient
from mealie.schema.cookbook.cookbook import SaveCookBook
from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_category import TagSave
from tests import data
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
@@ -371,3 +372,53 @@ def test_cookbooks_from_other_households(api_client: TestClient, unique_user: Te
response = api_client.get(api_routes.recipes, params={"cookbook": h2_cookbook.slug}, headers=unique_user.token)
assert response.status_code == 200
@pytest.mark.parametrize("is_private_household", [True, False])
@pytest.mark.parametrize("household_lock_recipe_edits", [True, False])
def test_update_recipe_image_from_other_households(
api_client: TestClient,
unique_user: TestUser,
h2_user: TestUser,
is_private_household: bool,
household_lock_recipe_edits: bool,
):
household = unique_user.repos.households.get_one(h2_user.household_id)
assert household and household.preferences
household.preferences.private_household = is_private_household
household.preferences.lock_recipe_edits_from_other_households = household_lock_recipe_edits
unique_user.repos.household_preferences.update(household.id, household.preferences)
response = api_client.post(api_routes.recipes, json={"name": random_string()}, headers=h2_user.token)
assert response.status_code == 201
h2_recipe = h2_user.repos.recipes.get_one(response.json())
assert h2_recipe and h2_recipe.id
h2_recipe_id = str(h2_recipe.id)
response = api_client.get(api_routes.recipes_slug(h2_recipe_id), headers=unique_user.token)
assert response.status_code == 200
recipe_json = response.json()
assert recipe_json["id"] == h2_recipe_id
image_version = response.json()["image"]
data_payload = {"extension": "jpg"}
file_payload = {"image": data.images_test_image_1.read_bytes()}
response = api_client.put(
api_routes.recipes_slug_image(recipe_json["slug"]),
data=data_payload,
files=file_payload,
headers=unique_user.token,
)
if household_lock_recipe_edits:
assert response.status_code == 403
response = api_client.get(api_routes.recipes_slug(h2_recipe_id), headers=unique_user.token)
recipe_respons = response.json()
assert recipe_respons["image"] == image_version
else:
assert response.status_code == 200
response = api_client.get(api_routes.recipes_slug(h2_recipe_id), headers=unique_user.token)
recipe_respons = response.json()
assert recipe_respons["image"] is not None

View File

@@ -6,6 +6,7 @@ from fastapi.testclient import TestClient
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_settings import RecipeSettings
from tests import data
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
@@ -180,3 +181,33 @@ def test_admin_can_delete_locked_recipe_owned_by_another_user(
response = api_client.delete(api_routes.recipes_slug(slug), headers=admin_user.token)
assert response.status_code == 200
def test_user_can_update_recipe_image(api_client: TestClient, unique_user: TestUser):
data_payload = {"extension": "jpg"}
file_payload = {"image": data.images_test_image_1.read_bytes()}
household = unique_user.repos.households.get_one(unique_user.household_id)
assert household and household.preferences
household.preferences.private_household = True
household.preferences.lock_recipe_edits_from_other_households = True
unique_user.repos.household_preferences.update(household.id, household.preferences)
response = api_client.post(api_routes.recipes, json={"name": random_string()}, headers=unique_user.token)
assert response.status_code == 201
recipe_json = unique_user.repos.recipes.get_one(response.json())
assert recipe_json and recipe_json.id
assert recipe_json.image is None
recipe_id = str(recipe_json.id)
response = api_client.put(
api_routes.recipes_slug_image(recipe_json.slug),
data=data_payload,
files=file_payload,
headers=unique_user.token,
)
assert response.status_code == 200
response = api_client.get(api_routes.recipes_slug(recipe_id), headers=unique_user.token)
recipe_respons = response.json()
assert recipe_respons["image"] is not None