openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

feat(dev): migrate internal fake stubs to standalone FastAPI mockservices container

Open mekarpeles opened this issue 5 months ago • 1 comments

Problem / Opportunity

Open Library's local dev environment currently embeds four fake-service stubs directly inside the web.py app at /internal/fake/* in openlibrary/plugins/upstream/account.py. This approach has several problems:

  1. Tightly coupled — mock behavior lives in the production app process; if web goes down, mocks go down
  2. Incomplete coverage — ia_availability_api_v2_url still hits real archive.org in dev; the borrow-status URL (https://archive.org/services/borrow/{id}?action=status) is hardcoded in lending.py and not configurable
  3. Email not inspectable — dummy_sendmail: True logs to stdout; no way to read sent email from tests
  4. Amazon PA-API has no mock — vendors.py calls webservices.amazon.com with no dev intercept
  5. reCAPTCHA has no mock — recaptcha_url still hits recaptcha.net in dev
  6. be-api FTS has no mock — plugin_inside.search_endpoint hits be-api.us.archive.org in dev

The goal is a standalone mockservices Docker container (FastAPI + mailpit) that dev environments point at instead of the in-app stubs or real external services. This unlocks reliable e2e tests that don't depend on archive.org being reachable.

Current State: Stubs in account.py

Four delegate.page classes currently embedded in the main web app:

Route Purpose Dev config key
POST /internal/fake/xauth Fakes IA xauthn (authenticate, info, issue_otp, redeem_otp, create ops) ia_xauth_api_url
GET /internal/fake/s3auth Fakes IA S3 auth validation (LOW foo:foo → authorized) ia_s3_auth_url
POST /internal/fake/loans Fakes IA loans API (empty {} response) ia_loan_api_url
POST /internal/fake/availability Fakes bulk availability API (empty {} response) (not yet wired in dev; ia_availability_api_v2_url still hits archive.org)

openlibrary.yml already has config keys for most of these:

ia_loan_api_url: http://web:8080/internal/fake/loans
ia_xauth_api_url: http://web:8080/internal/fake/xauth
ia_s3_auth_url: http://web:8080/internal/fake/s3auth
ia_availability_api_v2_url: https://archive.org/services/availability/  # ← still real!

Proposed Architecture

docker/mockservices/
  Dockerfile            # Python 3.12 + mailpit binary
  main.py               # FastAPI app with all mock endpoints
  requirements.txt      # fastapi, uvicorn

compose.override.yaml   # add `mockservices` service
conf/openlibrary.yml    # update all ia_* URLs → http://mockservices:8090/

mockservices runs on port 8090 (internal). Mailpit runs on 1025 (SMTP) and 8025 (web UI, exposed to host for manual inspection).

Mock endpoints to implement

Endpoint Method Path Behavior
xauth POST /services/xauthn/ op-dispatch: authenticate/info/issue_otp/redeem_otp/create
s3auth GET /services/s3auth/ LOW foo:foo → {authorized: true}
loans POST /services/loans/loan/ Return empty {}
availability POST /services/availability/ Return {"responses": {}}
borrow status GET /services/borrow/{identifier} Return {"status": "borrow_available"}
Amazon products POST /paapi5/getItems Return minimal fixture
reCAPTCHA POST /recaptcha/api/siteverify Always return {"success": true}
be-api FTS GET /fts/v1/search Return {"hits": []}
mailpit health GET / Mailpit web UI (port 8025)

Email (mailpit)

Replace dummy_sendmail: True with actual SMTP delivery to mailpit:

smtp_server: mockservices
smtp_port: 1025
dummy_sendmail: False

Tests can then inspect sent mail via mailpit's REST API: GET http://localhost:8025/api/v1/messages

lending.py hardcoded URL fix

lending.py line 599 has:

url = f"https://archive.org/services/borrow/{identifier}?action=status"

This needs a config_ia_borrow_status_url variable (pattern matches the other config_ia_* globals) and a corresponding ia_borrow_status_url key in openlibrary.yml.

Implementation Checklist

Phase 1 — Scaffold the container

  • [ ] Create docker/mockservices/Dockerfile (Python 3.12 slim + mailpit)
  • [ ] Create docker/mockservices/main.py (FastAPI app skeleton)
  • [ ] Create docker/mockservices/requirements.txt
  • [ ] Add mockservices service to compose.override.yaml (dev only)
  • [ ] Expose mailpit web UI on host port 8025

Phase 2 — Migrate existing web.py stubs

  • [ ] Implement /services/xauthn/ in FastAPI (port account.py::xauth logic)
  • [ ] Implement /services/s3auth/ in FastAPI (port account.py::s3auth logic)
  • [ ] Implement /services/loans/loan/ in FastAPI (port account.py::loans logic)
  • [ ] Update openlibrary.yml: ia_xauth_api_url: http://mockservices:8090/services/xauthn/
  • [ ] Update openlibrary.yml: ia_s3_auth_url: http://mockservices:8090/services/s3auth/
  • [ ] Update openlibrary.yml: ia_loan_api_url: http://mockservices:8090/services/loans/loan/
  • [ ] Remove account.py::xauth, account.py::s3auth, account.py::loans, account.py::availability stub classes

Phase 3 — Wire previously un-mocked services

  • [ ] Implement POST /services/availability/ — update openlibrary.yml: ia_availability_api_v2_url: http://mockservices:8090/services/availability/
  • [ ] Add config_ia_borrow_status_url to lending.py (alongside the other config_ia_* globals)
  • [ ] Implement GET /services/borrow/{identifier} — update openlibrary.yml: ia_borrow_status_url: http://mockservices:8090/services/borrow/
  • [ ] Implement POST /recaptcha/api/siteverify (always success: true) — update openlibrary.yml: recaptcha_url: http://mockservices:8090/recaptcha/api/siteverify
  • [ ] Implement GET /fts/v1/search (empty hits) — update openlibrary.yml plugin_inside.search_endpoint

Phase 4 — Email (mailpit)

  • [ ] Bundle mailpit in the Dockerfile (download binary from ghcr.io/axllent/mailpit)
  • [ ] Update openlibrary.yml: smtp_server: mockservices, smtp_port: 1025, dummy_sendmail: False
  • [ ] Add mailpit web UI port mapping (8025:8025) to compose.override.yaml
  • [ ] Document mailpit API for test inspection in CONTRIBUTING.md or docs/

Phase 5 — Amazon PA-API (stretch)

  • [ ] Implement POST /paapi5/getItems returning a fixture (title, cover URL, etc.)
  • [ ] Add amazon_api_url config key; update vendors.py to use it
  • [ ] Update openlibrary.yml: amazon_api_url: http://mockservices:8090/paapi5/

Related

  • openlibrary/plugins/upstream/account.py — current stub classes (~lines 95–175)
  • openlibrary/core/lending.py — config_ia_* globals, hardcoded borrow-status URL (line 599)
  • openlibrary/core/sendmail.py — dummy_sendmail path
  • openlibrary/core/vendors.py — AmazonAPI, AmazonCreatorsAPI classes
  • openlibrary/plugins/recaptcha/recaptcha.py — config.get("recaptcha_url")
  • conf/openlibrary.yml — current dev configuration
  • compose.override.yaml — dev Docker Compose overrides

mekarpeles avatar Jun 25 '26 19:06 mekarpeles

Context

Verified the following against the current codebase (master @ ~/Projects/openlibrary):

Stub classes confirmed in account.py (lines 97–180):

  • availability (POST, trivial empty {} response)
  • loans (POST, trivial empty {} response)
  • xauth (POST, non-trivial — op-dispatch for authenticate / info / issue_otp / redeem_otp; hardcodes dev OTP shortcut 123456 and returns S3 keys foo:foo)
  • s3auth (GET, accepts LOW foo:foo auth header)

Phase 2 migration of /services/xauthn/ is the most complex port — the xauth stub carries meaningful branching logic (5 op types, structured JSON responses). The other three stubs are trivial pass-throughs.

lending.py hardcoded URL confirmed (line 599):

url = f"https://archive.org/services/borrow/{identifier}?action=status"

No config_ia_borrow_status_url global exists yet. The addition follows the established pattern at lines 66–104 (10+ similar config_ia_* globals) — the change is ~3 lines.

ia_availability_api_v2_url still routes to real archive.org (openlibrary.yml line 170):

ia_availability_api_v2_url: https://archive.org/services/availability/  # real!

recaptcha.py is already config-driven — recaptcha.py:39 reads config.get("recaptcha_url"). No code change is needed; Phase 3 only requires a yml key update.

AmazonAPI host is already a constructor argument (vendors.py:92):

def __init__(self, key, secret, tag, host: str = "webservices.amazon.com", ...)

Phase 5 doesn't require changes to vendors.py's internal logic — only config plumbing to forward an amazon_api_url/amazon_api_host config key into the constructor. Simpler than it may appear.

Related: #957 — older issue about reCAPTCHA blocking local registration; this issue fully resolves that class of problem.


Issue Assessment

Open question: The issue states the goal is "reliable e2e tests that don't depend on archive.org being reachable" — it would strengthen the epic to have an explicit success test: e.g., a Playwright flow that completes login + borrow + return entirely within the mock container, or a CI job that asserts no outbound archive.org requests are made during the test suite. Not a blocker, but worth calling out in the breakdown.


Full triage checklist (maintainers / Richy)
  • [x] Triage — correct labels applied: Type: Feature Request, Type: Epic, Theme: Development, Module: Docker, Needs: Triage, Lead: @mekarpeles
  • [x] Problem / opportunity — problem is clear; 6 specific external-service gaps identified
  • [x] Scope — well-scoped across 5 phases with explicit phase boundaries
  • [x] Justification — e2e test enablement; dev environment isolation from real IA services
  • [ ] Success & testing criteria — implied (e2e tests possible) but no specific acceptance test defined
  • [x] Proposal & breakdown — detailed architecture, endpoint table, and phase checklist
  • [x] Richy context — relevant code, hardcoded URL, and config globals confirmed via direct file reads; no duplicate issue found
  • [x] References — related files listed in issue body

[!NOTE] This comment was automatically generated by Richy, via PAM, Open Library's Project AI Manager, on behalf of @mekarpeles. Richy is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting.

openlibrary-bot avatar Jun 25 '26 19:06 openlibrary-bot