From 8520fbce18b6077a6625f99f6835717594c960c0 Mon Sep 17 00:00:00 2001 From: benzkji Date: Fri, 27 Oct 2017 17:13:09 +0200 Subject: [PATCH 1/3] basic tests finished --- separate_users/models.py | 1 + separate_users/tests/settings.py | 25 +++++++++---------- separate_users/tests/settings_custom_user.py | 10 ++++++++ separate_users/tests/test_admin.py | 13 +++++++--- separate_users/tests/test_app/views.py | 0 .../__init__.py} | 0 .../tests/test_app_custom_user/admin.py | 5 ++++ .../migrations/__init__.py} | 0 .../tests/test_app_custom_user/models.py | 6 +++++ separate_users/tests/test_compatibility.py | 24 ++++++++---------- separate_users/tests/test_permissions.py | 22 +++++++++++++--- 11 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 separate_users/tests/settings_custom_user.py delete mode 100644 separate_users/tests/test_app/views.py rename separate_users/tests/{test_app/admin.py => test_app_custom_user/__init__.py} (100%) create mode 100644 separate_users/tests/test_app_custom_user/admin.py rename separate_users/tests/{test_app/models.py => test_app_custom_user/migrations/__init__.py} (100%) create mode 100644 separate_users/tests/test_app_custom_user/models.py diff --git a/separate_users/models.py b/separate_users/models.py index 50dd3a9..8198c41 100644 --- a/separate_users/models.py +++ b/separate_users/models.py @@ -57,6 +57,7 @@ def save(self, *args, **kwargs): self.is_staff = False super(FrontendUser, self).save(*args, **kwargs) + class Editor(SeparateUserBase, UserModel): objects = EditorManager() diff --git a/separate_users/tests/settings.py b/separate_users/tests/settings.py index 5bf189f..e22e8f2 100644 --- a/separate_users/tests/settings.py +++ b/separate_users/tests/settings.py @@ -46,17 +46,6 @@ 'migrations', 'fixtures', 'admin$', 'django_extensions', ] -EXTERNAL_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.messages', - 'django.contrib.sessions', - 'django.contrib.staticfiles', - 'django.contrib.sitemaps', - 'django.contrib.sites', -) - TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -79,11 +68,21 @@ }, ] +EXTERNAL_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.messages', + 'django.contrib.sessions', + 'django.contrib.staticfiles', + 'django.contrib.sitemaps', + 'django.contrib.sites', +] -INTERNAL_APPS = ( +INTERNAL_APPS = [ 'separate_users', 'separate_users.tests.test_app', -) +] MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/separate_users/tests/settings_custom_user.py b/separate_users/tests/settings_custom_user.py new file mode 100644 index 0000000..7fefa00 --- /dev/null +++ b/separate_users/tests/settings_custom_user.py @@ -0,0 +1,10 @@ +from .settings import * +# TODO: it doesnt work! + +AUTH_USER_MODEL = 'test_app_custom_user.CustomUser' + +del(INSTALLED_APPS[INSTALLED_APPS.index('separate_users.tests.test_app')]) + +print INSTALLED_APPS + +INSTALLED_APPS.insert(0, 'separate_users.tests.test_app_custom_user') diff --git a/separate_users/tests/test_admin.py b/separate_users/tests/test_admin.py index 7324c97..a54e7e7 100644 --- a/separate_users/tests/test_admin.py +++ b/separate_users/tests/test_admin.py @@ -13,6 +13,7 @@ class AdminTestCase(TestCase): TODO: check with custom User Model! """ def setUp(self): + self.auth_custom_user = 'test_app_custom_user.CustomUser' self.superuser = create_superuser() self.client.login(username='admin', password='secret') self.frontenduser = FrontendUser.objects.create(username='frontend') @@ -33,16 +34,22 @@ def test_editor_changelist(self): self.changelist('editor') # @override_settings( - # AUTH_USER_MODEL = 'test_app.CustomUser' + # AUTH_USER_MODEL = 'test_app_custom_user.CustomUser' # ) # def test_frontend_user_changelist_custom_user(self): # self.changelist('frontenduser') # # @override_settings( - # AUTH_USER_MODEL = 'test_app.CustomUser' + # AUTH_USER_MODEL = 'test_app_custom_user.CustomUser' # ) # def test_editor_changelist_custom_user(self): - # self.changelist('editor') + # with self.modify_settings(INSTALLED_APPS={ + # 'append': 'test_app_custom_user', + # 'remove': [ + # 'test_app', + # ], + # }): + # self.changelist('editor') def change(self, user_version, id): url = reverse( diff --git a/separate_users/tests/test_app/views.py b/separate_users/tests/test_app/views.py deleted file mode 100644 index e69de29..0000000 diff --git a/separate_users/tests/test_app/admin.py b/separate_users/tests/test_app_custom_user/__init__.py similarity index 100% rename from separate_users/tests/test_app/admin.py rename to separate_users/tests/test_app_custom_user/__init__.py diff --git a/separate_users/tests/test_app_custom_user/admin.py b/separate_users/tests/test_app_custom_user/admin.py new file mode 100644 index 0000000..5ef0f20 --- /dev/null +++ b/separate_users/tests/test_app_custom_user/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import CustomUser + +admin.site.register(CustomUser, UserAdmin) diff --git a/separate_users/tests/test_app/models.py b/separate_users/tests/test_app_custom_user/migrations/__init__.py similarity index 100% rename from separate_users/tests/test_app/models.py rename to separate_users/tests/test_app_custom_user/migrations/__init__.py diff --git a/separate_users/tests/test_app_custom_user/models.py b/separate_users/tests/test_app_custom_user/models.py new file mode 100644 index 0000000..34d487b --- /dev/null +++ b/separate_users/tests/test_app_custom_user/models.py @@ -0,0 +1,6 @@ +from django.conf import settings +from django.contrib.auth.models import AbstractUser + + +class CustomUser(AbstractUser): + pass diff --git a/separate_users/tests/test_compatibility.py b/separate_users/tests/test_compatibility.py index 4787279..c8c75a3 100644 --- a/separate_users/tests/test_compatibility.py +++ b/separate_users/tests/test_compatibility.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -from time import sleep - -from django.contrib.auth.models import User +from django.test import override_settings from django.test.testcases import TestCase @@ -10,9 +8,6 @@ class AdminTestCase(TestCase): TODO: check for pre 1.11 ImproperlyConfigured etc. TODO: check that MIGRATION_MODULES is configured (migration are written into this app!) """ - username = 'admin' - password = 'admin' - def setUp(self): pass @@ -20,13 +15,16 @@ def tearDown(self): pass def test_improperly_pre_1_11_with_custom_user_model(self): - exit() + pass + @override_settings( + MIGRATION_MODULES = {} + ) def test_improperly_no_migration_modules(self): - exit() - - def test_frontend_user_changeview(self): - exit() + """ + TOOD: trigger improperly configured!!? but how? + :return: + """ + from separate_users.models import FrontendUser + FrontendUser.objects.all() - def test_staff_user_changeview(self): - exit() diff --git a/separate_users/tests/test_permissions.py b/separate_users/tests/test_permissions.py index 134a52e..5930b15 100644 --- a/separate_users/tests/test_permissions.py +++ b/separate_users/tests/test_permissions.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from django.contrib.auth.models import Permission from django.core.urlresolvers import reverse from django.test import Client from django.test import TestCase @@ -7,7 +8,6 @@ class PermissionsTestCase(TestCase): """ 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. """ def setUp(self): pass @@ -15,8 +15,22 @@ def setUp(self): def tearDown(self): pass - def no_permissions_initially(self): - exit() + def test_no_permissions_initially(self): + """ + check if not there initially - this will fail if django fixes the bug! nice to know. + """ + self.assertEqual(self.permission_count('separate_users', 'add_frontenduser'), 0) + self.assertEqual(self.permission_count('separate_users', 'add_editor'), 0) def test_permission_management_command(self): - exit() + from django.core.management import call_command + call_command('fix_proxy_permissions', ) + self.assertEqual(self.permission_count('separate_users', 'add_frontenduser'), 1) + self.assertEqual(self.permission_count('separate_users', 'add_editor'), 1) + + def permission_count(self, app_label, codename): + count = Permission.objects.filter( + content_type__app_label=app_label, + codename=codename, + ).count() + return count \ No newline at end of file From 18669ec1fb72f09953c9d5d94b7ebd16c17fcf85 Mon Sep 17 00:00:00 2001 From: benzkji Date: Fri, 27 Oct 2017 18:31:15 +0200 Subject: [PATCH 2/3] docs --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7dc60a6..452a3c6 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,19 @@ # django-separate-users -separate staff and non staff users with two proxy models (FrontendUser and Editor). + +Separate staff and non staff users with two proxy models (FrontendUser and Editor). Nothing fancy, but as I ended up doing this again and again, this is a simple plug and forget solution, that I'll probably use in many projects from now on. +- minimal requirement are the `is_staff` and `is_superuser` fields on your user model - staff users can be given the right to edit non staff users (currently not possible, or everyone can make everyone a superuser) - fieldsets for staff and non staff users can be defined via settings (not yet) - better admin list views (filters, is_active, etc) -NOTE / WARNING: With django<1.11, it's not possible to run this app with as custom +[//]: # (NOTE / WARNING: With django<1.11, it's not possible to run this app with as custom `settings.AUTH_USER_MODEL`. See https://stackoverflow.com/questions/46935758/djangos-get-user-model-only-in-1-11-during-import-time +t) +NOTE: Custom user models are not supported (yet). ## Usage From 893294ee5b79bd920af4be48edb51193a944dcc6 Mon Sep 17 00:00:00 2001 From: benzkji Date: Sun, 5 Nov 2017 21:26:09 +0100 Subject: [PATCH 3/3] django 1.8/1.9 --- .../management/commands/fix_proxy_permissions.py | 10 +++++++++- separate_users/tests/test_admin.py | 7 +++++-- setup.py | 2 +- tox.ini | 8 ++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/separate_users/management/commands/fix_proxy_permissions.py b/separate_users/management/commands/fix_proxy_permissions.py index d43dfe6..244891c 100644 --- a/separate_users/management/commands/fix_proxy_permissions.py +++ b/separate_users/management/commands/fix_proxy_permissions.py @@ -1,3 +1,4 @@ +import inspect import sys from django.contrib.auth.management import _get_all_permissions @@ -10,6 +11,7 @@ # where this comes from: https://gist.github.com/magopian/7543724#gistcomment-2185491 # as django adds permissions for proxy models in another app for the proxies parent # also, epic: https://code.djangoproject.com/ticket/11154 +# also: https://stackoverflow.com/questions/38391729/how-to-retrieve-all-permissions-of-a-specific-model-in-django class Command(BaseCommand): help = "Fix permissions for proxy models." @@ -21,7 +23,13 @@ def handle(self, *args, **options): app_label=opts.app_label, model=opts.object_name.lower()) - for codename, name in _get_all_permissions(opts): + argspecs = inspect.getargspec(_get_all_permissions) + if len(argspecs[0]) == 2: + # django < 1.10 + all_permissions = _get_all_permissions(opts, ctype) + else: + all_permissions = _get_all_permissions(opts) + for codename, name in all_permissions: sys.stdout.write(' --{}\n'.format(codename)) p, created = Permission.objects.get_or_create( codename=codename, diff --git a/separate_users/tests/test_admin.py b/separate_users/tests/test_admin.py index a54e7e7..05e2684 100644 --- a/separate_users/tests/test_admin.py +++ b/separate_users/tests/test_admin.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- -from django.test import override_settings +# from django.test import override_settings from django.test.testcases import TestCase -from django.urls import reverse +try: + from django.urls import reverse +except ImportError: + from django.core.urlresolvers import reverse from separate_users.models import Editor, FrontendUser from separate_users.tests.utils.django_utils import create_superuser diff --git a/setup.py b/setup.py index 386d682..17b76dc 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ def read(fname): packages=find_packages(), install_requires=( # 'Django>=1.3,<1.5', # no need to limit while in development - 'Django>=1.11', + 'Django>=1.8', ), include_package_data=True, zip_safe=False, diff --git a/tox.ini b/tox.ini index 655e28e..e9a6595 100644 --- a/tox.ini +++ b/tox.ini @@ -16,20 +16,20 @@ deps = [testenv:django18] deps = - django>=1.8, <1.9 {[base]deps} + django>=1.8, <1.9 [testenv:django19] deps = - django>=1.9, <1.10 {[base]deps} + django>=1.9, <1.10 [testenv:django110] deps = - django>=1.10, <1.11 {[base]deps} + django>=1.10, <1.11 [testenv:django111] deps = + {[base]deps} django>=1.11, <1.12 - {[base]deps} \ No newline at end of file