2021-11-22 20:10:48 -09:00
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
2022-02-13 12:23:42 -09:00
|
|
|
from pydantic import UUID4
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
from mealie.schema.recipe.recipe import Recipe
|
2022-10-18 14:49:41 -08:00
|
|
|
from tests.utils import api_routes
|
2021-11-22 20:10:48 -09:00
|
|
|
from tests.utils.factories import random_string
|
|
|
|
from tests.utils.fixture_schemas import TestUser
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def unique_recipe(api_client: TestClient, unique_user: TestUser):
|
|
|
|
payload = {"name": random_string(length=20)}
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.post(api_routes.recipes, json=payload, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
response_data = response.json()
|
2022-10-18 14:49:41 -08:00
|
|
|
recipe_response = api_client.get(api_routes.recipes_slug(response_data), headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
return Recipe(**recipe_response.json())
|
|
|
|
|
|
|
|
|
2022-02-13 12:23:42 -09:00
|
|
|
def random_comment(recipe_id: UUID4) -> dict:
|
2021-11-22 20:10:48 -09:00
|
|
|
if recipe_id is None:
|
|
|
|
raise ValueError("recipe_id is required")
|
|
|
|
return {
|
2022-02-13 12:23:42 -09:00
|
|
|
"recipeId": str(recipe_id),
|
2021-11-22 20:10:48 -09:00
|
|
|
"text": random_string(length=50),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
|
|
|
|
# Create Comment
|
|
|
|
create_data = random_comment(unique_recipe.id)
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
response_data = response.json()
|
|
|
|
|
2022-02-13 12:23:42 -09:00
|
|
|
assert response_data["recipeId"] == str(unique_recipe.id)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response_data["text"] == create_data["text"]
|
|
|
|
assert response_data["userId"] == unique_user.user_id
|
|
|
|
|
|
|
|
# Check for Proper Association
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.get(api_routes.recipes_slug_comments(unique_recipe.slug), headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
response_data = response.json()
|
|
|
|
|
|
|
|
assert len(response_data) == 1
|
2022-02-13 12:23:42 -09:00
|
|
|
assert response_data[0]["recipeId"] == str(unique_recipe.id)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response_data[0]["text"] == create_data["text"]
|
|
|
|
assert response_data[0]["userId"] == unique_user.user_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
|
|
|
|
# Create Comment
|
|
|
|
create_data = random_comment(unique_recipe.id)
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
comment_id = response.json()["id"]
|
|
|
|
|
|
|
|
# Update Comment
|
|
|
|
update_data = random_comment(unique_recipe.id)
|
|
|
|
update_data["id"] = comment_id
|
|
|
|
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.put(api_routes.comments_item_id(comment_id), json=update_data, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
response_data = response.json()
|
|
|
|
|
2022-02-13 12:23:42 -09:00
|
|
|
assert response_data["recipeId"] == str(unique_recipe.id)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response_data["text"] == update_data["text"]
|
|
|
|
assert response_data["userId"] == unique_user.user_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
|
|
|
|
# Create Comment
|
|
|
|
create_data = random_comment(unique_recipe.id)
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
# Delete Comment
|
|
|
|
comment_id = response.json()["id"]
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.delete(api_routes.comments_item_id(comment_id), headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
# Validate Deletion
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.get(api_routes.comments_item_id(comment_id), headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_admin_can_delete(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser, admin_user: TestUser):
|
|
|
|
# Create Comment
|
|
|
|
create_data = random_comment(unique_recipe.id)
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
# Delete Comment
|
|
|
|
comment_id = response.json()["id"]
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.delete(api_routes.comments_item_id(comment_id), headers=admin_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
# Validate Deletion
|
2022-10-18 14:49:41 -08:00
|
|
|
response = api_client.get(api_routes.comments_item_id(comment_id), headers=admin_user.token)
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
assert response.status_code == 404
|