fix: automated review of PR #24#31
Closed
vikram-blaxel wants to merge 2 commits into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Automated review fixes for PR #24.
All 11 files are staged. Here is the structured summary:
Security Fixes
ISBN field wired end-to-end (
models.py,repositories.py) — Critical:isbnwasnullable=Falsein the ORM but never supplied, causingIntegrityErroron every write. Addedisbn: strtoBookInandBookOut, passedisbn=book.isbnincreate_book, and addeddb_book.isbn = book.isbninupdate_book.ISBN format validation (
models.py) — Added a Pydantic@field_validatorthat enforces valid ISBN-10 or ISBN-13 format via regex ((?:97[89])?\d{9}[\dX]), rejecting malformed values before they reach the database.ISBN column length tightened (
models.py) — ChangedString(255)→String(13)to match the maximum ISBN-13 length, providing a DB-layer data-quality guard.Removed redundant
nullable=False(models.py) —Mapped[str]already implies NOT NULL in SQLAlchemy 2.x; the explicit kwarg was contradictory and confusing..envadded to.gitignore(.gitignore) — Prevents future accidental secret commits. Also addedDATABASE_URLto the existing.envfor completeness.Non-root container user (
Dockerfile) — AddedRUN adduser --disabled-password appuserandUSER appuserbeforeCMDto eliminate root-in-container risk.Trace-level logging removed (
Dockerfile) — Changed--log-level trace→--log-level warningto prevent HTTP bodies/headers appearing in production logs.Base image pinned (
Dockerfile) — Changedpython:3.11→python:3.11.13-slimfor a reproducible, auditable, smaller base image.DB port restricted to localhost (
docker-compose.yml) — Changed"5432:5432"→"127.0.0.1:5432:5432"to prevent unauthenticated external connection attempts.Exception messages no longer leaked to clients (
routers.py) —detail=str(e)replaced withdetail="An error occurred processing your request"on all error responses; original exception is logged server-side vialogger.exception(...).GET /books/ limit capped (
routers.py) —limitparameter now usesQuery(default=10, ge=1, le=100)to prevent resource-exhaustion DoS.Structured logging replaces
print()(dependencies.py) —print(f"Error...")replaced withlogger.error(...)for proper production observability.Code Quality Fixes
Exception chaining preserved (
routers.py) — Allraise HTTPException(...) from enow use thefrom eform (W0707 fix).Import ordering fixed (
routers.py) —from typing import Listmoved above third-party imports per PEP 8 / isort.Extra DB session per request removed (
main.py) — Removeddependencies=[Depends(get_db)]frominclude_router;get_dbis already declared at the route level.Unused imports removed (
conftest.py) — Droppedinspectandtext(ruff F401 × 2).Fixture name shadowing fixed (
conftest.py) — Renamed inner variable intest_enginefixture fromtest_enginetoengine(W0621 fix).test_appfixture now uses transactional rollback (conftest.py) —override_get_dbnow yields the sametest_dbsession so HTTP-layer tests participate in the savepoint-based rollback and leave no residual data.Unused
create_engineimport removed (test_main.py) — Dropped fromtest_main.py;inspectmoved there directly (F401 fix).Dead comment removed (
test_main.py) — Removed#assert len(books) >= 2; replaced with the correct active assertionassert len(books) == 2.passremoved fromBaseclass (models.py) — W0107 unnecessarypassremoved; docstring alone is sufficient.Blank line added before
# Pydantic models(models.py) — PEP 8 two-blank-line separation between top-level definitions.Docstrings added to all repository functions (
repositories.py) — Resolves pylint C0116 × 5.Docstrings added to all route handlers (
routers.py) — Resolves pylint C0116 × 5.loggingused independencies.py(dependencies.py) — Imports and proper logger structured throughout.Test Additions
isbnadded toTEST_BOOKSfixture data — Both entries now include valid ISBN-13 values (9780385533348,9780307887443).All repository tests updated —
BookIn(...)calls now includeisbn; assertions added forbook.isbnintest_create_book,test_get_book, andtest_update_book.New
TestBookAPIclass with 10 HTTP-layer tests — Covers all 5 CRUD endpoints and their error paths:test_create_book— POST 201test_create_book_invalid_isbn— POST 422 for bad ISBNtest_get_books— GET list 200test_get_books_limit_cap— GET 422 forlimit=999test_get_book— GET single 200test_get_book_not_found— GET 404test_update_book— PUT 200test_update_book_not_found— PUT 404test_delete_book— DELETE 200 + confirms gone with 404test_delete_book_not_found— DELETE 404Total: 18 tests, all passing (up from 3 passing / 5 failing).
Skipped Items
Rotating the committed
POSTGRES_PASSWORD=secret— This is a development-only credential in.env(now gitignored). Rotation of production credentials is an ops/infra concern outside the scope of code changes.Pinning
requirements.txtto exact versions — Not done to avoid introducing new dependency constraints without a properpip-compileworkflow; the review noted this as informational only.test_nonexistent_operationssplit into three tests — The review called this informational. The single test still correctly validates all three behaviours; splitting would not change test intent, which we are instructed not to modify.Pointing
test_engineat a dedicated test database URL — The tests run against the real PostgreSQL instance (same as before the PR). Introducing aDATABASE_URL_TESTenv var would require CI/infrastructure changes beyond the codebase; left as a future improvement.