test_forum_models.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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, Report
  6. from flaskbb.user.models import User
  7. def test_category_save(database):
  8. """Test the save category method."""
  9. category = Category(title="Test Category")
  10. category.save()
  11. assert category.title == "Test Category"
  12. def test_category_delete(category):
  13. """Test the delete category method."""
  14. category.delete()
  15. category = Category.query.filter_by(id=category.id).first()
  16. assert category is None
  17. def test_category_delete_with_user(topic):
  18. """Test the delete category method with recounting the users post counts."""
  19. user = topic.user
  20. forum = topic.forum
  21. category = topic.forum.category
  22. assert user.post_count == 1
  23. assert forum.post_count == 1
  24. assert forum.topic_count == 1
  25. category.delete([user])
  26. assert user.post_count == 0
  27. category = Category.query.filter_by(id=category.id).first()
  28. topic = Topic.query.filter_by(id=topic.id).first()
  29. assert category is None
  30. # The topic should also be deleted
  31. assert topic is None
  32. def test_category_delete_with_forum(forum):
  33. """When deleting a category, all of his forums should also be deleted."""
  34. forum.category.delete()
  35. assert forum is not None
  36. assert forum.category is not None
  37. category = Category.query.filter_by(id=forum.category.id).first()
  38. forum = Forum.query.filter_by(id=forum.id).first()
  39. assert forum is None
  40. assert category is None
  41. def test_forum_save(category, moderator_user):
  42. """Test the save forum method"""
  43. forum = Forum(title="Test Forum", category_id=category.id)
  44. forum.save()
  45. assert forum.title == "Test Forum"
  46. # Test with adding a moderator
  47. forum.save([moderator_user])
  48. assert forum.moderators == [moderator_user]
  49. def test_forum_delete(forum):
  50. """Test the delete forum method."""
  51. forum.delete()
  52. forum = Forum.query.filter_by(id=forum.id).first()
  53. assert forum is None
  54. def test_forum_delete_with_user_and_topic(topic, user):
  55. """Now test the delete forum method with a topic inside."""
  56. assert user.post_count == 1
  57. topic.forum.delete([user])
  58. forum = Forum.query.filter_by(id=topic.forum_id).first()
  59. assert forum is None
  60. assert user.post_count == 0
  61. def test_forum_update_last_post(topic, user):
  62. """Test the update last post method."""
  63. post = Post(content="Test Content 2")
  64. post.save(topic=topic, user=user)
  65. assert topic.forum.last_post == post
  66. post.delete()
  67. topic.forum.update_last_post()
  68. assert topic.forum.last_post == topic.first_post
  69. def test_forum_update_read(database, user, topic):
  70. """Test the update read method."""
  71. forumsread = ForumsRead.query.\
  72. filter(ForumsRead.user_id == user.id,
  73. ForumsRead.forum_id == topic.forum_id).first()
  74. topicsread = TopicsRead.query.\
  75. filter(TopicsRead.user_id == user.id,
  76. TopicsRead.topic_id == topic.id).first()
  77. forum = topic.forum
  78. with current_app.test_request_context():
  79. # Test with logged in user
  80. login_user(user)
  81. # Should return False because topicsread is None
  82. assert not forum.update_read(current_user, forumsread, topicsread)
  83. # This is the first time the user visits the topic
  84. topicsread = TopicsRead()
  85. topicsread.user_id = user.id
  86. topicsread.topic_id = topic.id
  87. topicsread.forum_id = topic.forum_id
  88. topicsread.last_read = datetime.utcnow()
  89. topicsread.save()
  90. # hence, we also need to create a new forumsread entry
  91. assert forum.update_read(current_user, forumsread, topicsread)
  92. forumsread = ForumsRead.query.\
  93. filter(ForumsRead.user_id == user.id,
  94. ForumsRead.forum_id == topic.forum_id).first()
  95. # everything should be up-to-date now
  96. assert not forum.update_read(current_user, forumsread, topicsread)
  97. post = Post(content="Test Content")
  98. post.save(user=user, topic=topic)
  99. # Updating the topicsread tracker
  100. topicsread.last_read = datetime.utcnow()
  101. topicsread.save()
  102. # now the forumsread tracker should also need a update
  103. assert forum.update_read(current_user, forumsread, topicsread)
  104. logout_user()
  105. # should fail because the user is logged out
  106. assert not forum.update_read(current_user, forumsread, topicsread)
  107. def test_forum_update_read_two_topics(database, user, topic, topic_moderator):
  108. """Test if the ForumsRead tracker will be updated if there are two topics
  109. and where one is unread and the other is read.
  110. """
  111. forumsread = ForumsRead.query.\
  112. filter(ForumsRead.user_id == user.id,
  113. ForumsRead.forum_id == topic.forum_id).first()
  114. forum = topic.forum
  115. with current_app.test_request_context():
  116. # Test with logged in user
  117. login_user(user)
  118. # This is the first time the user visits the topic
  119. topicsread = TopicsRead()
  120. topicsread.user_id = user.id
  121. topicsread.topic_id = topic.id
  122. topicsread.forum_id = topic.forum_id
  123. topicsread.last_read = datetime.utcnow()
  124. topicsread.save()
  125. # will not create a entry because there is still one unread topic
  126. assert not forum.update_read(current_user, forumsread, topicsread)
  127. def test_forum_url(forum):
  128. assert forum.url == "http://localhost:5000/forum/1-test-forum"
  129. def test_forum_slugify(forum):
  130. assert forum.slug == "test-forum"
  131. def test_topic_save(forum, user):
  132. """Test the save topic method with creating and editing a topic."""
  133. post = Post(content="Test Content")
  134. topic = Topic(title="Test Title")
  135. assert forum.last_post_id is None
  136. assert forum.post_count == 0
  137. assert forum.topic_count == 0
  138. topic.save(forum=forum, post=post, user=user)
  139. assert topic.title == "Test Title"
  140. topic.title = "Test Edit Title"
  141. topic.save()
  142. assert topic.title == "Test Edit Title"
  143. # The first post in the topic is also the last post
  144. assert topic.first_post_id == post.id
  145. assert topic.last_post_id == post.id
  146. assert forum.last_post_id == post.id
  147. assert forum.post_count == 1
  148. assert forum.topic_count == 1
  149. def test_topic_delete(topic):
  150. """Test the delete topic method"""
  151. assert topic.user.post_count == 1
  152. assert topic.post_count == 1
  153. assert topic.forum.topic_count == 1
  154. assert topic.forum.post_count == 1
  155. topic.delete(users=[topic.user])
  156. forum = Forum.query.filter_by(id=topic.forum_id).first()
  157. user = User.query.filter_by(id=topic.user_id).first()
  158. topic = Topic.query.filter_by(id=topic.id).first()
  159. assert topic is None
  160. assert user.post_count == 0
  161. assert forum.topic_count == 0
  162. assert forum.post_count == 0
  163. assert forum.last_post_id is None
  164. def test_topic_merge(topic):
  165. """Tests the topic merge method."""
  166. topic_other = Topic(title="Test Topic Merge")
  167. post = Post(content="Test Content Merge")
  168. topic_other.save(post=post, user=topic.user, forum=topic.forum)
  169. # Save the last_post_id in another variable because topic_other will be
  170. # overwritten later
  171. last_post_other = topic_other.last_post_id
  172. assert topic_other.merge(topic)
  173. # I just want to be sure that the topic is deleted
  174. topic_other = Topic.query.filter_by(id=topic_other.id).first()
  175. assert topic_other is None
  176. assert topic.post_count == 2
  177. assert topic.last_post_id == last_post_other
  178. def test_topic_merge_other_forum(topic):
  179. """You cannot merge a topic with a topic from another forum."""
  180. forum_other = Forum(title="Test Forum 2", category_id=1)
  181. forum_other.save()
  182. topic_other = Topic(title="Test Topic 2")
  183. post_other = Post(content="Test Content 2")
  184. topic_other.save(user=topic.user, forum=forum_other, post=post_other)
  185. assert not topic.merge(topic_other)
  186. def test_topic_move(topic):
  187. """Tests the topic move method."""
  188. forum_other = Forum(title="Test Forum 2", category_id=1)
  189. forum_other.save()
  190. forum_old = Forum.query.filter_by(id=topic.forum_id).first()
  191. assert topic.move(forum_other)
  192. assert forum_old.topics == []
  193. assert forum_old.last_post_id == 0
  194. assert forum_old.topic_count == 0
  195. assert forum_old.post_count == 0
  196. assert forum_other.last_post_id == topic.last_post_id
  197. assert forum_other.topic_count == 1
  198. assert forum_other.post_count == 1
  199. def test_topic_move_same_forum(topic):
  200. """You cannot move a topic within the same forum."""
  201. assert not topic.move(topic.forum)
  202. def test_topic_tracker_needs_update(database, user, topic):
  203. """Tests if the topicsread tracker needs an update if a new post has been
  204. submitted.
  205. """
  206. forumsread = ForumsRead.query.\
  207. filter(ForumsRead.user_id == user.id,
  208. ForumsRead.forum_id == topic.forum_id).first()
  209. topicsread = TopicsRead.query.\
  210. filter(TopicsRead.user_id == user.id,
  211. TopicsRead.topic_id == topic.id).first()
  212. with current_app.test_request_context():
  213. assert topic.tracker_needs_update(forumsread, topicsread)
  214. # Update the tracker
  215. topicsread = TopicsRead()
  216. topicsread.user_id = user.id
  217. topicsread.topic_id = topic.id
  218. topicsread.forum_id = topic.forum_id
  219. topicsread.last_read = datetime.utcnow()
  220. topicsread.save()
  221. forumsread = ForumsRead()
  222. forumsread.user_id = user.id
  223. forumsread.forum_id = topic.forum_id
  224. forumsread.last_read = datetime.utcnow()
  225. forumsread.save()
  226. # Now the topic should be read
  227. assert not topic.tracker_needs_update(forumsread, topicsread)
  228. post = Post(content="Test Content")
  229. post.save(topic=topic, user=user)
  230. assert topic.tracker_needs_update(forumsread, topicsread)
  231. def test_topic_tracker_needs_update_cleared(database, user, topic):
  232. """Tests if the topicsread needs an update if the forum has been marked
  233. as cleared.
  234. """
  235. forumsread = ForumsRead.query.\
  236. filter(ForumsRead.user_id == user.id,
  237. ForumsRead.forum_id == topic.forum_id).first()
  238. topicsread = TopicsRead.query.\
  239. filter(TopicsRead.user_id == user.id,
  240. TopicsRead.topic_id == topic.id).first()
  241. with current_app.test_request_context():
  242. assert topic.tracker_needs_update(forumsread, topicsread)
  243. # Update the tracker
  244. forumsread = ForumsRead()
  245. forumsread.user_id = user.id
  246. forumsread.forum_id = topic.forum_id
  247. forumsread.last_read = datetime.utcnow()
  248. forumsread.cleared = datetime.utcnow()
  249. forumsread.save()
  250. # Now the topic should be read
  251. assert not topic.tracker_needs_update(forumsread, topicsread)
  252. def test_topic_update_read(database, user, topic):
  253. """Tests the update read method if the topic is unread/read."""
  254. forumsread = ForumsRead.query.\
  255. filter(ForumsRead.user_id == user.id,
  256. ForumsRead.forum_id == topic.forum_id).first()
  257. with current_app.test_request_context():
  258. # Test with logged in user
  259. login_user(user)
  260. assert current_user.is_authenticated()
  261. # Update the tracker
  262. assert topic.update_read(current_user, topic.forum, forumsread)
  263. # Because the tracker is already up-to-date, it shouldn't update it
  264. # again.
  265. assert not topic.update_read(current_user, topic.forum, forumsread)
  266. # Adding a new post - now the tracker shouldn't be up-to-date anymore.
  267. post = Post(content="Test Content")
  268. post.save(topic=topic, user=user)
  269. forumsread = ForumsRead.query.\
  270. filter(ForumsRead.user_id == user.id,
  271. ForumsRead.forum_id == topic.forum_id).first()
  272. # Test tracker length
  273. current_app.config["TRACKER_LENGTH"] = 0
  274. assert not topic.update_read(current_user, topic.forum, forumsread)
  275. current_app.config["TRACKER_LENGTH"] = 1
  276. assert topic.update_read(current_user, topic.forum, forumsread)
  277. # Test with logged out user
  278. logout_user()
  279. assert not current_user.is_authenticated()
  280. assert not topic.update_read(current_user, topic.forum, forumsread)
  281. def test_topic_url(topic):
  282. assert topic.url == "http://localhost:5000/topic/1-test-topic-normal"
  283. def test_topic_slug(topic):
  284. assert topic.slug == "test-topic-normal"
  285. def test_post_save(topic, user):
  286. """Tests the save post method."""
  287. post = Post(content="Test Content")
  288. post.save(topic=topic, user=user)
  289. assert post.content == "Test Content"
  290. post.content = "Test Edit Content"
  291. post.save()
  292. assert post.content == "Test Edit Content"
  293. assert topic.user.post_count == 2
  294. assert topic.post_count == 2
  295. assert topic.last_post == post
  296. assert topic.forum.post_count == 2
  297. def test_post_delete(topic):
  298. """Tests the delete post method with three different post types.
  299. The three types are:
  300. * First Post
  301. * A post between the first and last post (middle)
  302. * Last Post
  303. """
  304. post_middle = Post(content="Test Content Middle")
  305. post_middle.save(topic=topic, user=topic.user)
  306. post_last = Post(content="Test Content Last")
  307. post_last.save(topic=topic, user=topic.user)
  308. assert topic.post_count == 3
  309. assert topic.forum.post_count == 3
  310. assert topic.user.post_count == 3
  311. post_middle.delete()
  312. # Check the last posts
  313. assert topic.last_post == post_last
  314. assert topic.forum.last_post == post_last
  315. post_last.delete()
  316. # That was a bit trickier..
  317. assert topic.post_count == 1
  318. assert topic.forum.post_count == 1
  319. assert topic.user.post_count == 1
  320. assert topic.first_post_id == topic.last_post_id
  321. assert topic.forum.last_post_id == topic.last_post_id
  322. def test_report(topic, user):
  323. """Tests if the reports can be saved/edited and deleted with the
  324. implemented save and delete methods."""
  325. report = Report(reason="Test Report")
  326. report.save(user=user, post=topic.first_post)
  327. assert report.reason == "Test Report"
  328. report.reason = "Test Report Edited"
  329. report.save()
  330. assert report.reason == "Test Report Edited"
  331. report.delete()
  332. report = Report.query.filter_by(id=report.id).first()
  333. assert report is None
  334. def test_forumsread(topic, user):
  335. """Tests if the forumsread tracker can be saved/edited and deleted with the
  336. implemented save and delete methods."""
  337. forumsread = ForumsRead()
  338. forumsread.user_id = user.id
  339. forumsread.forum_id = topic.forum_id
  340. forumsread.last_read = datetime.utcnow()
  341. forumsread.save()
  342. assert forumsread is not None
  343. forumsread.delete()
  344. forumsread = ForumsRead.query.filter_by(forum_id=forumsread.forum_id).first()
  345. assert forumsread is None
  346. def test_topicsread(topic, user):
  347. """Tests if the topicsread trakcer can be saved/edited and deleted with the
  348. implemented save and delete methods."""
  349. topicsread = TopicsRead()
  350. topicsread.user_id = user.id
  351. topicsread.topic_id = topic.id
  352. topicsread.forum_id = topic.forum_id
  353. topicsread.last_read = datetime.utcnow()
  354. topicsread.save()
  355. assert topicsread is not None
  356. topicsread.delete()
  357. topicsread = TopicsRead.query.filter_by(topic_id=topicsread.topic_id).first()
  358. assert topicsread is None