test.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.utils import timezone
  4. from ..acl.test import patch_user_acl
  5. from ..categories.models import Category
  6. from ..core.utils import slugify
  7. from ..users.test import create_test_user
  8. from .checksums import update_post_checksum
  9. from .models import Poll, Post, Thread
  10. default_category_acl = {
  11. "can_see": 1,
  12. "can_browse": 1,
  13. "can_see_all_threads": 1,
  14. "can_see_own_threads": 0,
  15. "can_hide_threads": 0,
  16. "can_approve_content": 0,
  17. "can_edit_posts": 0,
  18. "can_hide_posts": 0,
  19. "can_hide_own_posts": 0,
  20. "can_merge_threads": 0,
  21. "can_close_threads": 0,
  22. }
  23. def patch_category_acl(acl_patch=None):
  24. def patch_acl(_, user_acl):
  25. category = Category.objects.get(slug="first-category")
  26. category_acl = user_acl["categories"][category.id]
  27. category_acl.update(default_category_acl)
  28. if acl_patch:
  29. category_acl.update(acl_patch)
  30. cleanup_patched_acl(user_acl, category_acl, category)
  31. return patch_user_acl(patch_acl)
  32. def patch_other_user_category_acl(acl_patch=None):
  33. def patch_acl(user, user_acl):
  34. if user.slug != "otheruser":
  35. return
  36. category = Category.objects.get(slug="first-category")
  37. category_acl = user_acl["categories"][category.id]
  38. category_acl.update(default_category_acl)
  39. if acl_patch:
  40. category_acl.update(acl_patch)
  41. cleanup_patched_acl(user_acl, category_acl, category)
  42. return patch_user_acl(patch_acl)
  43. def patch_other_category_acl(acl_patch=None):
  44. def patch_acl(_, user_acl):
  45. src_category = Category.objects.get(slug="first-category")
  46. category_acl = user_acl["categories"][src_category.id].copy()
  47. dst_category = Category.objects.get(slug="other-category")
  48. user_acl["categories"][dst_category.id] = category_acl
  49. category_acl.update(default_category_acl)
  50. if acl_patch:
  51. category_acl.update(acl_patch)
  52. cleanup_patched_acl(user_acl, category_acl, dst_category)
  53. return patch_user_acl(patch_acl)
  54. def patch_private_threads_acl(acl_patch=None):
  55. def patch_acl(_, user_acl):
  56. category = Category.objects.private_threads()
  57. category_acl = user_acl["categories"][category.id]
  58. category_acl.update(default_category_acl)
  59. if acl_patch:
  60. category_acl.update(acl_patch)
  61. cleanup_patched_acl(user_acl, category_acl, category)
  62. return patch_user_acl(patch_acl)
  63. def other_user_cant_use_private_threads(user, user_acl):
  64. if user.slug == "otheruser":
  65. user_acl.update({"can_use_private_threads": False})
  66. def create_category_acl_patch(category_slug, acl_patch):
  67. def created_category_acl_patch(_, user_acl):
  68. category = Category.objects.get(slug=category_slug)
  69. category_acl = user_acl["categories"].get(category.id, {})
  70. category_acl.update(default_category_acl)
  71. if acl_patch:
  72. category_acl.update(acl_patch)
  73. cleanup_patched_acl(user_acl, category_acl, category)
  74. return created_category_acl_patch
  75. def cleanup_patched_acl(user_acl, category_acl, category):
  76. visible_categories = user_acl["visible_categories"]
  77. browseable_categories = user_acl["browseable_categories"]
  78. if not category_acl["can_see"] and category.id in visible_categories:
  79. visible_categories.remove(category.id)
  80. if not category_acl["can_see"] and category.id in browseable_categories:
  81. browseable_categories.remove(category.id)
  82. if not category_acl["can_browse"] and category.id in browseable_categories:
  83. browseable_categories.remove(category.id)
  84. if category_acl["can_see"] and category.id not in visible_categories:
  85. visible_categories.append(category.id)
  86. if category_acl["can_browse"] and category.id not in browseable_categories:
  87. browseable_categories.append(category.id)
  88. User = get_user_model()
  89. def post_thread(
  90. category,
  91. title="Test thread",
  92. poster="Tester",
  93. is_global=False,
  94. is_pinned=False,
  95. is_unapproved=False,
  96. is_hidden=False,
  97. is_closed=False,
  98. started_on=None,
  99. ):
  100. started_on = started_on or timezone.now()
  101. kwargs = {
  102. "category": category,
  103. "title": title,
  104. "slug": slugify(title),
  105. "started_on": started_on,
  106. "last_post_on": started_on,
  107. "is_unapproved": is_unapproved,
  108. "is_hidden": is_hidden,
  109. "is_closed": is_closed,
  110. }
  111. if is_global:
  112. kwargs["weight"] = 2
  113. elif is_pinned:
  114. kwargs["weight"] = 1
  115. try:
  116. kwargs.update(
  117. {
  118. "starter": poster,
  119. "starter_name": poster.username,
  120. "starter_slug": poster.slug,
  121. "last_poster": poster,
  122. "last_poster_name": poster.username,
  123. "last_poster_slug": poster.slug,
  124. }
  125. )
  126. except AttributeError:
  127. kwargs.update(
  128. {
  129. "starter_name": poster,
  130. "starter_slug": slugify(poster),
  131. "last_poster_name": poster,
  132. "last_poster_slug": slugify(poster),
  133. }
  134. )
  135. thread = Thread.objects.create(**kwargs)
  136. reply_thread(
  137. thread,
  138. poster=poster,
  139. posted_on=started_on,
  140. is_hidden=is_hidden,
  141. is_unapproved=is_unapproved,
  142. )
  143. return thread
  144. def reply_thread(
  145. thread,
  146. poster="Tester",
  147. message="I am test message",
  148. is_unapproved=False,
  149. is_hidden=False,
  150. is_event=False,
  151. is_protected=False,
  152. has_reports=False,
  153. has_open_reports=False,
  154. posted_on=None,
  155. ):
  156. posted_on = posted_on or thread.last_post_on + timedelta(minutes=5)
  157. kwargs = {
  158. "category": thread.category,
  159. "thread": thread,
  160. "original": message,
  161. "parsed": message,
  162. "checksum": "nope",
  163. "posted_on": posted_on,
  164. "updated_on": posted_on,
  165. "is_event": is_event,
  166. "is_unapproved": is_unapproved,
  167. "is_hidden": is_hidden,
  168. "is_protected": is_protected,
  169. "has_reports": has_reports,
  170. "has_open_reports": has_open_reports,
  171. }
  172. try:
  173. kwargs.update({"poster": poster, "poster_name": poster.username})
  174. except AttributeError:
  175. kwargs.update({"poster_name": poster})
  176. post = Post.objects.create(**kwargs)
  177. update_post_checksum(post)
  178. post.save()
  179. thread.synchronize()
  180. thread.save()
  181. thread.category.synchronize()
  182. thread.category.save()
  183. return post
  184. def post_poll(thread, poster):
  185. poll = Poll.objects.create(
  186. category=thread.category,
  187. thread=thread,
  188. poster=poster,
  189. poster_name=poster.username,
  190. poster_slug=poster.slug,
  191. question="Lorem ipsum dolor met?",
  192. choices=[
  193. {"hash": "aaaaaaaaaaaa", "label": "Alpha", "votes": 1},
  194. {"hash": "bbbbbbbbbbbb", "label": "Beta", "votes": 0},
  195. {"hash": "gggggggggggg", "label": "Gamma", "votes": 2},
  196. {"hash": "dddddddddddd", "label": "Delta", "votes": 1},
  197. ],
  198. allowed_choices=2,
  199. votes=4,
  200. )
  201. # one user voted for Alpha choice
  202. try:
  203. user = User.objects.get(slug="user")
  204. except User.DoesNotExist:
  205. user = create_test_user("User", "user@example.com")
  206. poll.pollvote_set.create(
  207. category=thread.category,
  208. thread=thread,
  209. voter=user,
  210. voter_name=user.username,
  211. voter_slug=user.slug,
  212. choice_hash="aaaaaaaaaaaa",
  213. )
  214. # test user voted on third and last choices
  215. poll.pollvote_set.create(
  216. category=thread.category,
  217. thread=thread,
  218. voter=poster,
  219. voter_name=poster.username,
  220. voter_slug=poster.slug,
  221. choice_hash="gggggggggggg",
  222. )
  223. poll.pollvote_set.create(
  224. category=thread.category,
  225. thread=thread,
  226. voter=poster,
  227. voter_name=poster.username,
  228. voter_slug=poster.slug,
  229. choice_hash="dddddddddddd",
  230. )
  231. # somebody else voted on third option before being deleted
  232. poll.pollvote_set.create(
  233. category=thread.category,
  234. thread=thread,
  235. voter_name="deleted",
  236. voter_slug="deleted",
  237. choice_hash="gggggggggggg",
  238. )
  239. return poll
  240. def like_post(post, liker=None, username=None):
  241. if not post.last_likes:
  242. post.last_likes = []
  243. if liker:
  244. like = post.postlike_set.create(
  245. category=post.category,
  246. thread=post.thread,
  247. liker=liker,
  248. liker_name=liker.username,
  249. liker_slug=liker.slug,
  250. )
  251. post.last_likes = [
  252. {"id": liker.id, "username": liker.username}
  253. ] + post.last_likes
  254. else:
  255. like = post.postlike_set.create(
  256. category=post.category,
  257. thread=post.thread,
  258. liker_name=username,
  259. liker_slug=slugify(username),
  260. )
  261. post.last_likes = [{"id": None, "username": username}] + post.last_likes
  262. post.likes += 1
  263. post.save()
  264. return like