]> git.openstreetmap.org Git - rails.git/blob - test/controllers/messages_controller_test.rb
Add frozen_string_literal comments to ruby files
[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     session_for(user)
195
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/
206           end
207         end
208       end
209     end
210   end
211
212   ##
213   # test the show action
214   def test_show
215     user = create(:user)
216     recipient_user = create(:user)
217     other_user = create(:user)
218     message = create(:message, :unread, :sender => user, :recipient => recipient_user)
219
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))
223
224     # Login as the wrong user
225     session_for(other_user)
226
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]
231
232     # Login as the message sender
233     session_for(user)
234
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
242
243     # Login as the message recipient
244     session_for(recipient_user)
245
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
253
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"
258   end
259
260   ##
261   # test the destroy action
262   def test_destroy
263     user = create(:user)
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)
268
269     # Check that destroying a message requires us to login
270     delete message_path(read_message)
271     assert_response :forbidden
272
273     # Login as a user with no messages
274     session_for(other_user)
275
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"
280
281     # Login as the message recipient_user
282     session_for(user)
283
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
291
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
299
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"
304   end
305 end