3 class MessagesControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/user/username/inbox", :method => :get },
9 { :controller => "messages", :action => "inbox", :display_name => "username" }
12 { :path => "/user/username/outbox", :method => :get },
13 { :controller => "messages", :action => "outbox", :display_name => "username" }
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 => "/message/read/1", :method => :get },
25 { :controller => "messages", :action => "show", :message_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/delete/1", :method => :post },
37 { :controller => "messages", :action => "destroy", :message_id => "1" }
42 # test fetching new message page when not logged in
44 # Check that the new message page requires us to login
46 get :new, :params => { :display_name => user.display_name }
47 assert_redirected_to login_path(:referer => new_message_path(:display_name => user.display_name))
51 # test fetching new message page when logged in
53 # Login as a normal user
55 recipient_user = create(:user)
56 session[:user] = user.id
58 # Check that the new message page loads
59 get :new, :params => { :display_name => recipient_user.display_name }
60 assert_response :success
62 assert_select "title", "Send message | OpenStreetMap"
63 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
64 assert_select "input#message_title", :count => 1
65 assert_select "textarea#message_body", :count => 1
66 assert_select "input[type='submit'][value='Send']", :count => 1
71 # test fetching new message page with body and title
72 def test_new_get_with_params
73 # Login as a normal user
75 recipient_user = create(:user)
76 session[:user] = user.id
78 # Check that we can't send a message from a GET request
79 assert_difference "ActionMailer::Base.deliveries.size", 0 do
80 assert_difference "Message.count", 0 do
82 :params => { :display_name => recipient_user.display_name,
83 :message => { :title => "Test Message", :body => "Test message body" } }
86 assert_response :success
88 assert_select "title", "Send message | OpenStreetMap"
89 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
90 assert_select "input#message_title", :count => 1 do
91 assert_select "[value='Test Message']"
93 assert_select "textarea#message_body", :text => "Test message body", :count => 1
94 assert_select "input[type='submit'][value='Send']", :count => 1
99 # test posting new message page with no body
100 def test_new_post_no_body
101 # Login as a normal user
103 recipient_user = create(:user)
104 session[:user] = user.id
106 # Check that the subject is preserved over errors
107 assert_difference "ActionMailer::Base.deliveries.size", 0 do
108 assert_difference "Message.count", 0 do
110 :params => { :display_name => recipient_user.display_name,
111 :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='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
118 assert_select "input#message_title", :count => 1 do
119 assert_select "[value='Test Message']"
121 assert_select "textarea#message_body", :text => "", :count => 1
122 assert_select "input[type='submit'][value='Send']", :count => 1
127 # test posting new message page with no title
128 def test_new_post_no_title
129 # Login as a normal user
131 recipient_user = create(:user)
132 session[:user] = user.id
134 # Check that the body text is preserved over errors
135 assert_difference "ActionMailer::Base.deliveries.size", 0 do
136 assert_difference "Message.count", 0 do
138 :params => { :display_name => recipient_user.display_name,
139 :message => { :title => "", :body => "Test message body" } }
142 assert_response :success
143 assert_template "new"
144 assert_select "title", "Send message | OpenStreetMap"
145 assert_select "form[action='#{new_message_path(:display_name => recipient_user.display_name)}']", :count => 1 do
146 assert_select "input#message_title", :count => 1 do
147 assert_select "[value='']"
149 assert_select "textarea#message_body", :text => "Test message body", :count => 1
150 assert_select "input[type='submit'][value='Send']", :count => 1
155 # test posting new message page sends message
156 def test_new_post_send
157 # Login as a normal user
159 recipient_user = create(:user)
160 session[:user] = user.id
162 # Check that sending a message works
163 assert_difference "ActionMailer::Base.deliveries.size", 1 do
164 assert_difference "Message.count", 1 do
166 :params => { :display_name => recipient_user.display_name,
167 :message => { :title => "Test Message", :body => "Test message body" } }
170 assert_redirected_to inbox_path(:display_name => user.display_name)
171 assert_equal "Message sent", flash[:notice]
172 e = ActionMailer::Base.deliveries.first
173 assert_equal [recipient_user.email], e.to
174 assert_equal "[OpenStreetMap] Test Message", e.subject
175 assert_match /Test message body/, e.text_part.decoded
176 assert_match /Test message body/, e.html_part.decoded
177 assert_match %r{#{SERVER_URL}/message/read/}, e.text_part.decoded
178 ActionMailer::Base.deliveries.clear
180 assert_equal user.id, m.from_user_id
181 assert_equal recipient_user.id, m.to_user_id
182 assert_in_delta Time.now, m.sent_on, 2
183 assert_equal "Test Message", m.title
184 assert_equal "Test message body", m.body
185 assert_equal "markdown", m.body_format
187 # Asking to send a message with a bogus user name should fail
188 get :new, :params => { :display_name => "non_existent_user" }
189 assert_response :not_found
190 assert_template "user/no_such_user"
191 assert_select "h1", "The user non_existent_user does not exist"
195 # test the new action message limit
197 # Login as a normal user
199 recipient_user = create(:user)
200 session[:user] = user.id
202 # Check that sending a message fails when the message limit is hit
203 assert_no_difference "ActionMailer::Base.deliveries.size" do
204 assert_no_difference "Message.count" do
205 with_message_limit(0) do
207 :params => { :display_name => recipient_user.display_name,
208 :message => { :title => "Test Message", :body => "Test message body" } }
209 assert_response :success
210 assert_template "new"
211 assert_select ".error", /wait a while/
218 # test the reply action
221 recipient_user = create(:user)
222 other_user = create(:user)
223 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
225 # Check that the message reply page requires us to login
226 get :reply, :params => { :message_id => unread_message.id }
227 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
229 # Login as the wrong user
230 session[:user] = other_user.id
232 # Check that we can't reply to somebody else's message
233 get :reply, :params => { :message_id => unread_message.id }
234 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
235 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]
237 # Login as the right user
238 session[:user] = recipient_user.id
240 # Check that the message reply page loads
241 get :reply, :params => { :message_id => unread_message.id }
242 assert_response :success
243 assert_template "new"
244 assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
245 assert_select "form[action='#{new_message_path(:display_name => user.display_name)}']", :count => 1 do
246 assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
247 assert_select "textarea#message_body", :count => 1
248 assert_select "input[type='submit'][value='Send']", :count => 1
250 assert_equal true, Message.find(unread_message.id).message_read
252 # Asking to reply to a message with no ID should fail
253 assert_raise ActionController::UrlGenerationError do
257 # Asking to reply to a message with a bogus ID should fail
258 get :reply, :params => { :message_id => 99999 }
259 assert_response :not_found
260 assert_template "no_such_message"
264 # test the show action
267 recipient_user = create(:user)
268 other_user = create(:user)
269 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
271 # Check that the show message page requires us to login
272 get :show, :params => { :message_id => unread_message.id }
273 assert_redirected_to login_path(:referer => message_path(:message_id => unread_message.id))
275 # Login as the wrong user
276 session[:user] = other_user.id
278 # Check that we can't read the message
279 get :show, :params => { :message_id => unread_message.id }
280 assert_redirected_to login_path(:referer => message_path(:message_id => unread_message.id))
281 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]
283 # Login as the message sender
284 session[:user] = user.id
286 # Check that the message sender can read the message
287 get :show, :params => { :message_id => unread_message.id }
288 assert_response :success
289 assert_template "show"
290 assert_equal false, Message.find(unread_message.id).message_read
292 # Login as the message recipient
293 session[:user] = recipient_user.id
295 # Check that the message recipient can read the message
296 get :show, :params => { :message_id => unread_message.id }
297 assert_response :success
298 assert_template "show"
299 assert_equal true, Message.find(unread_message.id).message_read
301 # Asking to read a message with no ID should fail
302 assert_raise ActionController::UrlGenerationError do
306 # Asking to read a message with a bogus ID should fail
307 get :show, :params => { :message_id => 99999 }
308 assert_response :not_found
309 assert_template "no_such_message"
313 # test the inbox action
316 other_user = create(:user)
317 read_message = create(:message, :read, :recipient => user)
318 # Check that the inbox page requires us to login
319 get :inbox, :params => { :display_name => user.display_name }
320 assert_redirected_to login_path(:referer => inbox_path(:display_name => user.display_name))
323 session[:user] = user.id
325 # Check that we can view our inbox when logged in
326 get :inbox, :params => { :display_name => user.display_name }
327 assert_response :success
328 assert_template "inbox"
329 assert_select "table.messages", :count => 1 do
330 assert_select "tr", :count => 2
331 assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
334 # Check that we can't view somebody else's inbox when logged in
335 get :inbox, :params => { :display_name => other_user.display_name }
336 assert_redirected_to inbox_path(:display_name => user.display_name)
340 # test the outbox action
343 other_user = create(:user)
344 create(:message, :sender => user)
346 # Check that the outbox page requires us to login
347 get :outbox, :params => { :display_name => user.display_name }
348 assert_redirected_to login_path(:referer => outbox_path(:display_name => user.display_name))
351 session[:user] = user.id
353 # Check that we can view our outbox when logged in
354 get :outbox, :params => { :display_name => user.display_name }
355 assert_response :success
356 assert_template "outbox"
357 assert_select "table.messages", :count => 1 do
358 assert_select "tr", :count => 2
359 assert_select "tr.inbox-row", :count => 1
362 # Check that we can't view somebody else's outbox when logged in
363 get :outbox, :params => { :display_name => other_user.display_name }
364 assert_redirected_to outbox_path(:display_name => user.display_name)
368 # test the mark action
371 recipient_user = create(:user)
372 other_user = create(:user)
373 unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
375 # Check that the marking a message requires us to login
376 post :mark, :params => { :message_id => unread_message.id }
377 assert_response :forbidden
379 # Login as a user with no messages
380 session[:user] = other_user.id
382 # Check that marking a message we didn't send or receive fails
383 post :mark, :params => { :message_id => unread_message.id }
384 assert_response :not_found
385 assert_template "no_such_message"
387 # Login as the message recipient_user
388 session[:user] = recipient_user.id
390 # Check that the marking a message read works
391 post :mark, :params => { :message_id => unread_message.id, :mark => "read" }
392 assert_redirected_to inbox_path(:display_name => recipient_user.display_name)
393 assert_equal true, Message.find(unread_message.id).message_read
395 # Check that the marking a message unread works
396 post :mark, :params => { :message_id => unread_message.id, :mark => "unread" }
397 assert_redirected_to inbox_path(:display_name => recipient_user.display_name)
398 assert_equal false, Message.find(unread_message.id).message_read
400 # Check that the marking a message read via XHR works
401 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "read" }
402 assert_response :success
403 assert_template "mark"
404 assert_equal true, Message.find(unread_message.id).message_read
406 # Check that the marking a message unread via XHR works
407 post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "unread" }
408 assert_response :success
409 assert_template "mark"
410 assert_equal false, Message.find(unread_message.id).message_read
412 # Asking to mark a message with no ID should fail
413 assert_raise ActionController::UrlGenerationError do
417 # Asking to mark a message with a bogus ID should fail
418 post :mark, :params => { :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 post :destroy, :params => { :message_id => read_message.id }
434 assert_response :forbidden
436 # Login as a user with no messages
437 session[:user] = other_user.id
439 # Check that destroying a message we didn't send or receive fails
440 post :destroy, :params => { :message_id => read_message.id }
441 assert_response :not_found
442 assert_template "no_such_message"
444 # Login as the message recipient_user
445 session[:user] = user.id
447 # Check that the destroy a received message works
448 post :destroy, :params => { :message_id => read_message.id }
449 assert_redirected_to inbox_path(:display_name => user.display_name)
450 assert_equal "Message deleted", flash[:notice]
451 m = Message.find(read_message.id)
452 assert_equal true, m.from_user_visible
453 assert_equal false, m.to_user_visible
455 # Check that the destroying a sent message works
456 post :destroy, :params => { :message_id => sent_message.id, :referer => outbox_path(:display_name => user.display_name) }
457 assert_redirected_to outbox_path(:display_name => user.display_name)
458 assert_equal "Message deleted", flash[:notice]
459 m = Message.find(sent_message.id)
460 assert_equal false, m.from_user_visible
461 assert_equal true, m.to_user_visible
463 # Asking to destroy a message with no ID should fail
464 assert_raise ActionController::UrlGenerationError do
468 # Asking to destroy a message with a bogus ID should fail
469 post :destroy, :params => { :message_id => 99999 }
470 assert_response :not_found
471 assert_template "no_such_message"
476 def with_message_limit(value)
477 max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
478 Object.const_set("MAX_MESSAGES_PER_HOUR", value)
482 Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
483 Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)