fix: reject whitespace-only recipe comments (#8295)
This commit is contained in:
1 parent
6fa04c345c
commit
2bcba88a97
5 files changed
+38
-6
No files matched your search
+6
-2
@@ -35,7 +35,7 @@
|
||||
<div class="ml-auto mt-1">
|
||||
<BaseButton
|
||||
size="small"
|
||||
:disabled="!comment"
|
||||
:disabled="!comment.trim()"
|
||||
@click="submitComment"
|
||||
>
|
||||
<template #icon>
|
||||
@@ -96,9 +96,13 @@ const { user } = usePageUser();
|
||||
const comment = ref("");
|
||||
|
||||
async function submitComment() {
|
||||
const text = comment.value.trim();
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
const { data } = await api.recipes.comments.createOne({
|
||||
recipeId: recipe.value.id,
|
||||
text: comment.value,
|
||||
text,
|
||||
});
|
||||
|
||||
if (data) {
|
||||
|
||||
@@ -526,9 +526,9 @@ export interface RecipeNote {
|
||||
referenceId?: string;
|
||||
}
|
||||
export interface RecipeCommentOut {
|
||||
id: string;
|
||||
recipeId: string;
|
||||
text: string;
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
userId: string;
|
||||
|
||||
@@ -320,9 +320,9 @@ export interface RecipeNote {
|
||||
referenceId?: string;
|
||||
}
|
||||
export interface RecipeCommentOut {
|
||||
id: string;
|
||||
recipeId: string;
|
||||
text: string;
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
userId: string;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import UUID4, ConfigDict
|
||||
from pydantic import UUID4, ConfigDict, field_validator
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm.interfaces import LoaderOption
|
||||
|
||||
@@ -19,10 +19,19 @@ class UserBase(MealieModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
def validate_comment_text(value: str) -> str:
|
||||
stripped = value.strip()
|
||||
if not stripped:
|
||||
raise ValueError("comment text must not be empty")
|
||||
return stripped
|
||||
|
||||
|
||||
class RecipeCommentCreate(MealieModel):
|
||||
recipe_id: UUID4
|
||||
text: str
|
||||
|
||||
_validate_text = field_validator("text")(validate_comment_text)
|
||||
|
||||
|
||||
class RecipeCommentSave(RecipeCommentCreate):
|
||||
user_id: UUID4
|
||||
@@ -32,10 +41,13 @@ class RecipeCommentUpdate(MealieModel):
|
||||
id: UUID4
|
||||
text: str
|
||||
|
||||
_validate_text = field_validator("text")(validate_comment_text)
|
||||
|
||||
class RecipeCommentOut(RecipeCommentCreate):
|
||||
|
||||
class RecipeCommentOut(MealieModel):
|
||||
id: UUID4
|
||||
recipe_id: UUID4
|
||||
text: str
|
||||
created_at: datetime
|
||||
updated_at: datetime = UpdatedAtField(...)
|
||||
user_id: UUID4
|
||||
|
||||
@@ -142,3 +142,19 @@ def test_user_can_comment_on_other_household(api_client: TestClient, unique_reci
|
||||
response = api_client.get(api_routes.comments_item_id(comment_id), headers=h2_user.token)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize("text", ["", " ", "\n\t \n"])
|
||||
def test_whitespace_only_comment_rejected(
|
||||
api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser, text: str
|
||||
):
|
||||
create_data = {"recipeId": str(unique_recipe.id), "text": text}
|
||||
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_comment_text_is_trimmed(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
|
||||
create_data = {"recipeId": str(unique_recipe.id), "text": " hello "}
|
||||
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
|
||||
assert response.status_code == 201
|
||||
assert response.json()["text"] == "hello"
|
||||
Reference in new issue
Block a user