PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/post_test.rb

https://github.com/newrooky/lftwb
Ruby | 103 lines | 81 code | 21 blank | 1 comment | 1 complexity | fbebb6b63d6010a137014194b701259a MD5 | raw file
Possible License(s): MIT
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. class PostTest < Test::Unit::TestCase
  3. context "A Post instance" do
  4. should "select posts" do
  5. assert_equal [posts(:pdi), posts(:pdi_reply), posts(:pdi_rebuttal)], topics(:pdi).posts
  6. end
  7. should "find topic" do
  8. assert_equal topics(:pdi), posts(:pdi_reply).topic
  9. end
  10. should "require body for post" do
  11. p = topics(:pdi).posts.build
  12. p.valid?
  13. assert p.errors.on(:body)
  14. end
  15. should "create reply" do
  16. counts = lambda { [Post.count, forums(:rails).posts_count, users(:admin).posts_count, topics(:pdi).posts_count] }
  17. equal = lambda { [forums(:rails).topics_count] }
  18. old_counts = counts.call
  19. old_equal = equal.call
  20. p = create_post topics(:pdi), :body => 'blah'
  21. assert_valid p
  22. [forums(:rails), users(:admin), topics(:pdi)].each &:reload
  23. assert_equal old_counts.collect { |n| n + 1}, counts.call
  24. assert_equal old_equal, equal.call
  25. end
  26. should "update cached data" do
  27. p = create_post topics(:pdi), :body => 'ok, ill get right on it'
  28. assert_valid p
  29. topics(:pdi).reload
  30. assert_equal p.id, topics(:pdi).last_post_id
  31. assert_equal p.user_id, topics(:pdi).replied_by
  32. assert_equal p.created_at.to_i, topics(:pdi).replied_at.to_i
  33. end
  34. should "delete last post and fix topic cached data" do
  35. posts(:pdi_rebuttal).destroy
  36. assert_equal posts(:pdi_reply), topics(:pdi).last_post
  37. assert_equal posts(:pdi_reply).user_id, topics(:pdi).replied_by
  38. assert_equal posts(:pdi_reply).created_at.to_i, topics(:pdi).replied_at.to_i
  39. end
  40. should "delete only remaining post and clear topic" do
  41. posts(:sticky).destroy
  42. assert_raises ActiveRecord::RecordNotFound do
  43. topics(:sticky)
  44. end
  45. end
  46. should "create reply and set forum from topic" do
  47. p = create_post topics(:pdi), :body => 'blah'
  48. assert_equal topics(:pdi).forum_id, p.forum_id
  49. end
  50. should "delete reply" do
  51. counts = lambda { [Post.count, forums(:rails).posts_count, users(:sam).posts_count, topics(:pdi).posts_count] }
  52. equal = lambda { [forums(:rails).topics_count] }
  53. old_counts = counts.call
  54. old_equal = equal.call
  55. posts(:pdi_reply).destroy
  56. [forums(:rails), users(:sam), topics(:pdi)].each &:reload
  57. assert_equal old_counts.collect { |n| n - 1}, counts.call
  58. assert_equal old_equal, equal.call
  59. end
  60. should "edit own post" do
  61. assert posts(:shield).editable_by?(users(:sam))
  62. end
  63. should "edit post as admin" do
  64. assert posts(:shield).editable_by?(users(:admin))
  65. end
  66. should "edit post as moderator" do
  67. assert posts(:pdi).editable_by?(users(:sam))
  68. end
  69. should "not edit post in own topic" do
  70. assert !posts(:shield_reply).editable_by?(users(:sam))
  71. end
  72. end
  73. protected
  74. def create_post(topic, options = {})
  75. returning topic.posts.build(options) do |p|
  76. p.user = users(:admin)
  77. p.save
  78. # post should inherit the forum from the topic
  79. assert_equal p.topic.forum, p.forum
  80. end
  81. end
  82. end