Ticket #16862: django-16862.2.2.diff

File django-16862.2.2.diff, 5.9 KB (added by marw85, 13 years ago)

added tests, but change breaks other tests

  • django/contrib/admin/util.py

    diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
    index 61182a6..6e01bdc 100644
    a b def get_deleted_objects(objs, opts, user, admin_site, using):  
    116116                                None, (quote(obj._get_pk_val()),))
    117117            p = '%s.%s' % (opts.app_label,
    118118                           opts.get_delete_permission())
    119             if not user.has_perm(p):
     119            if not user.has_perm(p, obj):
    120120                perms_needed.add(opts.verbose_name)
    121121            # Display a link to the admin page.
    122122            return mark_safe(u'%s: <a href="%s">%s</a>' %
    def reverse_field_path(model, path):  
    361361    for piece in pieces:
    362362        field, model, direct, m2m = parent._meta.get_field_by_name(piece)
    363363        # skip trailing data field if extant:
    364         if len(reversed_path) == len(pieces)-1: # final iteration
     364        if len(reversed_path) == len(pieces) - 1: # final iteration
    365365            try:
    366366                get_model_from_relation(field)
    367367            except NotRelationField:
  • tests/regressiontests/admin_util/tests.py

    diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py
    index 8113f2e..3b3b707 100644
    a b from django.conf import settings  
    66from django.contrib import admin
    77from django.contrib.admin import helpers
    88from django.contrib.admin.util import (display_for_field, label_for_field,
    9     lookup_field, NestedObjects)
     9    lookup_field, NestedObjects, get_deleted_objects)
    1010from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
    1111from django.contrib.sites.models import Site
     12from django.contrib.auth.models import User
    1213from django.db import models, DEFAULT_DB_ALIAS
    1314from django import forms
    1415from django.test import TestCase
    15 from django.utils import unittest
    1616from django.utils.formats import localize
    1717from django.utils.safestring import mark_safe
    1818
    class NestedObjectsTests(TestCase):  
    7070        # Should not require additional queries to populate the nested graph.
    7171        self.assertNumQueries(2, self._collect, 0)
    7272
    73 class UtilTests(unittest.TestCase):
     73class UtilTests(TestCase):
     74    urls = 'regressiontests.admin_util.urls'
     75
    7476    def test_values_from_lookup_field(self):
    7577        """
    7678        Regression test for #12654: lookup_field
    class UtilTests(unittest.TestCase):  
    218220        )
    219221        self.assertEqual(
    220222            label_for_field("test_from_model", Article,
    221                 model_admin = MockModelAdmin,
    222                 return_attr = True
     223                model_admin=MockModelAdmin,
     224                return_attr=True
    223225            ),
    224226            ("not Really the Model", MockModelAdmin.test_from_model)
    225227        )
    class UtilTests(unittest.TestCase):  
    266268        # safestring should not be escaped
    267269        class MyForm(forms.Form):
    268270            text = forms.CharField(label=mark_safe('<i>text</i>'))
    269             cb   = forms.BooleanField(label=mark_safe('<i>cb</i>'))
     271            cb = forms.BooleanField(label=mark_safe('<i>cb</i>'))
    270272
    271273        form = MyForm()
    272274        self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
    class UtilTests(unittest.TestCase):  
    277279        # normal strings needs to be escaped
    278280        class MyForm(forms.Form):
    279281            text = forms.CharField(label='&text')
    280             cb   = forms.BooleanField(label='&cb')
     282            cb = forms.BooleanField(label='&cb')
    281283
    282284        form = MyForm()
    283285        self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
    284286                         '<label for="id_text" class="required inline">&amp;text:</label>')
    285287        self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
    286288                         '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb</label>')
     289
     290    def test_get_deleted_objects_has_perm(self):
     291
     292        class MockAdminSite:
     293            name = 'admin-ns'
     294            _registry = [User]
     295
     296        class MockPrivilegedUser(User):
     297            my_usernames = ['special']
     298            def has_perm(self, perm, obj=None):
     299                if obj and obj.username in self.my_usernames:
     300                    return True
     301                return False
     302
     303        privileged_user = MockPrivilegedUser()
     304
     305        # privileged_user don't have permission to modify all users,
     306        # but have permission to user with username 'test'
     307        # get_deleted_objects have to pass object for privileges check
     308        obj = User(username='special')
     309        obj.save()
     310        (deleted_objects, perms_needed, protected) = get_deleted_objects(
     311            [obj], None, privileged_user, MockAdminSite(), None)
     312        self.assertFalse(obj._meta.verbose_name in perms_needed)
     313
     314        obj = User(username='common')
     315        obj.save()
     316        (deleted_objects, perms_needed, protected) = get_deleted_objects(
     317            [obj], None, privileged_user, MockAdminSite(), None)
     318        self.assertTrue(obj._meta.verbose_name in perms_needed)
  • tests/regressiontests/admin_util/urls.py

    diff --git a/tests/regressiontests/admin_util/urls.py b/tests/regressiontests/admin_util/urls.py
    index e69de29..22f4717 100644
    a b  
     1from __future__ import absolute_import
     2
     3from django.conf.urls import patterns, url, include
     4
     5
     6class URLObject(object):
     7    def __init__(self, app_name, namespace):
     8        self.app_name = app_name
     9        self.namespace = namespace
     10
     11    def urls(self):
     12        return patterns('',
     13            url(r'^inner/(\d+)/$', 'empty_view', name='auth_user_change'),
     14        ), self.app_name, self.namespace
     15    urls = property(urls)
     16
     17testobj1 = URLObject('testapp', 'admin-ns')
     18
     19urlpatterns = patterns('regressiontests.admin_util.urls',
     20    (r'^test1/', include(testobj1.urls)),
     21)
Back to Top