3 class MessagesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/messages/inbox", :method => :get },
9 { :controller => "messages", :action => "inbox" }
12 { :path => "/messages/outbox", :method => :get },
13 { :controller => "messages", :action => "outbox" }
16 { :path => "/message/new/username", :method => :get },
17 { :controller => "messages", :action => "new", :display_name => "username" }
20 { :path => "/messages", :method => :post },
21 { :controller => "messages", :action => "create" }
24 { :path => "/messages/1", :method => :get },
25 { :controller => "messages", :action => "show", :id => "1" }
28 { :path => "/messages/1/mark", :method => :post },
29 { :controller => "messages", :action => "mark", :message_id => "1" }
32 { :path => "/messages/1/reply", :method => :get },
33 { :controller => "messages", :action => "reply", :message_id => "1" }
36 { :path => "/messages/1/reply", :method => :post },
37 { :controller => "messages", :action => "reply", :message_id => "1" }
40 { :path => "/messages/1", :method => :delete },
41 { :controller => "messages", :action => "destroy", :id => "1" }
46 # test fetching new message page when not logged in
48 # Check that the new message page requires us to login
50 get new_message_path(user)
51 assert_redirected_to login_path(:referer => new_message_path(user))
55 # test fetching new message page when logged in
57 # Login as a normal user
59 recipient_user = create(:user)
62 # Check that the new message page loads
63 get new_message_path(recipient_user)
64 assert_response :success
66 assert_select "title", "Send message | OpenStreetMap"
67 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
68 assert_select "form[action='/messages']", :count => 1 do
69 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
70 assert_select "input#message_title", :count => 1
71 assert_select "textarea#message_body", :count => 1
72 assert_select "input[type='submit'][value='Send']", :count => 1
77 # test fetching new message page with body and title
78 def test_new_get_with_params
79 # Login as a normal user
81 recipient_user = create(:user)
84 # Check that we can't send a message from a GET request
85 assert_difference "ActionMailer::Base.deliveries.size", 0 do
86 assert_difference "Message.count", 0 do
87 perform_enqueued_jobs do
88 get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
92 assert_response :success
94 assert_select "title", "Send message | OpenStreetMap"
95 assert_select "form[action='/messages']", :count => 1 do
96 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
97 assert_select "input#message_title", :count => 1 do
98 assert_select "[value='Test Message']"
100 assert_select "textarea#message_body", :text => "Test message body", :count => 1
101 assert_select "input[type='submit'][value='Send']", :count => 1
106 # test posting new message page with no body
107 def test_new_post_no_body
108 # Login as a normal user
110 recipient_user = create(:user)
113 # Check that the subject is preserved over errors
114 assert_difference "ActionMailer::Base.deliveries.size", 0 do
115 assert_difference "Message.count", 0 do
116 perform_enqueued_jobs do
117 post messages_path(:display_name => recipient_user.display_name,
118 :message => { :title => "Test Message", :body => "" })
122 assert_response :success
123 assert_template "new"
124 assert_select "title", "Send message | OpenStreetMap"
125 assert_select "form[action='/messages']", :count => 1 do
126 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
127 assert_select "input#message_title", :count => 1 do
128 assert_select "[value='Test Message']"
130 assert_select "textarea#message_body", :text => "", :count => 1
131 assert_select "input[type='submit'][value='Send']", :count => 1
136 # test posting new message page with no title
137 def test_new_post_no_title
138 # Login as a normal user
140 recipient_user = create(:user)
143 # Check that the body text is preserved over errors
144 assert_difference "ActionMailer::Base.deliveries.size", 0 do
145 assert_difference "Message.count", 0 do
146 perform_enqueued_jobs do
147 post messages_path(:display_name => recipient_user.display_name,
148 :message => { :title => "", :body => "Test message body" })
152 assert_response :success
153 assert_template "new"
154 assert_select "title", "Send message | OpenStreetMap"
155 assert_select "form[action='/messages']", :count => 1 do
156 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
157 assert_select "input#message_title", :count => 1 do
158 assert_select "[value='']"
160 assert_select "textarea#message_body", :text => "Test message body", :count => 1
161 assert_select "input[type='submit'][value='Send']", :count => 1
166 # test posting new message page sends message
167 def test_new_post_send
168 # Login as a normal user
170 recipient_user = create(:user)
173 # Check that sending a message works
174 assert_difference "ActionMailer::Base.deliveries.size", 1 do
175 assert_difference "Message.count", 1 do
176 perform_enqueued_jobs do
177 post messages_path(:display_name => recipient_user.display_name,
178 :message => { :title => "Test Message", :body => "Test message body" })
182 assert_redirected_to inbox_messages_path
183 assert_equal "Message sent", flash[:notice]
184 e = ActionMailer::Base.deliveries.first
185 assert_equal [recipient_user.email], e.to
186 assert_equal "[OpenStreetMap] Test Message", e.subject
187 assert_match(/Test message body/, e.text_part.decoded)
188 assert_match(/Test message body/, e.html_part.decoded)
189 assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
190 ActionMailer::Base.deliveries.clear
192 assert_equal user.id, m.from_user_id
193 assert_equal recipient_user.id, m.to_user_id
194 assert_in_delta Time.now.utc, m.sent_on, 2
195 assert_equal "Test Message", m.title
196 assert_equal "Test message body", m.body
197 assert_equal "markdown", m.body_format
199 # Asking to send a message with a bogus user name should fail
200 get new_message_path("non_existent_user")
201 assert_response :not_found
202 assert_template "users/no_such_user"
203 assert_select "h1", "The user non_existent_user does not exist"
207 # test the new action message limit
209 # Login as a normal user
211 recipient_user = create(:user)
214 # Check that sending a message fails when the message limit is hit
215 assert_no_difference "ActionMailer::Base.deliveries.size" do
216 assert_no_difference "Message.count" do
217 with_settings(:max_messages_per_hour => 0) do
218 perform_enqueued_jobs do
219 post messages_path(:display_name => recipient_user.display_name,
220 :message => { :title => "Test Message", :body => "Test message body" })
221 assert_response :success
222 assert_template "new"
223 assert_select ".alert.alert-danger", /wait a while/
231 # test the reply action
234 recipient_user = create(:user)
235 other_user = create(:user)
236 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
238 # Check that the message reply page requires us to login
239 get message_reply_path(message)
240 assert_redirected_to login_path(:referer => message_reply_path(message))
242 # Login as the wrong user
243 session_for(other_user)
245 # Check that we can't reply to somebody else's message
246 get message_reply_path(message)
247 assert_redirected_to login_path(:referer => message_reply_path(message))
248 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]
250 # Login as the right user
251 session_for(recipient_user)
253 # Check that the message reply page loads
254 get message_reply_path(message)
255 assert_response :success
256 assert_template "new"
257 assert_select "title", "Re: #{message.title} | OpenStreetMap"
258 assert_select "form[action='/messages']", :count => 1 do
259 assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
260 assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
261 assert_select "textarea#message_body", :count => 1
262 assert_select "input[type='submit'][value='Send']", :count => 1
264 assert Message.find(message.id).message_read
266 # Login as the sending user
269 # Check that the message reply page loads
270 get message_reply_path(message)
271 assert_response :success
272 assert_template "new"
273 assert_select "title", "Re: #{message.title} | OpenStreetMap"
274 assert_select "form[action='/messages']", :count => 1 do
275 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
276 assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
277 assert_select "textarea#message_body", :count => 1
278 assert_select "input[type='submit'][value='Send']", :count => 1
281 # Asking to reply to a message with a bogus ID should fail
282 get message_reply_path(99999)
283 assert_response :not_found
284 assert_template "no_such_message"
288 # test the show action
291 recipient_user = create(:user)
292 other_user = create(:user)
293 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
295 # Check that the show message page requires us to login
296 get message_path(message)
297 assert_redirected_to login_path(:referer => message_path(message))
299 # Login as the wrong user
300 session_for(other_user)
302 # Check that we can't read the message
303 get message_path(message)
304 assert_redirected_to login_path(:referer => message_path(message))
305 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]
307 # Login as the message sender
310 # Check that the message sender can read the message and that Reply button is available
311 get message_path(message)
312 assert_response :success
313 assert_template "show"
314 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
315 assert_select "a.btn.btn-primary", :text => "Reply"
316 assert_not Message.find(message.id).message_read
318 # Login as the message recipient
319 session_for(recipient_user)
321 # Check that the message recipient can read the message and that Reply button is available
322 get message_path(message)
323 assert_response :success
324 assert_template "show"
325 assert_select "a[href='#{user_path user}']", :text => user.display_name
326 assert_select "a.btn.btn-primary", :text => "Reply"
327 assert Message.find(message.id).message_read
329 # Asking to read a message with a bogus ID should fail
330 get message_path(99999)
331 assert_response :not_found
332 assert_template "no_such_message"
336 # test the inbox action
339 read_message = create(:message, :read, :recipient => user)
340 # Check that the inbox page requires us to login
341 get inbox_messages_path
342 assert_redirected_to login_path(:referer => inbox_messages_path)
347 # Check that we can view our inbox when logged in
348 get inbox_messages_path
349 assert_response :success
350 assert_template "inbox"
351 assert_select ".content-inner > table.messages-table > tbody", :count => 1 do
352 assert_select "tr", :count => 1
353 assert_select "tr#inbox-#{read_message.id}", :count => 1 do
354 assert_select "a[href='#{user_path read_message.sender}']", :text => read_message.sender.display_name
355 assert_select "a[href='#{message_path read_message}']", :text => read_message.title
361 # test the outbox action
364 message = create(:message, :sender => user)
366 # Check that the outbox page requires us to login
367 get outbox_messages_path
368 assert_redirected_to login_path(:referer => outbox_messages_path)
373 # Check that we can view our outbox when logged in
374 get outbox_messages_path
375 assert_response :success
376 assert_template "outbox"
377 assert_select ".content-inner > table.messages-table > tbody", :count => 1 do
378 assert_select "tr", :count => 1
379 assert_select "tr#outbox-#{message.id}", :count => 1 do
380 assert_select "a[href='#{user_path message.recipient}']", :text => message.recipient.display_name
381 assert_select "a[href='#{message_path message}']", :text => message.title
387 # test the mark action
389 sender_user = create(:user)
390 recipient_user = create(:user)
391 other_user = create(:user)
392 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
394 # Check that the marking a message requires us to login
395 post message_mark_path(message)
396 assert_response :forbidden
398 # Login as a user with no messages
399 session_for(other_user)
401 # Check that marking a message we didn't send or receive fails
402 post message_mark_path(message)
403 assert_response :not_found
404 assert_template "no_such_message"
406 # Login as the message sender_user
407 session_for(sender_user)
409 # Check that marking a message we sent fails
410 post message_mark_path(message)
411 assert_response :not_found
412 assert_template "no_such_message"
414 # Login as the message recipient_user
415 session_for(recipient_user)
417 # Check that the marking a message read works
418 post message_mark_path(message, :mark => "read")
419 assert_redirected_to inbox_messages_path
420 assert Message.find(message.id).message_read
422 # Check that the marking a message unread works
423 post message_mark_path(message, :mark => "unread")
424 assert_redirected_to inbox_messages_path
425 assert_not Message.find(message.id).message_read
427 # Check that the marking a message read works and redirects to inbox from the message page
428 post message_mark_path(message, :mark => "read"), :headers => { :referer => message_path(message) }
429 assert_redirected_to inbox_messages_path
430 assert Message.find(message.id).message_read
432 # Check that the marking a message unread works and redirects to inbox from the message page
433 post message_mark_path(message, :mark => "unread"), :headers => { :referer => message_path(message) }
434 assert_redirected_to inbox_messages_path
435 assert_not Message.find(message.id).message_read
437 # Asking to mark a message with a bogus ID should fail
438 post message_mark_path(99999)
439 assert_response :not_found
440 assert_template "no_such_message"
444 # test the mark action for messages from muted users
446 sender_user = create(:user)
447 recipient_user = create(:user)
448 create(:user_mute, :owner => recipient_user, :subject => sender_user)
449 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
451 session_for(recipient_user)
453 # Check that the marking a message read works
454 post message_mark_path(message, :mark => "read")
455 assert_redirected_to muted_messages_path
456 assert Message.find(message.id).message_read
458 # Check that the marking a message unread works
459 post message_mark_path(message, :mark => "unread")
460 assert_redirected_to muted_messages_path
461 assert_not Message.find(message.id).message_read
465 # test the destroy action
468 second_user = create(:user)
469 other_user = create(:user)
470 read_message = create(:message, :read, :recipient => user, :sender => second_user)
471 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
473 # Check that destroying a message requires us to login
474 delete message_path(read_message)
475 assert_response :forbidden
477 # Login as a user with no messages
478 session_for(other_user)
480 # Check that destroying a message we didn't send or receive fails
481 delete message_path(read_message)
482 assert_response :not_found
483 assert_template "no_such_message"
485 # Login as the message recipient_user
488 # Check that the destroy a received message works
489 delete message_path(read_message)
490 assert_redirected_to inbox_messages_path
491 assert_equal "Message deleted", flash[:notice]
492 m = Message.find(read_message.id)
493 assert m.from_user_visible
494 assert_not m.to_user_visible
496 # Check that the destroying a sent message works
497 delete message_path(sent_message, :referer => outbox_messages_path)
498 assert_redirected_to outbox_messages_path
499 assert_equal "Message deleted", flash[:notice]
500 m = Message.find(sent_message.id)
501 assert_not m.from_user_visible
502 assert m.to_user_visible
504 # Asking to destroy a message with a bogus ID should fail
505 delete message_path(99999)
506 assert_response :not_found
507 assert_template "no_such_message"