3 class MessagesControllerTest < ActionController::TestCase
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, :params => { :display_name => user.display_name }
51 assert_redirected_to login_path(:referer => new_message_path(:display_name => user.display_name))
55 # test fetching new message page when logged in
57 # Login as a normal user
59 recipient_user = create(:user)
60 session[:user] = user.id
62 # Check that the new message page loads
63 get :new, :params => { :display_name => recipient_user.display_name }
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)
81 session[:user] = user.id
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
88 :params => { :display_name => recipient_user.display_name,
89 :message => { :title => "Test Message", :body => "Test message body" } }
93 assert_response :success
95 assert_select "title", "Send message | OpenStreetMap"
96 assert_select "form[action='/messages']", :count => 1 do
97 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
98 assert_select "input#message_title", :count => 1 do
99 assert_select "[value='Test Message']"
101 assert_select "textarea#message_body", :text => "Test message body", :count => 1
102 assert_select "input[type='submit'][value='Send']", :count => 1
107 # test posting new message page with no body
108 def test_new_post_no_body
109 # Login as a normal user
111 recipient_user = create(:user)
112 session[:user] = user.id
114 # Check that the subject is preserved over errors
115 assert_difference "ActionMailer::Base.deliveries.size", 0 do
116 assert_difference "Message.count", 0 do
117 perform_enqueued_jobs do
119 :params => { :display_name => recipient_user.display_name,
120 :message => { :title => "Test Message", :body => "" } }
124 assert_response :success
125 assert_template "new"
126 assert_select "title", "Send message | OpenStreetMap"
127 assert_select "form[action='/messages']", :count => 1 do
128 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
129 assert_select "input#message_title", :count => 1 do
130 assert_select "[value='Test Message']"
132 assert_select "textarea#message_body", :text => "", :count => 1
133 assert_select "input[type='submit'][value='Send']", :count => 1
138 # test posting new message page with no title
139 def test_new_post_no_title
140 # Login as a normal user
142 recipient_user = create(:user)
143 session[:user] = user.id
145 # Check that the body text is preserved over errors
146 assert_difference "ActionMailer::Base.deliveries.size", 0 do
147 assert_difference "Message.count", 0 do
148 perform_enqueued_jobs do
150 :params => { :display_name => recipient_user.display_name,
151 :message => { :title => "", :body => "Test message body" } }
155 assert_response :success
156 assert_template "new"
157 assert_select "title", "Send message | OpenStreetMap"
158 assert_select "form[action='/messages']", :count => 1 do
159 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
160 assert_select "input#message_title", :count => 1 do
161 assert_select "[value='']"
163 assert_select "textarea#message_body", :text => "Test message body", :count => 1
164 assert_select "input[type='submit'][value='Send']", :count => 1
169 # test posting new message page sends message
170 def test_new_post_send
171 # Login as a normal user
173 recipient_user = create(:user)
174 session[:user] = user.id
176 # Check that sending a message works
177 assert_difference "ActionMailer::Base.deliveries.size", 1 do
178 assert_difference "Message.count", 1 do
179 perform_enqueued_jobs do
181 :params => { :display_name => recipient_user.display_name,
182 :message => { :title => "Test Message", :body => "Test message body" } }
186 assert_redirected_to inbox_messages_path
187 assert_equal "Message sent", flash[:notice]
188 e = ActionMailer::Base.deliveries.first
189 assert_equal [recipient_user.email], e.to
190 assert_equal "[OpenStreetMap] Test Message", e.subject
191 assert_match(/Test message body/, e.text_part.decoded)
192 assert_match(/Test message body/, e.html_part.decoded)
193 assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
194 ActionMailer::Base.deliveries.clear
196 assert_equal user.id, m.from_user_id
197 assert_equal recipient_user.id, m.to_user_id
198 assert_in_delta Time.now, m.sent_on, 2
199 assert_equal "Test Message", m.title
200 assert_equal "Test message body", m.body
201 assert_equal "markdown", m.body_format
203 # Asking to send a message with a bogus user name should fail
204 get :new, :params => { :display_name => "non_existent_user" }
205 assert_response :not_found
206 assert_template "users/no_such_user"
207 assert_select "h1", "The user non_existent_user does not exist"
211 # test the new action message limit
213 # Login as a normal user
215 recipient_user = create(:user)
216 session[:user] = user.id
218 # Check that sending a message fails when the message limit is hit
219 assert_no_difference "ActionMailer::Base.deliveries.size" do
220 assert_no_difference "Message.count" do
221 with_message_limit(0) do
222 perform_enqueued_jobs do
224 :params => { :display_name => recipient_user.display_name,
225 :message => { :title => "Test Message", :body => "Test message body" } }
226 assert_response :success
227 assert_template "new"
228 assert_select ".error", /wait a while/
236 # test the reply action
239 recipient_user = create(:user)
240 other_user = create(:user)
241 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
243 # Check that the message reply page requires us to login
244 get :reply, :params => { :message_id => unread_message.id }
245 assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
247 # Login as the wrong user
248 session[:user] = other_user.id
250 # Check that we can't reply to somebody else's message
251 get :reply, :params => { :message_id => unread_message.id }
252 assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
253 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 login as the correct user in order to reply.", flash[:notice]
255 # Login as the right user
256 session[:user] = recipient_user.id
258 # Check that the message reply page loads
259 get :reply, :params => { :message_id => unread_message.id }
260 assert_response :success
261 assert_template "new"
262 assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
263 assert_select "form[action='/messages']", :count => 1 do
264 assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
265 assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
266 assert_select "textarea#message_body", :count => 1
267 assert_select "input[type='submit'][value='Send']", :count => 1
269 assert_equal true, Message.find(unread_message.id).message_read
271 # Asking to reply to a message with no ID should fail
272 assert_raise ActionController::UrlGenerationError do
276 # Asking to reply to a message with a bogus ID should fail
277 get :reply, :params => { :message_id => 99999 }
278 assert_response :not_found
279 assert_template "no_such_message"
283 # test the show action
286 recipient_user = create(:user)
287 other_user = create(:user)
288 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
290 # Check that the show message page requires us to login
291 get :show, :params => { :id => unread_message.id }
292 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
294 # Login as the wrong user
295 session[:user] = other_user.id
297 # Check that we can't read the message
298 get :show, :params => { :id => unread_message.id }
299 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
300 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 login as the correct user in order to read it.", flash[:notice]
302 # Login as the message sender
303 session[:user] = user.id
305 # Check that the message sender can read the message
306 get :show, :params => { :id => unread_message.id }
307 assert_response :success
308 assert_template "show"
309 assert_equal false, Message.find(unread_message.id).message_read
311 # Login as the message recipient
312 session[:user] = recipient_user.id
314 # Check that the message recipient can read the message
315 get :show, :params => { :id => unread_message.id }
316 assert_response :success
317 assert_template "show"
318 assert_equal true, Message.find(unread_message.id).message_read
320 # Asking to read a message with no ID should fail
321 assert_raise ActionController::UrlGenerationError do
325 # Asking to read a message with a bogus ID should fail
326 get :show, :params => { :id => 99999 }
327 assert_response :not_found
328 assert_template "no_such_message"
332 # test the inbox action
335 read_message = create(:message, :read, :recipient => user)
336 # Check that the inbox page requires us to login
338 assert_redirected_to login_path(:referer => inbox_messages_path)
341 session[:user] = user.id
343 # Check that we can view our inbox when logged in
345 assert_response :success
346 assert_template "inbox"
347 assert_select ".content-inner > table", :count => 1 do
348 assert_select "tr", :count => 2
349 assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
354 # test the outbox action
357 create(:message, :sender => user)
359 # Check that the outbox page requires us to login
361 assert_redirected_to login_path(:referer => outbox_messages_path)
364 session[:user] = user.id
366 # Check that we can view our outbox when logged in
368 assert_response :success
369 assert_template "outbox"
370 assert_select ".content-inner > table", :count => 1 do
371 assert_select "tr", :count => 2
372 assert_select "tr.inbox-row", :count => 1
377 # test the mark action
380 recipient_user = create(:user)
381 other_user = create(:user)
382 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
384 # Check that the marking a message requires us to login
385 post :mark, :params => { :message_id => unread_message.id }
386 assert_response :forbidden
388 # Login as a user with no messages
389 session[:user] = other_user.id
391 # Check that marking a message we didn't send or receive fails
392 post :mark, :params => { :message_id => unread_message.id }
393 assert_response :not_found
394 assert_template "no_such_message"
396 # Login as the message recipient_user
397 session[:user] = recipient_user.id
399 # Check that the marking a message read works
400 post :mark, :params => { :message_id => unread_message.id, :mark => "read" }
401 assert_redirected_to inbox_messages_path
402 assert_equal true, Message.find(unread_message.id).message_read
404 # Check that the marking a message unread works
405 post :mark, :params => { :message_id => unread_message.id, :mark => "unread" }
406 assert_redirected_to inbox_messages_path
407 assert_equal false, Message.find(unread_message.id).message_read
409 # Check that the marking a message read via XHR works
410 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "read" }
411 assert_response :success
412 assert_template "mark"
413 assert_equal true, Message.find(unread_message.id).message_read
415 # Check that the marking a message unread via XHR works
416 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "unread" }
417 assert_response :success
418 assert_template "mark"
419 assert_equal false, Message.find(unread_message.id).message_read
421 # Asking to mark a message with no ID should fail
422 assert_raise ActionController::UrlGenerationError do
426 # Asking to mark a message with a bogus ID should fail
427 post :mark, :params => { :message_id => 99999 }
428 assert_response :not_found
429 assert_template "no_such_message"
433 # test the destroy action
436 second_user = create(:user)
437 other_user = create(:user)
438 read_message = create(:message, :read, :recipient => user, :sender => second_user)
439 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
441 # Check that destroying a message requires us to login
442 delete :destroy, :params => { :id => read_message.id }
443 assert_response :forbidden
445 # Login as a user with no messages
446 session[:user] = other_user.id
448 # Check that destroying a message we didn't send or receive fails
449 delete :destroy, :params => { :id => read_message.id }
450 assert_response :not_found
451 assert_template "no_such_message"
453 # Login as the message recipient_user
454 session[:user] = user.id
456 # Check that the destroy a received message works
457 delete :destroy, :params => { :id => read_message.id }
458 assert_redirected_to inbox_messages_path
459 assert_equal "Message deleted", flash[:notice]
460 m = Message.find(read_message.id)
461 assert_equal true, m.from_user_visible
462 assert_equal false, m.to_user_visible
464 # Check that the destroying a sent message works
465 delete :destroy, :params => { :id => sent_message.id, :referer => outbox_messages_path }
466 assert_redirected_to outbox_messages_path
467 assert_equal "Message deleted", flash[:notice]
468 m = Message.find(sent_message.id)
469 assert_equal false, m.from_user_visible
470 assert_equal true, m.to_user_visible
472 # Asking to destroy a message with no ID should fail
473 assert_raise ActionController::UrlGenerationError do
477 # Asking to destroy a message with a bogus ID should fail
478 delete :destroy, :params => { :id => 99999 }
479 assert_response :not_found
480 assert_template "no_such_message"
485 def with_message_limit(value)
486 max_messages_per_hour = Settings.max_messages_per_hour
487 Settings.max_messages_per_hour = value
491 Settings.max_messages_per_hour = max_messages_per_hour