testutils.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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({
  89. 'poster': poster,
  90. 'poster_name': poster.username,
  91. })
  92. except AttributeError:
  93. kwargs.update({'poster_name': poster})
  94. post = Post.objects.create(**kwargs)
  95. update_post_checksum(post)
  96. post.save()
  97. thread.synchronize()
  98. thread.save()
  99. thread.category.synchronize()
  100. thread.category.save()
  101. return post
  102. def post_poll(thread, poster):
  103. poll = Poll.objects.create(
  104. category=thread.category,
  105. thread=thread,
  106. poster=poster,
  107. poster_name=poster.username,
  108. poster_slug=poster.slug,
  109. poster_ip='127.0.0.1',
  110. question="Lorem ipsum dolor met?",
  111. choices=[
  112. {
  113. 'hash': 'aaaaaaaaaaaa',
  114. 'label': 'Alpha',
  115. 'votes': 1
  116. },
  117. {
  118. 'hash': 'bbbbbbbbbbbb',
  119. 'label': 'Beta',
  120. 'votes': 0
  121. },
  122. {
  123. 'hash': 'gggggggggggg',
  124. 'label': 'Gamma',
  125. 'votes': 2
  126. },
  127. {
  128. 'hash': 'dddddddddddd',
  129. 'label': 'Delta',
  130. 'votes': 1
  131. },
  132. ],
  133. allowed_choices=2,
  134. votes=4
  135. )
  136. # one user voted for Alpha choice
  137. try:
  138. user = UserModel.objects.get(slug='bob')
  139. except UserModel.DoesNotExist:
  140. user = UserModel.objects.create_user('bob', 'bob@test.com', 'Pass.123')
  141. poll.pollvote_set.create(
  142. category=thread.category,
  143. thread=thread,
  144. voter=user,
  145. voter_name=user.username,
  146. voter_slug=user.slug,
  147. voter_ip='127.0.0.1',
  148. choice_hash='aaaaaaaaaaaa',
  149. )
  150. # test user voted on third and last choices
  151. poll.pollvote_set.create(
  152. category=thread.category,
  153. thread=thread,
  154. voter=poster,
  155. voter_name=poster.username,
  156. voter_slug=poster.slug,
  157. voter_ip='127.0.0.1',
  158. choice_hash='gggggggggggg',
  159. )
  160. poll.pollvote_set.create(
  161. category=thread.category,
  162. thread=thread,
  163. voter=poster,
  164. voter_name=poster.username,
  165. voter_slug=poster.slug,
  166. voter_ip='127.0.0.1',
  167. choice_hash='dddddddddddd',
  168. )
  169. # somebody else voted on third option before being deleted
  170. poll.pollvote_set.create(
  171. category=thread.category,
  172. thread=thread,
  173. voter_name='deleted',
  174. voter_slug='deleted',
  175. voter_ip='127.0.0.1',
  176. choice_hash='gggggggggggg',
  177. )
  178. return poll
  179. def like_post(post, liker=None, username=None):
  180. if not post.last_likes:
  181. post.last_likes = []
  182. if liker:
  183. like = post.postlike_set.create(
  184. category=post.category,
  185. thread=post.thread,
  186. liker=liker,
  187. liker_name=liker.username,
  188. liker_slug=liker.slug,
  189. liker_ip='127.0.0.1',
  190. )
  191. post.last_likes = [{
  192. 'id': liker.id,
  193. 'username': liker.username,
  194. }] + post.last_likes
  195. else:
  196. like = post.postlike_set.create(
  197. category=post.category,
  198. thread=post.thread,
  199. liker_name=username,
  200. liker_slug=slugify(username),
  201. liker_ip='127.0.0.1',
  202. )
  203. post.last_likes = [{
  204. 'id': None,
  205. 'username': username,
  206. }] + post.last_likes
  207. post.likes += 1
  208. post.save()
  209. return like