test_thread_model.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from misago.forums.models import Forum
  6. from misago.threads.models import Thread, Post
  7. class ThreadModelTests(TestCase):
  8. def setUp(self):
  9. datetime = timezone.now()
  10. self.forum = Forum.objects.filter(role="forum")[:1][0]
  11. self.thread = Thread(
  12. forum=self.forum,
  13. weight=0,
  14. started_on=datetime,
  15. starter_name='Tester',
  16. starter_slug='tester',
  17. last_post_on=datetime,
  18. last_poster_name='Tester',
  19. last_poster_slug='tester')
  20. self.thread.set_title("Test thread")
  21. self.thread.save()
  22. post = Post.objects.create(
  23. forum=self.forum,
  24. thread=self.thread,
  25. poster_name='Tester',
  26. poster_ip='127.0.0.1',
  27. original="Hello! I am test message!",
  28. parsed="<p>Hello! I am test message!</p>",
  29. checksum="nope",
  30. posted_on=datetime,
  31. updated_on=datetime)
  32. self.thread.first_post = post
  33. self.thread.last_post = post
  34. self.thread.save()
  35. def test_synchronize(self):
  36. """synchronize method updates thread data to reflect its contents"""
  37. User = get_user_model()
  38. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  39. self.assertEqual(self.thread.replies, 0)
  40. datetime = timezone.now() + timedelta(5)
  41. post = Post.objects.create(
  42. forum=self.forum,
  43. thread=self.thread,
  44. poster=user,
  45. poster_name=user.username,
  46. poster_ip='127.0.0.1',
  47. original="Hello! I am test message!",
  48. parsed="<p>Hello! I am test message!</p>",
  49. checksum="nope",
  50. posted_on=datetime,
  51. updated_on=datetime)
  52. # first sync call, updates last thread
  53. self.thread.synchronize()
  54. self.assertEqual(self.thread.last_post, post)
  55. self.assertEqual(self.thread.last_post_on, post.posted_on)
  56. self.assertEqual(self.thread.last_poster, user)
  57. self.assertEqual(self.thread.last_poster_name, user.username)
  58. self.assertEqual(self.thread.last_poster_slug, user.slug)
  59. self.assertFalse(self.thread.has_reported_posts)
  60. self.assertFalse(self.thread.has_moderated_posts)
  61. self.assertFalse(self.thread.has_hidden_posts)
  62. self.assertEqual(self.thread.replies, 1)
  63. # add moderated post
  64. moderated_post = Post.objects.create(
  65. forum=self.forum,
  66. thread=self.thread,
  67. poster=user,
  68. poster_name=user.username,
  69. poster_ip='127.0.0.1',
  70. original="Hello! I am test message!",
  71. parsed="<p>Hello! I am test message!</p>",
  72. checksum="nope",
  73. posted_on=datetime + timedelta(5),
  74. updated_on=datetime + timedelta(5),
  75. is_moderated=True)
  76. self.thread.synchronize()
  77. self.assertEqual(self.thread.last_post, post)
  78. self.assertEqual(self.thread.last_post_on, post.posted_on)
  79. self.assertEqual(self.thread.last_poster, user)
  80. self.assertEqual(self.thread.last_poster_name, user.username)
  81. self.assertEqual(self.thread.last_poster_slug, user.slug)
  82. self.assertFalse(self.thread.has_reported_posts)
  83. self.assertTrue(self.thread.has_moderated_posts)
  84. self.assertFalse(self.thread.has_hidden_posts)
  85. self.assertEqual(self.thread.replies, 1)
  86. # add hidden post
  87. hidden_post = Post.objects.create(
  88. forum=self.forum,
  89. thread=self.thread,
  90. poster=user,
  91. poster_name=user.username,
  92. poster_ip='127.0.0.1',
  93. original="Hello! I am test message!",
  94. parsed="<p>Hello! I am test message!</p>",
  95. checksum="nope",
  96. posted_on=datetime + timedelta(10),
  97. updated_on=datetime + timedelta(10),
  98. is_hidden=True)
  99. self.thread.synchronize()
  100. self.assertEqual(self.thread.last_post, post)
  101. self.assertEqual(self.thread.last_post_on, post.posted_on)
  102. self.assertEqual(self.thread.last_poster, user)
  103. self.assertEqual(self.thread.last_poster_name, user.username)
  104. self.assertEqual(self.thread.last_poster_slug, user.slug)
  105. self.assertFalse(self.thread.has_reported_posts)
  106. self.assertTrue(self.thread.has_moderated_posts)
  107. self.assertTrue(self.thread.has_hidden_posts)
  108. self.assertEqual(self.thread.replies, 1)
  109. # unhide post
  110. hidden_post.is_hidden = False
  111. hidden_post.save()
  112. # last post changed to unhidden one
  113. self.thread.synchronize()
  114. self.assertEqual(self.thread.last_post, hidden_post)
  115. self.assertEqual(self.thread.last_post_on, hidden_post.posted_on)
  116. self.assertEqual(self.thread.last_poster, user)
  117. self.assertEqual(self.thread.last_poster_name, user.username)
  118. self.assertEqual(self.thread.last_poster_slug, user.slug)
  119. self.assertFalse(self.thread.has_reported_posts)
  120. self.assertTrue(self.thread.has_moderated_posts)
  121. self.assertFalse(self.thread.has_hidden_posts)
  122. self.assertEqual(self.thread.replies, 2)
  123. # unmoderate post
  124. moderated_post.is_moderated = False
  125. moderated_post.save()
  126. # last post not changed, but flags and count did
  127. self.thread.synchronize()
  128. self.assertEqual(self.thread.last_post, hidden_post)
  129. self.assertEqual(self.thread.last_post_on, hidden_post.posted_on)
  130. self.assertEqual(self.thread.last_poster, user)
  131. self.assertEqual(self.thread.last_poster_name, user.username)
  132. self.assertEqual(self.thread.last_poster_slug, user.slug)
  133. self.assertFalse(self.thread.has_reported_posts)
  134. self.assertFalse(self.thread.has_moderated_posts)
  135. self.assertFalse(self.thread.has_hidden_posts)
  136. self.assertEqual(self.thread.replies, 3)
  137. def test_set_first_post(self):
  138. """set_first_post sets first post and poster data on thread"""
  139. User = get_user_model()
  140. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  141. datetime = timezone.now() + timedelta(5)
  142. post = Post.objects.create(
  143. forum=self.forum,
  144. thread=self.thread,
  145. poster=user,
  146. poster_name=user.username,
  147. poster_ip='127.0.0.1',
  148. original="Hello! I am test message!",
  149. parsed="<p>Hello! I am test message!</p>",
  150. checksum="nope",
  151. posted_on=datetime,
  152. updated_on=datetime)
  153. self.thread.set_first_post(post)
  154. self.assertEqual(self.thread.first_post, post)
  155. self.assertEqual(self.thread.started_on, post.posted_on)
  156. self.assertEqual(self.thread.starter, user)
  157. self.assertEqual(self.thread.starter_name, user.username)
  158. self.assertEqual(self.thread.starter_slug, user.slug)
  159. def test_set_last_post(self):
  160. """set_last_post sets first post and poster data on thread"""
  161. User = get_user_model()
  162. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  163. datetime = timezone.now() + timedelta(5)
  164. post = Post.objects.create(
  165. forum=self.forum,
  166. thread=self.thread,
  167. poster=user,
  168. poster_name=user.username,
  169. poster_ip='127.0.0.1',
  170. original="Hello! I am test message!",
  171. parsed="<p>Hello! I am test message!</p>",
  172. checksum="nope",
  173. posted_on=datetime,
  174. updated_on=datetime)
  175. self.thread.set_last_post(post)
  176. self.assertEqual(self.thread.last_post, post)
  177. self.assertEqual(self.thread.last_post_on, post.posted_on)
  178. self.assertEqual(self.thread.last_poster, user)
  179. self.assertEqual(self.thread.last_poster_name, user.username)
  180. self.assertEqual(self.thread.last_poster_slug, user.slug)
  181. def test_move(self):
  182. """move(new_forum) moves thread to other forum"""
  183. # pick category instead of forum (so we don't have to create one)
  184. new_forum = Forum.objects.filter(role="category")[:1][0]
  185. self.thread.move(new_forum)
  186. self.assertEqual(self.thread.forum, new_forum)
  187. for post in self.thread.post_set.all():
  188. self.assertEqual(post.forum_id, new_forum.id)
  189. def test_merge(self):
  190. """merge(other_thread) moves other thread content to this thread"""
  191. with self.assertRaises(ValueError):
  192. self.thread.merge(self.thread)
  193. datetime = timezone.now() + timedelta(5)
  194. other_thread = Thread(
  195. forum=self.forum,
  196. weight=0,
  197. started_on=datetime,
  198. starter_name='Tester',
  199. starter_slug='tester',
  200. last_post_on=datetime,
  201. last_poster_name='Tester',
  202. last_poster_slug='tester')
  203. other_thread.set_title("Other thread")
  204. other_thread.save()
  205. post = Post.objects.create(
  206. forum=self.forum,
  207. thread=other_thread,
  208. poster_name='Admin',
  209. poster_ip='127.0.0.1',
  210. original="Hello! I am other message!",
  211. parsed="<p>Hello! I am other message!</p>",
  212. checksum="nope",
  213. posted_on=datetime,
  214. updated_on=datetime)
  215. other_thread.first_post = post
  216. other_thread.last_post = post
  217. other_thread.save()
  218. self.thread.merge(other_thread)
  219. self.thread.synchronize()
  220. self.assertEqual(self.thread.replies, 1)
  221. self.assertEqual(self.thread.last_post, post)
  222. self.assertEqual(self.thread.last_post_on, post.posted_on)
  223. self.assertEqual(self.thread.last_poster_name, "Admin")
  224. self.assertEqual(self.thread.last_poster_slug, "admin")