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