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