Skip to content

Multi-db: databases not being blocked #1127

@meshy

Description

@meshy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions