Repository navigation
feat: add JWT-protected entries CRUD - #38
Conversation
DarynaBb
left a comment
There was a problem hiding this comment.
Nice work: ownership checks are solid (filtering by user_id in the query plus returning 404 for other users' entries is the right call), and the tests cover the main cases well.
One real bug before merge (PATCH with null → 500, see the inline comment). The rest are small suggestions.
| days: int | None = Field(default=None, ge=0) | ||
|
|
||
| class EntryUpdate(BaseModel): | ||
| kind: Literal["receipt", "trip"] | None = None |
There was a problem hiding this comment.
Bug: PATCH with explicit null crashes with 500.
kind, entry_date and category are NOT NULL in the DB, but EntryUpdate accepts null for them. Because of exclude_unset=True, an explicitly sent null is set, so it goes straight into setattr and the commit fails:
PATCH /entries/1 {"kind": null}
→ IntegrityError: NOT NULL constraint failed: entries.kind (500)
I checked locally: the same happens for category and entry_date.
One way to fix it: keep the fields optional (so they can be omitted) but reject explicit null:
from pydantic import field_validator
class EntryUpdate(BaseModel):
...
@field_validator("kind", "entry_date", "category")
@classmethod
def not_null(cls, value):
if value is None:
raise ValueError("must not be null")
return valueValidators don't run on defaults, so omitted fields still work and null returns 422. A test for this case would also help.
| kind: Literal["receipt", "trip"] | None = None | ||
| amount: int | None = Field(default=None, ge=0) | ||
| entry_date: date | None = None | ||
| category: Literal[ |
There was a problem hiding this comment.
Minor: the kind/category literals are copied 3 times here (and once more in the DB check constraint). Consider defining them once, e.g. EntryKind = Literal["receipt", "trip"] and EntryCategory = Literal[...], then reusing them. Otherwise adding a category means editing several places, and it's easy to miss one.
| current_user: CurrentUserDependency, | ||
| ): | ||
| entry = session.exec( | ||
| select(Entry).where( |
There was a problem hiding this comment.
Nit: this "select by id + user_id, else 404" block appears in get, update and delete. A small helper such as get_own_entry_or_404(session, entry_id, user) would remove the duplication and keep the ownership check in one place.
|
Addressed the PATCH null handling issue and the suggested cleanup: -Required entry fields now reject explicit null with 422 Ruff and the full pytest suite pass locally. |
Adds JWT-protected CRUD operations for entries.
Changes:
-Add create, list, get, update and delete endpoints for /entries
-Restrict all operations to the authenticated user's own entries
-Add schemas for entry creation, updates and responses
-Hide internal fields such as user_id and photo_path
-Add success, authentication and ownership tests
Testing:
Ruff and all the pytest tests pass locally.
Closes #22