]> git.openstreetmap.org Git - rails.git/blob - test/controllers/messages_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2167'
[rails.git] / test / controllers / messages_controller_test.rb
1 require "test_helper"
2
3 class MessagesControllerTest < ActionController::TestCase
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/messages/inbox", :method => :get },
9       { :controller => "messages", :action => "inbox" }
10     )
11     assert_routing(
12       { :path => "/messages/outbox", :method => :get },
13       { :controller => "messages", :action => "outbox" }
14     )
15     assert_routing(
16       { :path => "/message/new/username", :method => :get },
17       { :controller => "messages", :action => "new", :display_name => "username" }
18     )
19     assert_routing(
20       { :path => "/messages", :method => :post },
21       { :controller => "messages", :action => "create" }
22     )
23     assert_routing(
24       { :path => "/messages/1", :method => :get },
25       { :controller => "messages", :action => "show", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/messages/1/mark", :method => :post },
29       { :controller => "messages", :action => "mark", :message_id => "1" }
30     )
31     assert_routing(
32       { :path => "/messages/1/reply", :method => :get },
33       { :controller => "messages", :action => "reply", :message_id => "1" }
34     )
35     assert_routing(
36       { :path => "/messages/1/reply", :method => :post },
37       { :controller => "messages", :action => "reply", :message_id => "1" }
38     )
39     assert_routing(
40       { :path => "/messages/1", :method => :delete },
41       { :controller => "messages", :action => "destroy", :id => "1" }
42     )
43   end
44
45   ##
46   # test fetching new message page when not logged in
47   def test_new_no_login
48     # Check that the new message page requires us to login
49     user = create(:user)
50     get :new, :params => { :display_name => user.display_name }
51     assert_redirected_to login_path(:referer => new_message_path(:display_name => user.display_name))
52   end
53
54   ##
55   # test fetching new message page when logged in
56   def test_new_form
57     # Login as a normal user
58     user = create(:user)
59     recipient_user = create(:user)
60     session[:user] = user.id
61
62     # Check that the new message page loads
63     get :new, :params => { :display_name => recipient_user.display_name }
64     assert_response :success
65     assert_template "new"
66     assert_select "title", "Send message | OpenStreetMap"
67     assert_select "form[action='/messages']", :count => 1 do
68       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
69       assert_select "input#message_title", :count => 1
70       assert_select "textarea#message_body", :count => 1
71       assert_select "input[type='submit'][value='Send']", :count => 1
72     end
73   end
74
75   ##
76   # test fetching new message page with body and title
77   def test_new_get_with_params
78     # Login as a normal user
79     user = create(:user)
80     recipient_user = create(:user)
81     session[:user] = user.id
82
83     # Check that we can't send a message from a GET request
84     assert_difference "ActionMailer::Base.deliveries.size", 0 do
85       assert_difference "Message.count", 0 do
86         perform_enqueued_jobs do
87           get :new,
88               :params => { :display_name => recipient_user.display_name,
89                            :message => { :title => "Test Message", :body => "Test message body" } }
90         end
91       end
92     end
93     assert_response :success
94     assert_template "new"
95     assert_select "title", "Send message | OpenStreetMap"
96     assert_select "form[action='/messages']", :count => 1 do
97       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
98       assert_select "input#message_title", :count => 1 do
99         assert_select "[value='Test Message']"
100       end
101       assert_select "textarea#message_body", :text => "Test message body", :count => 1
102       assert_select "input[type='submit'][value='Send']", :count => 1
103     end
104   end
105
106   ##
107   # test posting new message page with no body
108   def test_new_post_no_body
109     # Login as a normal user
110     user = create(:user)
111     recipient_user = create(:user)
112     session[:user] = user.id
113
114     # Check that the subject is preserved over errors
115     assert_difference "ActionMailer::Base.deliveries.size", 0 do
116       assert_difference "Message.count", 0 do
117         perform_enqueued_jobs do
118           post :new,
119                :params => { :display_name => recipient_user.display_name,
120                             :message => { :title => "Test Message", :body => "" } }
121         end
122       end
123     end
124     assert_response :success
125     assert_template "new"
126     assert_select "title", "Send message | OpenStreetMap"
127     assert_select "form[action='/messages']", :count => 1 do
128       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
129       assert_select "input#message_title", :count => 1 do
130         assert_select "[value='Test Message']"
131       end
132       assert_select "textarea#message_body", :text => "", :count => 1
133       assert_select "input[type='submit'][value='Send']", :count => 1
134     end
135   end
136
137   ##
138   # test posting new message page with no title
139   def test_new_post_no_title
140     # Login as a normal user
141     user = create(:user)
142     recipient_user = create(:user)
143     session[:user] = user.id
144
145     # Check that the body text is preserved over errors
146     assert_difference "ActionMailer::Base.deliveries.size", 0 do
147       assert_difference "Message.count", 0 do
148         perform_enqueued_jobs do
149           post :new,
150                :params => { :display_name => recipient_user.display_name,
151                             :message => { :title => "", :body => "Test message body" } }
152         end
153       end
154     end
155     assert_response :success
156     assert_template "new"
157     assert_select "title", "Send message | OpenStreetMap"
158     assert_select "form[action='/messages']", :count => 1 do
159       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
160       assert_select "input#message_title", :count => 1 do
161         assert_select "[value='']"
162       end
163       assert_select "textarea#message_body", :text => "Test message body", :count => 1
164       assert_select "input[type='submit'][value='Send']", :count => 1
165     end
166   end
167
168   ##
169   # test posting new message page sends message
170   def test_new_post_send
171     # Login as a normal user
172     user = create(:user)
173     recipient_user = create(:user)
174     session[:user] = user.id
175
176     # Check that sending a message works
177     assert_difference "ActionMailer::Base.deliveries.size", 1 do
178       assert_difference "Message.count", 1 do
179         perform_enqueued_jobs do
180           post :create,
181                :params => { :display_name => recipient_user.display_name,
182                             :message => { :title => "Test Message", :body => "Test message body" } }
183         end
184       end
185     end
186     assert_redirected_to inbox_messages_path
187     assert_equal "Message sent", flash[:notice]
188     e = ActionMailer::Base.deliveries.first
189     assert_equal [recipient_user.email], e.to
190     assert_equal "[OpenStreetMap] Test Message", e.subject
191     assert_match(/Test message body/, e.text_part.decoded)
192     assert_match(/Test message body/, e.html_part.decoded)
193     assert_match %r{#{SERVER_URL}/messages/[0-9]+}, e.text_part.decoded
194     ActionMailer::Base.deliveries.clear
195     m = Message.last
196     assert_equal user.id, m.from_user_id
197     assert_equal recipient_user.id, m.to_user_id
198     assert_in_delta Time.now, m.sent_on, 2
199     assert_equal "Test Message", m.title
200     assert_equal "Test message body", m.body
201     assert_equal "markdown", m.body_format
202
203     # Asking to send a message with a bogus user name should fail
204     get :new, :params => { :display_name => "non_existent_user" }
205     assert_response :not_found
206     assert_template "users/no_such_user"
207     assert_select "h1", "The user non_existent_user does not exist"
208   end
209
210   ##
211   # test the new action message limit
212   def test_new_limit
213     # Login as a normal user
214     user = create(:user)
215     recipient_user = create(:user)
216     session[:user] = user.id
217
218     # Check that sending a message fails when the message limit is hit
219     assert_no_difference "ActionMailer::Base.deliveries.size" do
220       assert_no_difference "Message.count" do
221         with_message_limit(0) do
222           perform_enqueued_jobs do
223             post :create,
224                  :params => { :display_name => recipient_user.display_name,
225                               :message => { :title => "Test Message", :body => "Test message body" } }
226             assert_response :success
227             assert_template "new"
228             assert_select ".error", /wait a while/
229           end
230         end
231       end
232     end
233   end
234
235   ##
236   # test the reply action
237   def test_reply
238     user = create(:user)
239     recipient_user = create(:user)
240     other_user = create(:user)
241     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
242
243     # Check that the message reply page requires us to login
244     get :reply, :params => { :message_id => unread_message.id }
245     assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
246
247     # Login as the wrong user
248     session[:user] = other_user.id
249
250     # Check that we can't reply to somebody else's message
251     get :reply, :params => { :message_id => unread_message.id }
252     assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
253     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]
254
255     # Login as the right user
256     session[:user] = recipient_user.id
257
258     # Check that the message reply page loads
259     get :reply, :params => { :message_id => unread_message.id }
260     assert_response :success
261     assert_template "new"
262     assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
263     assert_select "form[action='/messages']", :count => 1 do
264       assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
265       assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
266       assert_select "textarea#message_body", :count => 1
267       assert_select "input[type='submit'][value='Send']", :count => 1
268     end
269     assert_equal true, Message.find(unread_message.id).message_read
270
271     # Asking to reply to a message with no ID should fail
272     assert_raise ActionController::UrlGenerationError do
273       get :reply
274     end
275
276     # Asking to reply to a message with a bogus ID should fail
277     get :reply, :params => { :message_id => 99999 }
278     assert_response :not_found
279     assert_template "no_such_message"
280   end
281
282   ##
283   # test the show action
284   def test_show
285     user = create(:user)
286     recipient_user = create(:user)
287     other_user = create(:user)
288     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
289
290     # Check that the show message page requires us to login
291     get :show, :params => { :id => unread_message.id }
292     assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
293
294     # Login as the wrong user
295     session[:user] = other_user.id
296
297     # Check that we can't read the message
298     get :show, :params => { :id => unread_message.id }
299     assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
300     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]
301
302     # Login as the message sender
303     session[:user] = user.id
304
305     # Check that the message sender can read the message
306     get :show, :params => { :id => unread_message.id }
307     assert_response :success
308     assert_template "show"
309     assert_equal false, Message.find(unread_message.id).message_read
310
311     # Login as the message recipient
312     session[:user] = recipient_user.id
313
314     # Check that the message recipient can read the message
315     get :show, :params => { :id => unread_message.id }
316     assert_response :success
317     assert_template "show"
318     assert_equal true, Message.find(unread_message.id).message_read
319
320     # Asking to read a message with no ID should fail
321     assert_raise ActionController::UrlGenerationError do
322       get :show
323     end
324
325     # Asking to read a message with a bogus ID should fail
326     get :show, :params => { :id => 99999 }
327     assert_response :not_found
328     assert_template "no_such_message"
329   end
330
331   ##
332   # test the inbox action
333   def test_inbox
334     user = create(:user)
335     read_message = create(:message, :read, :recipient => user)
336     # Check that the inbox page requires us to login
337     get :inbox
338     assert_redirected_to login_path(:referer => inbox_messages_path)
339
340     # Login
341     session[:user] = user.id
342
343     # Check that we can view our inbox when logged in
344     get :inbox
345     assert_response :success
346     assert_template "inbox"
347     assert_select "table.messages", :count => 1 do
348       assert_select "tr", :count => 2
349       assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
350     end
351   end
352
353   ##
354   # test the outbox action
355   def test_outbox
356     user = create(:user)
357     create(:message, :sender => user)
358
359     # Check that the outbox page requires us to login
360     get :outbox
361     assert_redirected_to login_path(:referer => outbox_messages_path)
362
363     # Login
364     session[:user] = user.id
365
366     # Check that we can view our outbox when logged in
367     get :outbox
368     assert_response :success
369     assert_template "outbox"
370     assert_select "table.messages", :count => 1 do
371       assert_select "tr", :count => 2
372       assert_select "tr.inbox-row", :count => 1
373     end
374   end
375
376   ##
377   # test the mark action
378   def test_mark
379     user = create(:user)
380     recipient_user = create(:user)
381     other_user = create(:user)
382     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
383
384     # Check that the marking a message requires us to login
385     post :mark, :params => { :message_id => unread_message.id }
386     assert_response :forbidden
387
388     # Login as a user with no messages
389     session[:user] = other_user.id
390
391     # Check that marking a message we didn't send or receive fails
392     post :mark, :params => { :message_id => unread_message.id }
393     assert_response :not_found
394     assert_template "no_such_message"
395
396     # Login as the message recipient_user
397     session[:user] = recipient_user.id
398
399     # Check that the marking a message read works
400     post :mark, :params => { :message_id => unread_message.id, :mark => "read" }
401     assert_redirected_to inbox_messages_path
402     assert_equal true, Message.find(unread_message.id).message_read
403
404     # Check that the marking a message unread works
405     post :mark, :params => { :message_id => unread_message.id, :mark => "unread" }
406     assert_redirected_to inbox_messages_path
407     assert_equal false, Message.find(unread_message.id).message_read
408
409     # Check that the marking a message read via XHR works
410     post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "read" }
411     assert_response :success
412     assert_template "mark"
413     assert_equal true, Message.find(unread_message.id).message_read
414
415     # Check that the marking a message unread via XHR works
416     post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "unread" }
417     assert_response :success
418     assert_template "mark"
419     assert_equal false, Message.find(unread_message.id).message_read
420
421     # Asking to mark a message with no ID should fail
422     assert_raise ActionController::UrlGenerationError do
423       post :mark
424     end
425
426     # Asking to mark a message with a bogus ID should fail
427     post :mark, :params => { :message_id => 99999 }
428     assert_response :not_found
429     assert_template "no_such_message"
430   end
431
432   ##
433   # test the destroy action
434   def test_destroy
435     user = create(:user)
436     second_user = create(:user)
437     other_user = create(:user)
438     read_message = create(:message, :read, :recipient => user, :sender => second_user)
439     sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
440
441     # Check that destroying a message requires us to login
442     delete :destroy, :params => { :id => read_message.id }
443     assert_response :forbidden
444
445     # Login as a user with no messages
446     session[:user] = other_user.id
447
448     # Check that destroying a message we didn't send or receive fails
449     delete :destroy, :params => { :id => read_message.id }
450     assert_response :not_found
451     assert_template "no_such_message"
452
453     # Login as the message recipient_user
454     session[:user] = user.id
455
456     # Check that the destroy a received message works
457     delete :destroy, :params => { :id => read_message.id }
458     assert_redirected_to inbox_messages_path
459     assert_equal "Message deleted", flash[:notice]
460     m = Message.find(read_message.id)
461     assert_equal true, m.from_user_visible
462     assert_equal false, m.to_user_visible
463
464     # Check that the destroying a sent message works
465     delete :destroy, :params => { :id => sent_message.id, :referer => outbox_messages_path }
466     assert_redirected_to outbox_messages_path
467     assert_equal "Message deleted", flash[:notice]
468     m = Message.find(sent_message.id)
469     assert_equal false, m.from_user_visible
470     assert_equal true, m.to_user_visible
471
472     # Asking to destroy a message with no ID should fail
473     assert_raise ActionController::UrlGenerationError do
474       post :destroy
475     end
476
477     # Asking to destroy a message with a bogus ID should fail
478     delete :destroy, :params => { :id => 99999 }
479     assert_response :not_found
480     assert_template "no_such_message"
481   end
482
483   private
484
485   def with_message_limit(value)
486     max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
487     Object.const_set("MAX_MESSAGES_PER_HOUR", value)
488
489     yield
490
491     Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
492     Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)
493   end
494 end