Repository navigation
feat: use a configuration without an app - #742
Conversation
Foca.use_config() makes the configuration the one that get_config() returns outside app contexts and registers its databases without creating indexes, so that scripts need neither Flask, an app context nor set_app_config(), and write nothing to the database (#741). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Scripts of apps with access control read the stored policy rules; use_config() registers their collection too, via the part of register_access_control() that registers it, now shared. use_config() returns None, and the docs say that it gives no app context (review of #741). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideAPIAdd the public
Update the configuration-access API documentation and deprecation guidance to point callers to
ImplementationExtract configuration selection and access-control policy-collection registration so direct configuration use and app startup can share the registration logic.
DocumentationExplain how scripts use configured collections without an app and update the migration guidance for
Replace the note that no Flask-free replacement exists with a before-and-after example for
Add
TestsCover configuration use without an app, collection and policy registration, index behavior, configuration precedence, and later app creation.
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 #742 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 74 74
Lines 9385 9400 +15
=========================================
+ Hits 9385 9400 +15
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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="foca/security/access_control/register_access_control.py" line_range="553" />
<code_context>
- registered_db, registered_collection, added_db = registered
- registered_db_conf = mongo_config.dbs.get(registered_db)
- if added_db:
- mongo_config.dbs.pop(registered_db, None)
- elif (
- registered_db_conf is not None
</code_context>
<issue_to_address>
**Repeated registration drops configured collections**
When access control is enabled, `use_config()` creates the policy-only database, and the caller adds an app collection to that database before calling `use_config()` again, `added_db` remains true even if the caller later adds app collections to that database, so `_register_policy_collection()` removes the entire database on the next `use_config()` call and those collections disappear from `get_collection()`.
Before removing the database, preserve it if it now contains app collections, removing only the previously registered policy collection.
</issue_to_address>
### Comment 2
<location path="tests/test_foca.py" line_range="705" />
<code_context>
+ assert collection.name == access_control.collection_name
+ assert not list(collection.find())
+ foca.create_app()
+ assert get_collection(*names).full_name == collection.full_name
+
+
</code_context>
<issue_to_address>
**Separate policy database passes**
When app creation registers the policy collection against a different MongoDB client, when app creation registers the policy collection against a different MongoDB client, `test_use_config_access_control` line 705 still passes because it compares only `full_name`; both clients can expose the same database and collection name while the app cannot see the previously registered collection’s data.
Verify shared backing data across app creation, for example by inserting a document before it and reading that document afterward.
</issue_to_address>Linked issues
#741 feat: use a configuration without creating an app · 5 of 5 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Provide a public, framework-neutral way to use a configuration without creating an app. | ✅ Met | Foca.use_config() makes its configuration available through get_config() outside app contexts. |
| 2 | Make configured collections usable without creating indexes. | ✅ Met | use_config() registers configured databases and collections with index creation disabled; it also registers the stored policy-rules collection when access control is enabled. |
| 3 | Work without a Flask app context and define which configuration is used when apps and use_config() calls are mixed. |
✅ Met | The docstrings define the most-recent-call rule outside app contexts and app-specific configuration inside an app context; tests cover both. |
| 4 | Cover the API and behavior with tests, and update the Preparing for the ASGI release guide, migration guide, and API reference. | ✅ Met | Tests cover use without an app, collection registration, precedence, and app reuse; the guides are updated, and the API reference is generated from the updated docstrings. |
| 5 | Ship the feature in 0.22.0. | ✅ Met | The 0.22.0 release notes list Foca.use_config() as a feature. |
Sourcery assessment
Needs a human reviewer. 2 findings to address first, and use_config() registers configured collections, including the access-control policy collection, so a registration bug could send a script’s database operations to the wrong collection; any records already written would remain after reverting and need repair. The method itself creates no indexes or other database writes.
Blocking findings: foca/security/access_control/register_access_control.py:553, tests/test_foca.py:705
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Scripts and tools that use a FOCA configuration and its collections without creating the app had no replacement for the deprecated
set_app_config(): they attached the configuration to a bare Flask app and ran in its app context.Foca.use_config()replaces that, without Flask or an app context:get_config()returns the configuration outside app contexts; the most recent ofuse_config()and creating an app (of anyFocainstance) wins, and in an app context the app's own configuration is used, as before.None. Internals shared with app creation:_use_config(), and_register_policy_collection(), moved out ofregister_access_control()unchanged.get_config()'s error without any configuration and the deprecation warning ofset_app_config()now point toFoca.use_config().Docs: Preparing for the ASGI release (Configuration: before/after example instead of "no Flask-free replacement in 0.22"), the migration guide (table row for
set_app_config()and the rule ofget_config()), the database configuration (scripts), the API reference (from the docstrings ofFoca.use_config(),get_config()andget_collection()), and the 0.22.0 release notes (Features).Closes #741
How cloud-registry would switch (not changed here)
cloud_registry/namespace_subjects.py,_read_only_app()for--dry-run:create_foca()andrefuse_old_format().Flask(__name__),set_app_config(), adding the collection of the stored rules, clearing the indexes of the collections andregister_mongodb(): callfoca.use_config()instead, which registers all configured collections and that of the stored rules without creating indexes.main(), run the dry run without an app context (e.g.,contextlib.nullcontext()), and keepcreate_app().app.app_context()for real runs, which need the Casbin adapter (mark_changed()) that only apps register.Verification
ruff format,ruff check,mypy foca tests: clean.coverage run -m pytestwith MongoDB 7.0: 4895 passed, 2 skipped;coverage report: 100% (before the last, test- and docs-only commit; its new tests and the release-notes and docs tests pass).mkdocs build --strict: OK.CI passed, tests on Python 3.11 to 3.15, Codecov, Read the Docs).tests/test_foca.py: use without an app or app context and without indexes; without databases; twice; with access control (policy collection registered, with and without app databases, thencreate_app()); a clashing policy collection; the most recent ofuse_config()andcreate_app()wins, and the app context keeps its own; an app created afterwards shares the registered collections and creates its indexes.Declined review findings
_register_policy_collection()passescreate_indexes=Falseon to_register_database()fromuse_config(): on that path the app databases are already registered (so the merge branch only sets the collection's client), and a new policy database has no indexes, so the flag can't change whatuse_config()writes; the refactor keeps the default (db.create_indexes_on_startup) for apps, and the existing access-control tests pass.use_config()across the docs: each page keeps a short version for its readers and links to Preparing for the ASGI release.register_access_control()(repeatedcreate_app()behaves the same), and changing the configured collections between registrations isn't a supported flow.Open questions
None.
🤖 Generated with Claude Code