]> git.openstreetmap.org Git - rails.git/blob - test/controllers/messages_controller_test.rb
Merge pull request #7052 from openstreetmap/translatewiki
[rails.git] / test / controllers / messages_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class MessagesControllerTest < ActionDispatch::IntegrationTest
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/messages/new/username", :method => :get },
11       { :controller => "messages", :action => "new", :display_name => "username" }
12     )
13     assert_routing(
14       { :path => "/messages", :method => :post },
15       { :controller => "messages", :action => "create" }
16     )
17     assert_routing(
18       { :path => "/messages/1", :method => :get },
19       { :controller => "messages", :action => "show", :id => "1" }
20     )
21     assert_routing(
22       { :path => "/messages/1", :method => :delete },
23       { :controller => "messages", :action => "destroy", :id => "1" }
24     )
25   end
26
27   ##
28   # test fetching new message page when not logged in
29   def test_new_no_login
30     # Check that the new message page requires us to login
31     user = create(:user)
32     get new_message_path(user)
33     assert_redirected_to login_path(:referer => new_message_path(user))
34   end
35
36   ##
37   # test fetching new message page when logged in
38   def test_new_form
39     # Login as a normal user
40     user = create(:user)
41     recipient_user = create(:user)
42     session_for(user)
43
44     # Check that the new message page loads
45     get new_message_path(recipient_user)
46     assert_response :success
47     assert_template "new"
48     assert_select "title", "Send message | OpenStreetMap"
49     assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
50     assert_select "form[action='/messages']", :count => 1 do
51       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
52       assert_select "input#message_title", :count => 1
53       assert_select "textarea#message_body", :count => 1
54       assert_select "input[type='submit'][value='Send']", :count => 1
55     end
56   end
57
58   ##
59   # test fetching new message page with body and title
60   def test_new_get_with_params
61     # Login as a normal user
62     user = create(:user)
63     recipient_user = create(:user)
64     session_for(user)
65
66     # Check that we can't send a message from a GET request
67     assert_difference "ActionMailer::Base.deliveries.size", 0 do
68       assert_difference "Message.count", 0 do
69         perform_enqueued_jobs do
70           get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
71         end
72       end
73     end
74     assert_response :success
75     assert_template "new"
76     assert_select "title", "Send message | OpenStreetMap"
77     assert_select "form[action='/messages']", :count => 1 do
78       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
79       assert_select "input#message_title", :count => 1 do
80         assert_select "[value='Test Message']"
81       end
82       assert_select "textarea#message_body", :text => "Test message body", :count => 1
83       assert_select "input[type='submit'][value='Send']", :count => 1
84     end
85   end
86
87   ##
88   # test posting new message page with no body
89   def test_new_post_no_body
90     # Login as a normal user
91     user = create(:user)
92     recipient_user = create(:user)
93     session_for(user)
94
95     # Check that the subject is preserved over errors
96     assert_difference "ActionMailer::Base.deliveries.size", 0 do
97       assert_difference "Message.count", 0 do
98         perform_enqueued_jobs do
99           post messages_path(:display_name => recipient_user.display_name,
100                              :message => { :title => "Test Message", :body => "" })
101         end
102       end
103     end
104     assert_response :success
105     assert_template "new"
106     assert_select "title", "Send message | OpenStreetMap"
107     assert_select "form[action='/messages']", :count => 1 do
108       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
109       assert_select "input#message_title", :count => 1 do
110         assert_select "[value='Test Message']"
111       end
112       assert_select "textarea#message_body", :text => "", :count => 1
113       assert_select "input[type='submit'][value='Send']", :count => 1
114     end
115   end
116
117   ##
118   # test posting new message page with no title
119   def test_new_post_no_title
120     # Login as a normal user
121     user = create(:user)
122     recipient_user = create(:user)
123     session_for(user)
124
125     # Check that the body text is preserved over errors
126     assert_difference "ActionMailer::Base.deliveries.size", 0 do
127       assert_difference "Message.count", 0 do
128         perform_enqueued_jobs do
129           post messages_path(:display_name => recipient_user.display_name,
130                              :message => { :title => "", :body => "Test message body" })
131         end
132       end
133     end
134     assert_response :success
135     assert_template "new"
136     assert_select "title", "Send message | OpenStreetMap"
137     assert_select "form[action='/messages']", :count => 1 do
138       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
139       assert_select "input#message_title", :count => 1 do
140         assert_select "[value='']"
141       end
142       assert_select "textarea#message_body", :text => "Test message body", :count => 1
143       assert_select "input[type='submit'][value='Send']", :count => 1
144     end
145   end
146
147   ##
148   # test posting new message page sends message
149   def test_new_post_send
150     # Login as a normal user
151     user = create(:user)
152     recipient_user = create(:user)
153     session_for(user)
154
155     # Check that sending a message works
156     assert_difference "ActionMailer::Base.deliveries.size", 1 do
157       assert_difference "Message.count", 1 do
158         perform_enqueued_jobs do
159           post messages_path(:display_name => recipient_user.display_name,
160                              :message => { :title => "Test Message", :body => "Test message body" })
161         end
162       end
163     end
164     assert_redirected_to messages_outbox_path
165     assert_equal "Message sent", flash[:notice]
166     e = ActionMailer::Base.deliveries.first
167     assert_equal [recipient_user.email], e.to
168     assert_equal "[OpenStreetMap] Test Message", e.subject
169     assert_match(/Test message body/, e.text_part.decoded)
170     assert_match(/Test message body/, e.html_part.decoded)
171     assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
172
173     m = Message.last
174     assert_equal user.id, m.from_user_id
175     assert_equal recipient_user.id, m.to_user_id
176     assert_in_delta Time.now.utc, m.sent_on, 2
177     assert_equal "Test Message", m.title
178     assert_equal "Test message body", m.body
179     assert_equal "markdown", m.body_format
180
181     # Asking to send a message with a bogus user name should fail
182     get new_message_path("non_existent_user")
183     assert_response :not_found
184     assert_template "users/no_such_user"
185     assert_select "h1", "The user non_existent_user does not exist"
186   end
187
188   ##
189   # test the new action message limit
190   def test_new_limit
191     # Login as a normal user
192     user = create(:user)
193     recipient_user = create(:user)
194     message = create(:message, :unread, :sender => user, :recipient => recipient_user)
195     session_for(user)
196
197     # Check that sending a message fails when the message limit is hit
198     assert_no_difference "ActionMailer::Base.deliveries.size" do
199       assert_no_difference "Message.count" do
200         with_settings(:max_messages_per_hour => 1) do
201           perform_enqueued_jobs do
202             post messages_path(:display_name => recipient_user.display_name,
203                                :message => { :title => "Test Message", :body => "Test message body" })
204             assert_response :success
205             assert_template "new"
206             assert_select ".alert.alert-danger", /wait a while/
207           end
208         end
209       end
210     end
211
212     message.update(:from_user_visible => false)
213
214     # Check that sending a message still fails when message is deleted
215     assert_no_difference "ActionMailer::Base.deliveries.size" do
216       assert_no_difference "Message.count" do
217         with_settings(:max_messages_per_hour => 1) 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 show action
232   def test_show
233     user = create(:user)
234     recipient_user = create(:user)
235     other_user = create(:user)
236     message = create(:message, :unread, :sender => user, :recipient => recipient_user)
237
238     # Check that the show message page requires us to login
239     get message_path(message)
240     assert_redirected_to login_path(:referer => message_path(message))
241
242     # Login as the wrong user
243     session_for(other_user)
244
245     # Check that we can't read the message
246     get message_path(message)
247     assert_redirected_to login_path(:referer => message_path(message))
248     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]
249
250     # Login as the message sender
251     session_for(user)
252
253     # Check that the message sender can read the message and that Reply button is available
254     get message_path(message)
255     assert_response :success
256     assert_template "show"
257     assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
258     assert_select "a.btn.btn-primary", :text => "Reply"
259     assert_not Message.find(message.id).message_read
260
261     # Login as the message recipient
262     session_for(recipient_user)
263
264     # Check that the message recipient can read the message and that Reply button is available
265     get message_path(message)
266     assert_response :success
267     assert_template "show"
268     assert_select "a[href='#{user_path user}']", :text => user.display_name
269     assert_select "a.btn.btn-primary", :text => "Reply"
270     assert Message.find(message.id).message_read
271
272     # Asking to read a message with a bogus ID should fail
273     get message_path(99999)
274     assert_response :not_found
275     assert_template "no_such_message"
276   end
277
278   ##
279   # test the destroy action
280   def test_destroy
281     user = create(:user)
282     second_user = create(:user)
283     other_user = create(:user)
284     read_message = create(:message, :read, :recipient => user, :sender => second_user)
285     sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
286
287     # Check that destroying a message requires us to login
288     delete message_path(read_message)
289     assert_response :forbidden
290
291     # Login as a user with no messages
292     session_for(other_user)
293
294     # Check that destroying a message we didn't send or receive fails
295     delete message_path(read_message)
296     assert_response :not_found
297     assert_template "no_such_message"
298
299     # Login as the message recipient_user
300     session_for(user)
301
302     # Check that the destroy a received message works
303     delete message_path(read_message)
304     assert_redirected_to messages_inbox_path
305     assert_equal "Message deleted", flash[:notice]
306     m = Message.find(read_message.id)
307     assert m.from_user_visible
308     assert_not m.to_user_visible
309
310     # Check that the destroying a sent message works
311     delete message_path(sent_message, :referer => messages_outbox_path)
312     assert_redirected_to messages_outbox_path
313     assert_equal "Message deleted", flash[:notice]
314     m = Message.find(sent_message.id)
315     assert_not m.from_user_visible
316     assert m.to_user_visible
317
318     # Asking to destroy a message with a bogus ID should fail
319     delete message_path(99999)
320     assert_response :not_found
321     assert_template "no_such_message"
322   end
323 end