Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-db: databases not being blocked #1127

Open
meshy opened this issue Jul 3, 2024 · 0 comments
Open

Multi-db: databases not being blocked #1127

meshy opened this issue Jul 3, 2024 · 0 comments

Comments

@meshy
Copy link
Contributor

meshy commented Jul 3, 2024

Hi!

I've found that databases are not always blocked when they should be in a multi-db setup. (Related to #924)

Allowing access to a single database can grant access to them all.

I've boiled this down to a minimal failing example, but I haven't yet found the cause or solution.

# test_example.py
import pytest
from django.db.transaction import atomic


@pytest.mark.django_db(databases=["default"])
def test_wrong_db():
    with pytest.raises(RuntimeError, match="Database access not allowed"):
        with atomic(using="secondary"):
            pass
# example_settings.py
import os

env = os.environ

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": env.get("DB_NAME", default="example"),
        "USER": env.get("DB_USER", default=""),
        "PASSWORD": env.get("DB_PASSWORD", default=""),
        "HOST": env.get("DB_HOST", default=""),
        "PORT": env.get("DB_PORT", default=5432),
        "TEST": {"NAME": env.get("DB_TEST_NAME", default="example-test")},
    },
    "secondary": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": env.get("DB2_NAME", default="secondary"),
        "USER": env.get("DB2_USER", default=""),
        "PASSWORD": env.get("DB2_PASSWORD", default=""),
        "HOST": env.get("DB2_HOST", default=""),
        "PORT": env.get("DB2_PORT", default=5432),
        "TEST": {"NAME": env.get("DB2_TEST_NAME", default="example-secondary-test")},
    },
}
================================ test session starts =================================
platform linux -- Python 3.10.12, pytest-8.2.2, pluggy-1.5.0
django: version: 5.0.6, settings: example_settings (from ini)
rootdir: /home/charlie/code/pytest-django-multidb
configfile: pyproject.toml
plugins: django-4.8.0
collected 1 item                                                                     

test_example.py F                                                              [100%]

====================================== FAILURES ======================================
___________________________________ test_wrong_db ____________________________________

    @pytest.mark.django_db(databases=["default"])
    def test_wrong_db():
>       with pytest.raises(RuntimeError, match="Database access not allowed"):
E       Failed: DID NOT RAISE <class 'RuntimeError'>

test_example.py:7: Failed
============================== short test summary info ===============================
FAILED test_example.py::test_wrong_db - Failed: DID NOT RAISE <class 'RuntimeError'>
================================= 1 failed in 0.24s ==================================

Versions in use:

  • django==5.0.6
  • psycopg==3.2.1
  • pytest-django==4.8.0
  • pytest==8.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant