Files
mealie/tests/integration_tests/user_household_tests/test_group_mealplan.py
T

663 lines
27 KiB
Python

import random
from datetime import UTC, date, datetime, timedelta
from uuid import UUID
import pytest
from dateutil.tz import tzlocal
from fastapi.testclient import TestClient
from mealie.schema.household.household import HouseholdSummary
from mealie.schema.labels.multi_purpose_label import MultiPurposeLabelSave, MultiPurposeLabelSummary
from mealie.schema.meal_plan.new_meal import PlanEntryCreate
from mealie.schema.meal_plan.plan_rules import PlanRulesDay, PlanRulesOut, PlanRulesSave, PlanRulesType
from mealie.schema.recipe.recipe import RecipeDraft
from mealie.schema.recipe.recipe_ingredient import IngredientFoodOut, IngredientFoodSave, RecipeIngredient
from mealie.schema.recipe.recipe_organizers import CategoryOut, CategorySave, TagOut, TagSave
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
def route_all_slice(page: int, perPage: int, start_date: str, end_date: str):
return (
f"{api_routes.households_mealplans}?page={page}&perPage={perPage}&start_date={start_date}&end_date={end_date}"
)
def create_recipe(
unique_user: TestUser,
tags: list[TagOut] | None = None,
categories: list[CategoryOut] | None = None,
foods: list[IngredientFoodOut] | None = None,
):
return unique_user.repos.recipes.create(
RecipeDraft(
user_id=unique_user.user_id,
group_id=UUID(unique_user.group_id),
name=random_string(),
tags=tags or [],
recipe_category=categories or [],
recipe_ingredient=[RecipeIngredient(food=food) for food in foods or []],
)
)
def create_food_with_label(unique_user: TestUser, label: MultiPurposeLabelSummary) -> IngredientFoodOut:
return unique_user.repos.ingredient_foods.create(
IngredientFoodSave(name=random_string(), group_id=UUID(unique_user.group_id), label_id=label.id)
)
def create_label(unique_user: TestUser) -> MultiPurposeLabelSummary:
return unique_user.repos.group_multi_purpose_labels.create(
MultiPurposeLabelSave(name=random_string(), group_id=UUID(unique_user.group_id))
)
def create_rule(
unique_user: TestUser,
day: PlanRulesDay,
entry_type: PlanRulesType,
tags: list[TagOut] | None = None,
categories: list[CategoryOut] | None = None,
households: list[HouseholdSummary] | None = None,
food_labels: list[MultiPurposeLabelSummary] | None = None,
excluded_food_labels: list[MultiPurposeLabelSummary] | None = None,
):
qf_parts: list[str] = []
if tags:
qf_parts.append(f"tags.id CONTAINS ALL [{','.join([str(tag.id) for tag in tags])}]")
if categories:
qf_parts.append(f"recipe_category.id CONTAINS ALL [{','.join([str(cat.id) for cat in categories])}]")
if households:
qf_parts.append(f"household_id IN [{','.join([str(household.id) for household in households])}]")
if food_labels:
ids = ",".join([str(label.id) for label in food_labels])
qf_parts.append(f"recipe_ingredient.food.label_id IN [{ids}]")
if excluded_food_labels:
ids = ",".join([str(label.id) for label in excluded_food_labels])
qf_parts.append(f"recipe_ingredient.food.label_id NOT IN [{ids}]")
query_filter_string = " AND ".join(qf_parts)
return unique_user.repos.group_meal_plan_rules.create(
PlanRulesSave(
group_id=UUID(unique_user.group_id),
household_id=UUID(unique_user.household_id),
day=day,
entry_type=entry_type,
query_filter_string=query_filter_string,
)
)
def test_create_mealplan_no_recipe(api_client: TestClient, unique_user: TestUser):
title = random_string(length=25)
text = random_string(length=25)
new_plan = PlanEntryCreate(
date=datetime.now(UTC).date(), entry_type="breakfast", title=title, text=text
).model_dump()
new_plan["date"] = datetime.now(UTC).date().strftime("%Y-%m-%d")
response = api_client.post(api_routes.households_mealplans, json=new_plan, headers=unique_user.token)
assert response.status_code == 201
response_json = response.json()
assert response_json["title"] == title
assert response_json["text"] == text
def test_create_mealplan_with_recipe(api_client: TestClient, unique_user: TestUser):
recipe_name = random_string(length=25)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token)
assert response.status_code == 201
response = api_client.get(api_routes.recipes_slug(recipe_name), headers=unique_user.token)
recipe = response.json()
recipe_id = recipe["id"]
new_plan = PlanEntryCreate(date=datetime.now(UTC).date(), entry_type="dinner", recipe_id=recipe_id).model_dump(
by_alias=True
)
new_plan["date"] = datetime.now(UTC).date().strftime("%Y-%m-%d")
new_plan["recipeId"] = str(recipe_id)
response = api_client.post(api_routes.households_mealplans, json=new_plan, headers=unique_user.token)
response_json = response.json()
assert response.status_code == 201
assert response_json["recipe"]["slug"] == recipe_name
def create_mealplan_with_recipe(api_client: TestClient, unique_user: TestUser, **entry_fields) -> dict:
recipe = create_recipe(unique_user)
payload = {
"date": datetime.now(UTC).date().strftime("%Y-%m-%d"),
"entryType": "dinner",
"recipeId": str(recipe.id),
**entry_fields,
}
response = api_client.post(api_routes.households_mealplans, json=payload, headers=unique_user.token)
assert response.status_code == 201
return response.json()
def test_create_mealplan_recipe_scale_defaults_to_one(api_client: TestClient, unique_user: TestUser):
plan = create_mealplan_with_recipe(api_client, unique_user)
assert plan["recipeScale"] == 1
# the default is what's stored, not just what the create response reports
response = api_client.get(api_routes.households_mealplans_item_id(plan["id"]), headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipeScale"] == 1
stored = unique_user.repos.meals.get_one(plan["id"])
assert stored is not None
assert stored.recipe_scale == 1
def test_create_mealplan_with_recipe_scale(api_client: TestClient, unique_user: TestUser):
plan = create_mealplan_with_recipe(api_client, unique_user, recipeScale=2.5)
assert plan["recipeScale"] == 2.5
response = api_client.get(api_routes.households_mealplans_item_id(plan["id"]), headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipeScale"] == 2.5
def test_update_mealplan_recipe_scale(api_client: TestClient, unique_user: TestUser):
plan = create_mealplan_with_recipe(api_client, unique_user, recipeScale=2)
plan["recipeScale"] = 0.5
response = api_client.put(api_routes.households_mealplans_item_id(plan["id"]), json=plan, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipeScale"] == 0.5
response = api_client.get(api_routes.households_mealplans_item_id(plan["id"]), headers=unique_user.token)
assert response.json()["recipeScale"] == 0.5
# the scale belongs to the entry, so it's included when listing a date range
today = plan["date"]
response = api_client.get(route_all_slice(1, -1, today, today), headers=unique_user.token)
assert response.status_code == 200
listed = next(item for item in response.json()["items"] if item["id"] == plan["id"])
assert listed["recipeScale"] == 0.5
@pytest.mark.parametrize("recipe_scale", [0, -1])
def test_mealplan_recipe_scale_must_be_positive(api_client: TestClient, unique_user: TestUser, recipe_scale: float):
recipe = create_recipe(unique_user)
payload = {
"date": datetime.now(UTC).date().strftime("%Y-%m-%d"),
"entryType": "dinner",
"recipeId": str(recipe.id),
"recipeScale": recipe_scale,
}
response = api_client.post(api_routes.households_mealplans, json=payload, headers=unique_user.token)
assert response.status_code == 422
plan = create_mealplan_with_recipe(api_client, unique_user, recipeScale=2)
response = api_client.put(
api_routes.households_mealplans_item_id(plan["id"]),
json={**plan, "recipeScale": recipe_scale},
headers=unique_user.token,
)
assert response.status_code == 422
response = api_client.get(api_routes.households_mealplans_item_id(plan["id"]), headers=unique_user.token)
assert response.json()["recipeScale"] == 2
def test_crud_mealplan(api_client: TestClient, unique_user: TestUser):
new_plan = PlanEntryCreate(
date=datetime.now(UTC).date(),
entry_type="breakfast",
title=random_string(),
text=random_string(),
).model_dump()
# Create
new_plan["date"] = datetime.now(UTC).date().strftime("%Y-%m-%d")
response = api_client.post(api_routes.households_mealplans, json=new_plan, headers=unique_user.token)
response_json = response.json()
assert response.status_code == 201
plan_id = response_json["id"]
# Update
response_json["title"] = random_string()
response_json["text"] = random_string()
response = api_client.put(
api_routes.households_mealplans_item_id(plan_id), headers=unique_user.token, json=response_json
)
assert response.status_code == 200
assert response.json()["title"] == response_json["title"]
assert response.json()["text"] == response_json["text"]
# Delete
response = api_client.delete(api_routes.households_mealplans_item_id(plan_id), headers=unique_user.token)
assert response.status_code == 200
response = api_client.get(api_routes.households_mealplans_item_id(plan_id), headers=unique_user.token)
assert response.status_code == 404
def test_get_all_mealplans(api_client: TestClient, unique_user: TestUser):
for _ in range(3):
new_plan = PlanEntryCreate(
date=datetime.now(UTC).date(),
entry_type="breakfast",
title=random_string(),
text=random_string(),
).model_dump()
new_plan["date"] = datetime.now(UTC).date().strftime("%Y-%m-%d")
response = api_client.post(api_routes.households_mealplans, json=new_plan, headers=unique_user.token)
assert response.status_code == 201
response = api_client.get(
api_routes.households_mealplans, headers=unique_user.token, params={"page": 1, "perPage": -1}
)
assert response.status_code == 200
assert len(response.json()["items"]) >= 3
def test_get_slice_mealplans(api_client: TestClient, unique_user: TestUser):
# Make List of 10 dates from now to +10 days
dates = [datetime.now(UTC).date() + timedelta(days=x) for x in range(10)]
# Make a list of 10 meal plans
meal_plans = [
PlanEntryCreate(date=date, entry_type="breakfast", title=random_string(), text=random_string()).model_dump()
for date in dates
]
# Add the meal plans to the database
for meal_plan in meal_plans:
meal_plan["date"] = meal_plan["date"].strftime("%Y-%m-%d")
response = api_client.post(api_routes.households_mealplans, json=meal_plan, headers=unique_user.token)
assert response.status_code == 201
# Get meal slice of meal plans from database
slices = [dates, dates[1:2], dates[2:3], dates[3:4], dates[4:5]]
for date_range in slices:
start_date = date_range[0].strftime("%Y-%m-%d")
end_date = date_range[-1].strftime("%Y-%m-%d")
response = api_client.get(route_all_slice(1, -1, start_date, end_date), headers=unique_user.token)
assert response.status_code == 200
response_json = response.json()
for meal_plan in response_json["items"]:
assert meal_plan["date"] in [date.strftime("%Y-%m-%d") for date in date_range]
def test_get_mealplan_today(api_client: TestClient, unique_user: TestUser):
# Create Meal Plans for today
test_meal_plans = [
PlanEntryCreate(
date=datetime.now(tzlocal()).date(), entry_type="breakfast", title=random_string(), text=random_string()
).model_dump()
for _ in range(3)
]
# Add the meal plans to the database
for meal_plan in test_meal_plans:
meal_plan["date"] = meal_plan["date"].strftime("%Y-%m-%d")
response = api_client.post(api_routes.households_mealplans, json=meal_plan, headers=unique_user.token)
assert response.status_code == 201
# Get meal plan for today
response = api_client.get(api_routes.households_mealplans_today, headers=unique_user.token)
assert response.status_code == 200
response_json = response.json()
for meal_plan in response_json:
assert meal_plan["date"] == datetime.now(tzlocal()).date().strftime("%Y-%m-%d")
def create_entry(api_client: TestClient, unique_user: TestUser, **kwargs) -> dict:
data = PlanEntryCreate(entry_type="dinner", title=random_string(), **kwargs).model_dump(by_alias=True, mode="json")
response = api_client.post(api_routes.households_mealplans, json=data, headers=unique_user.token)
assert response.status_code == 201
return response.json()
@pytest.mark.parametrize(
"end_date_offset, status_code", [(None, 201), (6, 201), (0, 422), (5, 422), (7, 422), (-6, 422)]
)
def test_create_mealplan_end_date_must_be_a_week(
api_client: TestClient, unique_user: TestUser, end_date_offset: int | None, status_code: int
):
start = datetime.now(UTC).date()
end = start + timedelta(days=end_date_offset) if end_date_offset is not None else None
payload = {
"date": str(start),
"endDate": str(end) if end else None,
"entryType": "dinner",
"title": random_string(),
}
response = api_client.post(api_routes.households_mealplans, json=payload, headers=unique_user.token)
assert response.status_code == status_code
def test_get_slice_mealplans_week_entries(api_client: TestClient, unique_user: TestUser):
day = datetime.now(UTC).date() + timedelta(days=365)
day_entry = create_entry(api_client, unique_user, date=day)
week_start = day - timedelta(days=3)
week_entry = create_entry(api_client, unique_user, date=week_start, end_date=week_start + timedelta(days=6))
def get_ids(start: date, end: date, include_week_entries: bool) -> list[int]:
params = {"page": 1, "perPage": -1, "start_date": str(start), "end_date": str(end)}
if include_week_entries:
params["include_week_entries"] = "true"
response = api_client.get(api_routes.households_mealplans, params=params, headers=unique_user.token)
assert response.status_code == 200
return [item["id"] for item in response.json()["items"]]
# week entries are opt-in, so existing clients only see entries on a specific day
ids = get_ids(day, day, include_week_entries=False)
assert day_entry["id"] in ids
assert week_entry["id"] not in ids
# a week entry is returned for any range it overlaps, even one starting after the week does
ids = get_ids(day, day, include_week_entries=True)
assert day_entry["id"] in ids
assert week_entry["id"] in ids
# ranges entirely after or before the week don't return it
assert week_entry["id"] not in get_ids(day + timedelta(days=4), day + timedelta(days=10), True)
assert week_entry["id"] not in get_ids(day - timedelta(days=10), day - timedelta(days=4), True)
def test_get_mealplan_today_skips_week_entries(api_client: TestClient, unique_user: TestUser):
today = datetime.now(tzlocal()).date()
week_entry = create_entry(api_client, unique_user, date=today, end_date=today + timedelta(days=6))
response = api_client.get(api_routes.households_mealplans_today, headers=unique_user.token)
assert response.status_code == 200
assert week_entry["id"] not in [item["id"] for item in response.json()]
def test_get_mealplan_with_rules_categories_and_tags_filter(api_client: TestClient, unique_user: TestUser):
tags = [
unique_user.repos.tags.create(TagSave(name=random_string(), group_id=unique_user.group_id)) for _ in range(4)
]
categories = [
unique_user.repos.categories.create(CategorySave(name=random_string(), group_id=unique_user.group_id))
for _ in range(4)
]
[
create_recipe(unique_user, tags=[tag], categories=[category])
for tag, category in zip(tags, categories, strict=True)
]
[create_recipe(unique_user) for _ in range(5)]
i = random.randint(0, 3)
tag = tags[i]
category = categories[i]
rule = create_rule(
unique_user,
day=PlanRulesDay.saturday,
entry_type=PlanRulesType.breakfast,
tags=[tag],
categories=[category],
)
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
recipe_data = response.json()["recipe"]
assert recipe_data["tags"][0]["name"] == tag.name
assert recipe_data["recipeCategory"][0]["name"] == category.name
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_food_label_filter(api_client: TestClient, unique_user: TestUser):
label = create_label(unique_user)
other_label = create_label(unique_user)
recipe = create_recipe(unique_user, foods=[create_food_with_label(unique_user, label)])
[create_recipe(unique_user, foods=[create_food_with_label(unique_user, other_label)]) for _ in range(5)]
rule = create_rule(
unique_user,
day=PlanRulesDay.saturday,
entry_type=PlanRulesType.breakfast,
food_labels=[label],
)
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_excluded_food_label_filter(api_client: TestClient, unique_user: TestUser):
"""Excluding a food label should exclude the whole recipe, not just the matching ingredient."""
excluded_label = create_label(unique_user)
other_label = create_label(unique_user)
# this recipe contains both an excluded ingredient and an allowed one, so it must not be picked
create_recipe(
unique_user,
foods=[
create_food_with_label(unique_user, excluded_label),
create_food_with_label(unique_user, other_label),
],
)
recipe = create_recipe(unique_user, foods=[create_food_with_label(unique_user, other_label)])
rule = create_rule(
unique_user,
day=PlanRulesDay.saturday,
entry_type=PlanRulesType.breakfast,
food_labels=[other_label],
excluded_food_labels=[excluded_label],
)
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_date_and_type_filter(api_client: TestClient, unique_user: TestUser):
tags = [
unique_user.repos.tags.create(TagSave(name=random_string(), group_id=unique_user.group_id)) for _ in range(4)
]
recipes = [create_recipe(unique_user, tags=[tag]) for tag in tags]
[create_recipe(unique_user) for _ in range(5)]
rules: list[PlanRulesOut] = []
rules.append(
create_rule(unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.breakfast, tags=[tags[0]])
)
rules.append(create_rule(unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.dinner, tags=[tags[1]]))
rules.append(create_rule(unique_user, day=PlanRulesDay.sunday, entry_type=PlanRulesType.breakfast, tags=[tags[2]]))
rules.append(create_rule(unique_user, day=PlanRulesDay.sunday, entry_type=PlanRulesType.dinner, tags=[tags[3]]))
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipes[0].slug
finally:
for rule in rules:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_rules_for_unset_day_skips_weekday_specific_rules(unique_user: TestUser):
any_day_rule = create_rule(unique_user, day=PlanRulesDay.unset, entry_type=PlanRulesType.unset)
saturday_rule = create_rule(unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.unset)
try:
rules = unique_user.repos.group_meal_plan_rules.get_rules(PlanRulesDay.unset, PlanRulesType.breakfast)
assert {rule.id for rule in rules} == {any_day_rule.id}
rules = unique_user.repos.group_meal_plan_rules.get_rules(PlanRulesDay.saturday, PlanRulesType.breakfast)
assert {rule.id for rule in rules} == {any_day_rule.id, saturday_rule.id}
finally:
unique_user.repos.group_meal_plan_rules.delete(any_day_rule.id)
unique_user.repos.group_meal_plan_rules.delete(saturday_rule.id)
def test_get_mealplan_with_rules_week_entry_skips_weekday_specific_rules(api_client: TestClient, unique_user: TestUser):
any_day_tag, saturday_tag = [
unique_user.repos.tags.create(TagSave(name=random_string(), group_id=unique_user.group_id)) for _ in range(2)
]
recipe = create_recipe(unique_user, tags=[any_day_tag])
[create_recipe(unique_user) for _ in range(5)]
rules = [
create_rule(unique_user, day=PlanRulesDay.unset, entry_type=PlanRulesType.breakfast, tags=[any_day_tag]),
create_rule(unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.breakfast, tags=[saturday_tag]),
]
try:
# a day entry on a Saturday gets both rules, which no recipe satisfies
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 404
# a week entry starting on that Saturday isn't on a specific weekday, so the Saturday rule is skipped
payload = {"date": "2023-02-25", "endDate": "2023-03-03", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
assert response.json()["endDate"] == "2023-03-03"
finally:
for rule in rules:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_includes_other_households(
api_client: TestClient, unique_user: TestUser, h2_user: TestUser
):
tag = h2_user.repos.tags.create(TagSave(name=random_string(), group_id=h2_user.group_id))
recipe = create_recipe(h2_user, tags=[tag])
rule = create_rule(unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.breakfast, tags=[tag])
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_households_filter(api_client: TestClient, unique_user: TestUser, h2_user: TestUser):
tag = unique_user.repos.tags.create(TagSave(name=random_string(), group_id=unique_user.group_id))
recipe = create_recipe(unique_user, tags=[tag])
[create_recipe(h2_user, tags=[tag]) for _ in range(10)]
household = unique_user.repos.households.get_by_slug_or_id(unique_user.household_id)
assert household
rule = create_rule(
unique_user, day=PlanRulesDay.saturday, entry_type=PlanRulesType.breakfast, tags=[tag], households=[household]
)
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def test_get_mealplan_with_rules_households_filter_includes_any_households(
api_client: TestClient, unique_user: TestUser, h2_user: TestUser
):
tag = unique_user.repos.tags.create(TagSave(name=random_string(), group_id=unique_user.group_id))
recipe = create_recipe(h2_user, tags=[tag])
household = unique_user.repos.households.get_by_slug_or_id(unique_user.household_id)
assert household
h2_household = unique_user.repos.households.get_by_slug_or_id(h2_user.household_id)
assert h2_household
rule = create_rule(
unique_user,
day=PlanRulesDay.saturday,
entry_type=PlanRulesType.breakfast,
tags=[tag],
households=[household, h2_household],
)
try:
payload = {"date": "2023-02-25", "entryType": "breakfast"}
response = api_client.post(api_routes.households_mealplans_random, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert response.json()["recipe"]["slug"] == recipe.slug
finally:
unique_user.repos.group_meal_plan_rules.delete(rule.id)
def _plan_payload(recipe_id) -> dict:
return {
"date": datetime.now(UTC).date().strftime("%Y-%m-%d"),
"entryType": "dinner",
"recipeId": str(recipe_id),
}
def test_create_mealplan_rejects_recipe_from_other_group(
api_client: TestClient, unique_user: TestUser, g2_user: TestUser
):
recipe = create_recipe(unique_user)
response = api_client.post(api_routes.households_mealplans, json=_plan_payload(recipe.id), headers=g2_user.token)
assert response.status_code == 404
assert recipe.name not in response.text
def test_update_mealplan_rejects_recipe_from_other_group(
api_client: TestClient, unique_user: TestUser, g2_user: TestUser
):
foreign_recipe = create_recipe(unique_user)
own_recipe = create_recipe(g2_user)
plan = api_client.post(
api_routes.households_mealplans, json=_plan_payload(own_recipe.id), headers=g2_user.token
).json()
plan["recipeId"] = str(foreign_recipe.id)
response = api_client.put(api_routes.households_mealplans_item_id(plan["id"]), json=plan, headers=g2_user.token)
assert response.status_code == 404
assert foreign_recipe.name not in response.text
response = api_client.get(api_routes.households_mealplans_item_id(plan["id"]), headers=g2_user.token)
assert response.json()["recipeId"] == str(own_recipe.id)
def test_mealplan_accepts_recipe_from_other_household_in_group(
api_client: TestClient, unique_user: TestUser, h2_user: TestUser
):
recipe = create_recipe(h2_user)
response = api_client.post(
api_routes.households_mealplans, json=_plan_payload(recipe.id), headers=unique_user.token
)
assert response.status_code == 201
assert response.json()["recipe"]["id"] == str(recipe.id)