from dataclasses import dataclass from uuid import UUID from fastapi.testclient import TestClient from mealie.schema.recipe.recipe import RecipeOut from tests.utils import random_string from tests.utils.fixture_schemas import TestUser # The validators router is hidden from the OpenAPI schema, so these aren't in the generated # api_routes helpers. VALIDATORS_USER_NAME = "/api/validators/user/name" VALIDATORS_USER_EMAIL = "/api/validators/user/email" VALIDATORS_GROUP = "/api/validators/group" VALIDATORS_RECIPE = "/api/validators/recipe" @dataclass() class SimpleCase: value: str is_valid: bool def test_validators_username(api_client: TestClient, unique_user: TestUser): users = [ SimpleCase(value=unique_user.username, is_valid=False), SimpleCase(value=random_string(), is_valid=True), ] for user in users: response = api_client.get(VALIDATORS_USER_NAME + "?name=" + user.value) assert response.status_code == 200 response_data = response.json() assert response_data["valid"] == user.is_valid def test_validators_email(api_client: TestClient, unique_user: TestUser): emails = [ SimpleCase(value=unique_user.email, is_valid=False), SimpleCase(value=f"{random_string()}@example.com", is_valid=True), ] for user in emails: response = api_client.get(VALIDATORS_USER_EMAIL + "?email=" + user.value) assert response.status_code == 200 response_data = response.json() assert response_data["valid"] == user.is_valid def test_validators_user_allow_current_user(api_client: TestClient, unique_user: TestUser, g2_user: TestUser): for route, value in [ (VALIDATORS_USER_NAME + "?name=" + unique_user.username, True), (VALIDATORS_USER_EMAIL + "?email=" + unique_user.email, True), (VALIDATORS_USER_NAME + "?name=" + g2_user.username, False), (VALIDATORS_USER_EMAIL + "?email=" + g2_user.email, False), ]: response = api_client.get(route, headers=unique_user.token) assert response.status_code == 200 assert response.json()["valid"] is value def test_validators_group_name(api_client: TestClient, unique_user: TestUser): database = unique_user.repos group = database.groups.get_one(unique_user.group_id) assert group groups = [ SimpleCase(value=group.name, is_valid=False), SimpleCase(value=random_string(), is_valid=True), ] for user in groups: response = api_client.get(VALIDATORS_GROUP + "?name=" + user.value) assert response.status_code == 200 response_data = response.json() assert response_data["valid"] == user.is_valid @dataclass(slots=True) class RecipeValidators: name: str group: UUID | str is_valid: bool def test_validators_recipe(api_client: TestClient, random_recipe: RecipeOut): recipes = [ RecipeValidators(name=random_recipe.name or "", group=random_recipe.group_id, is_valid=False), RecipeValidators(name=random_string(), group=random_recipe.group_id, is_valid=True), RecipeValidators(name=random_string(), group=random_recipe.group_id, is_valid=True), ] for recipe in recipes: response = api_client.get(VALIDATORS_RECIPE + f"?group_id={recipe.group}&name={recipe.name}") assert response.status_code == 200 response_data = response.json() assert response_data["valid"] == recipe.is_valid