| 1 | from django.contrib.admin.sites import AdminSite |
| 2 | from django.contrib.auth.models import Group, Permission, User |
| 3 | from django.contrib.comments.admin import CommentsAdmin |
| 4 | from django.contrib.comments.models import Comment |
| 5 | from django.http import HttpRequest |
| 6 | from django.test import TestCase |
| 7 | |
| 8 | class 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 | |