1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 21:45:25 +02:00

feature: proper multi-tenant-support (#969)(WIP)

* update naming

* refactor tests to use shared structure

* shorten names

* add tools test case

* refactor to support multi-tenant

* set group_id on creation

* initial refactor for multitenant tags/cats

* spelling

* additional test case for same valued resources

* fix recipe update tests

* apply indexes to foreign keys

* fix performance regressions

* handle unknown exception

* utility decorator for function debugging

* migrate recipe_id to UUID

* GUID for recipes

* remove unused import

* move image functions into package

* move utilities to packages dir

* update import

* linter

* image image and asset routes

* update assets and images to use UUIDs

* fix migration base

* image asset test coverage

* use ids for categories and tag crud functions

* refactor recipe organizer test suite to reduce duplication

* add uuid serlization utility

* organizer base router

* slug routes testing and fixes

* fix postgres error

* adopt UUIDs

* move tags, categories, and tools under "organizers" umbrella

* update composite label

* generate ts types

* fix import error

* update frontend types

* fix type errors

* fix postgres errors

* fix #978

* add null check for title validation

* add note in docs on multi-tenancy
This commit is contained in:
Hayden 2022-02-13 12:23:42 -09:00 committed by GitHub
parent 9a82a172cb
commit c617251f4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
157 changed files with 1866 additions and 1578 deletions

View file

@ -7,7 +7,8 @@ from fastapi.testclient import TestClient
from mealie.core.dependencies.dependencies import validate_file_token
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe_bulk_actions import ExportTypes
from mealie.schema.recipe.recipe_category import TagIn
from mealie.schema.recipe.recipe_category import CategorySave, TagSave
from tests import utils
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
@ -20,8 +21,8 @@ class Routes:
bulk_delete = "api/recipes/bulk-actions/delete"
bulk_export = "api/recipes/bulk-actions/export"
bulk_export_download = bulk_export + "/download"
bulk_export_purge = bulk_export + "/purge"
bulk_export_download = f"{bulk_export}/download"
bulk_export_purge = f"{bulk_export}/purge"
@pytest.fixture(scope="function")
@ -53,12 +54,12 @@ def test_bulk_tag_recipes(
tags = []
for _ in range(3):
tag_name = random_string()
tag = database.tags.create(TagIn(name=tag_name))
tag = database.tags.create(TagSave(group_id=unique_user.group_id, name=tag_name))
tags.append(tag.dict())
payload = {"recipes": ten_slugs, "tags": tags}
response = api_client.post(Routes.bulk_tag, json=payload, headers=unique_user.token)
response = api_client.post(Routes.bulk_tag, json=utils.jsonify(payload), headers=unique_user.token)
assert response.status_code == 200
# Validate Recipes are Tagged
@ -79,12 +80,12 @@ def test_bulk_categorize_recipes(
categories = []
for _ in range(3):
cat_name = random_string()
cat = database.tags.create(TagIn(name=cat_name))
cat = database.categories.create(CategorySave(group_id=unique_user.group_id, name=cat_name))
categories.append(cat.dict())
payload = {"recipes": ten_slugs, "categories": categories}
response = api_client.post(Routes.bulk_categorize, json=payload, headers=unique_user.token)
response = api_client.post(Routes.bulk_categorize, json=utils.jsonify(payload), headers=unique_user.token)
assert response.status_code == 200
# Validate Recipes are Categorized
@ -140,7 +141,7 @@ def test_bulk_export_recipes(api_client: TestClient, unique_user: TestUser, ten_
assert validate_file_token(response_data["fileToken"]) == Path(export_path)
# Use Export Token to donwload export
response = api_client.get("/api/utils/download?token=" + response_data["fileToken"])
response = api_client.get(f'/api/utils/download?token={response_data["fileToken"]}')
assert response.status_code == 200

View file

@ -1,5 +1,6 @@
import pytest
from fastapi.testclient import TestClient
from pydantic import UUID4
from mealie.schema.recipe.recipe import Recipe
from tests.utils.factories import random_string
@ -32,11 +33,11 @@ def unique_recipe(api_client: TestClient, unique_user: TestUser):
return Recipe(**recipe_response.json())
def random_comment(recipe_id: int) -> dict:
def random_comment(recipe_id: UUID4) -> dict:
if recipe_id is None:
raise ValueError("recipe_id is required")
return {
"recipeId": recipe_id,
"recipeId": str(recipe_id),
"text": random_string(length=50),
}
@ -49,7 +50,7 @@ def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
response_data = response.json()
assert response_data["recipeId"] == unique_recipe.id
assert response_data["recipeId"] == str(unique_recipe.id)
assert response_data["text"] == create_data["text"]
assert response_data["userId"] == unique_user.user_id
@ -60,7 +61,7 @@ def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
response_data = response.json()
assert len(response_data) == 1
assert response_data[0]["recipeId"] == unique_recipe.id
assert response_data[0]["recipeId"] == str(unique_recipe.id)
assert response_data[0]["text"] == create_data["text"]
assert response_data[0]["userId"] == unique_user.user_id
@ -83,7 +84,7 @@ def test_update_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
response_data = response.json()
assert response_data["recipeId"] == unique_recipe.id
assert response_data["recipeId"] == str(unique_recipe.id)
assert response_data["text"] == update_data["text"]
assert response_data["userId"] == unique_user.user_id

View file

@ -10,8 +10,10 @@ from recipe_scrapers._abstract import AbstractScraper
from recipe_scrapers._schemaorg import SchemaOrg
from slugify import slugify
from mealie.services.scraper import scraper
from mealie.schema.recipe.recipe import RecipeCategory
from mealie.services.recipe.recipe_data_service import RecipeDataService
from mealie.services.scraper.scraper_strategies import RecipeScraperOpenGraph
from tests import utils
from tests.utils.app_routes import AppRoutes
from tests.utils.fixture_schemas import TestUser
from tests.utils.recipe_data import RecipeSiteTestCase, get_recipe_test_cases
@ -73,8 +75,8 @@ def test_create_by_url(
)
# Skip image downloader
monkeypatch.setattr(
scraper,
"download_image_for_recipe",
RecipeDataService,
"scrape_image",
lambda *_: "TEST_IMAGE",
)
@ -88,7 +90,11 @@ def test_create_by_url(
@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_read_update(
api_client: TestClient, api_routes: AppRoutes, recipe_data: RecipeSiteTestCase, unique_user: TestUser
api_client: TestClient,
api_routes: AppRoutes,
recipe_data: RecipeSiteTestCase,
unique_user: TestUser,
recipe_categories: list[RecipeCategory],
):
recipe_url = api_routes.recipes_recipe_slug(recipe_data.expected_slug)
response = api_client.get(recipe_url, headers=unique_user.token)
@ -103,14 +109,9 @@ def test_read_update(
recipe["notes"] = test_notes
test_categories = [
{"name": "one", "slug": "one"},
{"name": "two", "slug": "two"},
{"name": "three", "slug": "three"},
]
recipe["recipeCategory"] = test_categories
recipe["recipeCategory"] = [x.dict() for x in recipe_categories]
response = api_client.put(recipe_url, json=recipe, headers=unique_user.token)
response = api_client.put(recipe_url, json=utils.jsonify(recipe), headers=unique_user.token)
assert response.status_code == 200
assert json.loads(response.text).get("slug") == recipe_data.expected_slug
@ -121,10 +122,10 @@ def test_read_update(
assert recipe["notes"] == test_notes
assert len(recipe["recipeCategory"]) == len(test_categories)
assert len(recipe["recipeCategory"]) == len(recipe_categories)
test_name = [x["name"] for x in test_categories]
for cats in zip(recipe["recipeCategory"], test_categories):
test_name = [x.name for x in recipe_categories]
for cats in zip(recipe["recipeCategory"], recipe_categories):
assert cats[0]["name"] in test_name

View file

@ -0,0 +1,64 @@
import filecmp
from fastapi.testclient import TestClient
from slugify import slugify
from mealie.schema.recipe.recipe import Recipe
from tests import data
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
def test_recipe_assets_create(api_client: TestClient, unique_user: TestUser, recipe_ingredient_only: Recipe):
recipe = recipe_ingredient_only
payload = {
"slug": recipe.slug,
"name": random_string(10),
"icon": random_string(10),
"extension": "jpg",
}
file_payload = {
"file": data.images_test_image_1.read_bytes(),
}
response = api_client.post(
f"/api/recipes/{recipe.slug}/assets",
data=payload,
files=file_payload,
headers=unique_user.token,
)
assert response.status_code == 200
# Ensure asset was created
asset_path = recipe.asset_dir / str(slugify(payload["name"]) + "." + payload["extension"])
assert asset_path.exists()
assert filecmp.cmp(asset_path, data.images_test_image_1)
# Ensure asset data is included in recipe
response = api_client.get(f"/api/recipes/{recipe.slug}", headers=unique_user.token)
recipe_respons = response.json()
assert recipe_respons["assets"][0]["name"] == payload["name"]
def test_recipe_image_upload(api_client: TestClient, unique_user: TestUser, recipe_ingredient_only: Recipe):
data_payload = {"extension": "jpg"}
file_payload = {"image": data.images_test_image_1.read_bytes()}
response = api_client.put(
f"/api/recipes/{recipe_ingredient_only.slug}/image",
data=data_payload,
files=file_payload,
headers=unique_user.token,
)
assert response.status_code == 200
image_version = response.json()["image"]
# Get Recipe check for version
response = api_client.get(f"/api/recipes/{recipe_ingredient_only.slug}", headers=unique_user.token)
recipe_respons = response.json()
assert recipe_respons["image"] == image_version

View file

@ -89,7 +89,7 @@ def test_recipe_share_tokens_create_and_get_one(
recipe = database.recipes.get_one(slug)
payload = {
"recipe_id": recipe.id,
"recipeId": str(recipe.id),
}
response = api_client.post(Routes.base, json=payload, headers=unique_user.token)
@ -99,7 +99,7 @@ def test_recipe_share_tokens_create_and_get_one(
assert response.status_code == 200
response_data = response.json()
assert response_data["recipe"]["id"] == recipe.id
assert response_data["recipe"]["id"] == str(recipe.id)
def test_recipe_share_tokens_delete_one(