Skip to content

Commit 983d398

Browse files
committed
address coderabbit review
Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>
1 parent a00f750 commit 983d398

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/utils/saved_prompts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from sqlalchemy.exc import IntegrityError
44

5+
import constants
56
from app.database import get_session
67
from log import get_logger
78
from models.database.saved_prompts import SavedPrompt
@@ -142,7 +143,7 @@ def create_saved_prompt(
142143
Raises:
143144
SavedPromptLimitExceededError: If the user is already at the limit.
144145
SavedPromptConflictError: If insert violates a unique constraint
145-
(typically duplicate ``(user_id, name)``).
146+
(practically always duplicate ``(user_id, name)``).
146147
"""
147148
with get_session() as session:
148149
current_count = session.query(SavedPrompt).filter_by(user_id=user_id).count()
@@ -177,6 +178,9 @@ def create_saved_prompt(
177178
def list_saved_prompts_by_user(user_id: str) -> list[SavedPrompt]:
178179
"""List saved prompts for a user ordered by created_at descending.
179180
181+
Results are capped at ``SAVED_PROMPTS_MAX_PER_USER_UPPER_BOUND`` so a
182+
misconfigured or out-of-band insert path cannot materialize unbounded rows.
183+
180184
Parameters:
181185
user_id: Owner whose prompts should be returned.
182186
@@ -189,6 +193,7 @@ def list_saved_prompts_by_user(user_id: str) -> list[SavedPrompt]:
189193
session.query(SavedPrompt)
190194
.filter_by(user_id=user_id)
191195
.order_by(SavedPrompt.created_at.desc())
196+
.limit(constants.SAVED_PROMPTS_MAX_PER_USER_UPPER_BOUND)
192197
.all()
193198
)
194199

tests/unit/utils/test_saved_prompts.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
validate_saved_prompt_name,
2626
validate_saved_prompt_quota,
2727
)
28+
from utils.suid import get_suid
2829

2930

3031
@pytest.fixture(name="sqlite_engine")
@@ -60,6 +61,11 @@ def patch_saved_prompts_get_session_fixture(
6061
)
6162

6263
def _get_session() -> Session:
64+
"""Create a Session bound to the in-memory test engine.
65+
66+
Returns:
67+
Session: A new SQLAlchemy session for patched DAL calls.
68+
"""
6369
return session_factory()
6470

6571
mocker.patch("utils.saved_prompts.get_session", side_effect=_get_session)
@@ -330,6 +336,32 @@ def test_list_returns_only_that_users_prompts_ordered_by_created_at_desc(
330336
assert [p.id for p in results] == [newer.id, older.id]
331337
assert all(p.user_id == "user-1" for p in results)
332338

339+
def test_list_caps_at_configured_upper_bound(
340+
self, mocker: MockerFixture, sqlite_engine: Engine
341+
) -> None:
342+
"""Test list applies SAVED_PROMPTS_MAX_PER_USER_UPPER_BOUND as a hard cap."""
343+
mocker.patch(
344+
"utils.saved_prompts.constants.SAVED_PROMPTS_MAX_PER_USER_UPPER_BOUND",
345+
2,
346+
)
347+
session_factory = sessionmaker(
348+
autocommit=False, autoflush=False, bind=sqlite_engine
349+
)
350+
with session_factory() as session:
351+
for index in range(3):
352+
session.add(
353+
SavedPrompt(
354+
id=get_suid(),
355+
user_id="user-1",
356+
name=f"prompt-{index}",
357+
content=f"content-{index}",
358+
)
359+
)
360+
session.commit()
361+
362+
results = list_saved_prompts_by_user("user-1")
363+
assert len(results) == 2
364+
333365

334366
@pytest.mark.usefixtures("patch_saved_prompts_get_session")
335367
class TestDeleteSavedPromptByIdAndUser:

0 commit comments

Comments
 (0)