Ticket #17097: 17097.patch

File 17097.patch, 6.3 KB (added by George Hickman <george@…>, 13 years ago)

Full patch

  • django/contrib/comments/admin.py

    diff --git a/django/contrib/comments/admin.py b/django/contrib/comments/admin.py
    index 4cb9066..bfff028 100644
    a b class CommentsAdmin(admin.ModelAdmin):  
    2727
    2828    def get_actions(self, request):
    2929        actions = super(CommentsAdmin, self).get_actions(request)
    30         # Only superusers should be able to delete the comments from the DB.
    31         if not request.user.is_superuser and 'delete_selected' in actions:
     30        if not self.has_delete_permission(request) and 'delete_selected' in actions:
    3231            actions.pop('delete_selected')
    3332        if not request.user.has_perm('comments.can_moderate'):
    3433            if 'approve_comments' in actions:
  • new file django/contrib/comments/tests.py

    diff --git a/django/contrib/comments/tests.py b/django/contrib/comments/tests.py
    new file mode 100644
    index 0000000..2352edc
    - +  
     1from django.contrib.admin.sites import AdminSite
     2from django.contrib.auth.models import Group, Permission, User
     3from django.contrib.comments.admin import CommentsAdmin
     4from django.contrib.comments.models import Comment
     5from django.http import HttpRequest
     6from django.test import TestCase
     7
     8class CommentsAdminActionListTest(TestCase):
     9    """
     10    Run through the actions list and check that a user with a given set
     11    of permissions is given the corrent list of actions based on their
     12    permissions.
     13
     14    Ignoring the flag permissions on purpose here since they are
     15    separate from the main comment positions.
     16    """
     17    def setUp(self):
     18        self.permissions = Permission.objects.filter(content_type__model='comment')
     19
     20        self.add_permission = self.permissions.get(codename='add_comment')
     21        self.change_permission = self.permissions.get(codename='change_comment')
     22        self.delete_permission = self.permissions.get(codename='delete_comment')
     23        self.moderate_permission = self.permissions.get(codename='can_moderate')
     24
     25        self.group = Group.objects.create(name='test')
     26        self.user = User.objects.create(username='test', password='test', is_staff=True)
     27        self.user.groups.add(self.group)
     28
     29        self.request = HttpRequest()
     30        self.request.user = self.user
     31        self.comments_admin = CommentsAdmin(Comment, AdminSite())
     32
     33    def tearDown(self):
     34        self.user.delete()
     35        self.group.delete()
     36
     37    def test_user_with_all_perms(self):
     38        for permission in self.permissions:
     39            self.group.permissions.add(permission)
     40
     41        self.assertTrue(self.user.has_perm('comments.add_comment'))
     42        self.assertTrue(self.user.has_perm('comments.can_moderate'))
     43        self.assertTrue(self.user.has_perm('comments.change_comment'))
     44        self.assertTrue(self.user.has_perm('comments.delete_comment'))
     45
     46        self.assertTrue('approve_comments' in self.comments_admin.get_actions(self.request))
     47        self.assertTrue('delete_selected' in self.comments_admin.get_actions(self.request))
     48        self.assertTrue('remove_comments' in self.comments_admin.get_actions(self.request))
     49
     50    def test_user_with_add_perms(self):
     51        self.group.permissions.add(self.add_permission)
     52
     53        self.assertTrue(self.user.has_perm('comments.add_comment'))
     54        self.assertFalse(self.user.has_perm('comments.can_moderate'))
     55        self.assertFalse(self.user.has_perm('comments.change_comment'))
     56        self.assertFalse(self.user.has_perm('comments.delete_comment'))
     57
     58        self.assertFalse('approve_comments' in self.comments_admin.get_actions(self.request))
     59        self.assertFalse('delete_selected' in self.comments_admin.get_actions(self.request))
     60        self.assertFalse('remove_comments' in self.comments_admin.get_actions(self.request))
     61
     62    def test_user_with_change_perms(self):
     63        self.group.permissions.add(self.change_permission)
     64
     65        self.assertFalse(self.user.has_perm('comments.add_comment'))
     66        self.assertFalse(self.user.has_perm('comments.can_moderate'))
     67        self.assertTrue(self.user.has_perm('comments.change_comment'))
     68        self.assertFalse(self.user.has_perm('comments.delete_comment'))
     69
     70        self.assertFalse('approve_comments' in self.comments_admin.get_actions(self.request))
     71        self.assertFalse('delete_selected' in self.comments_admin.get_actions(self.request))
     72        self.assertFalse('remove_comments' in self.comments_admin.get_actions(self.request))
     73
     74    def test_user_with_delete_perms(self):
     75        self.group.permissions.add(self.delete_permission)
     76
     77        self.assertFalse(self.user.has_perm('comments.add_comment'))
     78        self.assertFalse(self.user.has_perm('comments.can_moderate'))
     79        self.assertFalse(self.user.has_perm('comments.change_comment'))
     80        self.assertTrue(self.user.has_perm('comments.delete_comment'))
     81
     82        self.assertFalse('approve_comments' in self.comments_admin.get_actions(self.request))
     83        self.assertTrue('delete_selected' in self.comments_admin.get_actions(self.request))
     84        self.assertFalse('remove_comments' in self.comments_admin.get_actions(self.request))
     85
     86    def test_user_with_moderate_perms(self):
     87        self.group.permissions.add(self.moderate_permission)
     88
     89        self.assertFalse(self.user.has_perm('comments.add_comment'))
     90        self.assertTrue(self.user.has_perm('comments.can_moderate'))
     91        self.assertFalse(self.user.has_perm('comments.change_comment'))
     92        self.assertFalse(self.user.has_perm('comments.delete_comment'))
     93
     94        self.assertTrue('approve_comments' in self.comments_admin.get_actions(self.request))
     95        self.assertFalse('delete_selected' in self.comments_admin.get_actions(self.request))
     96        self.assertTrue('remove_comments' in self.comments_admin.get_actions(self.request))
     97
     98    def test_user_with_no_perms(self):
     99        self.assertFalse(self.user.has_perm('comments.add_comment'))
     100        self.assertFalse(self.user.has_perm('comments.can_moderate'))
     101        self.assertFalse(self.user.has_perm('comments.change_comment'))
     102        self.assertFalse(self.user.has_perm('comments.delete_comment'))
     103
     104        self.assertFalse('approve_comments' in self.comments_admin.get_actions(self.request))
     105        self.assertFalse('delete_selected' in self.comments_admin.get_actions(self.request))
     106        self.assertFalse('remove_comments' in self.comments_admin.get_actions(self.request))
     107
Back to Top