3 class MessageControllerTest < ActionController::TestCase
7 # test all routes which lead to this controller
10 { :path => "/user/username/inbox", :method => :get },
11 { :controller => "message", :action => "inbox", :display_name => "username" }
14 { :path => "/user/username/outbox", :method => :get },
15 { :controller => "message", :action => "outbox", :display_name => "username" }
18 { :path => "/message/new/username", :method => :get },
19 { :controller => "message", :action => "new", :display_name => "username" }
22 { :path => "/message/new/username", :method => :post },
23 { :controller => "message", :action => "new", :display_name => "username" }
26 { :path => "/message/read/1", :method => :get },
27 { :controller => "message", :action => "read", :message_id => "1" }
30 { :path => "/message/mark/1", :method => :post },
31 { :controller => "message", :action => "mark", :message_id => "1" }
34 { :path => "/message/reply/1", :method => :get },
35 { :controller => "message", :action => "reply", :message_id => "1" }
38 { :path => "/message/reply/1", :method => :post },
39 { :controller => "message", :action => "reply", :message_id => "1" }
42 { :path => "/message/delete/1", :method => :post },
43 { :controller => "message", :action => "delete", :message_id => "1" }
48 # test fetching new message page when not logged in
50 # Check that the new message page requires us to login
51 get :new, :display_name => users(:public_user).display_name
52 assert_redirected_to login_path(:referer => new_message_path(:display_name => users(:public_user).display_name))
56 # test fetching new message page when logged in
58 # Login as a normal user
59 session[:user] = users(:normal_user).id
61 # Check that the new message page loads
62 get :new, :display_name => users(:public_user).display_name
63 assert_response :success
65 assert_select "title", "OpenStreetMap | Send message"
66 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
67 assert_select "input#message_title", :count => 1
68 assert_select "textarea#message_body", :count => 1
69 assert_select "input[type='submit'][value='Send']", :count => 1
74 # test fetching new message page with body and title
75 def test_new_get_with_params
76 # Login as a normal user
77 session[:user] = users(:normal_user).id
79 # Check that we can't send a message from a GET request
80 assert_difference "ActionMailer::Base.deliveries.size", 0 do
81 assert_difference "Message.count", 0 do
83 :display_name => users(:public_user).display_name,
84 :message => { :title => "Test Message", :body => "Test message body" }
87 assert_response :success
89 assert_select "title", "OpenStreetMap | Send message"
90 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
91 assert_select "input#message_title", :count => 1 do
92 assert_select "[value='Test Message']"
94 assert_select "textarea#message_body", :text => "Test message body", :count => 1
95 assert_select "input[type='submit'][value='Send']", :count => 1
100 # test posting new message page with no body
101 def test_new_post_no_body
102 # Login as a normal user
103 session[:user] = users(:normal_user).id
105 # Check that the subject is preserved over errors
106 assert_difference "ActionMailer::Base.deliveries.size", 0 do
107 assert_difference "Message.count", 0 do
109 :display_name => users(:public_user).display_name,
110 :message => { :title => "Test Message", :body => "" }
113 assert_response :success
114 assert_template "new"
115 assert_select "title", "OpenStreetMap | Send message"
116 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
117 assert_select "input#message_title", :count => 1 do
118 assert_select "[value='Test Message']"
120 assert_select "textarea#message_body", :text => "", :count => 1
121 assert_select "input[type='submit'][value='Send']", :count => 1
126 # test posting new message page with no title
127 def test_new_post_no_title
128 # Login as a normal user
129 session[:user] = users(:normal_user).id
131 # Check that the body text is preserved over errors
132 assert_difference "ActionMailer::Base.deliveries.size", 0 do
133 assert_difference "Message.count", 0 do
135 :display_name => users(:public_user).display_name,
136 :message => { :title => "", :body => "Test message body" }
139 assert_response :success
140 assert_template "new"
141 assert_select "title", "OpenStreetMap | Send message"
142 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
143 assert_select "input#message_title", :count => 1 do
144 assert_select "[value='']"
146 assert_select "textarea#message_body", :text => "Test message body", :count => 1
147 assert_select "input[type='submit'][value='Send']", :count => 1
152 # test posting new message page sends message
153 def test_new_post_send
154 # Login as a normal user
155 session[:user] = users(:normal_user).id
157 # Check that sending a message works
158 assert_difference "ActionMailer::Base.deliveries.size", 1 do
159 assert_difference "Message.count", 1 do
161 :display_name => users(:public_user).display_name,
162 :message => { :title => "Test Message", :body => "Test message body" }
165 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
166 assert_equal "Message sent", flash[:notice]
167 e = ActionMailer::Base.deliveries.first
168 assert_equal [users(:public_user).email], e.to
169 assert_equal "[OpenStreetMap] Test Message", e.subject
170 assert_match /Test message body/, e.text_part.decoded
171 assert_match /Test message body/, e.html_part.decoded
172 ActionMailer::Base.deliveries.clear
174 assert_equal users(:normal_user).id, m.from_user_id
175 assert_equal users(:public_user).id, m.to_user_id
176 assert_in_delta Time.now, m.sent_on, 2
177 assert_equal "Test Message", m.title
178 assert_equal "Test message body", m.body
179 assert_equal "markdown", m.body_format
181 # Asking to send a message with a bogus user name should fail
182 get :new, :display_name => "non_existent_user"
183 assert_response :not_found
184 assert_template "user/no_such_user"
185 assert_select "h1", "The user non_existent_user does not exist"
189 # test the new action message limit
191 # Login as a normal user
192 session[:user] = users(:normal_user).id
194 # Check that sending a message fails when the message limit is hit
195 assert_no_difference "ActionMailer::Base.deliveries.size" do
196 assert_no_difference "Message.count" do
197 with_message_limit(0) do
199 :display_name => users(:public_user).display_name,
200 :message => { :title => "Test Message", :body => "Test message body" }
201 assert_response :success
202 assert_template "new"
203 assert_select ".error", /wait a while/
210 # test the reply action
212 unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
214 # Check that the message reply page requires us to login
215 get :reply, :message_id => unread_message.id
216 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
218 # Login as the wrong user
219 session[:user] = users(:second_public_user).id
221 # Check that we can't reply to somebody else's message
222 get :reply, :message_id => unread_message.id
223 assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
224 assert_equal "You are logged in as `pulibc_test2' 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]
226 # Login as the right user
227 session[:user] = users(:public_user).id
229 # Check that the message reply page loads
230 get :reply, :message_id => unread_message.id
231 assert_response :success
232 assert_template "new"
233 assert_select "title", "OpenStreetMap | Re: #{unread_message.title}"
234 assert_select "form[action='#{new_message_path(:display_name => users(:normal_user).display_name)}']", :count => 1 do
235 assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
236 assert_select "textarea#message_body", :count => 1
237 assert_select "input[type='submit'][value='Send']", :count => 1
239 assert_equal true, Message.find(unread_message.id).message_read
241 # Asking to reply to a message with no ID should fail
242 assert_raise ActionController::UrlGenerationError do
246 # Asking to reply to a message with a bogus ID should fail
247 get :reply, :message_id => 99999
248 assert_response :not_found
249 assert_template "no_such_message"
253 # test the read action
255 unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
257 # Check that the read message page requires us to login
258 get :read, :message_id => unread_message.id
259 assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
261 # Login as the wrong user
262 session[:user] = users(:second_public_user).id
264 # Check that we can't read the message
265 get :read, :message_id => unread_message.id
266 assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
267 assert_equal "You are logged in as `pulibc_test2' 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]
269 # Login as the message sender
270 session[:user] = users(:normal_user).id
272 # Check that the message sender can read the message
273 get :read, :message_id => unread_message.id
274 assert_response :success
275 assert_template "read"
276 assert_equal false, Message.find(unread_message.id).message_read
278 # Login as the message recipient
279 session[:user] = users(:public_user).id
281 # Check that the message recipient can read the message
282 get :read, :message_id => unread_message.id
283 assert_response :success
284 assert_template "read"
285 assert_equal true, Message.find(unread_message.id).message_read
287 # Asking to read a message with no ID should fail
288 assert_raise ActionController::UrlGenerationError do
292 # Asking to read a message with a bogus ID should fail
293 get :read, :message_id => 99999
294 assert_response :not_found
295 assert_template "no_such_message"
299 # test the inbox action
301 read_message = create(:message, :read, :recipient => users(:normal_user))
302 # Check that the inbox page requires us to login
303 get :inbox, :display_name => users(:normal_user).display_name
304 assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
307 session[:user] = users(:normal_user).id
309 # Check that we can view our inbox when logged in
310 get :inbox, :display_name => users(:normal_user).display_name
311 assert_response :success
312 assert_template "inbox"
313 assert_select "table.messages", :count => 1 do
314 assert_select "tr", :count => 2
315 assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
318 # Check that we can't view somebody else's inbox when logged in
319 get :inbox, :display_name => users(:public_user).display_name
320 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
324 # test the outbox action
326 create(:message, :sender => users(:normal_user))
328 # Check that the outbox page requires us to login
329 get :outbox, :display_name => users(:normal_user).display_name
330 assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
333 session[:user] = users(:normal_user).id
335 # Check that we can view our outbox when logged in
336 get :outbox, :display_name => users(:normal_user).display_name
337 assert_response :success
338 assert_template "outbox"
339 assert_select "table.messages", :count => 1 do
340 assert_select "tr", :count => 2
341 assert_select "tr.inbox-row", :count => 1
344 # Check that we can't view somebody else's outbox when logged in
345 get :outbox, :display_name => users(:public_user).display_name
346 assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
350 # test the mark action
352 unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
354 # Check that the marking a message requires us to login
355 post :mark, :message_id => unread_message.id
356 assert_response :forbidden
358 # Login as a user with no messages
359 session[:user] = users(:second_public_user).id
361 # Check that marking a message we didn't send or receive fails
362 post :mark, :message_id => unread_message.id
363 assert_response :not_found
364 assert_template "no_such_message"
366 # Login as the message recipient
367 session[:user] = users(:public_user).id
369 # Check that the marking a message read works
370 post :mark, :message_id => unread_message.id, :mark => "read"
371 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
372 assert_equal true, Message.find(unread_message.id).message_read
374 # Check that the marking a message unread works
375 post :mark, :message_id => unread_message.id, :mark => "unread"
376 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
377 assert_equal false, Message.find(unread_message.id).message_read
379 # Check that the marking a message read via XHR works
380 xhr :post, :mark, :message_id => unread_message.id, :mark => "read"
381 assert_response :success
382 assert_template "mark"
383 assert_equal true, Message.find(unread_message.id).message_read
385 # Check that the marking a message unread via XHR works
386 xhr :post, :mark, :message_id => unread_message.id, :mark => "unread"
387 assert_response :success
388 assert_template "mark"
389 assert_equal false, Message.find(unread_message.id).message_read
391 # Asking to mark a message with no ID should fail
392 assert_raise ActionController::UrlGenerationError do
396 # Asking to mark a message with a bogus ID should fail
397 post :mark, :message_id => 99999
398 assert_response :not_found
399 assert_template "no_such_message"
403 # test the delete action
405 read_message = create(:message, :read, :recipient => users(:normal_user), :sender => users(:public_user))
406 sent_message = create(:message, :unread, :recipient => users(:public_user), :sender => users(:normal_user))
408 # Check that the deleting a message requires us to login
409 post :delete, :message_id => read_message.id
410 assert_response :forbidden
412 # Login as a user with no messages
413 session[:user] = users(:second_public_user).id
415 # Check that deleting a message we didn't send or receive fails
416 post :delete, :message_id => read_message.id
417 assert_response :not_found
418 assert_template "no_such_message"
420 # Login as the message recipient
421 session[:user] = users(:normal_user).id
423 # Check that the deleting a received message works
424 post :delete, :message_id => read_message.id
425 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
426 assert_equal "Message deleted", flash[:notice]
427 m = Message.find(read_message.id)
428 assert_equal true, m.from_user_visible
429 assert_equal false, m.to_user_visible
431 # Check that the deleting a sent message works
432 post :delete, :message_id => sent_message.id, :referer => outbox_path(:display_name => users(:normal_user).display_name)
433 assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
434 assert_equal "Message deleted", flash[:notice]
435 m = Message.find(sent_message.id)
436 assert_equal false, m.from_user_visible
437 assert_equal true, m.to_user_visible
439 # Asking to delete a message with no ID should fail
440 assert_raise ActionController::UrlGenerationError do
444 # Asking to delete a message with a bogus ID should fail
445 post :delete, :message_id => 99999
446 assert_response :not_found
447 assert_template "no_such_message"
452 def with_message_limit(value)
453 max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
454 Object.const_set("MAX_MESSAGES_PER_HOUR", value)
458 Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
459 Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)