1 # frozen_string_literal: true
5 class MessagesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/messages/new/username", :method => :get },
11 { :controller => "messages", :action => "new", :display_name => "username" }
14 { :path => "/messages", :method => :post },
15 { :controller => "messages", :action => "create" }
18 { :path => "/messages/1", :method => :get },
19 { :controller => "messages", :action => "show", :id => "1" }
22 { :path => "/messages/1", :method => :delete },
23 { :controller => "messages", :action => "destroy", :id => "1" }
28 # test fetching new message page when not logged in
30 # Check that the new message page requires us to login
32 get new_message_path(user)
33 assert_redirected_to login_path(:referer => new_message_path(user))
37 # test fetching new message page when logged in
39 # Login as a normal user
41 recipient_user = create(:user)
44 # Check that the new message page loads
45 get new_message_path(recipient_user)
46 assert_response :success
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
59 # test fetching new message page with body and title
60 def test_new_get_with_params
61 # Login as a normal user
63 recipient_user = create(:user)
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" })
74 assert_response :success
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']"
82 assert_select "textarea#message_body", :text => "Test message body", :count => 1
83 assert_select "input[type='submit'][value='Send']", :count => 1
88 # test posting new message page with no body
89 def test_new_post_no_body
90 # Login as a normal user
92 recipient_user = create(:user)
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 => "" })
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']"
112 assert_select "textarea#message_body", :text => "", :count => 1
113 assert_select "input[type='submit'][value='Send']", :count => 1
118 # test posting new message page with no title
119 def test_new_post_no_title
120 # Login as a normal user
122 recipient_user = create(:user)
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" })
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='']"
142 assert_select "textarea#message_body", :text => "Test message body", :count => 1
143 assert_select "input[type='submit'][value='Send']", :count => 1
148 # test posting new message page sends message
149 def test_new_post_send
150 # Login as a normal user
152 recipient_user = create(:user)
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" })
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
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
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"
189 # test the new action message limit
191 # Login as a normal user
193 recipient_user = create(:user)
196 # Check that sending a message fails when the message limit is hit
197 assert_no_difference "ActionMailer::Base.deliveries.size" do
198 assert_no_difference "Message.count" do
199 with_settings(:max_messages_per_hour => 0) do
200 perform_enqueued_jobs do
201 post messages_path(:display_name => recipient_user.display_name,
202 :message => { :title => "Test Message", :body => "Test message body" })
203 assert_response :success
204 assert_template "new"
205 assert_select ".alert.alert-danger", /wait a while/
213 # test the show action
216 recipient_user = create(:user)
217 other_user = create(:user)
218 message = create(:message, :unread, :sender => user, :recipient => recipient_user)
220 # Check that the show message page requires us to login
221 get message_path(message)
222 assert_redirected_to login_path(:referer => message_path(message))
224 # Login as the wrong user
225 session_for(other_user)
227 # Check that we can't read the message
228 get message_path(message)
229 assert_redirected_to login_path(:referer => message_path(message))
230 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]
232 # Login as the message sender
235 # Check that the message sender can read the message and that Reply button is available
236 get message_path(message)
237 assert_response :success
238 assert_template "show"
239 assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
240 assert_select "a.btn.btn-primary", :text => "Reply"
241 assert_not Message.find(message.id).message_read
243 # Login as the message recipient
244 session_for(recipient_user)
246 # Check that the message recipient can read the message and that Reply button is available
247 get message_path(message)
248 assert_response :success
249 assert_template "show"
250 assert_select "a[href='#{user_path user}']", :text => user.display_name
251 assert_select "a.btn.btn-primary", :text => "Reply"
252 assert Message.find(message.id).message_read
254 # Asking to read a message with a bogus ID should fail
255 get message_path(99999)
256 assert_response :not_found
257 assert_template "no_such_message"
261 # test the destroy action
264 second_user = create(:user)
265 other_user = create(:user)
266 read_message = create(:message, :read, :recipient => user, :sender => second_user)
267 sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
269 # Check that destroying a message requires us to login
270 delete message_path(read_message)
271 assert_response :forbidden
273 # Login as a user with no messages
274 session_for(other_user)
276 # Check that destroying a message we didn't send or receive fails
277 delete message_path(read_message)
278 assert_response :not_found
279 assert_template "no_such_message"
281 # Login as the message recipient_user
284 # Check that the destroy a received message works
285 delete message_path(read_message)
286 assert_redirected_to messages_inbox_path
287 assert_equal "Message deleted", flash[:notice]
288 m = Message.find(read_message.id)
289 assert m.from_user_visible
290 assert_not m.to_user_visible
292 # Check that the destroying a sent message works
293 delete message_path(sent_message, :referer => messages_outbox_path)
294 assert_redirected_to messages_outbox_path
295 assert_equal "Message deleted", flash[:notice]
296 m = Message.find(sent_message.id)
297 assert_not m.from_user_visible
298 assert m.to_user_visible
300 # Asking to destroy a message with a bogus ID should fail
301 delete message_path(99999)
302 assert_response :not_found
303 assert_template "no_such_message"