test_forum_models.py 17 KB

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