Repository navigation
test: add app test harness, fix ID retry - #90
Conversation
POST /services retried an insert after a duplicate key error, but generated the identifier only once, so a collision returned 500 after ten identical attempts. Generate a new identifier on every attempt. Also stop modifying the caller's request data: RegisterService now works on a copy, and inserts a copy so that pymongo's `_id` does not leak into it. The test for running out of identifiers never configured the one-character ID space it meant to set up; it now does, against an in-memory collection with a unique index. Fixes #72 Co-Authored-By: Claude Opus 5.5 <[email protected]>
postServiceInfo returned the status code as the string "201". connexion logs the status with "%d" at debug level, which fails for a string: logging prints a traceback, and wherever logging errors are raised (e.g. under pytest), the request ends in a 500. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add tests/conftest.py with an app created from config.yaml, MongoDB replaced by mongomock and the identity provider by a mock that issues and checks RS256 tokens. The fixtures work with FOCA 0.12 (flask_pymongo) and 0.14 (pymongo), so the same tests can verify the upgrade in #65. tests/test_app.py sends requests through the API, covering every endpoint: success paths, an empty database, PUT create vs. replace, 404s, schema validation (400), missing or invalid tokens (401) and the error response format. It also tests app.py's main(), which is no longer omitted from coverage. The app still fetches remote references in the GA4GH specs at startup, so these tests need network access until #76. Closes #32, closes #40 Part of #71 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the app and in-memory collection set up by hand in almost every unit test with the `unit_app`, `services` and `service_info` fixtures. The collections now have the unique indexes from config.yaml, so duplicate identifiers behave as in production. Tests that replaced the collection with a MagicMock now use the in-memory collection and also check what was stored. The MagicMock duplicate-key test is dropped; the duplicate-key retry test covers it against a real unique index. Closes #71 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR adds a production-configured end-to-end harness with mocked MongoDB and RS256 OIDC authentication, broadens API and startup coverage, refactors unit tests around shared fixtures, and fixes service ID retry/copying behavior plus the service-info status type. Sequence diagram for retrying service registration with fresh identifierssequenceDiagram
participant Client
participant RegisterService
participant MongoDB
Client->>RegisterService: register_metadata()
loop retry up to configured limit
RegisterService->>RegisterService: generate_id(charset, length)
RegisterService->>MongoDB: insert_one(document=dict(data))
alt unique identifier available
MongoDB-->>RegisterService: insert succeeds
RegisterService-->>Client: service registered
else DuplicateKeyError
MongoDB-->>RegisterService: DuplicateKeyError
end
end
RegisterService-->>Client: InternalServerError
Sequence diagram for service-info response statussequenceDiagram
participant Client
participant API
participant RegisterServiceInfo
Client->>API: POST /service-info
API->>RegisterServiceInfo: set_service_info_from_app_context(data)
RegisterServiceInfo-->>API: headers
API-->>Client: HTTP 201
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #90 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 6 +1
Lines 189 204 +15
=========================================
+ Hits 189 204 +15 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Now that app.py is measured, its `if __name__ == "__main__":` guard is the only line not covered, which made Codecov report a drop in project coverage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/conftest.py" line_range="153" />
<code_context>
+ connexion_app = Foca(
+ config_file=APP_DIR / "config.yaml",
+ custom_config_model="service_models.custom_config.CustomConfig",
+ ).create_app()
+ with connexion_app.app.app_context():
+ RegisterServiceInfo().set_service_info_from_config()
</code_context>
<issue_to_address>
**App tests require external network**
When the remote specification host is unavailable or unreachable, `Foca.create_app()` resolves remote OpenAPI references during fixture setup, so tests using `app` or `client` error before reaching their assertions when those hosts are unreachable, even if the application code is correct.
Use local copies of the referenced schemas or otherwise make reference resolution independent of network access.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the retry path is wrong, a POST could store a service under an incorrect or duplicate identifier; that record would remain after reverting, but it can be deleted and recreated. The change does not otherwise introduce an irreversible operation.
Blocking findings: tests/conftest.py:153
Summary
Fixes #71 and #72. Adds a test harness that goes through the app, so that the FOCA upgrade (#65) and access control (#62) can be verified, and fixes the bugs it found.
/servicesretried after a duplicate key error but generated the identifier only once, so a collision returned 500 after ten identical attempts. A new identifier is now drawn on every attempt.RegisterServicealso no longer modifies the caller's request data (it works on a copy and inserts a copy, so pymongo's_iddoesn't leak).postServiceInforeturned the status as the string"201". connexion logs it with%dat debug level, which fails. The app tests turned this into a 500, so it now returns the integer.tests/conftest.py):app/client: the app created fromconfig.yaml, with MongoDB replaced by mongomock and the identity provider (LS Login, the configured issuer) by a mock that issues and checks RS256 tokens.unit_app,services,service_info: the bare Flask app for unit tests, with in-memory collections that have the unique indexes fromconfig.yaml.flask_pymongo.PyMongo) and 0.14 (pymongo.MongoClient).tests/test_app.py): every endpoint through the API: success paths, an empty database, PUT create vs. replace, 404s, schema validation (400), missing tokens and tokens with a forged signature (401), running out of identifiers (500) and the error response format. 403 cannot occur before access control (Cloud Registry backend: public read access, service ownership, ECP_CLN:ADMIN admin role #62); feat!: add owner-based access control #66 tests it.main()inapp.pyis tested too, andapp.pyis no longer omitted from coverage.Closes #32, closes #40.
Verification
pytest(55 passed),flake8,black --check .andmypy cloud_registrypass. Coverage 100% (app.py'sif __name__ == "__main__":guard is excluded).devand pass with the fix.feat/foca-0.14(feat!: upgrade to FOCA 0.14.0 #65), all 55 tests pass on Python 3.12. The merge conflicts that feat!: upgrade to FOCA 0.14.0 #65 will have to resolve are inrequirements.txt,Dockerfileandtests/ga4gh/registry/test_service.py.dev) and a maintainability review in 2 rounds (medium and low findings on the tests fixed).Notes for reviewers
$refs in the GA4GH specs at startup, so the app tests need network access until Specs: resolve GA4GH schemas locally (offline start) #76.flask_pymongo, so the suite cannot even load on FOCA 0.14 (feat!: upgrade to FOCA 0.14.0 #65); and its mock identity provider accepts tokens signed with any key (a forged token gets 200), so authentication is only tested with tokens that cannot be decoded at all. Details are in a comment on fix: generate new ID on retry and add app test harness #89. Only one of the two should be merged.cryptographyandPyJWTare imported by the tests but not pinned inrequirements_dev.txt. Both come with the pinned FOCA, and pinning them separately risks version conflicts with it.was_replaced, the duplicate config-file check inapp.py, logging style) and the service-info bugs in Bug: service-info POST, precedence and headers #75 are deliberately left out.🤖 Generated with Claude Code
Summary by Sourcery
Fix service registration retries and request-data handling while introducing comprehensive API-level testing infrastructure.
New Features:
Bug Fixes:
Enhancements:
Tests: