Repository navigation
test: add integration tests with MongoDB - #76
Conversation
The tests that clone drs-filer from GitHub now clone a local stand-in, with Git rewriting the URL (url.<base>.insteadOf), so that they need no network access. They also check the checked-out commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Test registering, publishing and notifying through the API of the app as deployed (create_app(), with response validation), with a local subscriber. The tests use mongomock, or the MongoDB at MONGODB_TEST_URI; CI runs all tests in a matrix with mongomock (with coverage) and against MongoDB 7.0, the chart's default. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cover unreachable subscribers, publishing without subscriptions and unknown repositories, check that responses are validated, share the request helpers, close the app's client after each test and ignore MONGO_* settings from the environment. Run only the integration tests against MongoDB in CI, as the unit tests always use mongomock, and say so in the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep the refused port bound while it is used, clear the remaining FOCA settings from the environment that would affect the tests, and clarify what the fixtures and checks do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideConfigurationRun the full suite with mongomock and the integration suite against MongoDB 7.0, reporting coverage only from the mongomock job.
DocumentationDocument the test levels, network-free unit tests, MongoDB configuration, and how to run integration tests locally.
TestsReplace GitHub clones in unit tests with a local repository stand-in and assert the expected commit or tag is checked out.
Exercise the deployed API against mongomock or MongoDB, including registration, publishing, notification outcomes, and rejected requests.
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 #76 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 1257 1257
=========================================
Hits 1257 1257 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path=".github/workflows/ci.yml" line_range="128-129" />
<code_context>
+ services:
+ # Not started for an empty image (the `mongomock` entry)
+ mongodb:
+ image: ${{ matrix.image }}
+ ports: ["27017:27017"]
+ options: >-
+ --health-cmd "mongosh --quiet --eval 'db.adminCommand({ping: 1}).ok'"
</code_context>
<issue_to_address>
**Mongomock job fails during setup**
When the mongomock matrix entry is selected, the `mongodb` service still uses the empty `matrix.image`, so the runner rejects or fails to start the service before the mongomock tests run; the job fails instead of skipping MongoDB.
Define the `mongodb` service only for matrix entries with a non-empty image.
</issue_to_address>
### Comment 2
<location path="tests/integration/conftest.py" line_range="114" />
<code_context>
+ )
+ else:
+ monkeypatch.setenv("MONGO_URI", MONGODB_TEST_URI)
+ mongodb.drop_database(DB)
+ app = create_app().app
+ yield app.test_client()
</code_context>
<issue_to_address>
**Test runs delete shared database data**
When another test process passes the initial database check concurrently, or another process starts using the database after that check, the `mongodb` fixture check does not reserve `DB`, and the `api` fixture drops it before tests and again at session teardown, deleting another process's in-progress or newly created data and causing API failures or lost results.
Give each test process an isolated database and only drop that process's database.
Also at `tests/integration/conftest.py:63`, `tests/integration/conftest.py:70`.
</issue_to_address>
### Comment 3
<location path="tests/integration/test_api.py" line_range="231" />
<code_context>
+ """Users need to be verified by an admin before they can subscribe."""
+ user = register_user(api, verify=False)
+ repository = register_repository(api)
+ subscribe(api, user, repository["id"], subscriber.url, status=403)
+
+
</code_context>
<issue_to_address>
**Rejected requests can still create subscriptions**
When a subscription request creates a record before returning 403 or 404, when either rejected subscription request writes a record before returning its error, `test_unverified_user_cannot_subscribe` and `test_subscribe_to_unknown_repository` pass because `subscribe` checks only the HTTP status. The caller sees 403 or 404 while a subscription persists.
After each rejected request, assert that no subscription was persisted and that the related state is unchanged.
Also at `tests/integration/test_api.py:238`.
</issue_to_address>
### Comment 4
<location path="tests/pubgrade/endpoints/test_builds.py" line_range="599-606" />
<code_context>
repo_url=self.repository_url,
branch="dev",
- commit="122c34d",
+ commit=drs_filer.commit,
base_dir=".",
build_id="build123",
</code_context>
<issue_to_address>
**Broken clones pass the error test**
When `git_clone_and_checkout` raises `GitCloningError` on every call, `test_git_clone_and_checkout_git_command_error` (lines 595–609) puts the first valid clone inside `pytest.raises`; if `git_clone_and_checkout` raises `GitCloningError` for every clone, the test passes before reaching the second call. A regression that breaks all successful clones is therefore hidden.
Assert that the first valid clone succeeds outside `pytest.raises`, then check that the second clone raises.
</issue_to_address>Linked issues
#39 test: add integration tests with MongoDB · 4 of 4 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Run the integration tests with mongomock and MongoDB in a CI matrix using MONGODB_TEST_URI. | ✅ Met | The mongomock matrix entry runs the full test suite, including integration tests; the MongoDB 7.0 entry sets MONGODB_TEST_URI and runs tests/integration. |
| 2 | Test the API through the app with response validation on. | ✅ Met | The integration fixture creates the app with create_app(), and test_response_validation checks that response validation is enabled. |
| 3 | Use a local repository instead of GitHub for unit tests that clone a public repository. | ✅ Met | The clone tests use a locally created drs-filer repository, with Git URL rewriting configured by the fixture. |
| 4 | Test registering, publishing and notifying through the API against MongoDB in CI. | ✅ Met | The API tests exercise registration, publishing and subscriber notifications; the MongoDB CI entry runs them against its MongoDB 7.0 service. |
Sourcery assessment
Needs a human reviewer. 4 findings to address first, and when MONGODB_TEST_URI is set, the test fixture drops the pubgradeStore database before each test and again at session end. It refuses to run if that database exists initially, but another process that writes to it after the check could have its records deleted; reverting the PR would not restore them.
Blocking findings: .github/workflows/ci.yml:129, tests/integration/conftest.py:114, tests/integration/test_api.py:231, tests/pubgrade/endpoints/test_builds.py:606
Rejected subscriptions must not be stored, and the clone that should succeed in the clone-failure test now runs outside pytest.raises. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Integration tests of the API against a real database, following cloud-registry:
tests/integration/):create_app()with the app's own configuration and response validation.MONGODB_TEST_URI. Its databasepubgradeStoreis dropped before each test, and the tests refuse to start if it already exists.MONGO_*settings and its configuration overlay are cleared from the environment.testbecomes a matrix.Test (mongomock)runs all tests with coverage to Codecov.Test (MongoDB 7.0)runs the integration tests against a MongoDB 7.0 service, the chart's default. The unit tests always use mongomock, so they don't run twice.url.<base>.insteadOfin a Git configuration of the tests' own), so the tests need no network access. They also check the commit or tag that was checked out.docs/development.mdexplains the test levels,MONGODB_TEST_URIand the CI matrix.Closes #39
Verification
pytest --cov: 501 passed, 100% coverage, with no network access (run in a sandbox without github.com).MONGODB_TEST_URI=mongodb://127.0.0.1:27117 pytest tests/integrationagainst a throwawaymongo:7.0: 10 passed. The database is gone afterwards.ruff check,ruff format --check,mypy: clean.pre-commit run --all-filesandgitleaks-all: passed. actionlint was skipped locally (the sandbox can't download its Go toolchain); CI runs it.Test (mongomock): 501 passed with coverage.Test (MongoDB 7.0): the 10 integration tests pass against the service.Review
Full review: adversarial and quality reviewers, two rounds.
Round 1: no high findings. Fixed:
MONGO_*settings are ignored;matrix.database.Round 2: no high or medium findings. Fixed: the refused port stays bound while it is used, more FOCA environment settings are cleared, and comments are clearer.
Sourcery (4 threads):
Test (mongomock)passes); a separate database per test process (the test MongoDB is a throwaway for one run, as documented).CI: all checks pass on 3aeabdb.
Declined review findings
SimpleNamespacewith three attributes, used in five tests, is clear enough.GIT_CONFIG_GLOBAL(low). CI and current distributions have it, and older versions only fail the clone tests.Open questions
None.
🤖 Generated with Claude Code