2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2016-09-14 20:06:39 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from django.contrib.auth.models import User
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2018-12-07 22:52:28 +00:00
|
|
|
from django.urls import path, reverse
|
2016-09-14 20:06:39 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Router:
|
2016-09-14 20:06:39 +00:00
|
|
|
target_db = None
|
|
|
|
|
|
|
|
def db_for_read(self, model, **hints):
|
|
|
|
return self.target_db
|
|
|
|
|
|
|
|
db_for_write = db_for_read
|
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2016-09-14 20:06:39 +00:00
|
|
|
site = admin.AdminSite(name='test_adminsite')
|
|
|
|
site.register(User, admin_class=UserAdmin)
|
|
|
|
|
|
|
|
urlpatterns = [
|
2018-12-07 22:52:28 +00:00
|
|
|
path('admin/', site.urls),
|
2016-09-14 20:06:39 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF=__name__, DATABASE_ROUTERS=['%s.Router' % __name__])
|
|
|
|
class MultiDatabaseTests(TestCase):
|
2018-07-12 04:12:20 +00:00
|
|
|
databases = {'default', 'other'}
|
2016-09-14 20:06:39 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.superusers = {}
|
2019-12-20 19:49:56 +00:00
|
|
|
for db in cls.databases:
|
2016-09-14 20:06:39 +00:00
|
|
|
Router.target_db = db
|
|
|
|
cls.superusers[db] = User.objects.create_superuser(
|
|
|
|
username='admin', password='something', email='test@test.org',
|
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch('django.contrib.auth.admin.transaction')
|
|
|
|
def test_add_view(self, mock):
|
2019-12-20 19:49:56 +00:00
|
|
|
for db in self.databases:
|
2017-03-07 21:00:43 +00:00
|
|
|
with self.subTest(db_connection=db):
|
|
|
|
Router.target_db = db
|
|
|
|
self.client.force_login(self.superusers[db])
|
|
|
|
self.client.post(reverse('test_adminsite:auth_user_add'), {
|
|
|
|
'username': 'some_user',
|
|
|
|
'password1': 'helloworld',
|
|
|
|
'password2': 'helloworld',
|
|
|
|
})
|
|
|
|
mock.atomic.assert_called_with(using=db)
|