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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class MessagesControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :users, :email_addresses, :user_preferences, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
  20. def setup
  21. User.current = nil
  22. end
  23. def test_show
  24. get :show, :params => {
  25. :board_id => 1,
  26. :id => 1
  27. }
  28. assert_response :success
  29. assert_select 'h2', :text => 'First post'
  30. end
  31. def test_show_should_contain_reply_field_tags_for_quoting
  32. @request.session[:user_id] = 2
  33. get :show, :params => {
  34. :board_id => 1,
  35. :id => 1
  36. }
  37. assert_response :success
  38. # tags required by MessagesController#quote
  39. assert_select 'input#message_subject'
  40. assert_select 'textarea#message_content'
  41. assert_select 'div#reply'
  42. end
  43. def test_show_with_pagination
  44. message = Message.find(1)
  45. assert_difference 'Message.count', 30 do
  46. 30.times do
  47. message.children << Message.new(:subject => 'Reply',
  48. :content => 'Reply body',
  49. :author_id => 2,
  50. :board_id => 1)
  51. end
  52. end
  53. reply_ids = message.children.map(&:id).sort
  54. get :show, :params => {
  55. :board_id => 1,
  56. :id => 1,
  57. :r => reply_ids.last
  58. }
  59. assert_response :success
  60. assert_select 'a[href=?]', "/boards/1/topics/1?r=#{reply_ids.last}#message-#{reply_ids.last}"
  61. assert_select 'a[href=?]', "/boards/1/topics/1?r=#{reply_ids.first}#message-#{reply_ids.first}", 0
  62. end
  63. def test_show_with_reply_permission
  64. @request.session[:user_id] = 2
  65. get :show, :params => {
  66. :board_id => 1,
  67. :id => 1
  68. }
  69. assert_response :success
  70. assert_select 'div#reply textarea#message_content'
  71. end
  72. def test_show_message_not_found
  73. get :show, :params => {
  74. :board_id => 1,
  75. :id => 99999
  76. }
  77. assert_response 404
  78. end
  79. def test_show_message_from_invalid_board_should_respond_with_404
  80. get :show, :params => {
  81. :board_id => 999,
  82. :id => 1
  83. }
  84. assert_response 404
  85. end
  86. def test_get_new
  87. @request.session[:user_id] = 2
  88. get :new, :params => {
  89. :board_id => 1
  90. }
  91. assert_response :success
  92. assert_select 'input[name=?]', 'message[subject]'
  93. end
  94. def test_get_new_with_invalid_board
  95. @request.session[:user_id] = 2
  96. get :new, :params => {
  97. :board_id => 99
  98. }
  99. assert_response 404
  100. end
  101. def test_post_new
  102. @request.session[:user_id] = 2
  103. ActionMailer::Base.deliveries.clear
  104. with_settings :notified_events => %w(message_posted) do
  105. post :new, :params => {
  106. :board_id => 1,
  107. :message => {
  108. :subject => 'Test created message',
  109. :content => 'Message body'
  110. }
  111. }
  112. end
  113. assert_equal I18n.t(:notice_successful_create), flash[:notice]
  114. message = Message.find_by_subject('Test created message')
  115. assert_not_nil message
  116. assert_redirected_to "/boards/1/topics/#{message.to_param}"
  117. assert_equal 'Message body', message.content
  118. assert_equal 2, message.author_id
  119. assert_equal 1, message.board_id
  120. mails = ActionMailer::Base.deliveries
  121. assert_not_empty mails
  122. mails.each do |mail|
  123. assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
  124. assert_mail_body_match 'Message body', mail
  125. end
  126. # author
  127. assert_equal ['jsmith@somenet.foo'], mails[0].bcc
  128. # project member
  129. assert_equal ['dlopper@somenet.foo'], mails[1].bcc
  130. end
  131. def test_get_edit
  132. @request.session[:user_id] = 2
  133. get :edit, :params => {
  134. :board_id => 1,
  135. :id => 1
  136. }
  137. assert_response :success
  138. assert_select 'input[name=?][value=?]', 'message[subject]', 'First post'
  139. end
  140. def test_post_edit
  141. @request.session[:user_id] = 2
  142. post :edit, :params => {
  143. :board_id => 1,
  144. :id => 1,
  145. :message => {
  146. :subject => 'New subject',
  147. :content => 'New body'
  148. }
  149. }
  150. assert_redirected_to '/boards/1/topics/1'
  151. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  152. message = Message.find(1)
  153. assert_equal 'New subject', message.subject
  154. assert_equal 'New body', message.content
  155. end
  156. def test_post_edit_sticky_and_locked
  157. @request.session[:user_id] = 2
  158. post :edit, :params => {
  159. :board_id => 1,
  160. :id => 1,
  161. :message => {
  162. :subject => 'New subject',
  163. :content => 'New body',
  164. :locked => '1',
  165. :sticky => '1'
  166. }
  167. }
  168. assert_redirected_to '/boards/1/topics/1'
  169. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  170. message = Message.find(1)
  171. assert_equal true, message.sticky?
  172. assert_equal true, message.locked?
  173. end
  174. def test_post_edit_should_allow_to_change_board
  175. @request.session[:user_id] = 2
  176. post :edit, :params => {
  177. :board_id => 1,
  178. :id => 1,
  179. :message => {
  180. :subject => 'New subject',
  181. :content => 'New body',
  182. :board_id => 2
  183. }
  184. }
  185. assert_redirected_to '/boards/2/topics/1'
  186. message = Message.find(1)
  187. assert_equal Board.find(2), message.board
  188. end
  189. def test_reply
  190. @request.session[:user_id] = 2
  191. post :reply, :params => {
  192. :board_id => 1,
  193. :id => 1,
  194. :reply => {
  195. :content => 'This is a test reply',
  196. :subject => 'Test reply'
  197. }
  198. }
  199. reply = Message.order('id DESC').first
  200. assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
  201. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  202. assert Message.find_by_subject('Test reply')
  203. end
  204. def test_destroy_topic
  205. @request.session[:user_id] = 2
  206. assert_difference 'Message.count', -3 do
  207. post :destroy, :params => {
  208. :board_id => 1,
  209. :id => 1
  210. }
  211. end
  212. assert_redirected_to '/projects/ecookbook/boards/1'
  213. assert_equal I18n.t(:notice_successful_delete), flash[:notice]
  214. assert_nil Message.find_by_id(1)
  215. end
  216. def test_destroy_reply
  217. @request.session[:user_id] = 2
  218. assert_difference 'Message.count', -1 do
  219. post :destroy, :params => {
  220. :board_id => 1,
  221. :id => 2
  222. }
  223. end
  224. assert_redirected_to '/boards/1/topics/1?r=2'
  225. assert_equal I18n.t(:notice_successful_delete), flash[:notice]
  226. assert_nil Message.find_by_id(2)
  227. end
  228. def test_quote
  229. @request.session[:user_id] = 2
  230. get :quote, :params => {
  231. :board_id => 1,
  232. :id => 3
  233. },
  234. :xhr => true
  235. assert_response :success
  236. assert_equal 'text/javascript', response.content_type
  237. assert_include 'RE: First post', response.body
  238. assert_include '> An other reply', response.body
  239. end
  240. def test_preview_new
  241. @request.session[:user_id] = 2
  242. post :preview, :params => {
  243. :board_id => 1,
  244. :message => {
  245. :subject => ""
  246. },
  247. :text => "Previewed text"
  248. }
  249. assert_response :success
  250. assert_include 'Previewed text', response.body
  251. end
  252. def test_preview_edit
  253. @request.session[:user_id] = 2
  254. post :preview, :params => {
  255. :id => 4,
  256. :board_id => 1,
  257. :message => {
  258. :subject => "",
  259. },
  260. :text => "Previewed text"
  261. }
  262. assert_response :success
  263. assert_include 'Previewed text', response.body
  264. end
  265. end