Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14731 -- Respect ordering when creating the default permission…

…s. Thanks, chipx86.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14891 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 674c671cae5119146a302411b3f13e706577f47f 1 parent 867e935
Jannis Leidel authored December 12, 2010
316  django/contrib/auth/fixtures/permissionstestdata.json
... ...
@@ -0,0 +1,316 @@
  1
+[
  2
+    {
  3
+        "pk": 5, 
  4
+        "model": "contenttypes.contenttype", 
  5
+        "fields": {
  6
+            "model": "contenttype", 
  7
+            "name": "content type", 
  8
+            "app_label": "contenttypes"
  9
+        }
  10
+    }, 
  11
+    {
  12
+        "pk": 2, 
  13
+        "model": "contenttypes.contenttype", 
  14
+        "fields": {
  15
+            "model": "group", 
  16
+            "name": "group", 
  17
+            "app_label": "auth"
  18
+        }
  19
+    }, 
  20
+    {
  21
+        "pk": 8, 
  22
+        "model": "contenttypes.contenttype", 
  23
+        "fields": {
  24
+            "model": "logentry", 
  25
+            "name": "log entry", 
  26
+            "app_label": "admin"
  27
+        }
  28
+    }, 
  29
+    {
  30
+        "pk": 4, 
  31
+        "model": "contenttypes.contenttype", 
  32
+        "fields": {
  33
+            "model": "message", 
  34
+            "name": "message", 
  35
+            "app_label": "auth"
  36
+        }
  37
+    }, 
  38
+    {
  39
+        "pk": 1, 
  40
+        "model": "contenttypes.contenttype", 
  41
+        "fields": {
  42
+            "model": "permission", 
  43
+            "name": "permission", 
  44
+            "app_label": "auth"
  45
+        }
  46
+    }, 
  47
+    {
  48
+        "pk": 6, 
  49
+        "model": "contenttypes.contenttype", 
  50
+        "fields": {
  51
+            "model": "session", 
  52
+            "name": "session", 
  53
+            "app_label": "sessions"
  54
+        }
  55
+    }, 
  56
+    {
  57
+        "pk": 7, 
  58
+        "model": "contenttypes.contenttype", 
  59
+        "fields": {
  60
+            "model": "site", 
  61
+            "name": "site", 
  62
+            "app_label": "sites"
  63
+        }
  64
+    }, 
  65
+    {
  66
+        "pk": 3, 
  67
+        "model": "contenttypes.contenttype", 
  68
+        "fields": {
  69
+            "model": "user", 
  70
+            "name": "user", 
  71
+            "app_label": "auth"
  72
+        }
  73
+    }, 
  74
+    {
  75
+        "pk": 1, 
  76
+        "model": "sites.site", 
  77
+        "fields": {
  78
+            "domain": "example.com", 
  79
+            "name": "example.com"
  80
+        }
  81
+    }, 
  82
+    {
  83
+        "pk": 22, 
  84
+        "model": "auth.permission", 
  85
+        "fields": {
  86
+            "codename": "add_logentry", 
  87
+            "name": "Can add log entry", 
  88
+            "content_type": 8
  89
+        }
  90
+    }, 
  91
+    {
  92
+        "pk": 23, 
  93
+        "model": "auth.permission", 
  94
+        "fields": {
  95
+            "codename": "change_logentry", 
  96
+            "name": "Can change log entry", 
  97
+            "content_type": 8
  98
+        }
  99
+    }, 
  100
+    {
  101
+        "pk": 24, 
  102
+        "model": "auth.permission", 
  103
+        "fields": {
  104
+            "codename": "delete_logentry", 
  105
+            "name": "Can delete log entry", 
  106
+            "content_type": 8
  107
+        }
  108
+    }, 
  109
+    {
  110
+        "pk": 4, 
  111
+        "model": "auth.permission", 
  112
+        "fields": {
  113
+            "codename": "add_group", 
  114
+            "name": "Can add group", 
  115
+            "content_type": 2
  116
+        }
  117
+    }, 
  118
+    {
  119
+        "pk": 5, 
  120
+        "model": "auth.permission", 
  121
+        "fields": {
  122
+            "codename": "change_group", 
  123
+            "name": "Can change group", 
  124
+            "content_type": 2
  125
+        }
  126
+    }, 
  127
+    {
  128
+        "pk": 6, 
  129
+        "model": "auth.permission", 
  130
+        "fields": {
  131
+            "codename": "delete_group", 
  132
+            "name": "Can delete group", 
  133
+            "content_type": 2
  134
+        }
  135
+    }, 
  136
+    {
  137
+        "pk": 10, 
  138
+        "model": "auth.permission", 
  139
+        "fields": {
  140
+            "codename": "add_message", 
  141
+            "name": "Can add message", 
  142
+            "content_type": 4
  143
+        }
  144
+    }, 
  145
+    {
  146
+        "pk": 11, 
  147
+        "model": "auth.permission", 
  148
+        "fields": {
  149
+            "codename": "change_message", 
  150
+            "name": "Can change message", 
  151
+            "content_type": 4
  152
+        }
  153
+    }, 
  154
+    {
  155
+        "pk": 12, 
  156
+        "model": "auth.permission", 
  157
+        "fields": {
  158
+            "codename": "delete_message", 
  159
+            "name": "Can delete message", 
  160
+            "content_type": 4
  161
+        }
  162
+    }, 
  163
+    {
  164
+        "pk": 1, 
  165
+        "model": "auth.permission", 
  166
+        "fields": {
  167
+            "codename": "add_permission", 
  168
+            "name": "Can add permission", 
  169
+            "content_type": 1
  170
+        }
  171
+    }, 
  172
+    {
  173
+        "pk": 2, 
  174
+        "model": "auth.permission", 
  175
+        "fields": {
  176
+            "codename": "change_permission", 
  177
+            "name": "Can change permission", 
  178
+            "content_type": 1
  179
+        }
  180
+    }, 
  181
+    {
  182
+        "pk": 3, 
  183
+        "model": "auth.permission", 
  184
+        "fields": {
  185
+            "codename": "delete_permission", 
  186
+            "name": "Can delete permission", 
  187
+            "content_type": 1
  188
+        }
  189
+    }, 
  190
+    {
  191
+        "pk": 7, 
  192
+        "model": "auth.permission", 
  193
+        "fields": {
  194
+            "codename": "add_user", 
  195
+            "name": "Can add user", 
  196
+            "content_type": 3
  197
+        }
  198
+    }, 
  199
+    {
  200
+        "pk": 8, 
  201
+        "model": "auth.permission", 
  202
+        "fields": {
  203
+            "codename": "change_user", 
  204
+            "name": "Can change user", 
  205
+            "content_type": 3
  206
+        }
  207
+    }, 
  208
+    {
  209
+        "pk": 9, 
  210
+        "model": "auth.permission", 
  211
+        "fields": {
  212
+            "codename": "delete_user", 
  213
+            "name": "Can delete user", 
  214
+            "content_type": 3
  215
+        }
  216
+    }, 
  217
+    {
  218
+        "pk": 13, 
  219
+        "model": "auth.permission", 
  220
+        "fields": {
  221
+            "codename": "add_contenttype", 
  222
+            "name": "Can add content type", 
  223
+            "content_type": 5
  224
+        }
  225
+    }, 
  226
+    {
  227
+        "pk": 14, 
  228
+        "model": "auth.permission", 
  229
+        "fields": {
  230
+            "codename": "change_contenttype", 
  231
+            "name": "Can change content type", 
  232
+            "content_type": 5
  233
+        }
  234
+    }, 
  235
+    {
  236
+        "pk": 15, 
  237
+        "model": "auth.permission", 
  238
+        "fields": {
  239
+            "codename": "delete_contenttype", 
  240
+            "name": "Can delete content type", 
  241
+            "content_type": 5
  242
+        }
  243
+    }, 
  244
+    {
  245
+        "pk": 16, 
  246
+        "model": "auth.permission", 
  247
+        "fields": {
  248
+            "codename": "add_session", 
  249
+            "name": "Can add session", 
  250
+            "content_type": 6
  251
+        }
  252
+    }, 
  253
+    {
  254
+        "pk": 17, 
  255
+        "model": "auth.permission", 
  256
+        "fields": {
  257
+            "codename": "change_session", 
  258
+            "name": "Can change session", 
  259
+            "content_type": 6
  260
+        }
  261
+    }, 
  262
+    {
  263
+        "pk": 18, 
  264
+        "model": "auth.permission", 
  265
+        "fields": {
  266
+            "codename": "delete_session", 
  267
+            "name": "Can delete session", 
  268
+            "content_type": 6
  269
+        }
  270
+    }, 
  271
+    {
  272
+        "pk": 19, 
  273
+        "model": "auth.permission", 
  274
+        "fields": {
  275
+            "codename": "add_site", 
  276
+            "name": "Can add site", 
  277
+            "content_type": 7
  278
+        }
  279
+    }, 
  280
+    {
  281
+        "pk": 20, 
  282
+        "model": "auth.permission", 
  283
+        "fields": {
  284
+            "codename": "change_site", 
  285
+            "name": "Can change site", 
  286
+            "content_type": 7
  287
+        }
  288
+    }, 
  289
+    {
  290
+        "pk": 21, 
  291
+        "model": "auth.permission", 
  292
+        "fields": {
  293
+            "codename": "delete_site", 
  294
+            "name": "Can delete site", 
  295
+            "content_type": 7
  296
+        }
  297
+    }, 
  298
+    {
  299
+        "pk": 1, 
  300
+        "model": "auth.user", 
  301
+        "fields": {
  302
+            "username": "admin", 
  303
+            "first_name": "", 
  304
+            "last_name": "", 
  305
+            "is_active": true, 
  306
+            "is_superuser": true, 
  307
+            "is_staff": true, 
  308
+            "last_login": "2010-11-19 06:26:59", 
  309
+            "groups": [], 
  310
+            "user_permissions": [], 
  311
+            "password": "sha1$e72a2$950781326ec9546d172a20681ba3c762f23104b0", 
  312
+            "email": "admin@example.com", 
  313
+            "date_joined": "2010-11-19 06:26:59"
  314
+        }
  315
+    }
  316
+]
4  django/contrib/auth/management/__init__.py
@@ -23,14 +23,14 @@ def create_permissions(app, created_models, verbosity, **kwargs):
23 23
 
24 24
     # This will hold the permissions we're looking for as
25 25
     # (content_type, (codename, name))
26  
-    searched_perms = set()
  26
+    searched_perms = list()
27 27
     # The codenames and ctypes that should exist.
28 28
     ctypes = set()
29 29
     for klass in app_models:
30 30
         ctype = ContentType.objects.get_for_model(klass)
31 31
         ctypes.add(ctype)
32 32
         for perm in _get_all_permissions(klass._meta):
33  
-            searched_perms.add((ctype, perm))
  33
+            searched_perms.append((ctype, perm))
34 34
 
35 35
     # Find all the Permissions that have a context_type for a model we're
36 36
     # looking for.  We don't need to check for codenames since we already have
1  django/contrib/auth/tests/__init__.py
@@ -9,5 +9,6 @@
9 9
 from django.contrib.auth.tests.tokens import TokenGeneratorTest
10 10
 from django.contrib.auth.tests.views import PasswordResetTest, \
11 11
     ChangePasswordTest, LoginTest, LogoutTest, LoginURLSettings
  12
+from django.contrib.auth.tests.permissions import TestAuthPermissions
12 13
 
13 14
 # The password for the fixture data users is 'password'
32  django/contrib/auth/tests/permissions.py
... ...
@@ -0,0 +1,32 @@
  1
+try:
  2
+    from cStringIO import StringIO
  3
+except ImportError:
  4
+    from StringIO import StringIO
  5
+
  6
+from django.contrib.auth.management import create_permissions
  7
+from django.contrib.auth import models as auth_models
  8
+from django.contrib.contenttypes import models as contenttypes_models
  9
+from django.core.management import call_command
  10
+from django.test import TestCase
  11
+
  12
+
  13
+class TestAuthPermissions(TestCase):
  14
+    def test_permission_register_order(self):
  15
+        """Test that the order of registered permissions doesn't break"""
  16
+        # Changeset 14413 introduced a regression in the ordering of
  17
+        # newly created permissions for objects. When loading a fixture
  18
+        # after the initial creation (such as during unit tests), the
  19
+        # expected IDs for the permissions may not match up, leading to
  20
+        # SQL errors. This is ticket 14731
  21
+
  22
+        # Start with a clean slate and build the permissions as we
  23
+        # expect to see them in the fixtures.
  24
+        auth_models.Permission.objects.all().delete()
  25
+        contenttypes_models.ContentType.objects.all().delete()
  26
+        create_permissions(auth_models, [], verbosity=0)
  27
+        create_permissions(contenttypes_models, [], verbosity=0)
  28
+
  29
+        stderr = StringIO()
  30
+        call_command('loaddata', 'test_permissions.json',
  31
+                     verbosity=0, commit=False, stderr=stderr)
  32
+        self.assertEqual(stderr.getvalue(), '')

0 notes on commit 674c671

Please sign in to comment.
Something went wrong with that request. Please try again.