]> git.openstreetmap.org Git - rails.git/blob - test/controllers/message_controller_test.rb
Stub out requests to gravatar.com during tests.
[rails.git] / test / controllers / message_controller_test.rb
1 require "test_helper"
2
3 class MessageControllerTest < ActionController::TestCase
4   fixtures :users
5
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/user/username/inbox", :method => :get },
11       { :controller => "message", :action => "inbox", :display_name => "username" }
12     )
13     assert_routing(
14       { :path => "/user/username/outbox", :method => :get },
15       { :controller => "message", :action => "outbox", :display_name => "username" }
16     )
17     assert_routing(
18       { :path => "/message/new/username", :method => :get },
19       { :controller => "message", :action => "new", :display_name => "username" }
20     )
21     assert_routing(
22       { :path => "/message/new/username", :method => :post },
23       { :controller => "message", :action => "new", :display_name => "username" }
24     )
25     assert_routing(
26       { :path => "/message/read/1", :method => :get },
27       { :controller => "message", :action => "read", :message_id => "1" }
28     )
29     assert_routing(
30       { :path => "/message/mark/1", :method => :post },
31       { :controller => "message", :action => "mark", :message_id => "1" }
32     )
33     assert_routing(
34       { :path => "/message/reply/1", :method => :get },
35       { :controller => "message", :action => "reply", :message_id => "1" }
36     )
37     assert_routing(
38       { :path => "/message/reply/1", :method => :post },
39       { :controller => "message", :action => "reply", :message_id => "1" }
40     )
41     assert_routing(
42       { :path => "/message/delete/1", :method => :post },
43       { :controller => "message", :action => "delete", :message_id => "1" }
44     )
45   end
46
47   ##
48   # test the new action
49   def test_new
50     # Check that the new message page requires us to login
51     get :new, :display_name => users(:public_user).display_name
52     assert_redirected_to login_path(:referer => new_message_path(:display_name => users(:public_user).display_name))
53
54     # Login as a normal user
55     session[:user] = users(:normal_user).id
56
57     # Check that the new message page loads
58     get :new, :display_name => users(:public_user).display_name
59     assert_response :success
60     assert_template "new"
61     assert_select "title", "OpenStreetMap | Send message"
62     assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
63       assert_select "input#message_title", :count => 1
64       assert_select "textarea#message_body", :count => 1
65       assert_select "input[type='submit'][value='Send']", :count => 1
66     end
67
68     # Check that the subject is preserved over errors
69     assert_difference "ActionMailer::Base.deliveries.size", 0 do
70       assert_difference "Message.count", 0 do
71         post :new,
72              :display_name => users(:public_user).display_name,
73              :message => { :title => "Test Message", :body => "" }
74       end
75     end
76     assert_response :success
77     assert_template "new"
78     assert_select "title", "OpenStreetMap | Send message"
79     assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
80       assert_select "input#message_title", :count => 1 do
81         assert_select "[value='Test Message']"
82       end
83       assert_select "textarea#message_body", :text => "", :count => 1
84       assert_select "input[type='submit'][value='Send']", :count => 1
85     end
86
87     # Check that the body text is preserved over errors
88     assert_difference "ActionMailer::Base.deliveries.size", 0 do
89       assert_difference "Message.count", 0 do
90         post :new,
91              :display_name => users(:public_user).display_name,
92              :message => { :title => "", :body => "Test message body" }
93       end
94     end
95     assert_response :success
96     assert_template "new"
97     assert_select "title", "OpenStreetMap | Send message"
98     assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
99       assert_select "input#message_title", :count => 1 do
100         assert_select "[value='']"
101       end
102       assert_select "textarea#message_body", :text => "Test message body", :count => 1
103       assert_select "input[type='submit'][value='Send']", :count => 1
104     end
105
106     # Check that sending a message works
107     assert_difference "ActionMailer::Base.deliveries.size", 1 do
108       assert_difference "Message.count", 1 do
109         post :new,
110              :display_name => users(:public_user).display_name,
111              :message => { :title => "Test Message", :body => "Test message body" }
112       end
113     end
114     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
115     assert_equal "Message sent", flash[:notice]
116     e = ActionMailer::Base.deliveries.first
117     assert_equal [users(:public_user).email], e.to
118     assert_equal "[OpenStreetMap] Test Message", e.subject
119     assert_match /Test message body/, e.text_part.decoded
120     assert_match /Test message body/, e.html_part.decoded
121     ActionMailer::Base.deliveries.clear
122     m = Message.last
123     assert_equal users(:normal_user).id, m.from_user_id
124     assert_equal users(:public_user).id, m.to_user_id
125     assert_in_delta Time.now, m.sent_on, 2
126     assert_equal "Test Message", m.title
127     assert_equal "Test message body", m.body
128     assert_equal "markdown", m.body_format
129
130     # Asking to send a message with a bogus user name should fail
131     get :new, :display_name => "non_existent_user"
132     assert_response :not_found
133     assert_template "user/no_such_user"
134     assert_select "h1", "The user non_existent_user does not exist"
135   end
136
137   ##
138   # test the new action message limit
139   def test_new_limit
140     # Login as a normal user
141     session[:user] = users(:normal_user).id
142
143     # Check that sending a message fails when the message limit is hit
144     assert_no_difference "ActionMailer::Base.deliveries.size" do
145       assert_no_difference "Message.count" do
146         with_message_limit(0) do
147           post :new,
148                :display_name => users(:public_user).display_name,
149                :message => { :title => "Test Message", :body => "Test message body" }
150           assert_response :success
151           assert_template "new"
152           assert_select ".error", /wait a while/
153         end
154       end
155     end
156   end
157
158   ##
159   # test the reply action
160   def test_reply
161     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
162
163     # Check that the message reply page requires us to login
164     get :reply, :message_id => unread_message.id
165     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
166
167     # Login as the wrong user
168     session[:user] = users(:second_public_user).id
169
170     # Check that we can't reply to somebody else's message
171     get :reply, :message_id => unread_message.id
172     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
173     assert_equal "You are logged in as `pulibc_test2' but the message you have asked to reply to was not sent to that user. Please login as the correct user in order to reply.", flash[:notice]
174
175     # Login as the right user
176     session[:user] = users(:public_user).id
177
178     # Check that the message reply page loads
179     get :reply, :message_id => unread_message.id
180     assert_response :success
181     assert_template "new"
182     assert_select "title", "OpenStreetMap | Re: #{unread_message.title}"
183     assert_select "form[action='#{new_message_path(:display_name => users(:normal_user).display_name)}']", :count => 1 do
184       assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
185       assert_select "textarea#message_body", :count => 1
186       assert_select "input[type='submit'][value='Send']", :count => 1
187     end
188     assert_equal true, Message.find(unread_message.id).message_read
189
190     # Asking to reply to a message with no ID should fail
191     assert_raise ActionController::UrlGenerationError do
192       get :reply
193     end
194
195     # Asking to reply to a message with a bogus ID should fail
196     get :reply, :message_id => 99999
197     assert_response :not_found
198     assert_template "no_such_message"
199   end
200
201   ##
202   # test the read action
203   def test_read
204     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
205
206     # Check that the read message page requires us to login
207     get :read, :message_id => unread_message.id
208     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
209
210     # Login as the wrong user
211     session[:user] = users(:second_public_user).id
212
213     # Check that we can't read the message
214     get :read, :message_id => unread_message.id
215     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
216     assert_equal "You are logged in as `pulibc_test2' but the message you have asked to read was not sent by or to that user. Please login as the correct user in order to read it.", flash[:notice]
217
218     # Login as the message sender
219     session[:user] = users(:normal_user).id
220
221     # Check that the message sender can read the message
222     get :read, :message_id => unread_message.id
223     assert_response :success
224     assert_template "read"
225     assert_equal false, Message.find(unread_message.id).message_read
226
227     # Login as the message recipient
228     session[:user] = users(:public_user).id
229
230     # Check that the message recipient can read the message
231     get :read, :message_id => unread_message.id
232     assert_response :success
233     assert_template "read"
234     assert_equal true, Message.find(unread_message.id).message_read
235
236     # Asking to read a message with no ID should fail
237     assert_raise ActionController::UrlGenerationError do
238       get :read
239     end
240
241     # Asking to read a message with a bogus ID should fail
242     get :read, :message_id => 99999
243     assert_response :not_found
244     assert_template "no_such_message"
245   end
246
247   ##
248   # test the inbox action
249   def test_inbox
250     read_message = create(:message, :read, :recipient => users(:normal_user))
251     # Check that the inbox page requires us to login
252     get :inbox, :display_name => users(:normal_user).display_name
253     assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
254
255     # Login
256     session[:user] = users(:normal_user).id
257
258     # Check that we can view our inbox when logged in
259     get :inbox, :display_name => users(:normal_user).display_name
260     assert_response :success
261     assert_template "inbox"
262     assert_select "table.messages", :count => 1 do
263       assert_select "tr", :count => 2
264       assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
265     end
266
267     # Check that we can't view somebody else's inbox when logged in
268     get :inbox, :display_name => users(:public_user).display_name
269     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
270   end
271
272   ##
273   # test the outbox action
274   def test_outbox
275     create(:message, :sender => users(:normal_user))
276
277     # Check that the outbox page requires us to login
278     get :outbox, :display_name => users(:normal_user).display_name
279     assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
280
281     # Login
282     session[:user] = users(:normal_user).id
283
284     # Check that we can view our outbox when logged in
285     get :outbox, :display_name => users(:normal_user).display_name
286     assert_response :success
287     assert_template "outbox"
288     assert_select "table.messages", :count => 1 do
289       assert_select "tr", :count => 2
290       assert_select "tr.inbox-row", :count => 1
291     end
292
293     # Check that we can't view somebody else's outbox when logged in
294     get :outbox, :display_name => users(:public_user).display_name
295     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
296   end
297
298   ##
299   # test the mark action
300   def test_mark
301     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
302
303     # Check that the marking a message requires us to login
304     post :mark, :message_id => unread_message.id
305     assert_response :forbidden
306
307     # Login as a user with no messages
308     session[:user] = users(:second_public_user).id
309
310     # Check that marking a message we didn't send or receive fails
311     post :mark, :message_id => unread_message.id
312     assert_response :not_found
313     assert_template "no_such_message"
314
315     # Login as the message recipient
316     session[:user] = users(:public_user).id
317
318     # Check that the marking a message read works
319     post :mark, :message_id => unread_message.id, :mark => "read"
320     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
321     assert_equal true, Message.find(unread_message.id).message_read
322
323     # Check that the marking a message unread works
324     post :mark, :message_id => unread_message.id, :mark => "unread"
325     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
326     assert_equal false, Message.find(unread_message.id).message_read
327
328     # Check that the marking a message read via XHR works
329     xhr :post, :mark, :message_id => unread_message.id, :mark => "read"
330     assert_response :success
331     assert_template "mark"
332     assert_equal true, Message.find(unread_message.id).message_read
333
334     # Check that the marking a message unread via XHR works
335     xhr :post, :mark, :message_id => unread_message.id, :mark => "unread"
336     assert_response :success
337     assert_template "mark"
338     assert_equal false, Message.find(unread_message.id).message_read
339
340     # Asking to mark a message with no ID should fail
341     assert_raise ActionController::UrlGenerationError do
342       post :mark
343     end
344
345     # Asking to mark a message with a bogus ID should fail
346     post :mark, :message_id => 99999
347     assert_response :not_found
348     assert_template "no_such_message"
349   end
350
351   ##
352   # test the delete action
353   def test_delete
354     read_message = create(:message, :read, :recipient => users(:normal_user), :sender => users(:public_user))
355     sent_message = create(:message, :unread, :recipient => users(:public_user), :sender => users(:normal_user))
356
357     # Check that the deleting a message requires us to login
358     post :delete, :message_id => read_message.id
359     assert_response :forbidden
360
361     # Login as a user with no messages
362     session[:user] = users(:second_public_user).id
363
364     # Check that deleting a message we didn't send or receive fails
365     post :delete, :message_id => read_message.id
366     assert_response :not_found
367     assert_template "no_such_message"
368
369     # Login as the message recipient
370     session[:user] = users(:normal_user).id
371
372     # Check that the deleting a received message works
373     post :delete, :message_id => read_message.id
374     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
375     assert_equal "Message deleted", flash[:notice]
376     m = Message.find(read_message.id)
377     assert_equal true, m.from_user_visible
378     assert_equal false, m.to_user_visible
379
380     # Check that the deleting a sent message works
381     post :delete, :message_id => sent_message.id, :referer => outbox_path(:display_name => users(:normal_user).display_name)
382     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
383     assert_equal "Message deleted", flash[:notice]
384     m = Message.find(sent_message.id)
385     assert_equal false, m.from_user_visible
386     assert_equal true, m.to_user_visible
387
388     # Asking to delete a message with no ID should fail
389     assert_raise ActionController::UrlGenerationError do
390       post :delete
391     end
392
393     # Asking to delete a message with a bogus ID should fail
394     post :delete, :message_id => 99999
395     assert_response :not_found
396     assert_template "no_such_message"
397   end
398
399   private
400
401   def with_message_limit(value)
402     max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
403     Object.const_set("MAX_MESSAGES_PER_HOUR", value)
404
405     yield
406
407     Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
408     Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)
409   end
410 end