Strip whitespace from new todo list item names (#142889)

Strip whitspace from new todo list item names
pull/142898/head
Allen Porter 2025-04-13 17:42:42 -07:00 committed by GitHub
parent 5b8ca8d0ed
commit 658299ee21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -129,7 +129,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
vol.All( vol.All(
cv.make_entity_service_schema( cv.make_entity_service_schema(
{ {
vol.Required(ATTR_ITEM): vol.All(cv.string, vol.Length(min=1)), vol.Required(ATTR_ITEM): vol.All(
cv.string, str.strip, vol.Length(min=1)
),
**TODO_ITEM_FIELD_SCHEMA, **TODO_ITEM_FIELD_SCHEMA,
} }
), ),
@ -144,7 +146,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
cv.make_entity_service_schema( cv.make_entity_service_schema(
{ {
vol.Required(ATTR_ITEM): vol.All(cv.string, vol.Length(min=1)), vol.Required(ATTR_ITEM): vol.All(cv.string, vol.Length(min=1)),
vol.Optional(ATTR_RENAME): vol.All(cv.string, vol.Length(min=1)), vol.Optional(ATTR_RENAME): vol.All(
cv.string, str.strip, vol.Length(min=1)
),
vol.Optional(ATTR_STATUS): vol.In( vol.Optional(ATTR_STATUS): vol.In(
{TodoItemStatus.NEEDS_ACTION, TodoItemStatus.COMPLETED}, {TodoItemStatus.NEEDS_ACTION, TodoItemStatus.COMPLETED},
), ),

View File

@ -160,9 +160,18 @@ async def test_unsupported_websocket(
assert resp.get("error", {}).get("code") == "not_found" assert resp.get("error", {}).get("code") == "not_found"
@pytest.mark.parametrize(
("new_item_name"),
[
("New item"),
("New item "),
(" New item"),
],
)
async def test_add_item_service( async def test_add_item_service(
hass: HomeAssistant, hass: HomeAssistant,
test_entity: TodoListEntity, test_entity: TodoListEntity,
new_item_name: str,
) -> None: ) -> None:
"""Test adding an item in a To-do list.""" """Test adding an item in a To-do list."""
@ -171,7 +180,7 @@ async def test_add_item_service(
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
TodoServices.ADD_ITEM, TodoServices.ADD_ITEM,
{ATTR_ITEM: "New item"}, {ATTR_ITEM: new_item_name},
target={ATTR_ENTITY_ID: "todo.entity1"}, target={ATTR_ENTITY_ID: "todo.entity1"},
blocking=True, blocking=True,
) )
@ -209,6 +218,7 @@ async def test_add_item_service_raises(
[ [
({}, vol.Invalid, "required key not provided"), ({}, vol.Invalid, "required key not provided"),
({ATTR_ITEM: ""}, vol.Invalid, "length of value must be at least 1"), ({ATTR_ITEM: ""}, vol.Invalid, "length of value must be at least 1"),
({ATTR_ITEM: " "}, vol.Invalid, "length of value must be at least 1"),
( (
{ATTR_ITEM: "Submit forms", ATTR_DESCRIPTION: "Submit tax forms"}, {ATTR_ITEM: "Submit forms", ATTR_DESCRIPTION: "Submit tax forms"},
ServiceValidationError, ServiceValidationError,
@ -331,9 +341,18 @@ async def test_add_item_service_extended_fields(
assert item == expected_item assert item == expected_item
@pytest.mark.parametrize(
("new_item_name"),
[
("Updated item"),
("Updated item "),
(" Updated item "),
],
)
async def test_update_todo_item_service_by_id( async def test_update_todo_item_service_by_id(
hass: HomeAssistant, hass: HomeAssistant,
test_entity: TodoListEntity, test_entity: TodoListEntity,
new_item_name: str,
) -> None: ) -> None:
"""Test updating an item in a To-do list.""" """Test updating an item in a To-do list."""
@ -342,7 +361,7 @@ async def test_update_todo_item_service_by_id(
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
TodoServices.UPDATE_ITEM, TodoServices.UPDATE_ITEM,
{ATTR_ITEM: "1", ATTR_RENAME: "Updated item", ATTR_STATUS: "completed"}, {ATTR_ITEM: "1", ATTR_RENAME: new_item_name, ATTR_STATUS: "completed"},
target={ATTR_ENTITY_ID: "todo.entity1"}, target={ATTR_ENTITY_ID: "todo.entity1"},
blocking=True, blocking=True,
) )