testutils.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.utils import timezone
  4. from misago.core.utils import slugify
  5. from .checksums import update_post_checksum
  6. from .models import Poll, Post, Thread
  7. UserModel = get_user_model()
  8. def post_thread(category, title='Test thread', poster='Tester',
  9. is_global=False, is_pinned=False, is_unapproved=False,
  10. is_hidden=False, is_closed=False, started_on=None):
  11. started_on = started_on or timezone.now()
  12. kwargs = {
  13. 'category': category,
  14. 'title': title,
  15. 'slug': slugify(title),
  16. 'started_on': started_on,
  17. 'last_post_on': started_on,
  18. 'is_unapproved': is_unapproved,
  19. 'is_hidden': is_hidden,
  20. 'is_closed': is_closed
  21. }
  22. if is_global:
  23. kwargs['weight'] = 2
  24. elif is_pinned:
  25. kwargs['weight'] = 1
  26. try:
  27. kwargs.update({
  28. 'starter': poster,
  29. 'starter_name': poster.username,
  30. 'starter_slug': poster.slug,
  31. 'last_poster': poster,
  32. 'last_poster_name': poster.username,
  33. 'last_poster_slug': poster.slug,
  34. })
  35. except AttributeError:
  36. kwargs.update({
  37. 'starter_name': poster,
  38. 'starter_slug': slugify(poster),
  39. 'last_poster_name': poster,
  40. 'last_poster_slug': slugify(poster),
  41. })
  42. thread = Thread.objects.create(**kwargs)
  43. reply_thread(
  44. thread,
  45. poster=poster,
  46. posted_on=started_on,
  47. is_hidden=is_hidden,
  48. is_unapproved=is_unapproved,
  49. )
  50. return thread
  51. def reply_thread(thread, poster="Tester", message="I am test message",
  52. is_unapproved=False, is_hidden=False, is_event=False,
  53. has_reports=False, has_open_reports=False, posted_on=None, poster_ip='127.0.0.1'):
  54. posted_on = posted_on or thread.last_post_on + timedelta(minutes=5)
  55. kwargs = {
  56. 'category': thread.category,
  57. 'thread': thread,
  58. 'original': message,
  59. 'parsed': message,
  60. 'checksum': 'nope',
  61. 'poster_ip': poster_ip,
  62. 'posted_on': posted_on,
  63. 'updated_on': posted_on,
  64. 'is_event': is_event,
  65. 'is_unapproved': is_unapproved,
  66. 'is_hidden': is_hidden,
  67. 'has_reports': has_reports,
  68. 'has_open_reports': has_open_reports,
  69. }
  70. try:
  71. kwargs.update({'poster': poster, 'poster_name': poster.username})
  72. except AttributeError:
  73. kwargs.update({'poster_name': poster})
  74. post = Post.objects.create(**kwargs)
  75. update_post_checksum(post)
  76. post.save()
  77. thread.synchronize()
  78. thread.save()
  79. thread.category.synchronize()
  80. thread.category.save()
  81. return post
  82. def post_poll(thread, poster):
  83. poll = Poll.objects.create(
  84. category=thread.category,
  85. thread=thread,
  86. poster=poster,
  87. poster_name=poster.username,
  88. poster_slug=poster.slug,
  89. poster_ip='127.0.0.1',
  90. question="Lorem ipsum dolor met?",
  91. choices=[
  92. {
  93. 'hash': 'aaaaaaaaaaaa',
  94. 'label': 'Alpha',
  95. 'votes': 1
  96. },
  97. {
  98. 'hash': 'bbbbbbbbbbbb',
  99. 'label': 'Beta',
  100. 'votes': 0
  101. },
  102. {
  103. 'hash': 'gggggggggggg',
  104. 'label': 'Gamma',
  105. 'votes': 2
  106. },
  107. {
  108. 'hash': 'dddddddddddd',
  109. 'label': 'Delta',
  110. 'votes': 1
  111. }
  112. ],
  113. allowed_choices=2,
  114. votes=4
  115. )
  116. # one user voted for Alpha choice
  117. try:
  118. user = UserModel.objects.get(slug='bob')
  119. except UserModel.DoesNotExist:
  120. user = UserModel.objects.create_user(
  121. 'bob', 'bob@test.com', 'Pass.123')
  122. poll.pollvote_set.create(
  123. category=thread.category,
  124. thread=thread,
  125. voter=user,
  126. voter_name=user.username,
  127. voter_slug=user.slug,
  128. voter_ip='127.0.0.1',
  129. choice_hash='aaaaaaaaaaaa'
  130. )
  131. # test user voted on third and last choices
  132. poll.pollvote_set.create(
  133. category=thread.category,
  134. thread=thread,
  135. voter=poster,
  136. voter_name=poster.username,
  137. voter_slug=poster.slug,
  138. voter_ip='127.0.0.1',
  139. choice_hash='gggggggggggg'
  140. )
  141. poll.pollvote_set.create(
  142. category=thread.category,
  143. thread=thread,
  144. voter=poster,
  145. voter_name=poster.username,
  146. voter_slug=poster.slug,
  147. voter_ip='127.0.0.1',
  148. choice_hash='dddddddddddd'
  149. )
  150. # somebody else voted on third option before being deleted
  151. poll.pollvote_set.create(
  152. category=thread.category,
  153. thread=thread,
  154. voter_name='deleted',
  155. voter_slug='deleted',
  156. voter_ip='127.0.0.1',
  157. choice_hash='gggggggggggg'
  158. )
  159. return poll
  160. def like_post(post, liker=None, username=None):
  161. if not post.last_likes:
  162. post.last_likes = []
  163. if liker:
  164. like = post.postlike_set.create(
  165. category=post.category,
  166. thread=post.thread,
  167. liker=liker,
  168. liker_name=liker.username,
  169. liker_slug=liker.slug,
  170. liker_ip='127.0.0.1'
  171. )
  172. post.last_likes = [
  173. {
  174. 'id': liker.id,
  175. 'username': liker.username
  176. }
  177. ] + post.last_likes
  178. else:
  179. like = post.postlike_set.create(
  180. category=post.category,
  181. thread=post.thread,
  182. liker_name=username,
  183. liker_slug=slugify(username),
  184. liker_ip='127.0.0.1'
  185. )
  186. post.last_likes = [
  187. {
  188. 'id': None,
  189. 'username': username
  190. }
  191. ] + post.last_likes
  192. post.likes += 1
  193. post.save()
  194. return like