Browse Source

approve thread moderation action, made threads/read endpoint tests run

Rafał Pitoń 9 years ago
parent
commit
87a8611d5e

+ 17 - 0
misago/threads/api/threadendpoints/patch.py

@@ -106,6 +106,23 @@ def patch_is_closed(request, thread, value):
 thread_patch_endpoint.replace('is-closed', patch_is_closed)
 
 
+def patch_is_unapproved(request, thread, value):
+    if thread.acl.get('can_approve'):
+        if value:
+            raise PermissionDenied(_("Content approval can't be reversed."))
+
+        moderation.approve_thread(request.user, thread)
+
+        return {
+            'is_unapproved': thread.is_unapproved,
+            'has_unapproved_posts': thread.has_unapproved_posts,
+        }
+    else:
+        raise PermissionDenied(
+            _("You don't have permission to approve this thread."))
+thread_patch_endpoint.replace('is-unapproved', patch_is_unapproved)
+
+
 def patch_is_hidden(request, thread, value):
     if thread.acl.get('can_hide'):
         if value:

+ 6 - 4
misago/threads/serializers/thread.py

@@ -31,9 +31,10 @@ class ThreadSerializer(serializers.ModelSerializer):
             'weight',
             'category',
             'replies',
-            'is_closed',
-            'is_hidden',
             'is_read',
+            'is_unapproved',
+            'is_hidden',
+            'is_closed',
             'absolute_url',
             'last_poster_url',
             'last_post_url',
@@ -102,9 +103,10 @@ class ThreadListSerializer(ThreadSerializer):
             'last_poster_name',
             'last_poster_url',
             'last_post_on',
-            'is_closed',
-            'is_hidden',
             'is_read',
+            'is_unapproved',
+            'is_hidden',
+            'is_closed',
             'absolute_url',
             'last_post_url',
             'new_post_url',

+ 3 - 3
misago/threads/tests/test_thread_api.py

@@ -77,17 +77,17 @@ class ThreadDeleteApiTests(ThreadApiTestCase):
 
 class ThreadsReadApiTests(ThreadApiTestCase):
     def setUp(self):
-        super(ThreadSubscribeApiTests, self).setUp()
+        super(ThreadsReadApiTests, self).setUp()
         self.api_link = '/api/threads/read/'
 
-    def read_all_threads(self):
+    def test_read_all_threads(self):
         """api sets all threads as read"""
         response = self.client.post(self.api_link)
         self.assertEqual(response.status_code, 200)
 
         self.assertEqual(self.user.categoryread_set.count(), 2)
 
-    def read_threads_in_category(self):
+    def test_read_threads_in_category(self):
         """api sets threads in category as read"""
         response = self.client.post(
             '%s?category=%s' % (self.api_link, self.category.pk))

+ 36 - 0
misago/threads/tests/test_thread_patchapi.py → misago/threads/tests/test_thread_patch_api.py

@@ -406,6 +406,42 @@ class ThreadCloseApiTests(ThreadApiTestCase):
         self.assertTrue(thread_json['is_closed'])
 
 
+class ThreadApproveApiTests(ThreadApiTestCase):
+    def test_approve_thread(self):
+        """api makes it possible to approve thread"""
+        self.thread.is_unapproved = True
+        self.thread.save()
+
+        self.override_acl({
+            'can_approve_content': 1
+        })
+
+        response = self.client.patch(self.api_link, json.dumps([
+            {'op': 'replace', 'path': 'is-unapproved', 'value': False}
+        ]),
+        content_type="application/json")
+        self.assertEqual(response.status_code, 200)
+
+        thread_json = self.get_thread_json()
+        self.assertFalse(thread_json['is_unapproved'])
+
+    def test_unapprove_thread(self):
+        """api returns permission error on approval removal"""
+        self.override_acl({
+            'can_approve_content': 1
+        })
+
+        response = self.client.patch(self.api_link, json.dumps([
+            {'op': 'replace', 'path': 'is-unapproved', 'value': True}
+        ]),
+        content_type="application/json")
+        self.assertEqual(response.status_code, 400)
+
+        response_json = json.loads(response.content)
+        self.assertEqual(response_json['detail'][0],
+            "Content approval can't be reversed.")
+
+
 class ThreadHideApiTests(ThreadApiTestCase):
     def test_hide_thread(self):
         """api makes it possible to hide thread"""