Browse Source

Update thread when best answer is protected or unprotected

Rafał Pitoń 7 years ago
parent
commit
fed5947620

+ 6 - 0
misago/threads/moderation/posts.py

@@ -28,6 +28,9 @@ def protect_post(user, post):
     if not post.is_protected:
         post.is_protected = True
         post.save(update_fields=['is_protected'])
+        if post.is_best_answer:
+            post.thread.best_answer_is_protected = True
+            post.thread.save(update_fields=['best_answer_is_protected'])
         return True
     else:
         return False
@@ -37,6 +40,9 @@ def unprotect_post(user, post):
     if post.is_protected:
         post.is_protected = False
         post.save(update_fields=['is_protected'])
+        if post.is_best_answer:
+            post.thread.best_answer_is_protected = False
+            post.thread.save(update_fields=['best_answer_is_protected'])
         return True
     else:
         return False

+ 65 - 1
misago/threads/tests/test_thread_postpatch_api.py

@@ -10,7 +10,7 @@ from django.utils import timezone
 from misago.acl.testutils import override_acl
 from misago.categories.models import Category
 from misago.threads import testutils
-from misago.threads.models import Post
+from misago.threads.models import Thread, Post
 from misago.users.testutils import AuthenticatedUserTestCase
 
 
@@ -36,6 +36,9 @@ class ThreadPostPatchApiTestCase(AuthenticatedUserTestCase):
     def refresh_post(self):
         self.post = self.thread.post_set.get(pk=self.post.pk)
 
+    def refresh_thread(self):
+        self.thread = Thread.objects.get(pk=self.thread.pk)
+
     def override_acl(self, extra_acl=None):
         new_acl = self.user.acl_cache
         new_acl['categories'][self.category.pk].update({
@@ -128,6 +131,67 @@ class PostProtectApiTests(ThreadPostPatchApiTestCase):
         self.refresh_post()
         self.assertFalse(self.post.is_protected)
 
+    def test_protect_best_answer(self):
+        """api makes it possible to protect post"""
+        self.thread.set_best_answer(self.user, self.post)
+        self.thread.save()
+
+        self.assertFalse(self.thread.best_answer_is_protected)
+        
+        self.override_acl({'can_protect_posts': 1})
+
+        response = self.patch(
+            self.api_link, [
+                {
+                    'op': 'replace',
+                    'path': 'is-protected',
+                    'value': True,
+                },
+            ]
+        )
+        self.assertEqual(response.status_code, 200)
+
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['is_protected'])
+
+        self.refresh_post()
+        self.assertTrue(self.post.is_protected)
+
+        self.refresh_thread()
+        self.assertTrue(self.thread.best_answer_is_protected)
+
+    def test_unprotect_best_answer(self):
+        """api makes it possible to unprotect protected post"""
+        self.post.is_protected = True
+        self.post.save()
+
+        self.thread.set_best_answer(self.user, self.post)
+        self.thread.save()
+
+        self.assertTrue(self.thread.best_answer_is_protected)
+
+        self.override_acl({'can_protect_posts': 1})
+
+        response = self.patch(
+            self.api_link, [
+                {
+                    'op': 'replace',
+                    'path': 'is-protected',
+                    'value': False,
+                },
+            ]
+        )
+        self.assertEqual(response.status_code, 200)
+
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_protected'])
+
+        self.refresh_post()
+        self.assertFalse(self.post.is_protected)
+
+        self.refresh_thread()
+        self.assertFalse(self.thread.best_answer_is_protected)
+
     def test_protect_post_no_permission(self):
         """api validates permission to protect post"""
         self.override_acl({'can_protect_posts': 0})