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