test_forum_models.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. from datetime import datetime
  2. from flask import current_app
  3. from flask.ext.login import login_user, current_user, logout_user
  4. from flaskbb.forum.models import Category, Forum, Topic, Post, ForumsRead, \
  5. TopicsRead
  6. from flaskbb.user.models import User
  7. def test_category_save(database):
  8. category = Category(title="Test Category")
  9. category.save()
  10. assert category.title == "Test Category"
  11. def test_category_delete(category):
  12. category.delete()
  13. category = Category.query.filter_by(id=category.id).first()
  14. assert category is None
  15. def test_category_delete_with_user(topic_normal):
  16. user = topic_normal.user
  17. forum = topic_normal.forum
  18. category = topic_normal.forum.category
  19. assert user.post_count == 1
  20. assert forum.post_count == 1
  21. assert forum.topic_count == 1
  22. category.delete([user])
  23. assert user.post_count == 0
  24. category = Category.query.filter_by(id=category.id).first()
  25. topic = Topic.query.filter_by(id=topic_normal.id).first()
  26. assert category is None
  27. # The topic should also be deleted
  28. assert topic is None
  29. def test_category_delete_with_forum(forum):
  30. forum.category.delete()
  31. assert forum is not None
  32. assert forum.category is not None
  33. category = Category.query.filter_by(id=forum.category.id).first()
  34. forum = Forum.query.filter_by(id=forum.id).first()
  35. assert forum is None
  36. assert category is None
  37. def test_forum_save(category, moderator_user):
  38. forum = Forum(title="Test Forum", category_id=category.id)
  39. forum.save()
  40. assert forum.title == "Test Forum"
  41. # Test with adding a moderator
  42. forum.save([moderator_user])
  43. assert forum.moderators == [moderator_user]
  44. def test_forum_delete(forum):
  45. forum.delete()
  46. forum = Forum.query.filter_by(id=forum.id).first()
  47. assert forum is None
  48. def test_forum_delete_with_user(topic_normal, normal_user):
  49. assert normal_user.post_count == 1
  50. topic_normal.forum.delete([normal_user])
  51. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  52. assert forum is None
  53. assert normal_user.post_count == 0
  54. def test_forum_update_last_post(topic_normal, normal_user):
  55. post = Post(content="Test Content 2")
  56. post.save(topic=topic_normal, user=normal_user)
  57. assert topic_normal.forum.last_post == post
  58. post.delete()
  59. topic_normal.forum.update_last_post()
  60. assert topic_normal.forum.last_post == topic_normal.first_post
  61. def test_forum_update_read(database, normal_user, topic_normal):
  62. forumsread = ForumsRead.query.\
  63. filter(ForumsRead.user_id == normal_user.id,
  64. ForumsRead.forum_id == topic_normal.forum_id).first()
  65. topicsread = TopicsRead.query.\
  66. filter(TopicsRead.user_id == normal_user.id,
  67. TopicsRead.topic_id == topic_normal.id).first()
  68. forum = topic_normal.forum
  69. with current_app.test_request_context():
  70. # Test with logged in user
  71. login_user(normal_user)
  72. # Should return False because topicsread is None
  73. assert not forum.update_read(current_user, forumsread, topicsread)
  74. # This is the first time the user visits the topic
  75. topicsread = TopicsRead()
  76. topicsread.user_id = normal_user.id
  77. topicsread.topic_id = topic_normal.id
  78. topicsread.forum_id = topic_normal.forum_id
  79. topicsread.last_read = datetime.utcnow()
  80. topicsread.save()
  81. # hence, we also need to create a new entry
  82. assert forum.update_read(current_user, forumsread, topicsread)
  83. forumsread = ForumsRead.query.\
  84. filter(ForumsRead.user_id == normal_user.id,
  85. ForumsRead.forum_id == topic_normal.forum_id).first()
  86. # everything should be up-to-date now
  87. assert not forum.update_read(current_user, forumsread, topicsread)
  88. post = Post(content="Test Content")
  89. post.save(user=normal_user, topic=topic_normal)
  90. # Updating the topicsread tracker
  91. topicsread.last_read = datetime.utcnow()
  92. topicsread.save()
  93. # now the forumsread tracker should also need an update
  94. assert forum.update_read(current_user, forumsread, topicsread)
  95. logout_user()
  96. # should fail because the user is logged out
  97. assert not forum.update_read(current_user, forumsread, topicsread)
  98. def test_forum_update_read_two_topics(database, normal_user, topic_normal,
  99. topic_moderator):
  100. forumsread = ForumsRead.query.\
  101. filter(ForumsRead.user_id == normal_user.id,
  102. ForumsRead.forum_id == topic_normal.forum_id).first()
  103. forum = topic_normal.forum
  104. with current_app.test_request_context():
  105. # Test with logged in user
  106. login_user(normal_user)
  107. # This is the first time the user visits the topic
  108. topicsread = TopicsRead()
  109. topicsread.user_id = normal_user.id
  110. topicsread.topic_id = topic_normal.id
  111. topicsread.forum_id = topic_normal.forum_id
  112. topicsread.last_read = datetime.utcnow()
  113. topicsread.save()
  114. # will not create a entry because there is still one unread topic
  115. assert not forum.update_read(current_user, forumsread, topicsread)
  116. def test_forum_url(forum):
  117. assert forum.url == "http://localhost:5000/forum/1-test-forum"
  118. def test_forum_slugify(forum):
  119. assert forum.slug == "test-forum"
  120. def test_topic_save(forum, normal_user):
  121. post = Post(content="Test Content")
  122. topic = Topic(title="Test Title")
  123. assert forum.last_post_id is None
  124. assert forum.post_count == 0
  125. assert forum.topic_count == 0
  126. topic.save(forum=forum, post=post, user=normal_user)
  127. assert topic.title == "Test Title"
  128. topic.title = "Test Edit Title"
  129. topic.save()
  130. assert topic.title == "Test Edit Title"
  131. # The first post in the topic is also the last post
  132. assert topic.first_post_id == post.id
  133. assert topic.last_post_id == post.id
  134. assert forum.last_post_id == post.id
  135. assert forum.post_count == 1
  136. assert forum.topic_count == 1
  137. def test_topic_delete(topic_normal):
  138. assert topic_normal.user.post_count == 1
  139. assert topic_normal.post_count == 1
  140. assert topic_normal.forum.topic_count == 1
  141. assert topic_normal.forum.post_count == 1
  142. topic_normal.delete(users=[topic_normal.user])
  143. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  144. user = User.query.filter_by(id=topic_normal.user_id).first()
  145. topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
  146. assert topic_normal is None
  147. assert user.post_count == 0
  148. assert forum.topic_count == 0
  149. assert forum.post_count == 0
  150. assert forum.last_post_id is None
  151. def test_topic_merge(topic_normal):
  152. topic_other = Topic(title="Test Topic Merge")
  153. post = Post(content="Test Content Merge")
  154. topic_other.save(post=post, user=topic_normal.user, forum=topic_normal.forum)
  155. # Save the last_post_id in another variable because topic_other will be
  156. # overwritten later
  157. last_post_other = topic_other.last_post_id
  158. assert topic_other.merge(topic_normal)
  159. # I just want to be sure that the topic is deleted
  160. topic_other = Topic.query.filter_by(id=topic_other.id).first()
  161. assert topic_other is None
  162. assert topic_normal.post_count == 2
  163. assert topic_normal.last_post_id == last_post_other
  164. def test_topic_merge_other_forum(topic_normal):
  165. """You cannot merge a topic with a topic from another forum"""
  166. forum_other = Forum(title="Test Forum 2", category_id=1)
  167. forum_other.save()
  168. topic_other = Topic(title="Test Topic 2")
  169. post_other = Post(content="Test Content 2")
  170. topic_other.save(user=topic_normal.user, forum=forum_other, post=post_other)
  171. assert not topic_normal.merge(topic_other)
  172. def test_topic_move(topic_normal):
  173. forum_other = Forum(title="Test Forum 2", category_id=1)
  174. forum_other.save()
  175. forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first()
  176. assert topic_normal.move(forum_other)
  177. assert forum_old.topics == []
  178. assert forum_old.last_post_id == 0
  179. assert forum_old.topic_count == 0
  180. assert forum_old.post_count == 0
  181. assert forum_other.last_post_id == topic_normal.last_post_id
  182. assert forum_other.topic_count == 1
  183. assert forum_other.post_count == 1
  184. def test_topic_move_same_forum(topic_normal):
  185. assert not topic_normal.move(topic_normal.forum)
  186. def test_topic_tracker_needs_update(database, normal_user, topic_normal):
  187. forumsread = ForumsRead.query.\
  188. filter(ForumsRead.user_id == normal_user.id,
  189. ForumsRead.forum_id == topic_normal.forum_id).first()
  190. topicsread = TopicsRead.query.\
  191. filter(TopicsRead.user_id == normal_user.id,
  192. TopicsRead.topic_id == topic_normal.id).first()
  193. with current_app.test_request_context():
  194. assert topic_normal.tracker_needs_update(forumsread, topicsread)
  195. # Update the tracker
  196. topicsread = TopicsRead()
  197. topicsread.user_id = normal_user.id
  198. topicsread.topic_id = topic_normal.id
  199. topicsread.forum_id = topic_normal.forum_id
  200. topicsread.last_read = datetime.utcnow()
  201. topicsread.save()
  202. forumsread = ForumsRead()
  203. forumsread.user_id = normal_user.id
  204. forumsread.forum_id = topic_normal.forum_id
  205. forumsread.last_read = datetime.utcnow()
  206. forumsread.save()
  207. # Now the topic should be read
  208. assert not topic_normal.tracker_needs_update(forumsread, topicsread)
  209. post = Post(content="Test Content")
  210. post.save(topic=topic_normal, user=normal_user)
  211. assert topic_normal.tracker_needs_update(forumsread, topicsread)
  212. def test_topic_tracker_needs_update_cleared(database, normal_user, topic_normal):
  213. forumsread = ForumsRead.query.\
  214. filter(ForumsRead.user_id == normal_user.id,
  215. ForumsRead.forum_id == topic_normal.forum_id).first()
  216. topicsread = TopicsRead.query.\
  217. filter(TopicsRead.user_id == normal_user.id,
  218. TopicsRead.topic_id == topic_normal.id).first()
  219. with current_app.test_request_context():
  220. assert topic_normal.tracker_needs_update(forumsread, topicsread)
  221. # Update the tracker
  222. forumsread = ForumsRead()
  223. forumsread.user_id = normal_user.id
  224. forumsread.forum_id = topic_normal.forum_id
  225. forumsread.last_read = datetime.utcnow()
  226. forumsread.cleared = datetime.utcnow()
  227. forumsread.save()
  228. # Now the topic should be read
  229. assert not topic_normal.tracker_needs_update(forumsread, topicsread)
  230. def test_topic_update_read(database, normal_user, topic_normal):
  231. forumsread = ForumsRead.query.\
  232. filter(ForumsRead.user_id == normal_user.id,
  233. ForumsRead.forum_id == topic_normal.forum_id).first()
  234. with current_app.test_request_context():
  235. # Test with logged in user
  236. login_user(normal_user)
  237. assert current_user.is_authenticated()
  238. # Update the tracker
  239. assert topic_normal.update_read(current_user, topic_normal.forum,
  240. forumsread)
  241. # Because the tracker is already up-to-date, it shouldn't update it
  242. # again.
  243. assert not topic_normal.update_read(current_user, topic_normal.forum,
  244. forumsread)
  245. # Adding a new post - now the tracker shouldn't be up-to-date anymore.
  246. post = Post(content="Test Content")
  247. post.save(topic=topic_normal, user=normal_user)
  248. forumsread = ForumsRead.query.\
  249. filter(ForumsRead.user_id == normal_user.id,
  250. ForumsRead.forum_id == topic_normal.forum_id).first()
  251. # Test tracker length
  252. current_app.config["TRACKER_LENGTH"] = 0
  253. assert not topic_normal.update_read(current_user, topic_normal.forum,
  254. forumsread)
  255. current_app.config["TRACKER_LENGTH"] = 1
  256. assert topic_normal.update_read(current_user, topic_normal.forum,
  257. forumsread)
  258. # Test with logged out user
  259. logout_user()
  260. assert not current_user.is_authenticated()
  261. assert not topic_normal.update_read(current_user, topic_normal.forum,
  262. forumsread)
  263. def test_topic_url(topic_normal):
  264. assert topic_normal.url == "http://localhost:5000/topic/1-test-topic-normal"
  265. def test_topic_slug(topic_normal):
  266. assert topic_normal.slug == "test-topic-normal"
  267. def test_post_save(topic_normal, normal_user):
  268. post = Post(content="Test Content")
  269. post.save(topic=topic_normal, user=normal_user)
  270. assert post.content == "Test Content"
  271. post.content = "Test Edit Content"
  272. post.save()
  273. assert post.content == "Test Edit Content"
  274. assert topic_normal.user.post_count == 2
  275. assert topic_normal.post_count == 2
  276. assert topic_normal.last_post == post
  277. assert topic_normal.forum.post_count == 2
  278. def test_post_delete(topic_normal):
  279. post_middle = Post(content="Test Content Middle")
  280. post_middle.save(topic=topic_normal, user=topic_normal.user)
  281. post_last = Post(content="Test Content Last")
  282. post_last.save(topic=topic_normal, user=topic_normal.user)
  283. assert topic_normal.post_count == 3
  284. assert topic_normal.forum.post_count == 3
  285. assert topic_normal.user.post_count == 3
  286. post_middle.delete()
  287. # Check the last posts
  288. assert topic_normal.last_post == post_last
  289. assert topic_normal.forum.last_post == post_last
  290. post_last.delete()
  291. # That was a bit trickier..
  292. assert topic_normal.post_count == 1
  293. assert topic_normal.forum.post_count == 1
  294. assert topic_normal.user.post_count == 1
  295. assert topic_normal.first_post_id == topic_normal.last_post_id
  296. assert topic_normal.forum.last_post_id == topic_normal.last_post_id