From 9a003d80b3ea3725af8ff84808ac75f74e9269e8 Mon Sep 17 00:00:00 2001 From: benzkji Date: Fri, 27 Oct 2017 15:37:50 +0200 Subject: [PATCH] yes --- CHANGELOG.txt | 2 - separate_users/admin.py | 16 ++++- separate_users/models.py | 1 - separate_users/tests/test_admin.py | 76 ++++++++++++++++------ separate_users/tests/test_compatibility.py | 4 +- separate_users/tests/test_permissions.py | 17 ++--- separate_users/tests/utils/django_utils.py | 5 ++ 7 files changed, 80 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 3687194..ecb4cfa 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -7,5 +7,3 @@ ==== 0.0.1 === - initial release - - diff --git a/separate_users/admin.py b/separate_users/admin.py index 5ee49c9..20f2ec7 100644 --- a/separate_users/admin.py +++ b/separate_users/admin.py @@ -11,8 +11,20 @@ # admin.site.unregister(get_user_model()) +class SeparateUserAdminBase(UserAdmin): -class FrontendUserAdmin(UserAdmin): + def get_urls(self): + model_name_lower = self.model.__name__.lower() + return [ + url( + r'^(.+)/password/$', + self.admin_site.admin_view(self.user_change_password), + name='separate_users_{}_password_change'.format(model_name_lower), + ), + ] + super(SeparateUserAdminBase, self).get_urls() + + +class FrontendUserAdmin(SeparateUserAdminBase): readonly_fields = ['date_joined', 'last_login', 'is_staff', 'is_superuser', 'groups', 'user_permissions', ] list_filter = ['is_active', 'groups', ] list_display = ['username', 'is_active', 'get_groups', ] @@ -21,7 +33,7 @@ class FrontendUserAdmin(UserAdmin): admin.site.register(FrontendUser, FrontendUserAdmin) -class EditorAdmin(UserAdmin): +class EditorAdmin(SeparateUserAdminBase): exclude = [] readonly_fields = ['date_joined', 'last_login', 'is_staff', 'is_superuser', 'user_permissions', ] list_filter = ['is_active', 'groups', ] diff --git a/separate_users/models.py b/separate_users/models.py index d93a309..50dd3a9 100644 --- a/separate_users/models.py +++ b/separate_users/models.py @@ -44,7 +44,6 @@ class Meta: proxy = True def get_groups(self): - print self.groups.all() return ', '.join([str(item) for item in self.groups.all()]) get_groups.short_description = ("Groups") diff --git a/separate_users/tests/test_admin.py b/separate_users/tests/test_admin.py index f261089..7324c97 100644 --- a/separate_users/tests/test_admin.py +++ b/separate_users/tests/test_admin.py @@ -1,39 +1,73 @@ # -*- coding: utf-8 -*- -from time import sleep - -from django.contrib.auth.models import User -from django.core.urlresolvers import reverse +from django.test import override_settings from django.test.testcases import TestCase +from django.urls import reverse + +from separate_users.models import Editor, FrontendUser +from separate_users.tests.utils.django_utils import create_superuser class AdminTestCase(TestCase): """ - test some basic admin views - make test generic, + check some basic admin views + TODO: check with custom User Model! """ - username = 'admin' - password = 'admin' - def setUp(self): - pass + self.superuser = create_superuser() + self.client.login(username='admin', password='secret') + self.frontenduser = FrontendUser.objects.create(username='frontend') + self.editor = Editor.objects.create(username='editor') def tearDown(self): pass + def changelist(self, user_version): + url = reverse('admin:separate_users_{}_changelist'.format(user_version)) + response = self.client.get(url, follow=True) + self.assertEqual(response.status_code, 200) + def test_frontend_user_changelist(self): - exit() + self.changelist('frontenduser') + + def test_editor_changelist(self): + self.changelist('editor') + + # @override_settings( + # AUTH_USER_MODEL = 'test_app.CustomUser' + # ) + # def test_frontend_user_changelist_custom_user(self): + # self.changelist('frontenduser') + # + # @override_settings( + # AUTH_USER_MODEL = 'test_app.CustomUser' + # ) + # def test_editor_changelist_custom_user(self): + # self.changelist('editor') + + def change(self, user_version, id): + url = reverse( + 'admin:separate_users_{}_change'.format(user_version), + args=[id] + ) + response = self.client.get(url, follow=True) + self.assertEqual(response.status_code, 200) - def test_staff_user_changelist(self): - exit() + def test_change_frontenduser(self): + self.change('frontenduser', self.frontenduser.id) - def test_frontend_user_changeview(self): - exit() + def test_change_editor(self): + self.change('editor', self.editor.id) - def test_staff_user_changeview(self): - exit() + def password_change(self, user_version, id): + url = reverse( + 'admin:separate_users_{}_password_change'.format(user_version), + args=[id] + ) + response = self.client.get(url, follow=True) + self.assertEqual(response.status_code, 200) - def test_staff_user_passwordchange(self): - exit() + def test_frontenduser_password_change(self): + self.password_change('frontenduser', self.frontenduser.id) - def test_frontend_user_passwordchange(self): - exit() + def test_editor_password_change(self): + self.password_change('editor', self.editor.id) diff --git a/separate_users/tests/test_compatibility.py b/separate_users/tests/test_compatibility.py index c12a72d..4787279 100644 --- a/separate_users/tests/test_compatibility.py +++ b/separate_users/tests/test_compatibility.py @@ -19,10 +19,10 @@ def setUp(self): def tearDown(self): pass - def test_frontend_user_changelist(self): + def test_improperly_pre_1_11_with_custom_user_model(self): exit() - def test_staff_user_changelist(self): + def test_improperly_no_migration_modules(self): exit() def test_frontend_user_changeview(self): diff --git a/separate_users/tests/test_permissions.py b/separate_users/tests/test_permissions.py index 25e1793..134a52e 100644 --- a/separate_users/tests/test_permissions.py +++ b/separate_users/tests/test_permissions.py @@ -6,26 +6,17 @@ class PermissionsTestCase(TestCase): """ - TODO: check for pre 1.11 ImproperlyConfigured etc. - TODO: check that MIGRATION_MODULES is configured (migration are written into this app!) + check if permissions are there, after management command was run. + check if not there initially - this will fail if django fixes the bug! nice to know. """ - username = 'admin' - password = 'admin' - def setUp(self): pass def tearDown(self): pass - def test_frontend_user_changelist(self): - exit() - - def test_staff_user_changelist(self): - exit() - - def test_frontend_user_changeview(self): + def no_permissions_initially(self): exit() - def test_staff_user_changeview(self): + def test_permission_management_command(self): exit() diff --git a/separate_users/tests/utils/django_utils.py b/separate_users/tests/utils/django_utils.py index f9a9f8c..e6a3d7a 100644 --- a/separate_users/tests/utils/django_utils.py +++ b/separate_users/tests/utils/django_utils.py @@ -2,6 +2,11 @@ def create_superuser(): + try: + from django.contrib.auth import get_user_model + User = get_user_model() + except ImportError: + from django.contrib.auth.models import User # NOQA superuser = User.objects.create_superuser('admin', 'admin@free.fr', 'secret')