3 class MessageControllerTest < ActionController::TestCase
4 fixtures :users, :messages
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" }
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))
54 # Login as a normal user
55 session[:user] = users(:normal_user).id
57 # Check that the new message page loads
58 get :new, :display_name => users(:public_user).display_name
59 assert_response :success
61 assert_select "title", "OpenStreetMap | Send message"
62 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
63 assert_select "input#message_title", :count => 1
64 assert_select "textarea#message_body", :count => 1
65 assert_select "input[type='submit'][value='Send']", :count => 1
68 # Check that the subject is preserved over errors
69 assert_difference "ActionMailer::Base.deliveries.size", 0 do
70 assert_difference "Message.count", 0 do
72 :display_name => users(:public_user).display_name,
73 :message => { :title => "Test Message", :body => "" }
76 assert_response :success
78 assert_select "title", "OpenStreetMap | Send message"
79 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
80 assert_select "input#message_title", :count => 1 do
81 assert_select "[value='Test Message']"
83 assert_select "textarea#message_body", :text => "", :count => 1
84 assert_select "input[type='submit'][value='Send']", :count => 1
87 # Check that the body text is preserved over errors
88 assert_difference "ActionMailer::Base.deliveries.size", 0 do
89 assert_difference "Message.count", 0 do
91 :display_name => users(:public_user).display_name,
92 :message => { :title => "", :body => "Test message body" }
95 assert_response :success
97 assert_select "title", "OpenStreetMap | Send message"
98 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
99 assert_select "input#message_title", :count => 1 do
100 assert_select "[value='']"
102 assert_select "textarea#message_body", :text => "Test message body", :count => 1
103 assert_select "input[type='submit'][value='Send']", :count => 1
106 # Check that sending a message works
107 assert_difference "ActionMailer::Base.deliveries.size", 1 do
108 assert_difference "Message.count", 1 do
110 :display_name => users(:public_user).display_name,
111 :message => { :title => "Test Message", :body => "Test message body" }
114 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
115 assert_equal "Message sent", flash[:notice]
116 e = ActionMailer::Base.deliveries.first
117 assert_equal [users(:public_user).email], e.to
118 assert_equal "[OpenStreetMap] Test Message", e.subject
119 assert_match /Test message body/, e.text_part.decoded
120 assert_match /Test message body/, e.html_part.decoded
121 ActionMailer::Base.deliveries.clear
123 assert_equal users(:normal_user).id, m.from_user_id
124 assert_equal users(:public_user).id, m.to_user_id
125 assert_in_delta Time.now, m.sent_on, 2
126 assert_equal "Test Message", m.title
127 assert_equal "Test message body", m.body
128 assert_equal "markdown", m.body_format
130 # Asking to send a message with a bogus user name should fail
131 get :new, :display_name => "non_existent_user"
132 assert_response :not_found
133 assert_template "user/no_such_user"
134 assert_select "h1", "The user non_existent_user does not exist"
138 # test the new action message limit
140 # Login as a normal user
141 session[:user] = users(:normal_user).id
143 # Check that sending a message fails when the message limit is hit
144 assert_no_difference "ActionMailer::Base.deliveries.size" do
145 assert_no_difference "Message.count" do
146 with_message_limit(0) do
148 :display_name => users(:public_user).display_name,
149 :message => { :title => "Test Message", :body => "Test message body" }
150 assert_response :success
151 assert_template "new"
152 assert_select ".error", /wait a while/
159 # test the reply action
161 # Check that the message reply page requires us to login
162 get :reply, :message_id => messages(:unread_message).id
163 assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:unread_message).id))
165 # Login as the wrong user
166 session[:user] = users(:second_public_user).id
168 # Check that we can't reply to somebody else's message
169 get :reply, :message_id => messages(:unread_message).id
170 assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:unread_message).id))
171 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]
173 # Login as the right user
174 session[:user] = users(:public_user).id
176 # Check that the message reply page loads
177 get :reply, :message_id => messages(:unread_message).id
178 assert_response :success
179 assert_template "new"
180 assert_select "title", "OpenStreetMap | Re: test message 1"
181 assert_select "form[action='#{new_message_path(:display_name => users(:normal_user).display_name)}']", :count => 1 do
182 assert_select "input#message_title[value='Re: test message 1']", :count => 1
183 assert_select "textarea#message_body", :count => 1
184 assert_select "input[type='submit'][value='Send']", :count => 1
186 assert_equal true, Message.find(messages(:unread_message).id).message_read
188 # Asking to reply to a message with no ID should fail
189 assert_raise ActionController::UrlGenerationError do
193 # Asking to reply to a message with a bogus ID should fail
194 get :reply, :message_id => 99999
195 assert_response :not_found
196 assert_template "no_such_message"
200 # test the read action
202 # Check that the read message page requires us to login
203 get :read, :message_id => messages(:unread_message).id
204 assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
206 # Login as the wrong user
207 session[:user] = users(:second_public_user).id
209 # Check that we can't read the message
210 get :read, :message_id => messages(:unread_message).id
211 assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
212 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]
214 # Login as the message sender
215 session[:user] = users(:normal_user).id
217 # Check that the message sender can read the message
218 get :read, :message_id => messages(:unread_message).id
219 assert_response :success
220 assert_template "read"
221 assert_equal false, Message.find(messages(:unread_message).id).message_read
223 # Login as the message recipient
224 session[:user] = users(:public_user).id
226 # Check that the message recipient can read the message
227 get :read, :message_id => messages(:unread_message).id
228 assert_response :success
229 assert_template "read"
230 assert_equal true, Message.find(messages(:unread_message).id).message_read
232 # Asking to read a message with no ID should fail
233 assert_raise ActionController::UrlGenerationError do
237 # Asking to read a message with a bogus ID should fail
238 get :read, :message_id => 99999
239 assert_response :not_found
240 assert_template "no_such_message"
244 # test the inbox action
246 # Check that the inbox page requires us to login
247 get :inbox, :display_name => users(:normal_user).display_name
248 assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
251 session[:user] = users(:normal_user).id
253 # Check that we can view our inbox when logged in
254 get :inbox, :display_name => users(:normal_user).display_name
255 assert_response :success
256 assert_template "inbox"
257 assert_select "table.messages", :count => 1 do
258 assert_select "tr", :count => 2
259 assert_select "tr#inbox-#{messages(:read_message).id}.inbox-row", :count => 1
262 # Check that we can't view somebody else's inbox when logged in
263 get :inbox, :display_name => users(:public_user).display_name
264 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
268 # test the outbox action
270 # Check that the outbox page requires us to login
271 get :outbox, :display_name => users(:normal_user).display_name
272 assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
275 session[:user] = users(:normal_user).id
277 # Check that we can view our outbox when logged in
278 get :outbox, :display_name => users(:normal_user).display_name
279 assert_response :success
280 assert_template "outbox"
281 assert_select "table.messages", :count => 1 do
282 assert_select "tr", :count => 2
283 assert_select "tr.inbox-row", :count => 1
286 # Check that we can't view somebody else's outbox when logged in
287 get :outbox, :display_name => users(:public_user).display_name
288 assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
292 # test the mark action
294 # Check that the marking a message requires us to login
295 post :mark, :message_id => messages(:unread_message).id
296 assert_response :forbidden
298 # Login as a user with no messages
299 session[:user] = users(:second_public_user).id
301 # Check that marking a message we didn't send or receive fails
302 post :mark, :message_id => messages(:read_message).id
303 assert_response :not_found
304 assert_template "no_such_message"
306 # Login as the message recipient
307 session[:user] = users(:public_user).id
309 # Check that the marking a message read works
310 post :mark, :message_id => messages(:unread_message).id, :mark => "read"
311 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
312 assert_equal true, Message.find(messages(:unread_message).id).message_read
314 # Check that the marking a message unread works
315 post :mark, :message_id => messages(:unread_message).id, :mark => "unread"
316 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
317 assert_equal false, Message.find(messages(:unread_message).id).message_read
319 # Check that the marking a message read via XHR works
320 xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "read"
321 assert_response :success
322 assert_template "mark"
323 assert_equal true, Message.find(messages(:unread_message).id).message_read
325 # Check that the marking a message unread via XHR works
326 xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "unread"
327 assert_response :success
328 assert_template "mark"
329 assert_equal false, Message.find(messages(:unread_message).id).message_read
331 # Asking to mark a message with no ID should fail
332 assert_raise ActionController::UrlGenerationError do
336 # Asking to mark a message with a bogus ID should fail
337 post :mark, :message_id => 99999
338 assert_response :not_found
339 assert_template "no_such_message"
343 # test the delete action
345 # Check that the deleting a message requires us to login
346 post :delete, :message_id => messages(:read_message).id
347 assert_response :forbidden
349 # Login as a user with no messages
350 session[:user] = users(:second_public_user).id
352 # Check that deleting a message we didn't send or receive fails
353 post :delete, :message_id => messages(:read_message).id
354 assert_response :not_found
355 assert_template "no_such_message"
357 # Login as the message recipient
358 session[:user] = users(:normal_user).id
360 # Check that the deleting a received message works
361 post :delete, :message_id => messages(:read_message).id
362 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
363 assert_equal "Message deleted", flash[:notice]
364 m = Message.find(messages(:read_message).id)
365 assert_equal true, m.from_user_visible
366 assert_equal false, m.to_user_visible
368 # Check that the deleting a sent message works
369 post :delete, :message_id => messages(:unread_message).id, :referer => outbox_path(:display_name => users(:normal_user).display_name)
370 assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
371 assert_equal "Message deleted", flash[:notice]
372 m = Message.find(messages(:unread_message).id)
373 assert_equal false, m.from_user_visible
374 assert_equal true, m.to_user_visible
376 # Asking to delete a message with no ID should fail
377 assert_raise ActionController::UrlGenerationError do
381 # Asking to delete a message with a bogus ID should fail
382 post :delete, :message_id => 99999
383 assert_response :not_found
384 assert_template "no_such_message"
389 def with_message_limit(value)
390 max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
391 Object.const_set("MAX_MESSAGES_PER_HOUR", value)
395 Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
396 Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)