]> git.openstreetmap.org Git - rails.git/blob - test/controllers/message_controller_test.rb
Lazy lookups for translations in message_controller.rb
[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     assert_match %r{#{SERVER_URL}/message/read/}, e.text_part.decoded
182     ActionMailer::Base.deliveries.clear
183     m = Message.last
184     assert_equal user.id, m.from_user_id
185     assert_equal recipient_user.id, m.to_user_id
186     assert_in_delta Time.now, m.sent_on, 2
187     assert_equal "Test Message", m.title
188     assert_equal "Test message body", m.body
189     assert_equal "markdown", m.body_format
190
191     # Asking to send a message with a bogus user name should fail
192     get :new, :params => { :display_name => "non_existent_user" }
193     assert_response :not_found
194     assert_template "user/no_such_user"
195     assert_select "h1", "The user non_existent_user does not exist"
196   end
197
198   ##
199   # test the new action message limit
200   def test_new_limit
201     # Login as a normal user
202     user = create(:user)
203     recipient_user = create(:user)
204     session[:user] = user.id
205
206     # Check that sending a message fails when the message limit is hit
207     assert_no_difference "ActionMailer::Base.deliveries.size" do
208       assert_no_difference "Message.count" do
209         with_message_limit(0) do
210           post :new,
211                :params => { :display_name => recipient_user.display_name,
212                             :message => { :title => "Test Message", :body => "Test message body" } }
213           assert_response :success
214           assert_template "new"
215           assert_select ".error", /wait a while/
216         end
217       end
218     end
219   end
220
221   ##
222   # test the reply action
223   def test_reply
224     user = create(:user)
225     recipient_user = create(:user)
226     other_user = create(:user)
227     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
228
229     # Check that the message reply page requires us to login
230     get :reply, :params => { :message_id => unread_message.id }
231     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
232
233     # Login as the wrong user
234     session[:user] = other_user.id
235
236     # Check that we can't reply to somebody else's message
237     get :reply, :params => { :message_id => unread_message.id }
238     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
239     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]
240
241     # Login as the right user
242     session[:user] = recipient_user.id
243
244     # Check that the message reply page loads
245     get :reply, :params => { :message_id => unread_message.id }
246     assert_response :success
247     assert_template "new"
248     assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
249     assert_select "form[action='#{new_message_path(:display_name => user.display_name)}']", :count => 1 do
250       assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
251       assert_select "textarea#message_body", :count => 1
252       assert_select "input[type='submit'][value='Send']", :count => 1
253     end
254     assert_equal true, Message.find(unread_message.id).message_read
255
256     # Asking to reply to a message with no ID should fail
257     assert_raise ActionController::UrlGenerationError do
258       get :reply
259     end
260
261     # Asking to reply to a message with a bogus ID should fail
262     get :reply, :params => { :message_id => 99999 }
263     assert_response :not_found
264     assert_template "no_such_message"
265   end
266
267   ##
268   # test the read action
269   def test_read
270     user = create(:user)
271     recipient_user = create(:user)
272     other_user = create(:user)
273     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
274
275     # Check that the read message page requires us to login
276     get :read, :params => { :message_id => unread_message.id }
277     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
278
279     # Login as the wrong user
280     session[:user] = other_user.id
281
282     # Check that we can't read the message
283     get :read, :params => { :message_id => unread_message.id }
284     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
285     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]
286
287     # Login as the message sender
288     session[:user] = user.id
289
290     # Check that the message sender can read the message
291     get :read, :params => { :message_id => unread_message.id }
292     assert_response :success
293     assert_template "read"
294     assert_equal false, Message.find(unread_message.id).message_read
295
296     # Login as the message recipient
297     session[:user] = recipient_user.id
298
299     # Check that the message recipient can read the message
300     get :read, :params => { :message_id => unread_message.id }
301     assert_response :success
302     assert_template "read"
303     assert_equal true, Message.find(unread_message.id).message_read
304
305     # Asking to read a message with no ID should fail
306     assert_raise ActionController::UrlGenerationError do
307       get :read
308     end
309
310     # Asking to read a message with a bogus ID should fail
311     get :read, :params => { :message_id => 99999 }
312     assert_response :not_found
313     assert_template "no_such_message"
314   end
315
316   ##
317   # test the inbox action
318   def test_inbox
319     user = create(:user)
320     other_user = create(:user)
321     read_message = create(:message, :read, :recipient => user)
322     # Check that the inbox page requires us to login
323     get :inbox, :params => { :display_name => user.display_name }
324     assert_redirected_to login_path(:referer => inbox_path(:display_name => user.display_name))
325
326     # Login
327     session[:user] = user.id
328
329     # Check that we can view our inbox when logged in
330     get :inbox, :params => { :display_name => user.display_name }
331     assert_response :success
332     assert_template "inbox"
333     assert_select "table.messages", :count => 1 do
334       assert_select "tr", :count => 2
335       assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
336     end
337
338     # Check that we can't view somebody else's inbox when logged in
339     get :inbox, :params => { :display_name => other_user.display_name }
340     assert_redirected_to inbox_path(:display_name => user.display_name)
341   end
342
343   ##
344   # test the outbox action
345   def test_outbox
346     user = create(:user)
347     other_user = create(:user)
348     create(:message, :sender => user)
349
350     # Check that the outbox page requires us to login
351     get :outbox, :params => { :display_name => user.display_name }
352     assert_redirected_to login_path(:referer => outbox_path(:display_name => user.display_name))
353
354     # Login
355     session[:user] = user.id
356
357     # Check that we can view our outbox when logged in
358     get :outbox, :params => { :display_name => user.display_name }
359     assert_response :success
360     assert_template "outbox"
361     assert_select "table.messages", :count => 1 do
362       assert_select "tr", :count => 2
363       assert_select "tr.inbox-row", :count => 1
364     end
365
366     # Check that we can't view somebody else's outbox when logged in
367     get :outbox, :params => { :display_name => other_user.display_name }
368     assert_redirected_to outbox_path(:display_name => user.display_name)
369   end
370
371   ##
372   # test the mark action
373   def test_mark
374     user = create(:user)
375     recipient_user = create(:user)
376     other_user = create(:user)
377     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
378
379     # Check that the marking a message requires us to login
380     post :mark, :params => { :message_id => unread_message.id }
381     assert_response :forbidden
382
383     # Login as a user with no messages
384     session[:user] = other_user.id
385
386     # Check that marking a message we didn't send or receive fails
387     post :mark, :params => { :message_id => unread_message.id }
388     assert_response :not_found
389     assert_template "no_such_message"
390
391     # Login as the message recipient_user
392     session[:user] = recipient_user.id
393
394     # Check that the marking a message read works
395     post :mark, :params => { :message_id => unread_message.id, :mark => "read" }
396     assert_redirected_to inbox_path(:display_name => recipient_user.display_name)
397     assert_equal true, Message.find(unread_message.id).message_read
398
399     # Check that the marking a message unread works
400     post :mark, :params => { :message_id => unread_message.id, :mark => "unread" }
401     assert_redirected_to inbox_path(:display_name => recipient_user.display_name)
402     assert_equal false, Message.find(unread_message.id).message_read
403
404     # Check that the marking a message read via XHR works
405     post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "read" }
406     assert_response :success
407     assert_template "mark"
408     assert_equal true, Message.find(unread_message.id).message_read
409
410     # Check that the marking a message unread via XHR works
411     post :mark, :xhr => true, :params => { :message_id => unread_message.id, :mark => "unread" }
412     assert_response :success
413     assert_template "mark"
414     assert_equal false, Message.find(unread_message.id).message_read
415
416     # Asking to mark a message with no ID should fail
417     assert_raise ActionController::UrlGenerationError do
418       post :mark
419     end
420
421     # Asking to mark a message with a bogus ID should fail
422     post :mark, :params => { :message_id => 99999 }
423     assert_response :not_found
424     assert_template "no_such_message"
425   end
426
427   ##
428   # test the delete action
429   def test_delete
430     user = create(:user)
431     second_user = create(:user)
432     other_user = create(:user)
433     read_message = create(:message, :read, :recipient => user, :sender => second_user)
434     sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
435
436     # Check that the deleting a message requires us to login
437     post :delete, :params => { :message_id => read_message.id }
438     assert_response :forbidden
439
440     # Login as a user with no messages
441     session[:user] = other_user.id
442
443     # Check that deleting a message we didn't send or receive fails
444     post :delete, :params => { :message_id => read_message.id }
445     assert_response :not_found
446     assert_template "no_such_message"
447
448     # Login as the message recipient_user
449     session[:user] = user.id
450
451     # Check that the deleting a received message works
452     post :delete, :params => { :message_id => read_message.id }
453     assert_redirected_to inbox_path(:display_name => user.display_name)
454     assert_equal "Message deleted", flash[:notice]
455     m = Message.find(read_message.id)
456     assert_equal true, m.from_user_visible
457     assert_equal false, m.to_user_visible
458
459     # Check that the deleting a sent message works
460     post :delete, :params => { :message_id => sent_message.id, :referer => outbox_path(:display_name => user.display_name) }
461     assert_redirected_to outbox_path(:display_name => user.display_name)
462     assert_equal "Message deleted", flash[:notice]
463     m = Message.find(sent_message.id)
464     assert_equal false, m.from_user_visible
465     assert_equal true, m.to_user_visible
466
467     # Asking to delete a message with no ID should fail
468     assert_raise ActionController::UrlGenerationError do
469       post :delete
470     end
471
472     # Asking to delete a message with a bogus ID should fail
473     post :delete, :params => { :message_id => 99999 }
474     assert_response :not_found
475     assert_template "no_such_message"
476   end
477
478   private
479
480   def with_message_limit(value)
481     max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
482     Object.const_set("MAX_MESSAGES_PER_HOUR", value)
483
484     yield
485
486     Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
487     Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)
488   end
489 end