feat: Add feedback for bad JSON in HTML-JSON page (#7956)

This commit is contained in:
Michael Genson
2026-07-26 17:16:31 -05:00
committed by GitHub
parent 318f05f2ab
commit 5e886b11a1
5 changed files with 144 additions and 8 deletions

View File

@@ -29,6 +29,7 @@ from mealie.schema.recipe.recipe_notes import RecipeNote
from mealie.schema.recipe.recipe_tool import RecipeToolSave
from mealie.services.recipe.recipe_data_service import RecipeDataService
from mealie.services.scraper.recipe_scraper import DEFAULT_SCRAPER_STRATEGIES
from mealie.services.scraper.scraper import ParserErrors
from tests import utils
from tests.utils import api_routes
from tests.utils.factories import random_int, random_string
@@ -335,6 +336,46 @@ def test_create_by_html_or_json_stream_error(
assert "error" in event_types
@pytest.mark.parametrize(
"data",
[
# valid JSON, but not tagged as a schema.org Recipe
json.dumps({"name": "Test", "recipeIngredient": ["1 cup flour"], "recipeInstructions": [{"text": "Mix"}]}),
# not valid JSON at all
'{"name": "Test",,,}',
# no recipe data whatsoever
"<html><body>not a recipe</body></html>",
],
ids=["missing-schema-declaration", "malformed-json", "no-recipe-data"],
)
def test_create_by_html_or_json_stream_invalid_data(api_client: TestClient, unique_user: TestUser, data: str):
"""Unparseable data must report an error to the client, rather than silently ending the stream"""
response = api_client.post(
api_routes.recipes_create_html_or_json_stream,
json={"data": data},
headers=unique_user.token,
)
assert response.status_code == 200
events = parse_sse_events(response.text)
error_events = [e for e in events if e["event"] == "error"]
assert error_events
assert error_events[0]["data"]["message"] == ParserErrors.BAD_RECIPE_DATA.value
def test_create_by_html_or_json_invalid_data(api_client: TestClient, unique_user: TestUser):
response = api_client.post(
api_routes.recipes_create_html_or_json,
json={"data": json.dumps({"name": "Test"})},
headers=unique_user.token,
)
assert response.status_code == 400
assert response.json()["detail"]["message"] == ParserErrors.BAD_RECIPE_DATA.value
def test_create_recipe_from_zip(api_client: TestClient, unique_user: TestUser, tempdir: str):
database = unique_user.repos
recipe_name = random_string()