mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #35520 -- Avoided opening transaction for read-only ModelAdmin requests.
This commit is contained in:
@@ -30,6 +30,7 @@ urlpatterns = [
|
||||
@override_settings(ROOT_URLCONF=__name__, DATABASE_ROUTERS=["%s.Router" % __name__])
|
||||
class MultiDatabaseTests(TestCase):
|
||||
databases = {"default", "other"}
|
||||
READ_ONLY_METHODS = {"get", "options", "head", "trace"}
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
@@ -42,13 +43,17 @@ class MultiDatabaseTests(TestCase):
|
||||
email="test@test.org",
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
# Reset the routers' state between each test.
|
||||
Router.target_db = None
|
||||
|
||||
@mock.patch("django.contrib.auth.admin.transaction")
|
||||
def test_add_view(self, mock):
|
||||
for db in self.databases:
|
||||
with self.subTest(db_connection=db):
|
||||
Router.target_db = db
|
||||
self.client.force_login(self.superusers[db])
|
||||
self.client.post(
|
||||
response = self.client.post(
|
||||
reverse("test_adminsite:auth_user_add"),
|
||||
{
|
||||
"username": "some_user",
|
||||
@@ -56,4 +61,19 @@ class MultiDatabaseTests(TestCase):
|
||||
"password2": "helloworld",
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
mock.atomic.assert_called_with(using=db)
|
||||
|
||||
@mock.patch("django.contrib.auth.admin.transaction")
|
||||
def test_read_only_methods_add_view(self, mock):
|
||||
for db in self.databases:
|
||||
for method in self.READ_ONLY_METHODS:
|
||||
with self.subTest(db_connection=db, method=method):
|
||||
mock.mock_reset()
|
||||
Router.target_db = db
|
||||
self.client.force_login(self.superusers[db])
|
||||
response = getattr(self.client, method)(
|
||||
reverse("test_adminsite:auth_user_add")
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
mock.atomic.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user