From 674c671cae5119146a302411b3f13e706577f47f Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 12 Dec 2010 22:59:28 +0000 Subject: [PATCH] Fixed #14731 -- Respect ordering when creating the default permissions. Thanks, chipx86. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14891 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../auth/fixtures/permissionstestdata.json | 316 ++++++++++++++++++ django/contrib/auth/management/__init__.py | 4 +- django/contrib/auth/tests/__init__.py | 1 + django/contrib/auth/tests/permissions.py | 32 ++ 4 files changed, 351 insertions(+), 2 deletions(-) create mode 100644 django/contrib/auth/fixtures/permissionstestdata.json create mode 100644 django/contrib/auth/tests/permissions.py diff --git a/django/contrib/auth/fixtures/permissionstestdata.json b/django/contrib/auth/fixtures/permissionstestdata.json new file mode 100644 index 0000000000..3e2e290b65 --- /dev/null +++ b/django/contrib/auth/fixtures/permissionstestdata.json @@ -0,0 +1,316 @@ +[ + { + "pk": 5, + "model": "contenttypes.contenttype", + "fields": { + "model": "contenttype", + "name": "content type", + "app_label": "contenttypes" + } + }, + { + "pk": 2, + "model": "contenttypes.contenttype", + "fields": { + "model": "group", + "name": "group", + "app_label": "auth" + } + }, + { + "pk": 8, + "model": "contenttypes.contenttype", + "fields": { + "model": "logentry", + "name": "log entry", + "app_label": "admin" + } + }, + { + "pk": 4, + "model": "contenttypes.contenttype", + "fields": { + "model": "message", + "name": "message", + "app_label": "auth" + } + }, + { + "pk": 1, + "model": "contenttypes.contenttype", + "fields": { + "model": "permission", + "name": "permission", + "app_label": "auth" + } + }, + { + "pk": 6, + "model": "contenttypes.contenttype", + "fields": { + "model": "session", + "name": "session", + "app_label": "sessions" + } + }, + { + "pk": 7, + "model": "contenttypes.contenttype", + "fields": { + "model": "site", + "name": "site", + "app_label": "sites" + } + }, + { + "pk": 3, + "model": "contenttypes.contenttype", + "fields": { + "model": "user", + "name": "user", + "app_label": "auth" + } + }, + { + "pk": 1, + "model": "sites.site", + "fields": { + "domain": "example.com", + "name": "example.com" + } + }, + { + "pk": 22, + "model": "auth.permission", + "fields": { + "codename": "add_logentry", + "name": "Can add log entry", + "content_type": 8 + } + }, + { + "pk": 23, + "model": "auth.permission", + "fields": { + "codename": "change_logentry", + "name": "Can change log entry", + "content_type": 8 + } + }, + { + "pk": 24, + "model": "auth.permission", + "fields": { + "codename": "delete_logentry", + "name": "Can delete log entry", + "content_type": 8 + } + }, + { + "pk": 4, + "model": "auth.permission", + "fields": { + "codename": "add_group", + "name": "Can add group", + "content_type": 2 + } + }, + { + "pk": 5, + "model": "auth.permission", + "fields": { + "codename": "change_group", + "name": "Can change group", + "content_type": 2 + } + }, + { + "pk": 6, + "model": "auth.permission", + "fields": { + "codename": "delete_group", + "name": "Can delete group", + "content_type": 2 + } + }, + { + "pk": 10, + "model": "auth.permission", + "fields": { + "codename": "add_message", + "name": "Can add message", + "content_type": 4 + } + }, + { + "pk": 11, + "model": "auth.permission", + "fields": { + "codename": "change_message", + "name": "Can change message", + "content_type": 4 + } + }, + { + "pk": 12, + "model": "auth.permission", + "fields": { + "codename": "delete_message", + "name": "Can delete message", + "content_type": 4 + } + }, + { + "pk": 1, + "model": "auth.permission", + "fields": { + "codename": "add_permission", + "name": "Can add permission", + "content_type": 1 + } + }, + { + "pk": 2, + "model": "auth.permission", + "fields": { + "codename": "change_permission", + "name": "Can change permission", + "content_type": 1 + } + }, + { + "pk": 3, + "model": "auth.permission", + "fields": { + "codename": "delete_permission", + "name": "Can delete permission", + "content_type": 1 + } + }, + { + "pk": 7, + "model": "auth.permission", + "fields": { + "codename": "add_user", + "name": "Can add user", + "content_type": 3 + } + }, + { + "pk": 8, + "model": "auth.permission", + "fields": { + "codename": "change_user", + "name": "Can change user", + "content_type": 3 + } + }, + { + "pk": 9, + "model": "auth.permission", + "fields": { + "codename": "delete_user", + "name": "Can delete user", + "content_type": 3 + } + }, + { + "pk": 13, + "model": "auth.permission", + "fields": { + "codename": "add_contenttype", + "name": "Can add content type", + "content_type": 5 + } + }, + { + "pk": 14, + "model": "auth.permission", + "fields": { + "codename": "change_contenttype", + "name": "Can change content type", + "content_type": 5 + } + }, + { + "pk": 15, + "model": "auth.permission", + "fields": { + "codename": "delete_contenttype", + "name": "Can delete content type", + "content_type": 5 + } + }, + { + "pk": 16, + "model": "auth.permission", + "fields": { + "codename": "add_session", + "name": "Can add session", + "content_type": 6 + } + }, + { + "pk": 17, + "model": "auth.permission", + "fields": { + "codename": "change_session", + "name": "Can change session", + "content_type": 6 + } + }, + { + "pk": 18, + "model": "auth.permission", + "fields": { + "codename": "delete_session", + "name": "Can delete session", + "content_type": 6 + } + }, + { + "pk": 19, + "model": "auth.permission", + "fields": { + "codename": "add_site", + "name": "Can add site", + "content_type": 7 + } + }, + { + "pk": 20, + "model": "auth.permission", + "fields": { + "codename": "change_site", + "name": "Can change site", + "content_type": 7 + } + }, + { + "pk": 21, + "model": "auth.permission", + "fields": { + "codename": "delete_site", + "name": "Can delete site", + "content_type": 7 + } + }, + { + "pk": 1, + "model": "auth.user", + "fields": { + "username": "admin", + "first_name": "", + "last_name": "", + "is_active": true, + "is_superuser": true, + "is_staff": true, + "last_login": "2010-11-19 06:26:59", + "groups": [], + "user_permissions": [], + "password": "sha1$e72a2$950781326ec9546d172a20681ba3c762f23104b0", + "email": "admin@example.com", + "date_joined": "2010-11-19 06:26:59" + } + } +] \ No newline at end of file diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py index ce93958f0f..46dbea83f3 100644 --- a/django/contrib/auth/management/__init__.py +++ b/django/contrib/auth/management/__init__.py @@ -23,14 +23,14 @@ def create_permissions(app, created_models, verbosity, **kwargs): # This will hold the permissions we're looking for as # (content_type, (codename, name)) - searched_perms = set() + searched_perms = list() # The codenames and ctypes that should exist. ctypes = set() for klass in app_models: ctype = ContentType.objects.get_for_model(klass) ctypes.add(ctype) for perm in _get_all_permissions(klass._meta): - searched_perms.add((ctype, perm)) + searched_perms.append((ctype, perm)) # Find all the Permissions that have a context_type for a model we're # looking for. We don't need to check for codenames since we already have diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index dfd59956c5..64d30372b1 100644 --- a/django/contrib/auth/tests/__init__.py +++ b/django/contrib/auth/tests/__init__.py @@ -9,5 +9,6 @@ from django.contrib.auth.tests.signals import SignalTestCase from django.contrib.auth.tests.tokens import TokenGeneratorTest from django.contrib.auth.tests.views import PasswordResetTest, \ ChangePasswordTest, LoginTest, LogoutTest, LoginURLSettings +from django.contrib.auth.tests.permissions import TestAuthPermissions # The password for the fixture data users is 'password' diff --git a/django/contrib/auth/tests/permissions.py b/django/contrib/auth/tests/permissions.py new file mode 100644 index 0000000000..1f84231c62 --- /dev/null +++ b/django/contrib/auth/tests/permissions.py @@ -0,0 +1,32 @@ +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +from django.contrib.auth.management import create_permissions +from django.contrib.auth import models as auth_models +from django.contrib.contenttypes import models as contenttypes_models +from django.core.management import call_command +from django.test import TestCase + + +class TestAuthPermissions(TestCase): + def test_permission_register_order(self): + """Test that the order of registered permissions doesn't break""" + # Changeset 14413 introduced a regression in the ordering of + # newly created permissions for objects. When loading a fixture + # after the initial creation (such as during unit tests), the + # expected IDs for the permissions may not match up, leading to + # SQL errors. This is ticket 14731 + + # Start with a clean slate and build the permissions as we + # expect to see them in the fixtures. + auth_models.Permission.objects.all().delete() + contenttypes_models.ContentType.objects.all().delete() + create_permissions(auth_models, [], verbosity=0) + create_permissions(contenttypes_models, [], verbosity=0) + + stderr = StringIO() + call_command('loaddata', 'test_permissions.json', + verbosity=0, commit=False, stderr=stderr) + self.assertEqual(stderr.getvalue(), '')