]> git.openstreetmap.org Git - rails.git/blob - test/controllers/message_controller_test.rb
A few more edge cases
[rails.git] / test / controllers / message_controller_test.rb
1 require "test_helper"
2
3 class MessageControllerTest < ActionController::TestCase
4   fixtures :users, :messages
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.find(3)
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     # Check that the message reply page requires us to login
162     get :reply, :message_id => messages(:unread_message).id
163     assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:unread_message).id))
164
165     # Login as the wrong user
166     session[:user] = users(:second_public_user).id
167
168     # Check that we can't reply to somebody else's message
169     get :reply, :message_id => messages(:unread_message).id
170     assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:unread_message).id))
171     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]
172
173     # Login as the right user
174     session[:user] = users(:public_user).id
175
176     # Check that the message reply page loads
177     get :reply, :message_id => messages(:unread_message).id
178     assert_response :success
179     assert_template "new"
180     assert_select "title", "OpenStreetMap | Re: test message 1"
181     assert_select "form[action='#{new_message_path(:display_name => users(:normal_user).display_name)}']", :count => 1 do
182       assert_select "input#message_title[value='Re: test message 1']", :count => 1
183       assert_select "textarea#message_body", :count => 1
184       assert_select "input[type='submit'][value='Send']", :count => 1
185     end
186     assert_equal true, Message.find(messages(:unread_message).id).message_read
187
188     # Asking to reply to a message with no ID should fail
189     assert_raise ActionController::UrlGenerationError do
190       get :reply
191     end
192
193     # Asking to reply to a message with a bogus ID should fail
194     get :reply, :message_id => 99999
195     assert_response :not_found
196     assert_template "no_such_message"
197   end
198
199   ##
200   # test the read action
201   def test_read
202     # Check that the read message page requires us to login
203     get :read, :message_id => messages(:unread_message).id
204     assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
205
206     # Login as the wrong user
207     session[:user] = users(:second_public_user).id
208
209     # Check that we can't read the message
210     get :read, :message_id => messages(:unread_message).id
211     assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
212     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]
213
214     # Login as the message sender
215     session[:user] = users(:normal_user).id
216
217     # Check that the message sender can read the message
218     get :read, :message_id => messages(:unread_message).id
219     assert_response :success
220     assert_template "read"
221     assert_equal false, Message.find(messages(:unread_message).id).message_read
222
223     # Login as the message recipient
224     session[:user] = users(:public_user).id
225
226     # Check that the message recipient can read the message
227     get :read, :message_id => messages(:unread_message).id
228     assert_response :success
229     assert_template "read"
230     assert_equal true, Message.find(messages(:unread_message).id).message_read
231
232     # Asking to read a message with no ID should fail
233     assert_raise ActionController::UrlGenerationError do
234       get :read
235     end
236
237     # Asking to read a message with a bogus ID should fail
238     get :read, :message_id => 99999
239     assert_response :not_found
240     assert_template "no_such_message"
241   end
242
243   ##
244   # test the inbox action
245   def test_inbox
246     # Check that the inbox page requires us to login
247     get :inbox, :display_name => users(:normal_user).display_name
248     assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
249
250     # Login
251     session[:user] = users(:normal_user).id
252
253     # Check that we can view our inbox when logged in
254     get :inbox, :display_name => users(:normal_user).display_name
255     assert_response :success
256     assert_template "inbox"
257     assert_select "table.messages", :count => 1 do
258       assert_select "tr", :count => 2
259       assert_select "tr#inbox-#{messages(:read_message).id}.inbox-row", :count => 1
260     end
261
262     # Check that we can't view somebody else's inbox when logged in
263     get :inbox, :display_name => users(:public_user).display_name
264     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
265   end
266
267   ##
268   # test the outbox action
269   def test_outbox
270     # Check that the outbox page requires us to login
271     get :outbox, :display_name => users(:normal_user).display_name
272     assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
273
274     # Login
275     session[:user] = users(:normal_user).id
276
277     # Check that we can view our outbox when logged in
278     get :outbox, :display_name => users(:normal_user).display_name
279     assert_response :success
280     assert_template "outbox"
281     assert_select "table.messages", :count => 1 do
282       assert_select "tr", :count => 2
283       assert_select "tr.inbox-row", :count => 1
284     end
285
286     # Check that we can't view somebody else's outbox when logged in
287     get :outbox, :display_name => users(:public_user).display_name
288     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
289   end
290
291   ##
292   # test the mark action
293   def test_mark
294     # Check that the marking a message requires us to login
295     post :mark, :message_id => messages(:unread_message).id
296     assert_response :forbidden
297
298     # Login as a user with no messages
299     session[:user] = users(:second_public_user).id
300
301     # Check that marking a message we didn't send or receive fails
302     post :mark, :message_id => messages(:read_message).id
303     assert_response :not_found
304     assert_template "no_such_message"
305
306     # Login as the message recipient
307     session[:user] = users(:public_user).id
308
309     # Check that the marking a message read works
310     post :mark, :message_id => messages(:unread_message).id, :mark => "read"
311     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
312     assert_equal true, Message.find(messages(:unread_message).id).message_read
313
314     # Check that the marking a message unread works
315     post :mark, :message_id => messages(:unread_message).id, :mark => "unread"
316     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
317     assert_equal false, Message.find(messages(:unread_message).id).message_read
318
319     # Check that the marking a message read via XHR works
320     xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "read"
321     assert_response :success
322     assert_template "mark"
323     assert_equal true, Message.find(messages(:unread_message).id).message_read
324
325     # Check that the marking a message unread via XHR works
326     xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "unread"
327     assert_response :success
328     assert_template "mark"
329     assert_equal false, Message.find(messages(:unread_message).id).message_read
330
331     # Asking to mark a message with no ID should fail
332     assert_raise ActionController::UrlGenerationError do
333       post :mark
334     end
335
336     # Asking to mark a message with a bogus ID should fail
337     post :mark, :message_id => 99999
338     assert_response :not_found
339     assert_template "no_such_message"
340   end
341
342   ##
343   # test the delete action
344   def test_delete
345     # Check that the deleting a message requires us to login
346     post :delete, :message_id => messages(:read_message).id
347     assert_response :forbidden
348
349     # Login as a user with no messages
350     session[:user] = users(:second_public_user).id
351
352     # Check that deleting a message we didn't send or receive fails
353     post :delete, :message_id => messages(:read_message).id
354     assert_response :not_found
355     assert_template "no_such_message"
356
357     # Login as the message recipient
358     session[:user] = users(:normal_user).id
359
360     # Check that the deleting a received message works
361     post :delete, :message_id => messages(:read_message).id
362     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
363     assert_equal "Message deleted", flash[:notice]
364     m = Message.find(messages(:read_message).id)
365     assert_equal true, m.from_user_visible
366     assert_equal false, m.to_user_visible
367
368     # Check that the deleting a sent message works
369     post :delete, :message_id => messages(:unread_message).id, :referer => outbox_path(:display_name => users(:normal_user).display_name)
370     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
371     assert_equal "Message deleted", flash[:notice]
372     m = Message.find(messages(:unread_message).id)
373     assert_equal false, m.from_user_visible
374     assert_equal true, m.to_user_visible
375
376     # Asking to delete a message with no ID should fail
377     assert_raise ActionController::UrlGenerationError do
378       post :delete
379     end
380
381     # Asking to delete a message with a bogus ID should fail
382     post :delete, :message_id => 99999
383     assert_response :not_found
384     assert_template "no_such_message"
385   end
386
387   private
388
389   def with_message_limit(value)
390     max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
391     Object.const_set("MAX_MESSAGES_PER_HOUR", value)
392
393     yield
394
395     Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
396     Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)
397   end
398 end