]> git.openstreetmap.org Git - rails.git/blob - test/controllers/messages_controller_test.rb
Shorten user path helper calls in tests
[rails.git] / test / controllers / messages_controller_test.rb
1 require "test_helper"
2
3 class MessagesControllerTest < ActionDispatch::IntegrationTest
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_message_path(user)
51     assert_redirected_to login_path(:referer => new_message_path(user))
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_for(user)
61
62     # Check that the new message page loads
63     get new_message_path(recipient_user)
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_for(user)
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_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
88         end
89       end
90     end
91     assert_response :success
92     assert_template "new"
93     assert_select "title", "Send message | OpenStreetMap"
94     assert_select "form[action='/messages']", :count => 1 do
95       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
96       assert_select "input#message_title", :count => 1 do
97         assert_select "[value='Test Message']"
98       end
99       assert_select "textarea#message_body", :text => "Test message body", :count => 1
100       assert_select "input[type='submit'][value='Send']", :count => 1
101     end
102   end
103
104   ##
105   # test posting new message page with no body
106   def test_new_post_no_body
107     # Login as a normal user
108     user = create(:user)
109     recipient_user = create(:user)
110     session_for(user)
111
112     # Check that the subject is preserved over errors
113     assert_difference "ActionMailer::Base.deliveries.size", 0 do
114       assert_difference "Message.count", 0 do
115         perform_enqueued_jobs do
116           post messages_path(:display_name => recipient_user.display_name,
117                              :message => { :title => "Test Message", :body => "" })
118         end
119       end
120     end
121     assert_response :success
122     assert_template "new"
123     assert_select "title", "Send message | OpenStreetMap"
124     assert_select "form[action='/messages']", :count => 1 do
125       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
126       assert_select "input#message_title", :count => 1 do
127         assert_select "[value='Test Message']"
128       end
129       assert_select "textarea#message_body", :text => "", :count => 1
130       assert_select "input[type='submit'][value='Send']", :count => 1
131     end
132   end
133
134   ##
135   # test posting new message page with no title
136   def test_new_post_no_title
137     # Login as a normal user
138     user = create(:user)
139     recipient_user = create(:user)
140     session_for(user)
141
142     # Check that the body text is preserved over errors
143     assert_difference "ActionMailer::Base.deliveries.size", 0 do
144       assert_difference "Message.count", 0 do
145         perform_enqueued_jobs do
146           post messages_path(:display_name => recipient_user.display_name,
147                              :message => { :title => "", :body => "Test message body" })
148         end
149       end
150     end
151     assert_response :success
152     assert_template "new"
153     assert_select "title", "Send message | OpenStreetMap"
154     assert_select "form[action='/messages']", :count => 1 do
155       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
156       assert_select "input#message_title", :count => 1 do
157         assert_select "[value='']"
158       end
159       assert_select "textarea#message_body", :text => "Test message body", :count => 1
160       assert_select "input[type='submit'][value='Send']", :count => 1
161     end
162   end
163
164   ##
165   # test posting new message page sends message
166   def test_new_post_send
167     # Login as a normal user
168     user = create(:user)
169     recipient_user = create(:user)
170     session_for(user)
171
172     # Check that sending a message works
173     assert_difference "ActionMailer::Base.deliveries.size", 1 do
174       assert_difference "Message.count", 1 do
175         perform_enqueued_jobs do
176           post messages_path(:display_name => recipient_user.display_name,
177                              :message => { :title => "Test Message", :body => "Test message body" })
178         end
179       end
180     end
181     assert_redirected_to inbox_messages_path
182     assert_equal "Message sent", flash[:notice]
183     e = ActionMailer::Base.deliveries.first
184     assert_equal [recipient_user.email], e.to
185     assert_equal "[OpenStreetMap] Test Message", e.subject
186     assert_match(/Test message body/, e.text_part.decoded)
187     assert_match(/Test message body/, e.html_part.decoded)
188     assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
189     ActionMailer::Base.deliveries.clear
190     m = Message.last
191     assert_equal user.id, m.from_user_id
192     assert_equal recipient_user.id, m.to_user_id
193     assert_in_delta Time.now.utc, m.sent_on, 2
194     assert_equal "Test Message", m.title
195     assert_equal "Test message body", m.body
196     assert_equal "markdown", m.body_format
197
198     # Asking to send a message with a bogus user name should fail
199     get new_message_path("non_existent_user")
200     assert_response :not_found
201     assert_template "users/no_such_user"
202     assert_select "h1", "The user non_existent_user does not exist"
203   end
204
205   ##
206   # test the new action message limit
207   def test_new_limit
208     # Login as a normal user
209     user = create(:user)
210     recipient_user = create(:user)
211     session_for(user)
212
213     # Check that sending a message fails when the message limit is hit
214     assert_no_difference "ActionMailer::Base.deliveries.size" do
215       assert_no_difference "Message.count" do
216         with_settings(:max_messages_per_hour => 0) do
217           perform_enqueued_jobs do
218             post messages_path(:display_name => recipient_user.display_name,
219                                :message => { :title => "Test Message", :body => "Test message body" })
220             assert_response :success
221             assert_template "new"
222             assert_select ".alert.alert-danger", /wait a while/
223           end
224         end
225       end
226     end
227   end
228
229   ##
230   # test the reply action
231   def test_reply
232     user = create(:user)
233     recipient_user = create(:user)
234     other_user = create(:user)
235     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
236
237     # Check that the message reply page requires us to login
238     get message_reply_path(:message_id => unread_message)
239     assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
240
241     # Login as the wrong user
242     session_for(other_user)
243
244     # Check that we can't reply to somebody else's message
245     get message_reply_path(:message_id => unread_message)
246     assert_redirected_to login_path(:referer => message_reply_path(:message_id => unread_message.id))
247     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 log in as the correct user in order to reply.", flash[:notice]
248
249     # Login as the right user
250     session_for(recipient_user)
251
252     # Check that the message reply page loads
253     get message_reply_path(:message_id => unread_message)
254     assert_response :success
255     assert_template "new"
256     assert_select "title", "Re: #{unread_message.title} | OpenStreetMap"
257     assert_select "form[action='/messages']", :count => 1 do
258       assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
259       assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
260       assert_select "textarea#message_body", :count => 1
261       assert_select "input[type='submit'][value='Send']", :count => 1
262     end
263     assert Message.find(unread_message.id).message_read
264
265     # Asking to reply to a message with no ID should fail
266     get message_reply_path
267     assert_response :success
268
269     # Asking to reply to a message with a bogus ID should fail
270     get message_reply_path(:message_id => 99999)
271     assert_response :not_found
272     assert_template "no_such_message"
273   end
274
275   ##
276   # test the show action
277   def test_show
278     user = create(:user)
279     recipient_user = create(:user)
280     other_user = create(:user)
281     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
282
283     # Check that the show message page requires us to login
284     get message_path(:id => unread_message)
285     assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
286
287     # Login as the wrong user
288     session_for(other_user)
289
290     # Check that we can't read the message
291     get message_path(:id => unread_message)
292     assert_redirected_to login_path(:referer => message_path(:id => unread_message.id))
293     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]
294
295     # Login as the message sender
296     session_for(user)
297
298     # Check that the message sender can read the message
299     get message_path(:id => unread_message)
300     assert_response :success
301     assert_template "show"
302     assert_not Message.find(unread_message.id).message_read
303
304     # Login as the message recipient
305     session_for(recipient_user)
306
307     # Check that the message recipient can read the message
308     get message_path(:id => unread_message)
309     assert_response :success
310     assert_template "show"
311     assert Message.find(unread_message.id).message_read
312
313     # Asking to read a message with no ID should fail
314     get message_path
315     assert_response :success
316
317     # Asking to read a message with a bogus ID should fail
318     get message_path(:id => 99999)
319     assert_response :not_found
320     assert_template "no_such_message"
321   end
322
323   ##
324   # test the inbox action
325   def test_inbox
326     user = create(:user)
327     read_message = create(:message, :read, :recipient => user)
328     # Check that the inbox page requires us to login
329     get inbox_messages_path
330     assert_redirected_to login_path(:referer => inbox_messages_path)
331
332     # Login
333     session_for(user)
334
335     # Check that we can view our inbox when logged in
336     get inbox_messages_path
337     assert_response :success
338     assert_template "inbox"
339     assert_select ".content-inner > table", :count => 1 do
340       assert_select "tr", :count => 2
341       assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
342     end
343   end
344
345   ##
346   # test the outbox action
347   def test_outbox
348     user = create(:user)
349     create(:message, :sender => user)
350
351     # Check that the outbox page requires us to login
352     get outbox_messages_path
353     assert_redirected_to login_path(:referer => outbox_messages_path)
354
355     # Login
356     session_for(user)
357
358     # Check that we can view our outbox when logged in
359     get outbox_messages_path
360     assert_response :success
361     assert_template "outbox"
362     assert_select ".content-inner > table", :count => 1 do
363       assert_select "tr", :count => 2
364       assert_select "tr.inbox-row", :count => 1
365     end
366   end
367
368   ##
369   # test the mark action
370   def test_mark
371     user = create(:user)
372     recipient_user = create(:user)
373     other_user = create(:user)
374     unread_message = create(:message, :unread, :sender => user, :recipient => recipient_user)
375
376     # Check that the marking a message requires us to login
377     post message_mark_path(:message_id => unread_message)
378     assert_response :forbidden
379
380     # Login as a user with no messages
381     session_for(other_user)
382
383     # Check that marking a message we didn't send or receive fails
384     post message_mark_path(:message_id => unread_message)
385     assert_response :not_found
386     assert_template "no_such_message"
387
388     # Login as the message recipient_user
389     session_for(recipient_user)
390
391     # Check that the marking a message read works
392     post message_mark_path(:message_id => unread_message, :mark => "read")
393     assert_redirected_to inbox_messages_path
394     assert Message.find(unread_message.id).message_read
395
396     # Check that the marking a message unread works
397     post message_mark_path(:message_id => unread_message, :mark => "unread")
398     assert_redirected_to inbox_messages_path
399     assert_not Message.find(unread_message.id).message_read
400
401     # Check that the marking a message read via XHR works
402     post message_mark_path(:message_id => unread_message, :mark => "read"), :xhr => true
403     assert_response :success
404     assert_template "mark"
405     assert Message.find(unread_message.id).message_read
406
407     # Check that the marking a message unread via XHR works
408     post message_mark_path(:message_id => unread_message, :mark => "unread"), :xhr => true
409     assert_response :success
410     assert_template "mark"
411     assert_not Message.find(unread_message.id).message_read
412
413     # Asking to mark a message with no ID should fail
414     post message_mark_path
415     assert_redirected_to inbox_messages_path
416
417     # Asking to mark a message with a bogus ID should fail
418     post message_mark_path(: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     delete message_path(:id => read_message)
434     assert_response :forbidden
435
436     # Login as a user with no messages
437     session_for(other_user)
438
439     # Check that destroying a message we didn't send or receive fails
440     delete message_path(:id => read_message)
441     assert_response :not_found
442     assert_template "no_such_message"
443
444     # Login as the message recipient_user
445     session_for(user)
446
447     # Check that the destroy a received message works
448     delete message_path(:id => read_message)
449     assert_redirected_to inbox_messages_path
450     assert_equal "Message deleted", flash[:notice]
451     m = Message.find(read_message.id)
452     assert m.from_user_visible
453     assert_not m.to_user_visible
454
455     # Check that the destroying a sent message works
456     delete message_path(:id => sent_message, :referer => outbox_messages_path)
457     assert_redirected_to outbox_messages_path
458     assert_equal "Message deleted", flash[:notice]
459     m = Message.find(sent_message.id)
460     assert_not m.from_user_visible
461     assert m.to_user_visible
462
463     # Asking to destroy a message with no ID should fail
464     delete message_path
465     assert_redirected_to inbox_messages_path
466
467     # Asking to destroy a message with a bogus ID should fail
468     delete message_path(:id => 99999)
469     assert_response :not_found
470     assert_template "no_such_message"
471   end
472 end