|
@@ -1,8 +1,9 @@
|
|
|
from django.urls import reverse
|
|
|
|
|
|
-from misago.acl.testutils import override_acl
|
|
|
+from misago.acl.test import patch_user_acl
|
|
|
from misago.threads import testutils
|
|
|
from misago.threads.models import Thread, ThreadParticipant
|
|
|
+from misago.threads.test import patch_private_threads_acl
|
|
|
|
|
|
from .test_privatethreads import PrivateThreadsTestCase
|
|
|
|
|
@@ -23,16 +24,16 @@ class PrivateThreadsListApiTests(PrivateThreadsTestCase):
|
|
|
"detail": "You have to sign in to use private threads."
|
|
|
})
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": False})
|
|
|
def test_no_permission(self):
|
|
|
"""api requires user to have permission to be able to access it"""
|
|
|
- override_acl(self.user, {'can_use_private_threads': 0})
|
|
|
-
|
|
|
response = self.client.get(self.api_link)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
self.assertEqual(response.json(), {
|
|
|
"detail": "You can't use private threads."
|
|
|
})
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": True})
|
|
|
def test_empty_list(self):
|
|
|
"""api has no showstoppers on returning empty list"""
|
|
|
response = self.client.get(self.api_link)
|
|
@@ -41,6 +42,7 @@ class PrivateThreadsListApiTests(PrivateThreadsTestCase):
|
|
|
response_json = response.json()
|
|
|
self.assertEqual(response_json['count'], 0)
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": True})
|
|
|
def test_thread_visibility(self):
|
|
|
"""only participated threads are returned by private threads api"""
|
|
|
visible = testutils.post_thread(category=self.category, poster=self.user)
|
|
@@ -62,15 +64,14 @@ class PrivateThreadsListApiTests(PrivateThreadsTestCase):
|
|
|
self.assertEqual(response_json['results'][0]['id'], visible.id)
|
|
|
|
|
|
# threads with reported posts will also show to moderators
|
|
|
- override_acl(self.user, {'can_moderate_private_threads': 1})
|
|
|
-
|
|
|
- response = self.client.get(self.api_link)
|
|
|
- self.assertEqual(response.status_code, 200)
|
|
|
+ with patch_user_acl({"can_moderate_private_threads": True}):
|
|
|
+ response = self.client.get(self.api_link)
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
- response_json = response.json()
|
|
|
- self.assertEqual(response_json['count'], 2)
|
|
|
- self.assertEqual(response_json['results'][0]['id'], reported.id)
|
|
|
- self.assertEqual(response_json['results'][1]['id'], visible.id)
|
|
|
+ response_json = response.json()
|
|
|
+ self.assertEqual(response_json['count'], 2)
|
|
|
+ self.assertEqual(response_json['results'][0]['id'], reported.id)
|
|
|
+ self.assertEqual(response_json['results'][1]['id'], visible.id)
|
|
|
|
|
|
|
|
|
class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
|
|
@@ -90,28 +91,34 @@ class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
|
|
|
"detail": "You have to sign in to use private threads."
|
|
|
})
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": False})
|
|
|
def test_no_permission(self):
|
|
|
"""user needs to have permission to see private thread"""
|
|
|
- override_acl(self.user, {'can_use_private_threads': 0})
|
|
|
-
|
|
|
response = self.client.get(self.api_link)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
self.assertEqual(response.json(), {
|
|
|
"detail": "You can't use private threads."
|
|
|
})
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": True})
|
|
|
def test_no_participant(self):
|
|
|
"""user cant see thread he isn't part of"""
|
|
|
response = self.client.get(self.api_link)
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
+ @patch_user_acl({
|
|
|
+ "can_use_private_threads": True,
|
|
|
+ "can_moderate_private_threads": True,
|
|
|
+ })
|
|
|
def test_mod_not_reported(self):
|
|
|
"""moderator can't see private thread that has no reports"""
|
|
|
- override_acl(self.user, {'can_moderate_private_threads': 1})
|
|
|
-
|
|
|
response = self.client.get(self.api_link)
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
+ @patch_user_acl({
|
|
|
+ "can_use_private_threads": True,
|
|
|
+ "can_moderate_private_threads": False,
|
|
|
+ })
|
|
|
def test_reported_not_mod(self):
|
|
|
"""non-mod can't see private thread that has reported posts"""
|
|
|
self.thread.has_reported_posts = True
|
|
@@ -120,6 +127,7 @@ class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
|
|
|
response = self.client.get(self.api_link)
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": True})
|
|
|
def test_can_see_owner(self):
|
|
|
"""user can see thread he is owner of"""
|
|
|
ThreadParticipant.objects.set_owner(self.thread, self.user)
|
|
@@ -141,6 +149,7 @@ class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
|
|
|
]
|
|
|
)
|
|
|
|
|
|
+ @patch_user_acl({"can_use_private_threads": True})
|
|
|
def test_can_see_participant(self):
|
|
|
"""user can see thread he is participant of"""
|
|
|
ThreadParticipant.objects.add_participants(self.thread, [self.user])
|
|
@@ -162,10 +171,12 @@ class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
|
|
|
]
|
|
|
)
|
|
|
|
|
|
+ @patch_user_acl({
|
|
|
+ "can_use_private_threads": True,
|
|
|
+ "can_moderate_private_threads": True,
|
|
|
+ })
|
|
|
def test_mod_can_see_reported(self):
|
|
|
"""moderator can see private thread that has reports"""
|
|
|
- override_acl(self.user, {'can_moderate_private_threads': 1})
|
|
|
-
|
|
|
self.thread.has_reported_posts = True
|
|
|
self.thread.save()
|
|
|
|
|
@@ -186,30 +197,29 @@ class PrivateThreadDeleteApiTests(PrivateThreadsTestCase):
|
|
|
|
|
|
ThreadParticipant.objects.add_participants(self.thread, [self.user])
|
|
|
|
|
|
- def test_delete_thread_no_permission(self):
|
|
|
+ @patch_private_threads_acl({"can_hide_threads": 0})
|
|
|
+ def test_hide_thread_no_permission(self):
|
|
|
"""api tests permission to delete threads"""
|
|
|
- self.override_acl({'can_hide_threads': 0})
|
|
|
-
|
|
|
+
|
|
|
response = self.client.delete(self.api_link)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
-
|
|
|
self.assertEqual(
|
|
|
response.json()['detail'], "You can't delete threads in this category."
|
|
|
)
|
|
|
|
|
|
- self.override_acl({'can_hide_threads': 1})
|
|
|
|
|
|
+ @patch_private_threads_acl({"can_hide_threads": 1})
|
|
|
+ def test_delete_thread_no_permission(self):
|
|
|
+ """api tests permission to delete threads"""
|
|
|
response = self.client.delete(self.api_link)
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
-
|
|
|
self.assertEqual(
|
|
|
response.json()['detail'], "You can't delete threads in this category."
|
|
|
)
|
|
|
|
|
|
+ @patch_private_threads_acl({"can_hide_threads": 2})
|
|
|
def test_delete_thread(self):
|
|
|
"""DELETE to API link with permission deletes thread"""
|
|
|
- self.override_acl({'can_hide_threads': 2})
|
|
|
-
|
|
|
response = self.client.delete(self.api_link)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|