testutils.py 5.6 KB

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