Browse Source

fix tests suite

Rafał Pitoń 8 years ago
parent
commit
23026a0f25

+ 49 - 21
misago/threads/permissions/threads.py

@@ -470,18 +470,22 @@ def register_with(registry):
 ACL tests
 """
 def allow_see_thread(user, target):
-    category_acl = user.acl['categories'].get(target.category_id, {})
-    if not (category_acl.get('can_see') and category_acl.get('can_browse')):
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_see': False,
+        'can_browse': False
+    })
+
+    if not (category_acl['can_see'] and category_acl['can_browse']):
         raise Http404()
 
-    if target.is_hidden and (user.is_anonymous() or not category_acl.get('can_hide_threads')):
+    if target.is_hidden and (user.is_anonymous() or not category_acl['can_hide_threads']):
         raise Http404()
 
     if user.is_anonymous() or user.pk != target.starter_id:
-        if not category_acl.get('can_see_all_threads'):
+        if not category_acl['can_see_all_threads']:
             raise Http404()
 
-        if target.is_unapproved and not category_acl.get('can_approve_content'):
+        if target.is_unapproved and not category_acl['can_approve_content']:
             raise Http404()
 can_see_thread = return_boolean(allow_see_thread)
 
@@ -490,12 +494,15 @@ def allow_start_thread(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to start threads."))
 
-    category_acl = user.acl['categories'].get(target.pk, {})
+    category_acl = user.acl['categories'].get(target.pk, {
+        'can_close_threads': False,
+        'can_start_threads': False
+    })
 
-    if target.is_closed and not category_acl.get('can_close_threads', False):
+    if target.is_closed and not category_acl['can_close_threads']:
         raise PermissionDenied(_("This category is closed. You can't start new threads in it."))
 
-    if not category_acl.get('can_start_threads', False):
+    if not category_acl['can_start_threads']:
         raise PermissionDenied(_("You don't have permission to start new threads in this category."))
 can_start_thread = return_boolean(allow_start_thread)
 
@@ -504,15 +511,18 @@ def allow_reply_thread(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to reply threads."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_close_threads': False,
+        'can_reply_threads': False
+    })
 
-    if not category_acl.get('can_close_threads', False):
+    if not category_acl['can_close_threads']:
         if target.category.is_closed:
             raise PermissionDenied(_("This category is closed. You can't reply to threads in it."))
         if target.is_closed:
             raise PermissionDenied(_("You can't reply to closed threads in this category."))
 
-    if not category_acl.get('can_reply_threads', False):
+    if not category_acl['can_reply_threads']:
         raise PermissionDenied(_("You can't reply to threads in this category."))
 can_reply_thread = return_boolean(allow_reply_thread)
 
@@ -521,9 +531,11 @@ def allow_edit_thread(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to edit threads."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_edit_threads': False
+    })
 
-    if not category_acl.get('can_edit_threads', False):
+    if not category_acl['can_edit_threads']:
         raise PermissionDenied(_("You can't edit threads in this category."))
 
     if category_acl['can_edit_threads'] == 1:
@@ -552,7 +564,9 @@ def allow_edit_post(user, target):
     if target.is_event:
         raise PermissionDenied(_("Events can't be edited."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_edit_posts': False
+    })
 
     if not category_acl['can_edit_posts']:
         raise PermissionDenied(_("You can't edit posts in this category."))
@@ -586,7 +600,10 @@ def allow_unhide_post(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to reveal posts."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_hide_posts': 0,
+        'can_hide_own_posts': 0
+    })
 
     if not category_acl['can_hide_posts']:
         if not category_acl['can_hide_own_posts']:
@@ -620,7 +637,10 @@ def allow_hide_post(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to hide posts."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_hide_posts': 0,
+        'can_hide_own_posts': 0
+    })
 
     if not category_acl['can_hide_posts']:
         if not category_acl['can_hide_own_posts']:
@@ -654,7 +674,10 @@ def allow_delete_post(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to delete posts."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_hide_posts': 0,
+        'can_hide_own_posts': 0
+    })
 
     if category_acl['can_hide_posts'] != 2:
         if category_acl['can_hide_own_posts'] != 2:
@@ -688,7 +711,9 @@ def allow_protect_post(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to protect posts."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_protect_posts': False
+    })
 
     if not category_acl['can_protect_posts']:
         raise PermissionDenied(_("You can't protect posts in this category."))
@@ -701,7 +726,9 @@ def allow_approve_post(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to approve posts."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
+    category_acl = user.acl['categories'].get(target.category_id, {
+        'can_approve_content': False
+    })
 
     if not category_acl['can_approve_content']:
         raise PermissionDenied(_("You can't approve posts in this category."))
@@ -716,8 +743,9 @@ def allow_delete_event(user, target):
     if user.is_anonymous():
         raise PermissionDenied(_("You have to sign in to delete events."))
 
-    category_acl = user.acl['categories'].get(target.category_id, {})
-    if category_acl['can_hide_events'] != 2:
+    category_acl = user.acl['categories'].get(target.category_id)
+
+    if not category_acl or category_acl['can_hide_events'] != 2:
         raise PermissionDenied(_("You can't delete events in this category."))
 can_delete_event = return_boolean(allow_delete_event)
 

+ 8 - 5
misago/threads/tests/test_thread_merge_api.py

@@ -23,7 +23,8 @@ class ThreadMergeApiTests(ThreadsApiTestCase):
         self.api_link = reverse('misago:api:thread-merge', kwargs={'pk': self.thread.pk})
 
     def override_other_acl(self, acl=None):
-        final_acl = {
+        other_category_acl = self.user.acl['categories'][self.category.pk].copy()
+        other_category_acl.update({
             'can_see': 1,
             'can_browse': 1,
             'can_see_all_threads': 1,
@@ -34,14 +35,16 @@ class ThreadMergeApiTests(ThreadsApiTestCase):
             'can_hide_posts': 0,
             'can_hide_own_posts': 0,
             'can_merge_threads': 0
-        }
-        final_acl.update(acl or {})
+        })
+
+        if acl:
+            other_category_acl.update(acl)
 
         categories_acl = self.user.acl['categories']
-        categories_acl[self.category_b.pk] = final_acl
+        categories_acl[self.category_b.pk] = other_category_acl
 
         visible_categories = [self.category.pk]
-        if final_acl['can_see']:
+        if other_category_acl['can_see']:
             visible_categories.append(self.category_b.pk)
 
         override_acl(self.user, {

+ 6 - 5
misago/threads/tests/test_thread_patch_api.py

@@ -249,21 +249,22 @@ class ThreadMoveApiTests(ThreadPatchApiTestCase):
         self.category_b = Category.objects.get(slug='category-b')
 
     def override_other_acl(self, acl):
-        final_acl = {
+        other_category_acl = self.user.acl['categories'][self.category.pk].copy()
+        other_category_acl.update({
             'can_see': 1,
             'can_browse': 1,
             'can_see_all_threads': 1,
             'can_see_own_threads': 0,
             'can_hide_threads': 0,
             'can_approve_content': 0,
-        }
-        final_acl.update(acl)
+        })
+        other_category_acl.update(acl)
 
         categories_acl = self.user.acl['categories']
-        categories_acl[self.category_b.pk] = final_acl
+        categories_acl[self.category_b.pk] = other_category_acl
 
         visible_categories = [self.category.pk]
-        if final_acl['can_see']:
+        if other_category_acl['can_see']:
             visible_categories.append(self.category_b.pk)
 
         override_acl(self.user, {

+ 7 - 3
misago/threads/tests/test_threadslists.py

@@ -117,20 +117,24 @@ class ThreadsListTestCase(AuthenticatedUserTestCase):
             'can_approve_content': []
         }
 
+        # copy first category's acl to other categories to make base for overrides
+        for category in Category.objects.all_categories():
+            categories_acl['categories'][category.pk] = self.user.acl['categories'][self.first_category.pk].copy()
+
         if base_acl:
             categories_acl.update(base_acl)
 
         for category in Category.objects.all_categories():
             categories_acl['visible_categories'].append(category.pk)
             categories_acl['browseable_categories'].append(category.pk)
-            categories_acl['categories'][category.pk] = {
+            categories_acl['categories'][category.pk].update({
                 'can_see': 1,
                 'can_browse': 1,
                 'can_see_all_threads': 1,
                 'can_see_own_threads': 0,
                 'can_hide_threads': 0,
                 'can_approve_content': 0
-            }
+            })
 
             if category_acl:
                 categories_acl['categories'][category.pk].update(category_acl)
@@ -541,7 +545,7 @@ class ThreadsVisibilityTests(ThreadsListTestCase):
         self.assertContains(response,
             'subcategory-%s' % self.category_a.css_class)
         self.assertContains(response,
-            'subcategory-%s' % self.category_e.css_class)            
+            'subcategory-%s' % self.category_e.css_class)
         self.assertContains(response,
             'thread-category-%s' % self.category_a.css_class)
         self.assertContains(response,

+ 5 - 4
misago/threads/tests/test_threadview.py

@@ -22,7 +22,8 @@ class ThreadViewTestCase(AuthenticatedUserTestCase):
         self.thread = testutils.post_thread(category=self.category)
 
     def override_acl(self, acl=None):
-        final_acl = {
+        category_acl = self.user.acl['categories'][self.category.pk]
+        category_acl.update({
             'can_see': 1,
             'can_browse': 1,
             'can_see_all_threads': 1,
@@ -35,14 +36,14 @@ class ThreadViewTestCase(AuthenticatedUserTestCase):
             'can_close_threads': 0,
             'post_edit_time': 0,
             'can_hide_events': 0,
-        }
+        })
 
         if acl:
-            final_acl.update(acl)
+            category_acl.update(acl)
 
         override_acl(self.user, {
             'categories': {
-                self.category.pk: final_acl
+                self.category.pk: category_acl
             }
         })