summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorJake Howard <git@theorangeone.net>2024-06-11 19:27:49 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-04 11:38:58 +0200
commit53e674d5744faad61e52d8459c9198b2aa6f63dd (patch)
tree0b2de1867fbec42098705ae834ff9fa2af33f78f /tests/auth_tests
parent31837dbcb36f1ab57fb1b16cb0b126c55a1bdf01 (diff)
Fixed #35520 -- Avoided opening transaction for read-only ModelAdmin requests.
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/test_admin_multidb.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/tests/auth_tests/test_admin_multidb.py b/tests/auth_tests/test_admin_multidb.py
index ce2ae6b103..17b04faa65 100644
--- a/tests/auth_tests/test_admin_multidb.py
+++ b/tests/auth_tests/test_admin_multidb.py
@@ -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()