You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messages_controller_test.rb 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. require 'messages_controller'
  19. # Re-raise errors caught by the controller.
  20. class MessagesController; def rescue_action(e) raise e end; end
  21. class MessagesControllerTest < ActionController::TestCase
  22. fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
  23. def setup
  24. @controller = MessagesController.new
  25. @request = ActionController::TestRequest.new
  26. @response = ActionController::TestResponse.new
  27. User.current = nil
  28. end
  29. def test_show
  30. get :show, :board_id => 1, :id => 1
  31. assert_response :success
  32. assert_template 'show'
  33. assert_not_nil assigns(:board)
  34. assert_not_nil assigns(:project)
  35. assert_not_nil assigns(:topic)
  36. end
  37. def test_show_should_contain_reply_field_tags_for_quoting
  38. @request.session[:user_id] = 2
  39. get :show, :board_id => 1, :id => 1
  40. assert_response :success
  41. # tags required by MessagesController#quote
  42. assert_tag 'input', :attributes => {:id => 'message_subject'}
  43. assert_tag 'textarea', :attributes => {:id => 'message_content'}
  44. assert_tag 'div', :attributes => {:id => 'reply'}
  45. end
  46. def test_show_with_pagination
  47. message = Message.find(1)
  48. assert_difference 'Message.count', 30 do
  49. 30.times do
  50. message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
  51. end
  52. end
  53. get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
  54. assert_response :success
  55. assert_template 'show'
  56. replies = assigns(:replies)
  57. assert_not_nil replies
  58. assert !replies.include?(message.children.first(:order => 'id'))
  59. assert replies.include?(message.children.last(:order => 'id'))
  60. end
  61. def test_show_with_reply_permission
  62. @request.session[:user_id] = 2
  63. get :show, :board_id => 1, :id => 1
  64. assert_response :success
  65. assert_template 'show'
  66. assert_tag :div, :attributes => { :id => 'reply' },
  67. :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
  68. end
  69. def test_show_message_not_found
  70. get :show, :board_id => 1, :id => 99999
  71. assert_response 404
  72. end
  73. def test_get_new
  74. @request.session[:user_id] = 2
  75. get :new, :board_id => 1
  76. assert_response :success
  77. assert_template 'new'
  78. end
  79. def test_post_new
  80. @request.session[:user_id] = 2
  81. ActionMailer::Base.deliveries.clear
  82. Setting.notified_events = ['message_posted']
  83. post :new, :board_id => 1,
  84. :message => { :subject => 'Test created message',
  85. :content => 'Message body'}
  86. message = Message.find_by_subject('Test created message')
  87. assert_not_nil message
  88. assert_redirected_to "/boards/1/topics/#{message.to_param}"
  89. assert_equal 'Message body', message.content
  90. assert_equal 2, message.author_id
  91. assert_equal 1, message.board_id
  92. mail = ActionMailer::Base.deliveries.last
  93. assert_not_nil mail
  94. assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
  95. assert_mail_body_match 'Message body', mail
  96. # author
  97. assert mail.bcc.include?('jsmith@somenet.foo')
  98. # project member
  99. assert mail.bcc.include?('dlopper@somenet.foo')
  100. end
  101. def test_get_edit
  102. @request.session[:user_id] = 2
  103. get :edit, :board_id => 1, :id => 1
  104. assert_response :success
  105. assert_template 'edit'
  106. end
  107. def test_post_edit
  108. @request.session[:user_id] = 2
  109. post :edit, :board_id => 1, :id => 1,
  110. :message => { :subject => 'New subject',
  111. :content => 'New body'}
  112. assert_redirected_to '/boards/1/topics/1'
  113. message = Message.find(1)
  114. assert_equal 'New subject', message.subject
  115. assert_equal 'New body', message.content
  116. end
  117. def test_post_edit_sticky_and_locked
  118. @request.session[:user_id] = 2
  119. post :edit, :board_id => 1, :id => 1,
  120. :message => { :subject => 'New subject',
  121. :content => 'New body',
  122. :locked => '1',
  123. :sticky => '1'}
  124. assert_redirected_to '/boards/1/topics/1'
  125. message = Message.find(1)
  126. assert_equal true, message.sticky?
  127. assert_equal true, message.locked?
  128. end
  129. def test_post_edit_should_allow_to_change_board
  130. @request.session[:user_id] = 2
  131. post :edit, :board_id => 1, :id => 1,
  132. :message => { :subject => 'New subject',
  133. :content => 'New body',
  134. :board_id => 2}
  135. assert_redirected_to '/boards/2/topics/1'
  136. message = Message.find(1)
  137. assert_equal Board.find(2), message.board
  138. end
  139. def test_reply
  140. @request.session[:user_id] = 2
  141. post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
  142. reply = Message.find(:first, :order => 'id DESC')
  143. assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
  144. assert Message.find_by_subject('Test reply')
  145. end
  146. def test_destroy_topic
  147. @request.session[:user_id] = 2
  148. assert_difference 'Message.count', -3 do
  149. post :destroy, :board_id => 1, :id => 1
  150. end
  151. assert_redirected_to '/projects/ecookbook/boards/1'
  152. assert_nil Message.find_by_id(1)
  153. end
  154. def test_destroy_reply
  155. @request.session[:user_id] = 2
  156. assert_difference 'Message.count', -1 do
  157. post :destroy, :board_id => 1, :id => 2
  158. end
  159. assert_redirected_to '/boards/1/topics/1?r=2'
  160. assert_nil Message.find_by_id(2)
  161. end
  162. def test_quote
  163. @request.session[:user_id] = 2
  164. xhr :get, :quote, :board_id => 1, :id => 3
  165. assert_response :success
  166. assert_select_rjs :show, 'reply'
  167. end
  168. def test_preview_new
  169. @request.session[:user_id] = 2
  170. post :preview,
  171. :board_id => 1,
  172. :message => {:subject => "", :content => "Previewed text"}
  173. assert_response :success
  174. assert_template 'common/_preview'
  175. end
  176. def test_preview_edit
  177. @request.session[:user_id] = 2
  178. post :preview,
  179. :id => 4,
  180. :board_id => 1,
  181. :message => {:subject => "", :content => "Previewed text"}
  182. assert_response :success
  183. assert_template 'common/_preview'
  184. end
  185. end