3 class MessagesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/messages/new/username", :method => :get },
9 { :controller => "messages", :action => "new", :display_name => "username" }
12 { :path => "/messages", :method => :post },
13 { :controller => "messages", :action => "create" }
16 { :path => "/messages/1", :method => :get },
17 { :controller => "messages", :action => "show", :id => "1" }
20 { :path => "/messages/1/mark", :method => :post },
21 { :controller => "messages", :action => "mark", :message_id => "1" }
24 { :path => "/messages/1/reply", :method => :get },
25 { :controller => "messages", :action => "reply", :message_id => "1" }
28 { :path => "/messages/1/reply", :method => :post },
29 { :controller => "messages", :action => "reply", :message_id => "1" }
32 { :path => "/messages/1", :method => :delete },
33 { :controller => "messages", :action => "destroy", :id => "1" }
38 # test fetching new message page when not logged in
40 # Check that the new message page requires us to login
42 get new_message_path(user)
43 assert_redirected_to login_path(:referer => new_message_path(user))
47 # test fetching new message page when logged in
49 # Login as a normal user
51 recipient_user = create(:user)
54 # Check that the new message page loads
55 get new_message_path(recipient_user)
56 assert_response :success
58 assert_select "title", "Send message | OpenStreetMap"
59 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
60 assert_select "form[action='/messages']", :count => 1 do
61 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
62 assert_select "input#message_title", :count => 1
63 assert_select "textarea#message_body", :count => 1
64 assert_select "input[type='submit'][value='Send']", :count => 1
69 # test fetching new message page with body and title
70 def test_new_get_with_params
71 # Login as a normal user
73 recipient_user = create(:user)
76 # Check that we can't send a message from a GET request
77 assert_difference "ActionMailer::Base.deliveries.size", 0 do
78 assert_difference "Message.count", 0 do
79 perform_enqueued_jobs do
80 get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
84 assert_response :success
86 assert_select "title", "Send message | OpenStreetMap"
87 assert_select "form[action='/messages']", :count => 1 do
88 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
89 assert_select "input#message_title", :count => 1 do
90 assert_select "[value='Test Message']"
92 assert_select "textarea#message_body", :text => "Test message body", :count => 1
93 assert_select "input[type='submit'][value='Send']", :count => 1
98 # test posting new message page with no body
99 def test_new_post_no_body
100 # Login as a normal user
102 recipient_user = create(:user)
105 # Check that the subject is preserved over errors
106 assert_difference "ActionMailer::Base.deliveries.size", 0 do
107 assert_difference "Message.count", 0 do
108 perform_enqueued_jobs do
109 post messages_path(:display_name => recipient_user.display_name,
110 :message => { :title => "Test Message", :body => "" })
114 assert_response :success
115 assert_template "new"
116 assert_select "title", "Send message | OpenStreetMap"
117 assert_select "form[action='/messages']", :count => 1 do
118 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
119 assert_select "input#message_title", :count => 1 do
120 assert_select "[value='Test Message']"
122 assert_select "textarea#message_body", :text => "", :count => 1
123 assert_select "input[type='submit'][value='Send']", :count => 1
128 # test posting new message page with no title
129 def test_new_post_no_title
130 # Login as a normal user
132 recipient_user = create(:user)
135 # Check that the body text is preserved over errors
136 assert_difference "ActionMailer::Base.deliveries.size", 0 do
137 assert_difference "Message.count", 0 do
138 perform_enqueued_jobs do
139 post messages_path(:display_name => recipient_user.display_name,
140 :message => { :title => "", :body => "Test message body" })
144 assert_response :success
145 assert_template "new"
146 assert_select "title", "Send message | OpenStreetMap"
147 assert_select "form[action='/messages']", :count => 1 do
148 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
149 assert_select "input#message_title", :count => 1 do
150 assert_select "[value='']"
152 assert_select "textarea#message_body", :text => "Test message body", :count => 1
153 assert_select "input[type='submit'][value='Send']", :count => 1
158 # test posting new message page sends message
159 def test_new_post_send
160 # Login as a normal user
162 recipient_user = create(:user)
165 # Check that sending a message works
166 assert_difference "ActionMailer::Base.deliveries.size", 1 do
167 assert_difference "Message.count", 1 do
168 perform_enqueued_jobs do
169 post messages_path(:display_name => recipient_user.display_name,
170 :message => { :title => "Test Message", :body => "Test message body" })
174 assert_redirected_to messages_inbox_path
175 assert_equal "Message sent", flash[:notice]
176 e = ActionMailer::Base.deliveries.first
177 assert_equal [recipient_user.email], e.to
178 assert_equal "[OpenStreetMap] Test Message", e.subject
179 assert_match(/Test message body/, e.text_part.decoded)
180 assert_match(/Test message body/, e.html_part.decoded)
181 assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
182 ActionMailer::Base.deliveries.clear
184 assert_equal user.id, m.from_user_id
185 assert_equal recipient_user.id, m.to_user_id
186 assert_in_delta Time.now.utc, m.sent_on, 2
187 assert_equal "Test Message", m.title
188 assert_equal "Test message body", m.body
189 assert_equal "markdown", m.body_format
191 # Asking to send a message with a bogus user name should fail
192 get new_message_path("non_existent_user")
193 assert_response :not_found
194 assert_template "users/no_such_user"
195 assert_select "h1", "The user non_existent_user does not exist"
199 # test the new action message limit
201 # Login as a normal user
203 recipient_user = create(:user)
206 # Check that sending a message fails when the message limit is hit
207 assert_no_difference "ActionMailer::Base.deliveries.size" do
208 assert_no_difference "Message.count" do
209 with_settings(:max_messages_per_hour => 0) do
210 perform_enqueued_jobs do
211 post messages_path(:display_name => recipient_user.display_name,
212 :message => { :title => "Test Message", :body => "Test message body" })
213 assert_response :success
214 assert_template "new"
215 assert_select ".alert.alert-danger", /wait a while/
223 # test the reply action
226 recipient_user = create(:user)
227 other_user = create(:user)
228 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
230 # Check that the message reply page requires us to login
231 get message_reply_path(message)
232 assert_redirected_to login_path(:referer => message_reply_path(message))
234 # Login as the wrong user
235 session_for(other_user)
237 # Check that we can't reply to somebody else's message
238 get message_reply_path(message)
239 assert_redirected_to login_path(:referer => message_reply_path(message))
240 assert_equal "You are logged in as '#{other_user.display_name}' but the message you have asked to reply to was not sent to that user. Please log in as the correct user in order to reply.", flash[:notice]
242 # Login as the right user
243 session_for(recipient_user)
245 # Check that the message reply page loads
246 get message_reply_path(message)
247 assert_response :success
248 assert_template "new"
249 assert_select "title", "Re: #{message.title} | OpenStreetMap"
250 assert_select "form[action='/messages']", :count => 1 do
251 assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
252 assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
253 assert_select "textarea#message_body", :count => 1
254 assert_select "input[type='submit'][value='Send']", :count => 1
256 assert Message.find(message.id).message_read
258 # Login as the sending user
261 # Check that the message reply page loads
262 get message_reply_path(message)
263 assert_response :success
264 assert_template "new"
265 assert_select "title", "Re: #{message.title} | OpenStreetMap"
266 assert_select "form[action='/messages']", :count => 1 do
267 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
268 assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
269 assert_select "textarea#message_body", :count => 1
270 assert_select "input[type='submit'][value='Send']", :count => 1
273 # Asking to reply to a message with a bogus ID should fail
274 get message_reply_path(99999)
275 assert_response :not_found
276 assert_template "no_such_message"
280 # test the show action
283 recipient_user = create(:user)
284 other_user = create(:user)
285 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
287 # Check that the show message page requires us to login
288 get message_path(message)
289 assert_redirected_to login_path(:referer => message_path(message))
291 # Login as the wrong user
292 session_for(other_user)
294 # Check that we can't read the message
295 get message_path(message)
296 assert_redirected_to login_path(:referer => message_path(message))
297 assert_equal "You are logged in as '#{other_user.display_name}' but the message you have asked to read was not sent by or to that user. Please log in as the correct user in order to read it.", flash[:notice]
299 # Login as the message sender
302 # Check that the message sender can read the message and that Reply button is available
303 get message_path(message)
304 assert_response :success
305 assert_template "show"
306 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
307 assert_select "a.btn.btn-primary", :text => "Reply"
308 assert_not Message.find(message.id).message_read
310 # Login as the message recipient
311 session_for(recipient_user)
313 # Check that the message recipient can read the message and that Reply button is available
314 get message_path(message)
315 assert_response :success
316 assert_template "show"
317 assert_select "a[href='#{user_path user}']", :text => user.display_name
318 assert_select "a.btn.btn-primary", :text => "Reply"
319 assert Message.find(message.id).message_read
321 # Asking to read a message with a bogus ID should fail
322 get message_path(99999)
323 assert_response :not_found
324 assert_template "no_such_message"
328 # test the mark action
330 sender_user = create(:user)
331 recipient_user = create(:user)
332 other_user = create(:user)
333 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
335 # Check that the marking a message requires us to login
336 post message_mark_path(message)
337 assert_response :forbidden
339 # Login as a user with no messages
340 session_for(other_user)
342 # Check that marking a message we didn't send or receive fails
343 post message_mark_path(message)
344 assert_response :not_found
345 assert_template "no_such_message"
347 # Login as the message sender_user
348 session_for(sender_user)
350 # Check that marking a message we sent fails
351 post message_mark_path(message)
352 assert_response :not_found
353 assert_template "no_such_message"
355 # Login as the message recipient_user
356 session_for(recipient_user)
358 # Check that the marking a message read works
359 post message_mark_path(message, :mark => "read")
360 assert_redirected_to messages_inbox_path
361 assert Message.find(message.id).message_read
363 # Check that the marking a message unread works
364 post message_mark_path(message, :mark => "unread")
365 assert_redirected_to messages_inbox_path
366 assert_not Message.find(message.id).message_read
368 # Check that the marking a message read works and redirects to inbox from the message page
369 post message_mark_path(message, :mark => "read"), :headers => { :referer => message_path(message) }
370 assert_redirected_to messages_inbox_path
371 assert Message.find(message.id).message_read
373 # Check that the marking a message unread works and redirects to inbox from the message page
374 post message_mark_path(message, :mark => "unread"), :headers => { :referer => message_path(message) }
375 assert_redirected_to messages_inbox_path
376 assert_not Message.find(message.id).message_read
378 # Asking to mark a message with a bogus ID should fail
379 post message_mark_path(99999)
380 assert_response :not_found
381 assert_template "no_such_message"
385 # test the mark action for messages from muted users
387 sender_user = create(:user)
388 recipient_user = create(:user)
389 create(:user_mute, :owner => recipient_user, :subject => sender_user)
390 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
392 session_for(recipient_user)
394 # Check that the marking a message read works
395 post message_mark_path(message, :mark => "read")
396 assert_redirected_to messages_muted_inbox_path
397 assert Message.find(message.id).message_read
399 # Check that the marking a message unread works
400 post message_mark_path(message, :mark => "unread")
401 assert_redirected_to messages_muted_inbox_path
402 assert_not Message.find(message.id).message_read
406 # test the destroy action
409 second_user = create(:user)
410 other_user = create(:user)
411 read_message = create(:message, :read, :recipient => user, :sender => second_user)
412 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
414 # Check that destroying a message requires us to login
415 delete message_path(read_message)
416 assert_response :forbidden
418 # Login as a user with no messages
419 session_for(other_user)
421 # Check that destroying a message we didn't send or receive fails
422 delete message_path(read_message)
423 assert_response :not_found
424 assert_template "no_such_message"
426 # Login as the message recipient_user
429 # Check that the destroy a received message works
430 delete message_path(read_message)
431 assert_redirected_to messages_inbox_path
432 assert_equal "Message deleted", flash[:notice]
433 m = Message.find(read_message.id)
434 assert m.from_user_visible
435 assert_not m.to_user_visible
437 # Check that the destroying a sent message works
438 delete message_path(sent_message, :referer => messages_outbox_path)
439 assert_redirected_to messages_outbox_path
440 assert_equal "Message deleted", flash[:notice]
441 m = Message.find(sent_message.id)
442 assert_not m.from_user_visible
443 assert m.to_user_visible
445 # Asking to destroy a message with a bogus ID should fail
446 delete message_path(99999)
447 assert_response :not_found
448 assert_template "no_such_message"