Repository navigation
fix: generate new ID on retry and add app test harness - #89
montionugera wants to merge 2 commits into
Conversation
- Generate new random service identifier on each retry attempt in RegisterService (elixir-cloud-aai#72) - Preserve caller data dictionary by copying before ID assignment and DB insertion - Add app-level integration test harness via Connexion and FOCA (Closes elixir-cloud-aai#71) - Refactor unit test suite to use pytest fixtures and mongomock - Add test coverage for all endpoints, validation errors, and startup (Closes elixir-cloud-aai#32, Closes elixir-cloud-aai#40)
Reviewer's GuideThe PR fixes POST service ID collision retries, protects caller payloads, and corrects PUT replacement tracking and service-info status typing. It also introduces a full Connexion/FOCA application test harness with mocked MongoDB and OIDC dependencies, refactors unit tests around shared fixtures, and enables app-level coverage. Sequence diagram for service ID collision retrysequenceDiagram
participant Client
participant RegisterService
participant MongoDB
Client->>RegisterService: register_metadata(retries)
loop Each retry attempt
RegisterService->>RegisterService: generate_id(charset, length)
RegisterService->>MongoDB: insert_one(insert_doc)
alt DuplicateKeyError
MongoDB-->>RegisterService: DuplicateKeyError
else Insert succeeds
MongoDB-->>RegisterService: Insert result
RegisterService-->>Client: Service registered
end
end
RegisterService-->>Client: InternalServerError
Sequence diagram for PUT service replacement trackingsequenceDiagram
participant Client
participant RegisterService
participant MongoDB
Client->>RegisterService: register_metadata()
RegisterService->>MongoDB: replace_one(filter, replacement, upsert=True)
MongoDB-->>RegisterService: matched_count, modified_count
alt Document matched or modified
RegisterService->>RegisterService: was_replaced = True
end
RegisterService-->>Client: Replacement complete
Sequence diagram for service-info POST responsesequenceDiagram
participant Client
participant postServiceInfo
participant RegisterServiceInfo
Client->>postServiceInfo: POST service-info
postServiceInfo->>RegisterServiceInfo: set_service_info_from_app_context(data)
RegisterServiceInfo-->>postServiceInfo: headers
postServiceInfo-->>Client: 201 with headers
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_app.py" line_range="254" />
<code_context>
+ monkeypatch.setattr("connexion.apps.flask_app.FlaskApp.run", run_mock)
+
+ app_main()
+ assert run_mock.called
+
+ def test_app_main_missing_config(self, monkeypatch):
</code_context>
<issue_to_address>
**Startup test misses service-info registration**
When `app_main()` starts the app without registering service-info, `test_app_main_startup` asserts only `run_mock.called` at line 254, so `app_main()` can omit service-info registration and the test still passes as long as it starts the app.
Assert that startup persisted the configured service-info document before the patched app run completes.
</issue_to_address>
### Comment 2
<location path="tests/ga4gh/registry/test_service.py" line_range="117" />
<code_context>
+ ):
+ obj = RegisterService(data=data)
+ obj.register_metadata()
+ assert obj.data["id"] == "NEW_UNIQUE_ID"
+ assert "id" not in data
+ assert "_id" not in data
</code_context>
<issue_to_address>
**Retry test accepts unpersisted service**
When the retry assigns `NEW_UNIQUE_ID` to the object but does not insert it into the collection, `test_register_metadata_collision_retry_success` only checks `obj.data["id"]` at line 117; if the retry assigns `NEW_UNIQUE_ID` but skips inserting the service, the test still passes without verifying the registration succeeded.
Assert that the collection contains the registered service with `NEW_UNIQUE_ID` after the retry.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and a successful POST writes a service record under the newly generated ID, and that record remains after reverting this change; if the retry path writes an unintended record, it can be removed or corrected. The changed replacement flag may also affect the response for a no-op PUT, but that behavior is reversible.
Blocking findings: tests/test_app.py:254, tests/ga4gh/registry/test_service.py:117
| monkeypatch.setattr("connexion.apps.flask_app.FlaskApp.run", run_mock) | ||
|
|
||
| app_main() | ||
| assert run_mock.called |
There was a problem hiding this comment.
🟡 Medium · Startup test misses service-info registration
When app_main() starts the app without registering service-info, test_app_main_startup asserts only run_mock.called at line 254, so app_main() can omit service-info registration and the test still passes as long as it starts the app.
Assert that startup persisted the configured service-info document before the patched app run completes.
Prompt for AI agents
In `tests/test_app.py` at line 254:
**Startup test misses service-info registration**
When `app_main()` starts the app without registering service-info, `test_app_main_startup` asserts only `run_mock.called` at line 254, so `app_main()` can omit service-info registration and the test still passes as long as it starts the app.
Assert that startup persisted the configured service-info document before the patched app run completes.| ): | ||
| obj = RegisterService(data=data) | ||
| obj.register_metadata() | ||
| assert obj.data["id"] == "NEW_UNIQUE_ID" |
There was a problem hiding this comment.
🟡 Medium · Retry test accepts unpersisted service
When the retry assigns NEW_UNIQUE_ID to the object but does not insert it into the collection, test_register_metadata_collision_retry_success only checks obj.data["id"] at line 117; if the retry assigns NEW_UNIQUE_ID but skips inserting the service, the test still passes without verifying the registration succeeded.
Assert that the collection contains the registered service with NEW_UNIQUE_ID after the retry.
Prompt for AI agents
In `tests/ga4gh/registry/test_service.py` at line 117:
**Retry test accepts unpersisted service**
When the retry assigns `NEW_UNIQUE_ID` to the object but does not insert it into the collection, `test_register_metadata_collision_retry_success` only checks `obj.data["id"]` at line 117; if the retry assigns `NEW_UNIQUE_ID` but skips inserting the service, the test still passes without verifying the registration succeeded.
Assert that the collection contains the registered service with `NEW_UNIQUE_ID` after the retry.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #89 +/- ##
===========================================
- Coverage 100.00% 99.52% -0.48%
===========================================
Files 5 6 +1
Lines 189 211 +22
===========================================
+ Hits 189 210 +21
- Misses 0 1 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks a lot for picking this up so quickly! I reviewed it and ran it locally. Here is what I found. What works
Why it doesn't fully address #71 and #40 yet
How we'll proceed While you were working on this, I had a branch for the same issues in progress (the issues were drafted as part of a planned sequence). It is now up as #90. Its harness supports FOCA 0.12 and 0.14, checks token signatures, and also covers a 500 response. Since both PRs touch the same files, only one of them can be merged; I will decide which once both have been reviewed. Either way, your review of the problem was spot on, and thanks again for the contribution! |
Description
Fixes #71 and #72:
Service ID Collision Retry Fix (Bug: service ID retry never generates a new ID #72):
RegisterService.register_metadata, generates a new random service identifier on each retry iteration instead of re-attempting with the same conflicting ID.was_replaced = Trueon PUT when document was matched or modified.App-level Test Harness & Coverage (Tests: app-level test harness with fixtures #71):
tests/test_app.pyexercising all endpoints, OpenAPI validation, authentication, and startup.mongomockand mock OIDC token validation.cloud_registry/app.pycoverage inpyproject.tomlwith 99% overall test coverage.Tuple[None, int, Dict]status code type inpostServiceInfocontroller to prevent Connexion logger format errors.Closes #71
Closes #72
Closes #32
Closes #40
Summary by Sourcery
Prevent service ID collision retries from reusing identifiers and strengthen end-to-end application test coverage.
New Features:
Bug Fixes:
Enhancements:
Tests:
Chores: