3 class MessagesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/messages/new/username", :method => :get },
9 { :controller => "messages", :action => "new", :display_name => "username" }
12 { :path => "/messages", :method => :post },
13 { :controller => "messages", :action => "create" }
16 { :path => "/messages/1", :method => :get },
17 { :controller => "messages", :action => "show", :id => "1" }
20 { :path => "/messages/1/mark", :method => :post },
21 { :controller => "messages", :action => "mark", :message_id => "1" }
24 { :path => "/messages/1", :method => :delete },
25 { :controller => "messages", :action => "destroy", :id => "1" }
30 # test fetching new message page when not logged in
32 # Check that the new message page requires us to login
34 get new_message_path(user)
35 assert_redirected_to login_path(:referer => new_message_path(user))
39 # test fetching new message page when logged in
41 # Login as a normal user
43 recipient_user = create(:user)
46 # Check that the new message page loads
47 get new_message_path(recipient_user)
48 assert_response :success
50 assert_select "title", "Send message | OpenStreetMap"
51 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
52 assert_select "form[action='/messages']", :count => 1 do
53 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
54 assert_select "input#message_title", :count => 1
55 assert_select "textarea#message_body", :count => 1
56 assert_select "input[type='submit'][value='Send']", :count => 1
61 # test fetching new message page with body and title
62 def test_new_get_with_params
63 # Login as a normal user
65 recipient_user = create(:user)
68 # Check that we can't send a message from a GET request
69 assert_difference "ActionMailer::Base.deliveries.size", 0 do
70 assert_difference "Message.count", 0 do
71 perform_enqueued_jobs do
72 get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
76 assert_response :success
78 assert_select "title", "Send message | OpenStreetMap"
79 assert_select "form[action='/messages']", :count => 1 do
80 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
81 assert_select "input#message_title", :count => 1 do
82 assert_select "[value='Test Message']"
84 assert_select "textarea#message_body", :text => "Test message body", :count => 1
85 assert_select "input[type='submit'][value='Send']", :count => 1
90 # test posting new message page with no body
91 def test_new_post_no_body
92 # Login as a normal user
94 recipient_user = create(:user)
97 # Check that the subject is preserved over errors
98 assert_difference "ActionMailer::Base.deliveries.size", 0 do
99 assert_difference "Message.count", 0 do
100 perform_enqueued_jobs do
101 post messages_path(:display_name => recipient_user.display_name,
102 :message => { :title => "Test Message", :body => "" })
106 assert_response :success
107 assert_template "new"
108 assert_select "title", "Send message | OpenStreetMap"
109 assert_select "form[action='/messages']", :count => 1 do
110 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
111 assert_select "input#message_title", :count => 1 do
112 assert_select "[value='Test Message']"
114 assert_select "textarea#message_body", :text => "", :count => 1
115 assert_select "input[type='submit'][value='Send']", :count => 1
120 # test posting new message page with no title
121 def test_new_post_no_title
122 # Login as a normal user
124 recipient_user = create(:user)
127 # Check that the body text is preserved over errors
128 assert_difference "ActionMailer::Base.deliveries.size", 0 do
129 assert_difference "Message.count", 0 do
130 perform_enqueued_jobs do
131 post messages_path(:display_name => recipient_user.display_name,
132 :message => { :title => "", :body => "Test message body" })
136 assert_response :success
137 assert_template "new"
138 assert_select "title", "Send message | OpenStreetMap"
139 assert_select "form[action='/messages']", :count => 1 do
140 assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
141 assert_select "input#message_title", :count => 1 do
142 assert_select "[value='']"
144 assert_select "textarea#message_body", :text => "Test message body", :count => 1
145 assert_select "input[type='submit'][value='Send']", :count => 1
150 # test posting new message page sends message
151 def test_new_post_send
152 # Login as a normal user
154 recipient_user = create(:user)
157 # Check that sending a message works
158 assert_difference "ActionMailer::Base.deliveries.size", 1 do
159 assert_difference "Message.count", 1 do
160 perform_enqueued_jobs do
161 post messages_path(:display_name => recipient_user.display_name,
162 :message => { :title => "Test Message", :body => "Test message body" })
166 assert_redirected_to messages_outbox_path
167 assert_equal "Message sent", flash[:notice]
168 e = ActionMailer::Base.deliveries.first
169 assert_equal [recipient_user.email], e.to
170 assert_equal "[OpenStreetMap] Test Message", e.subject
171 assert_match(/Test message body/, e.text_part.decoded)
172 assert_match(/Test message body/, e.html_part.decoded)
173 assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
174 ActionMailer::Base.deliveries.clear
176 assert_equal user.id, m.from_user_id
177 assert_equal recipient_user.id, m.to_user_id
178 assert_in_delta Time.now.utc, m.sent_on, 2
179 assert_equal "Test Message", m.title
180 assert_equal "Test message body", m.body
181 assert_equal "markdown", m.body_format
183 # Asking to send a message with a bogus user name should fail
184 get new_message_path("non_existent_user")
185 assert_response :not_found
186 assert_template "users/no_such_user"
187 assert_select "h1", "The user non_existent_user does not exist"
191 # test the new action message limit
193 # Login as a normal user
195 recipient_user = create(:user)
198 # Check that sending a message fails when the message limit is hit
199 assert_no_difference "ActionMailer::Base.deliveries.size" do
200 assert_no_difference "Message.count" do
201 with_settings(:max_messages_per_hour => 0) do
202 perform_enqueued_jobs do
203 post messages_path(:display_name => recipient_user.display_name,
204 :message => { :title => "Test Message", :body => "Test message body" })
205 assert_response :success
206 assert_template "new"
207 assert_select ".alert.alert-danger", /wait a while/
215 # test the show action
218 recipient_user = create(:user)
219 other_user = create(:user)
220 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
222 # Check that the show message page requires us to login
223 get message_path(message)
224 assert_redirected_to login_path(:referer => message_path(message))
226 # Login as the wrong user
227 session_for(other_user)
229 # Check that we can't read the message
230 get message_path(message)
231 assert_redirected_to login_path(:referer => message_path(message))
232 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 log in as the correct user in order to read it.", flash[:notice]
234 # Login as the message sender
237 # Check that the message sender can read the message and that Reply button is available
238 get message_path(message)
239 assert_response :success
240 assert_template "show"
241 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
242 assert_select "a.btn.btn-primary", :text => "Reply"
243 assert_not Message.find(message.id).message_read
245 # Login as the message recipient
246 session_for(recipient_user)
248 # Check that the message recipient can read the message and that Reply button is available
249 get message_path(message)
250 assert_response :success
251 assert_template "show"
252 assert_select "a[href='#{user_path user}']", :text => user.display_name
253 assert_select "a.btn.btn-primary", :text => "Reply"
254 assert Message.find(message.id).message_read
256 # Asking to read a message with a bogus ID should fail
257 get message_path(99999)
258 assert_response :not_found
259 assert_template "no_such_message"
263 # test the mark action
265 sender_user = create(:user)
266 recipient_user = create(:user)
267 other_user = create(:user)
268 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
270 # Check that the marking a message requires us to login
271 post message_mark_path(message)
272 assert_response :forbidden
274 # Login as a user with no messages
275 session_for(other_user)
277 # Check that marking a message we didn't send or receive fails
278 post message_mark_path(message)
279 assert_response :not_found
280 assert_template "no_such_message"
282 # Login as the message sender_user
283 session_for(sender_user)
285 # Check that marking a message we sent fails
286 post message_mark_path(message)
287 assert_response :not_found
288 assert_template "no_such_message"
290 # Login as the message recipient_user
291 session_for(recipient_user)
293 # Check that the marking a message read works
294 post message_mark_path(message, :mark => "read")
295 assert_redirected_to messages_inbox_path
296 assert Message.find(message.id).message_read
298 # Check that the marking a message unread works
299 post message_mark_path(message, :mark => "unread")
300 assert_redirected_to messages_inbox_path
301 assert_not Message.find(message.id).message_read
303 # Check that the marking a message read works and redirects to inbox from the message page
304 post message_mark_path(message, :mark => "read"), :headers => { :referer => message_path(message) }
305 assert_redirected_to messages_inbox_path
306 assert Message.find(message.id).message_read
308 # Check that the marking a message unread works and redirects to inbox from the message page
309 post message_mark_path(message, :mark => "unread"), :headers => { :referer => message_path(message) }
310 assert_redirected_to messages_inbox_path
311 assert_not Message.find(message.id).message_read
313 # Asking to mark a message with a bogus ID should fail
314 post message_mark_path(99999)
315 assert_response :not_found
316 assert_template "no_such_message"
320 # test the mark action for messages from muted users
322 sender_user = create(:user)
323 recipient_user = create(:user)
324 create(:user_mute, :owner => recipient_user, :subject => sender_user)
325 message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user)
327 session_for(recipient_user)
329 # Check that the marking a message read works
330 post message_mark_path(message, :mark => "read")
331 assert_redirected_to messages_muted_inbox_path
332 assert Message.find(message.id).message_read
334 # Check that the marking a message unread works
335 post message_mark_path(message, :mark => "unread")
336 assert_redirected_to messages_muted_inbox_path
337 assert_not Message.find(message.id).message_read
341 # test the destroy action
344 second_user = create(:user)
345 other_user = create(:user)
346 read_message = create(:message, :read, :recipient => user, :sender => second_user)
347 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
349 # Check that destroying a message requires us to login
350 delete message_path(read_message)
351 assert_response :forbidden
353 # Login as a user with no messages
354 session_for(other_user)
356 # Check that destroying a message we didn't send or receive fails
357 delete message_path(read_message)
358 assert_response :not_found
359 assert_template "no_such_message"
361 # Login as the message recipient_user
364 # Check that the destroy a received message works
365 delete message_path(read_message)
366 assert_redirected_to messages_inbox_path
367 assert_equal "Message deleted", flash[:notice]
368 m = Message.find(read_message.id)
369 assert m.from_user_visible
370 assert_not m.to_user_visible
372 # Check that the destroying a sent message works
373 delete message_path(sent_message, :referer => messages_outbox_path)
374 assert_redirected_to messages_outbox_path
375 assert_equal "Message deleted", flash[:notice]
376 m = Message.find(sent_message.id)
377 assert_not m.from_user_visible
378 assert m.to_user_visible
380 # Asking to destroy a message with a bogus ID should fail
381 delete message_path(99999)
382 assert_response :not_found
383 assert_template "no_such_message"