Browse Source

bump tests coverage for allow_move_post

Rafał Pitoń 8 years ago
parent
commit
013bddb18a
2 changed files with 18 additions and 28 deletions
  1. 15 27
      misago/threads/api/postendpoints/move.py
  2. 3 1
      misago/threads/permissions/threads.py

+ 15 - 27
misago/threads/api/postendpoints/move.py

@@ -1,23 +1,18 @@
 from django.conf import settings
 from django.core.exceptions import PermissionDenied
 from django.http import Http404
-from django.utils.translation import ugettext as _
-from django.utils.translation import ungettext
+from django.utils import six
+from django.utils.translation import ugettext as _, ungettext
 
 from rest_framework.response import Response
 
-from ...permissions.threads import exclude_invisible_posts
+from ...permissions.threads import allow_move_post, exclude_invisible_posts
 from ...utils import get_thread_id_from_url
 
 
 MOVE_LIMIT = settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL
 
 
-class MoveError(Exception):
-    def __init__(self, msg):
-        self.msg = msg
-
-
 def posts_move_endpoint(request, thread, viewmodel):
     if not thread.acl['can_move_posts']:
         raise PermissionDenied(_("You can't move posts in this thread."))
@@ -25,8 +20,8 @@ def posts_move_endpoint(request, thread, viewmodel):
     try:
         new_thread = clean_thread_for_move(request, thread, viewmodel)
         posts = clean_posts_for_move(request, thread)
-    except MoveError as e:
-        return Response({'detail': e.msg}, status=400)
+    except PermissionDenied as e:
+        return Response({'detail': six.text_type(e)}, status=400)
 
     for post in posts:
         post.move(new_thread)
@@ -51,19 +46,17 @@ def posts_move_endpoint(request, thread, viewmodel):
 def clean_thread_for_move(request, thread, viewmodel):
     new_thread_id = get_thread_id_from_url(request, request.data.get('thread_url', None))
     if not new_thread_id:
-        raise MoveError(_("This is not a valid thread link."))
+        raise PermissionDenied(_("This is not a valid thread link."))
     if new_thread_id == thread.pk:
-        raise MoveError(_("Thread to move posts to is same as current one."))
+        raise PermissionDenied(_("Thread to move posts to is same as current one."))
 
     try:
         new_thread = viewmodel(request, new_thread_id, select_for_update=True).unwrap()
-    except PermissionDenied as e:
-        raise MoveError(e.args[0])
     except Http404:
-        raise MoveError(_("The thread you have entered link to doesn't exist or you don't have permission to see it."))
+        raise PermissionDenied(_("The thread you have entered link to doesn't exist or you don't have permission to see it."))
 
     if not new_thread.acl['can_reply']:
-        raise MoveError(_("You can't move posts to threads you can't reply."))
+        raise PermissionDenied(_("You can't move posts to threads you can't reply."))
 
     return new_thread
 
@@ -72,32 +65,27 @@ def clean_posts_for_move(request, thread):
     try:
         posts_ids = list(map(int, request.data.get('posts', [])))
     except (ValueError, TypeError):
-        raise MoveError(_("One or more post ids received were invalid."))
+        raise PermissionDenied(_("One or more post ids received were invalid."))
 
     if not posts_ids:
-        raise MoveError(_("You have to specify at least one post to move."))
+        raise PermissionDenied(_("You have to specify at least one post to move."))
     elif len(posts_ids) > MOVE_LIMIT:
         message = ungettext(
             "No more than %(limit)s post can be moved at single time.",
             "No more than %(limit)s posts can be moved at single time.",
             MOVE_LIMIT)
-        raise MoveError(message % {'limit': MOVE_LIMIT})
+        raise PermissionDenied(message % {'limit': MOVE_LIMIT})
 
     posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)
     posts_queryset = posts_queryset.select_for_update().filter(id__in=posts_ids).order_by('id')
 
     posts = []
     for post in posts_queryset:
-        if post.is_event:
-            raise MoveError(_("Events can't be moved."))
-        if post.pk == thread.first_post_id:
-            raise MoveError(_("You can't move thread's first post."))
-        if post.is_hidden and not thread.category.acl['can_hide_posts']:
-            raise MoveError(_("You can't move posts the content you can't see."))
-
+        post.thread = thread
+        allow_move_post(request.user, post)
         posts.append(post)
 
     if len(posts) != len(posts_ids):
-        raise MoveError(_("One or more posts to move could not be found."))
+        raise PermissionDenied(_("One or more posts to move could not be found."))
 
     return posts

+ 3 - 1
misago/threads/permissions/threads.py

@@ -746,9 +746,11 @@ def allow_move_post(user, target):
 
     if not category_acl['can_move_posts']:
         raise PermissionDenied(_("You can't move posts in this category."))
+    if target.is_event:
+        raise PermissionDenied(_("Events can't be moved."))
     if target.is_first_post:
         raise PermissionDenied(_("You can't move thread's first post."))
-    if not target.is_first_post and not category_acl['can_hide_posts'] and target.is_hidden:
+    if not category_acl['can_hide_posts'] and target.is_hidden:
         raise PermissionDenied(_("You can't move posts the content you can't see."))
 can_move_post = return_boolean(allow_move_post)