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 "form[action='/messages']", :count => 1 do
68 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
69 assert_select "input#message_title", :count => 1
70 assert_select "textarea#message_body", :count => 1
71 assert_select "input[type='submit'][value='Send']", :count => 1
76 # test fetching new message page with body and title
77 def test_new_get_with_params
78 # Login as a normal user
80 recipient_user = create(:user)
83 # Check that we can't send a message from a GET request
84 assert_difference "ActionMailer::Base.deliveries.size", 0 do
85 assert_difference "Message.count", 0 do
86 perform_enqueued_jobs do
87 get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
91 assert_response :success
93 assert_select "title", "Send message | OpenStreetMap"
94 assert_select "form[action='/messages']", :count => 1 do
95 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
96 assert_select "input#message_title", :count => 1 do
97 assert_select "[value='Test Message']"
99 assert_select "textarea#message_body", :text => "Test message body", :count => 1
100 assert_select "input[type='submit'][value='Send']", :count => 1
105 # test posting new message page with no body
106 def test_new_post_no_body
107 # Login as a normal user
109 recipient_user = create(:user)
112 # Check that the subject is preserved over errors
113 assert_difference "ActionMailer::Base.deliveries.size", 0 do
114 assert_difference "Message.count", 0 do
115 perform_enqueued_jobs do
116 post messages_path(:display_name => recipient_user.display_name,
117 :message => { :title => "Test Message", :body => "" })
121 assert_response :success
122 assert_template "new"
123 assert_select "title", "Send message | OpenStreetMap"
124 assert_select "form[action='/messages']", :count => 1 do
125 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
126 assert_select "input#message_title", :count => 1 do
127 assert_select "[value='Test Message']"
129 assert_select "textarea#message_body", :text => "", :count => 1
130 assert_select "input[type='submit'][value='Send']", :count => 1
135 # test posting new message page with no title
136 def test_new_post_no_title
137 # Login as a normal user
139 recipient_user = create(:user)
142 # Check that the body text is preserved over errors
143 assert_difference "ActionMailer::Base.deliveries.size", 0 do
144 assert_difference "Message.count", 0 do
145 perform_enqueued_jobs do
146 post messages_path(:display_name => recipient_user.display_name,
147 :message => { :title => "", :body => "Test message body" })
151 assert_response :success
152 assert_template "new"
153 assert_select "title", "Send message | OpenStreetMap"
154 assert_select "form[action='/messages']", :count => 1 do
155 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
156 assert_select "input#message_title", :count => 1 do
157 assert_select "[value='']"
159 assert_select "textarea#message_body", :text => "Test message body", :count => 1
160 assert_select "input[type='submit'][value='Send']", :count => 1
165 # test posting new message page sends message
166 def test_new_post_send
167 # Login as a normal user
169 recipient_user = create(:user)
172 # Check that sending a message works
173 assert_difference "ActionMailer::Base.deliveries.size", 1 do
174 assert_difference "Message.count", 1 do
175 perform_enqueued_jobs do
176 post messages_path(:display_name => recipient_user.display_name,
177 :message => { :title => "Test Message", :body => "Test message body" })
181 assert_redirected_to inbox_messages_path
182 assert_equal "Message sent", flash[:notice]
183 e = ActionMailer::Base.deliveries.first
184 assert_equal [recipient_user.email], e.to
185 assert_equal "[OpenStreetMap] Test Message", e.subject
186 assert_match(/Test message body/, e.text_part.decoded)
187 assert_match(/Test message body/, e.html_part.decoded)
188 assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
189 ActionMailer::Base.deliveries.clear
191 assert_equal user.id, m.from_user_id
192 assert_equal recipient_user.id, m.to_user_id
193 assert_in_delta Time.now.utc, m.sent_on, 2
194 assert_equal "Test Message", m.title
195 assert_equal "Test message body", m.body
196 assert_equal "markdown", m.body_format
198 # Asking to send a message with a bogus user name should fail
199 get new_message_path("non_existent_user")
200 assert_response :not_found
201 assert_template "users/no_such_user"
202 assert_select "h1", "The user non_existent_user does not exist"
206 # test the new action message limit
208 # Login as a normal user
210 recipient_user = create(:user)
213 # Check that sending a message fails when the message limit is hit
214 assert_no_difference "ActionMailer::Base.deliveries.size" do
215 assert_no_difference "Message.count" do
216 with_settings(:max_messages_per_hour => 0) do
217 perform_enqueued_jobs do
218 post messages_path(:display_name => recipient_user.display_name,
219 :message => { :title => "Test Message", :body => "Test message body" })
220 assert_response :success
221 assert_template "new"
222 assert_select ".alert.alert-danger", /wait a while/
230 # test the reply action
233 recipient_user = create(:user)
234 other_user = create(:user)
235 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
237 # Check that the message reply page requires us to login
238 get message_reply_path(:message_id => unread_message)
239 assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
241 # Login as the wrong user
242 session_for(other_user)
244 # Check that we can't reply to somebody else's message
245 get message_reply_path(:message_id => unread_message)
246 assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
247 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]
249 # Login as the right user
250 session_for(recipient_user)
252 # Check that the message reply page loads
253 get message_reply_path(:message_id => unread_message)
254 assert_response :success
255 assert_template "new"
256 assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
257 assert_select "form[action='/messages']", :count => 1 do
258 assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
259 assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
260 assert_select "textarea#message_body", :count => 1
261 assert_select "input[type='submit'][value='Send']", :count => 1
263 assert Message.find(unread_message.id).message_read
265 # Asking to reply to a message with no ID should fail
266 get message_reply_path
267 assert_response :success
269 # Asking to reply to a message with a bogus ID should fail
270 get message_reply_path(:message_id => 99999)
271 assert_response :not_found
272 assert_template "no_such_message"
276 # test the show action
279 recipient_user = create(:user)
280 other_user = create(:user)
281 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
283 # Check that the show message page requires us to login
284 get message_path(:id => unread_message)
285 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
287 # Login as the wrong user
288 session_for(other_user)
290 # Check that we can't read the message
291 get message_path(:id => unread_message)
292 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
293 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]
295 # Login as the message sender
298 # Check that the message sender can read the message
299 get message_path(:id => unread_message)
300 assert_response :success
301 assert_template "show"
302 assert_not Message.find(unread_message.id).message_read
304 # Login as the message recipient
305 session_for(recipient_user)
307 # Check that the message recipient can read the message
308 get message_path(:id => unread_message)
309 assert_response :success
310 assert_template "show"
311 assert Message.find(unread_message.id).message_read
313 # Asking to read a message with no ID should fail
315 assert_response :success
317 # Asking to read a message with a bogus ID should fail
318 get message_path(:id => 99999)
319 assert_response :not_found
320 assert_template "no_such_message"
324 # test the inbox action
327 read_message = create(:message, :read, :recipient => user)
328 # Check that the inbox page requires us to login
329 get inbox_messages_path
330 assert_redirected_to login_path(:referer => inbox_messages_path)
335 # Check that we can view our inbox when logged in
336 get inbox_messages_path
337 assert_response :success
338 assert_template "inbox"
339 assert_select ".content-inner > table", :count => 1 do
340 assert_select "tr", :count => 2
341 assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
346 # test the outbox action
349 create(:message, :sender => user)
351 # Check that the outbox page requires us to login
352 get outbox_messages_path
353 assert_redirected_to login_path(:referer => outbox_messages_path)
358 # Check that we can view our outbox when logged in
359 get outbox_messages_path
360 assert_response :success
361 assert_template "outbox"
362 assert_select ".content-inner > table", :count => 1 do
363 assert_select "tr", :count => 2
364 assert_select "tr.inbox-row", :count => 1
369 # test the mark action
372 recipient_user = create(:user)
373 other_user = create(:user)
374 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
376 # Check that the marking a message requires us to login
377 post message_mark_path(:message_id => unread_message)
378 assert_response :forbidden
380 # Login as a user with no messages
381 session_for(other_user)
383 # Check that marking a message we didn't send or receive fails
384 post message_mark_path(:message_id => unread_message)
385 assert_response :not_found
386 assert_template "no_such_message"
388 # Login as the message recipient_user
389 session_for(recipient_user)
391 # Check that the marking a message read works
392 post message_mark_path(:message_id => unread_message, :mark => "read")
393 assert_redirected_to inbox_messages_path
394 assert Message.find(unread_message.id).message_read
396 # Check that the marking a message unread works
397 post message_mark_path(:message_id => unread_message, :mark => "unread")
398 assert_redirected_to inbox_messages_path
399 assert_not Message.find(unread_message.id).message_read
401 # Check that the marking a message read via XHR works
402 post message_mark_path(:message_id => unread_message, :mark => "read"), :xhr => true
403 assert_response :success
404 assert_template "mark"
405 assert Message.find(unread_message.id).message_read
407 # Check that the marking a message unread via XHR works
408 post message_mark_path(:message_id => unread_message, :mark => "unread"), :xhr => true
409 assert_response :success
410 assert_template "mark"
411 assert_not Message.find(unread_message.id).message_read
413 # Asking to mark a message with no ID should fail
414 post message_mark_path
415 assert_redirected_to inbox_messages_path
417 # Asking to mark a message with a bogus ID should fail
418 post message_mark_path(:message_id => 99999)
419 assert_response :not_found
420 assert_template "no_such_message"
424 # test the destroy action
427 second_user = create(:user)
428 other_user = create(:user)
429 read_message = create(:message, :read, :recipient => user, :sender => second_user)
430 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
432 # Check that destroying a message requires us to login
433 delete message_path(:id => read_message)
434 assert_response :forbidden
436 # Login as a user with no messages
437 session_for(other_user)
439 # Check that destroying a message we didn't send or receive fails
440 delete message_path(:id => read_message)
441 assert_response :not_found
442 assert_template "no_such_message"
444 # Login as the message recipient_user
447 # Check that the destroy a received message works
448 delete message_path(:id => read_message)
449 assert_redirected_to inbox_messages_path
450 assert_equal "Message deleted", flash[:notice]
451 m = Message.find(read_message.id)
452 assert m.from_user_visible
453 assert_not m.to_user_visible
455 # Check that the destroying a sent message works
456 delete message_path(:id => sent_message, :referer => outbox_messages_path)
457 assert_redirected_to outbox_messages_path
458 assert_equal "Message deleted", flash[:notice]
459 m = Message.find(sent_message.id)
460 assert_not m.from_user_visible
461 assert m.to_user_visible
463 # Asking to destroy a message with no ID should fail
465 assert_redirected_to inbox_messages_path
467 # Asking to destroy a message with a bogus ID should fail
468 delete message_path(:id => 99999)
469 assert_response :not_found
470 assert_template "no_such_message"