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 => "/message/new/username", :method => :post },
21 { :controller => "messages", :action => "new", :display_name => "username" }
24 { :path => "/messages/1", :method => :get },
25 { :controller => "messages", :action => "show", :id => "1" }
28 { :path => "/message/mark/1", :method => :post },
29 { :controller => "messages", :action => "mark", :message_id => "1" }
32 { :path => "/message/reply/1", :method => :get },
33 { :controller => "messages", :action => "reply", :message_id => "1" }
36 { :path => "/message/reply/1", :method => :post },
37 { :controller => "messages", :action => "reply", :message_id => "1" }
40 { :path => "/message/delete/1", :method => :post },
41 { :controller => "messages", :action => "destroy", :message_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='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
68 assert_select "input#message_title", :count => 1
69 assert_select "textarea#message_body", :count => 1
70 assert_select "input[type='submit'][value='Send']", :count => 1
75 # test fetching new message page with body and title
76 def test_new_get_with_params
77 # Login as a normal user
79 recipient_user = create(:user)
80 session[:user] = user.id
82 # Check that we can't send a message from a GET request
83 assert_difference "ActionMailer::Base.deliveries.size", 0 do
84 assert_difference "Message.count", 0 do
86 :params => { :display_name => recipient_user.display_name,
87 :message => { :title => "Test Message", :body => "Test message body" } }
90 assert_response :success
92 assert_select "title", "Send message | OpenStreetMap"
93 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
94 assert_select "input#message_title", :count => 1 do
95 assert_select "[value='Test Message']"
97 assert_select "textarea#message_body", :text => "Test message body", :count => 1
98 assert_select "input[type='submit'][value='Send']", :count => 1
103 # test posting new message page with no body
104 def test_new_post_no_body
105 # Login as a normal user
107 recipient_user = create(:user)
108 session[:user] = user.id
110 # Check that the subject is preserved over errors
111 assert_difference "ActionMailer::Base.deliveries.size", 0 do
112 assert_difference "Message.count", 0 do
114 :params => { :display_name => recipient_user.display_name,
115 :message => { :title => "Test Message", :body => "" } }
118 assert_response :success
119 assert_template "new"
120 assert_select "title", "Send message | OpenStreetMap"
121 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
122 assert_select "input#message_title", :count => 1 do
123 assert_select "[value='Test Message']"
125 assert_select "textarea#message_body", :text => "", :count => 1
126 assert_select "input[type='submit'][value='Send']", :count => 1
131 # test posting new message page with no title
132 def test_new_post_no_title
133 # Login as a normal user
135 recipient_user = create(:user)
136 session[:user] = user.id
138 # Check that the body text is preserved over errors
139 assert_difference "ActionMailer::Base.deliveries.size", 0 do
140 assert_difference "Message.count", 0 do
142 :params => { :display_name => recipient_user.display_name,
143 :message => { :title => "", :body => "Test message body" } }
146 assert_response :success
147 assert_template "new"
148 assert_select "title", "Send message | OpenStreetMap"
149 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
150 assert_select "input#message_title", :count => 1 do
151 assert_select "[value='']"
153 assert_select "textarea#message_body", :text => "Test message body", :count => 1
154 assert_select "input[type='submit'][value='Send']", :count => 1
159 # test posting new message page sends message
160 def test_new_post_send
161 # Login as a normal user
163 recipient_user = create(:user)
164 session[:user] = user.id
166 # Check that sending a message works
167 assert_difference "ActionMailer::Base.deliveries.size", 1 do
168 assert_difference "Message.count", 1 do
170 :params => { :display_name => recipient_user.display_name,
171 :message => { :title => "Test Message", :body => "Test message body" } }
174 assert_redirected_to inbox_messages_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{#{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, 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, :params => { :display_name => "non_existent_user" }
193 assert_response :not_found
194 assert_template "user/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)
204 session[:user] = user.id
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_message_limit(0) do
211 :params => { :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 ".error", /wait a while/
222 # test the reply action
225 recipient_user = create(:user)
226 other_user = create(:user)
227 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
229 # Check that the message reply page requires us to login
230 get :reply, :params => { :message_id => unread_message.id }
231 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
233 # Login as the wrong user
234 session[:user] = other_user.id
236 # Check that we can't reply to somebody else's message
237 get :reply, :params => { :message_id => unread_message.id }
238 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
239 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]
241 # Login as the right user
242 session[:user] = recipient_user.id
244 # Check that the message reply page loads
245 get :reply, :params => { :message_id => unread_message.id }
246 assert_response :success
247 assert_template "new"
248 assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
249 assert_select "form[action='#{new_message_path(:display_name => user.display_name)}']", :count => 1 do
250 assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
251 assert_select "textarea#message_body", :count => 1
252 assert_select "input[type='submit'][value='Send']", :count => 1
254 assert_equal true, Message.find(unread_message.id).message_read
256 # Asking to reply to a message with no ID should fail
257 assert_raise ActionController::UrlGenerationError do
261 # Asking to reply to a message with a bogus ID should fail
262 get :reply, :params => { :message_id => 99999 }
263 assert_response :not_found
264 assert_template "no_such_message"
268 # test the show action
271 recipient_user = create(:user)
272 other_user = create(:user)
273 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
275 # Check that the show message page requires us to login
276 get :show, :params => { :id => unread_message.id }
277 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
279 # Login as the wrong user
280 session[:user] = other_user.id
282 # Check that we can't read the message
283 get :show, :params => { :id => unread_message.id }
284 assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
285 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]
287 # Login as the message sender
288 session[:user] = user.id
290 # Check that the message sender can read the message
291 get :show, :params => { :id => unread_message.id }
292 assert_response :success
293 assert_template "show"
294 assert_equal false, Message.find(unread_message.id).message_read
296 # Login as the message recipient
297 session[:user] = recipient_user.id
299 # Check that the message recipient can read the message
300 get :show, :params => { :id => unread_message.id }
301 assert_response :success
302 assert_template "show"
303 assert_equal true, Message.find(unread_message.id).message_read
305 # Asking to read a message with no ID should fail
306 assert_raise ActionController::UrlGenerationError do
310 # Asking to read a message with a bogus ID should fail
311 get :show, :params => { :id => 99999 }
312 assert_response :not_found
313 assert_template "no_such_message"
317 # test the inbox action
320 read_message = create(:message, :read, :recipient => user)
321 # Check that the inbox page requires us to login
323 assert_redirected_to login_path(:referer => inbox_messages_path)
326 session[:user] = user.id
328 # Check that we can view our inbox when logged in
330 assert_response :success
331 assert_template "inbox"
332 assert_select "table.messages", :count => 1 do
333 assert_select "tr", :count => 2
334 assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
339 # test the outbox action
342 create(:message, :sender => user)
344 # Check that the outbox page requires us to login
346 assert_redirected_to login_path(:referer => outbox_messages_path)
349 session[:user] = user.id
351 # Check that we can view our outbox when logged in
353 assert_response :success
354 assert_template "outbox"
355 assert_select "table.messages", :count => 1 do
356 assert_select "tr", :count => 2
357 assert_select "tr.inbox-row", :count => 1
362 # test the mark action
365 recipient_user = create(:user)
366 other_user = create(:user)
367 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
369 # Check that the marking a message requires us to login
370 post :mark, :params => { :message_id => unread_message.id }
371 assert_response :forbidden
373 # Login as a user with no messages
374 session[:user] = other_user.id
376 # Check that marking a message we didn't send or receive fails
377 post :mark, :params => { :message_id => unread_message.id }
378 assert_response :not_found
379 assert_template "no_such_message"
381 # Login as the message recipient_user
382 session[:user] = recipient_user.id
384 # Check that the marking a message read works
385 post :mark, :params => { :message_id => unread_message.id, :mark => "read" }
386 assert_redirected_to inbox_messages_path
387 assert_equal true, Message.find(unread_message.id).message_read
389 # Check that the marking a message unread works
390 post :mark, :params => { :message_id => unread_message.id, :mark => "unread" }
391 assert_redirected_to inbox_messages_path
392 assert_equal false, Message.find(unread_message.id).message_read
394 # Check that the marking a message read via XHR works
395 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "read" }
396 assert_response :success
397 assert_template "mark"
398 assert_equal true, Message.find(unread_message.id).message_read
400 # Check that the marking a message unread via XHR works
401 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "unread" }
402 assert_response :success
403 assert_template "mark"
404 assert_equal false, Message.find(unread_message.id).message_read
406 # Asking to mark a message with no ID should fail
407 assert_raise ActionController::UrlGenerationError do
411 # Asking to mark a message with a bogus ID should fail
412 post :mark, :params => { :message_id => 99999 }
413 assert_response :not_found
414 assert_template "no_such_message"
418 # test the destroy action
421 second_user = create(:user)
422 other_user = create(:user)
423 read_message = create(:message, :read, :recipient => user, :sender => second_user)
424 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
426 # Check that destroying a message requires us to login
427 post :destroy, :params => { :message_id => read_message.id }
428 assert_response :forbidden
430 # Login as a user with no messages
431 session[:user] = other_user.id
433 # Check that destroying a message we didn't send or receive fails
434 post :destroy, :params => { :message_id => read_message.id }
435 assert_response :not_found
436 assert_template "no_such_message"
438 # Login as the message recipient_user
439 session[:user] = user.id
441 # Check that the destroy a received message works
442 post :destroy, :params => { :message_id => read_message.id }
443 assert_redirected_to inbox_messages_path
444 assert_equal "Message deleted", flash[:notice]
445 m = Message.find(read_message.id)
446 assert_equal true, m.from_user_visible
447 assert_equal false, m.to_user_visible
449 # Check that the destroying a sent message works
450 post :destroy, :params => { :message_id => sent_message.id, :referer => outbox_messages_path }
451 assert_redirected_to outbox_messages_path
452 assert_equal "Message deleted", flash[:notice]
453 m = Message.find(sent_message.id)
454 assert_equal false, m.from_user_visible
455 assert_equal true, m.to_user_visible
457 # Asking to destroy a message with no ID should fail
458 assert_raise ActionController::UrlGenerationError do
462 # Asking to destroy a message with a bogus ID should fail
463 post :destroy, :params => { :message_id => 99999 }
464 assert_response :not_found
465 assert_template "no_such_message"
470 def with_message_limit(value)
471 max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
472 Object.const_set("MAX_MESSAGES_PER_HOUR", value)
476 Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
477 Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)