123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- import json
- from django.urls import reverse
- from misago.categories.models import Category
- from misago.readtracker import poststracker
- from misago.threads import testutils
- from misago.threads.models import Thread
- from misago.threads.serializers.moderation import POSTS_LIMIT
- from misago.threads.test import patch_category_acl, patch_other_category_acl
- from misago.users.testutils import AuthenticatedUserTestCase
- class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
- def setUp(self):
- super().setUp()
- self.category = Category.objects.get(slug="first-category")
- self.thread = testutils.post_thread(category=self.category)
- self.api_link = reverse(
- "misago:api:thread-post-move", kwargs={"thread_pk": self.thread.pk}
- )
- Category(name="Other category", slug="other-category").insert_at(
- self.category, position="last-child", save=True
- )
- self.other_category = Category.objects.get(slug="other-category")
- def test_anonymous_user(self):
- """you need to authenticate to move posts"""
- self.logout_user()
- response = self.client.post(
- self.api_link, json.dumps({}), content_type="application/json"
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(
- response.json(), {"detail": "This action is not available to guests."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_invalid_data(self):
- """api handles post that is invalid type"""
- response = self.client.post(
- self.api_link, "[]", content_type="application/json"
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "Invalid data. Expected a dictionary, but got list."},
- )
- response = self.client.post(
- self.api_link, "123", content_type="application/json"
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "Invalid data. Expected a dictionary, but got int."},
- )
- response = self.client.post(
- self.api_link, '"string"', content_type="application/json"
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "Invalid data. Expected a dictionary, but got str."},
- )
- response = self.client.post(
- self.api_link, "malformed", content_type="application/json"
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)"},
- )
- @patch_category_acl({"can_move_posts": False})
- def test_no_permission(self):
- """api validates permission to move"""
- response = self.client.post(
- self.api_link, json.dumps({}), content_type="application/json"
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(
- response.json(), {"detail": "You can't move posts in this thread."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_no_new_thread_url(self):
- """api validates if new thread url was given"""
- response = self.client.post(self.api_link)
- self.assertEqual(response.status_code, 400)
- self.assertEqual(response.json(), {"detail": "Enter link to new thread."})
- @patch_category_acl({"can_move_posts": True})
- def test_invalid_new_thread_url(self):
- """api validates new thread url"""
- response = self.client.post(
- self.api_link, {"new_thread": self.user.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": "This is not a valid thread link."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_current_new_thread_url(self):
- """api validates if new thread url points to current thread"""
- response = self.client.post(
- self.api_link, {"new_thread": self.thread.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "Thread to move posts to is same as current one."},
- )
- @patch_other_category_acl({"can_see": False})
- @patch_category_acl({"can_move_posts": True})
- def test_other_thread_exists(self):
- """api validates if other thread exists"""
- other_thread = testutils.post_thread(self.other_category)
- response = self.client.post(
- self.api_link, {"new_thread": other_thread.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {
- "detail": (
- "The thread you have entered link to doesn't exist "
- "or you don't have permission to see it."
- )
- },
- )
- @patch_other_category_acl({"can_browse": False})
- @patch_category_acl({"can_move_posts": True})
- def test_other_thread_is_invisible(self):
- """api validates if other thread is visible"""
- other_thread = testutils.post_thread(self.other_category)
- response = self.client.post(
- self.api_link, {"new_thread": other_thread.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {
- "detail": (
- "The thread you have entered link to doesn't exist "
- "or you don't have permission to see it."
- )
- },
- )
- @patch_other_category_acl({"can_reply_threads": False})
- @patch_category_acl({"can_move_posts": True})
- def test_other_thread_isnt_replyable(self):
- """api validates if other thread can be replied"""
- other_thread = testutils.post_thread(self.other_category)
- response = self.client.post(
- self.api_link, {"new_thread": other_thread.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "You can't move posts to threads you can't reply."},
- )
- @patch_category_acl({"can_move_posts": True})
- def test_empty_data(self):
- """api handles empty data"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(self.api_link)
- self.assertEqual(response.status_code, 400)
- self.assertEqual(response.json(), {"detail": "Enter link to new thread."})
- @patch_category_acl({"can_move_posts": True})
- def test_empty_posts_data_json(self):
- """api handles empty json data"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps({"new_thread": other_thread.get_absolute_url()}),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "You have to specify at least one post to move."},
- )
- @patch_category_acl({"can_move_posts": True})
- def test_empty_posts_data_form(self):
- """api handles empty form data"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link, {"new_thread": other_thread.get_absolute_url()}
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "You have to specify at least one post to move."},
- )
- @patch_category_acl({"can_move_posts": True})
- def test_no_posts_ids(self):
- """api rejects no posts ids"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps({"new_thread": other_thread.get_absolute_url(), "posts": []}),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "You have to specify at least one post to move."},
- )
- @patch_category_acl({"can_move_posts": True})
- def test_invalid_posts_data(self):
- """api handles invalid data"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {"new_thread": other_thread.get_absolute_url(), "posts": "string"}
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": 'Expected a list of items but got type "str".'}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_invalid_posts_ids(self):
- """api handles invalid post id"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [1, 2, "string"],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": "One or more post ids received were invalid."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_limit(self):
- """api rejects more posts than move limit"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": list(range(POSTS_LIMIT + 1)),
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {
- "detail": "No more than %s posts can be moved at single time."
- % POSTS_LIMIT
- },
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_invisible(self):
- """api validates posts visibility"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [
- testutils.reply_thread(self.thread, is_unapproved=True).pk
- ],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": "One or more posts to move could not be found."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_other_thread_posts(self):
- """api recjects attempt to move other thread's post"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [testutils.reply_thread(other_thread, is_hidden=True).pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": "One or more posts to move could not be found."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_event(self):
- """api rejects events move"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [testutils.reply_thread(self.thread, is_event=True).pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(response.json(), {"detail": "Events can't be moved."})
- @patch_category_acl({"can_move_posts": True})
- def test_move_first_post(self):
- """api rejects first post move"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [self.thread.first_post_id],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(), {"detail": "You can't move thread's first post."}
- )
- @patch_category_acl({"can_move_posts": True})
- def test_move_hidden_posts(self):
- """api recjects attempt to move urneadable hidden post"""
- other_thread = testutils.post_thread(self.category)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [testutils.reply_thread(self.thread, is_hidden=True).pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "You can't move posts the content you can't see."},
- )
- @patch_category_acl({"can_move_posts": True, "can_close_threads": False})
- def test_move_posts_closed_thread_no_permission(self):
- """api recjects attempt to move posts from closed thread"""
- other_thread = testutils.post_thread(self.category)
- self.thread.is_closed = True
- self.thread.save()
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [testutils.reply_thread(self.thread).pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "This thread is closed. You can't move posts in it."},
- )
- @patch_other_category_acl({"can_reply_threads": True, "can_close_threads": False})
- @patch_category_acl({"can_move_posts": True})
- def test_move_posts_closed_category_no_permission(self):
- """api recjects attempt to move posts from closed thread"""
- other_thread = testutils.post_thread(self.other_category)
- self.category.is_closed = True
- self.category.save()
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [testutils.reply_thread(self.thread).pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"detail": "This category is closed. You can't move posts in it."},
- )
- @patch_other_category_acl({"can_reply_threads": True})
- @patch_category_acl({"can_move_posts": True})
- def test_move_posts(self):
- """api moves posts to other thread"""
- other_thread = testutils.post_thread(self.other_category)
- posts = (
- testutils.reply_thread(self.thread).pk,
- testutils.reply_thread(self.thread).pk,
- testutils.reply_thread(self.thread).pk,
- testutils.reply_thread(self.thread).pk,
- )
- self.thread.refresh_from_db()
- self.assertEqual(self.thread.replies, 4)
- response = self.client.post(
- self.api_link,
- json.dumps({"new_thread": other_thread.get_absolute_url(), "posts": posts}),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 200)
- # replies were moved
- self.thread.refresh_from_db()
- self.assertEqual(self.thread.replies, 0)
- other_thread = Thread.objects.get(pk=other_thread.pk)
- self.assertEqual(other_thread.post_set.filter(pk__in=posts).count(), 4)
- self.assertEqual(other_thread.replies, 4)
- @patch_other_category_acl({"can_reply_threads": True})
- @patch_category_acl({"can_move_posts": True})
- def test_move_best_answer(self):
- """api moves best answer to other thread"""
- other_thread = testutils.post_thread(self.other_category)
- best_answer = testutils.reply_thread(self.thread)
- self.thread.set_best_answer(self.user, best_answer)
- self.thread.synchronize()
- self.thread.save()
- self.thread.refresh_from_db()
- self.assertEqual(self.thread.best_answer, best_answer)
- self.assertEqual(self.thread.replies, 1)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [best_answer.pk],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 200)
- # best_answer was moved and unmarked
- self.thread.refresh_from_db()
- self.assertEqual(self.thread.replies, 0)
- self.assertIsNone(self.thread.best_answer)
- other_thread = Thread.objects.get(pk=other_thread.pk)
- self.assertEqual(other_thread.replies, 1)
- self.assertIsNone(other_thread.best_answer)
- @patch_other_category_acl({"can_reply_threads": True})
- @patch_category_acl({"can_move_posts": True})
- def test_move_posts_reads(self):
- """api moves posts reads together with posts"""
- other_thread = testutils.post_thread(self.other_category)
- posts = (
- testutils.reply_thread(self.thread),
- testutils.reply_thread(self.thread),
- )
- self.thread.refresh_from_db()
- self.assertEqual(self.thread.replies, 2)
- poststracker.save_read(self.user, self.thread.first_post)
- for post in posts:
- poststracker.save_read(self.user, post)
- response = self.client.post(
- self.api_link,
- json.dumps(
- {
- "new_thread": other_thread.get_absolute_url(),
- "posts": [p.pk for p in posts],
- }
- ),
- content_type="application/json",
- )
- self.assertEqual(response.status_code, 200)
- other_thread = Thread.objects.get(pk=other_thread.pk)
- # postreads were removed
- postreads = self.user.postread_set.order_by("id")
- postreads_threads = list(postreads.values_list("thread_id", flat=True))
- self.assertEqual(postreads_threads, [self.thread.pk])
- postreads_categories = list(postreads.values_list("category_id", flat=True))
- self.assertEqual(postreads_categories, [self.category.pk])
|