]> git.openstreetmap.org Git - rails.git/blob - test/functional/message_controller_test.rb
Remove use of deprecated :comment attribute on link_to and button_to
[rails.git] / test / functional / message_controller_test.rb
1 require File.dirname(__FILE__) + '/../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     cookies["_osm_username"] = users(:normal_user).display_name
57
58     # Check that the new message page loads
59     get :new, :display_name => users(:public_user).display_name
60     assert_response :success
61     assert_template "new"
62     assert_select "title", "OpenStreetMap | Send message"
63     assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
64       assert_select "input#message_title", :count => 1
65       assert_select "textarea#message_body", :count => 1
66       assert_select "input[type='submit'][value='Send']", :count => 1
67     end
68
69     # Check that sending a message works
70     assert_difference "ActionMailer::Base.deliveries.size", 1 do
71       assert_difference "Message.count", 1 do
72         post :new,
73           :display_name => users(:public_user).display_name,
74           :message => { :title => "Test Message", :body => "Test message body" }
75       end
76     end
77     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
78     assert_equal "Message sent", flash[:notice]
79     e = ActionMailer::Base.deliveries.first
80     assert_equal [ users(:public_user).email ], e.to
81     assert_equal "[OpenStreetMap] Test Message", e.subject
82     assert_match /Test message body/, e.text_part.decoded
83     assert_match /Test message body/, e.html_part.decoded
84     ActionMailer::Base.deliveries.clear
85     m = Message.find(3)
86     assert_equal users(:normal_user).id, m.from_user_id
87     assert_equal users(:public_user).id, m.to_user_id
88     assert_in_delta Time.now, m.sent_on, 1
89     assert_equal "Test Message", m.title
90     assert_equal "Test message body", m.body
91     assert_equal "markdown", m.body_format
92
93     # Asking to send a message with no user name should fail
94     get :new
95     assert_response :not_found
96     assert_template "user/no_such_user"
97     assert_select "h2", "The user  does not exist"
98
99     # Asking to send a message with a bogus user name should fail
100     get :new, :display_name => "non_existent_user"
101     assert_response :not_found
102     assert_template "user/no_such_user"
103     assert_select "h2", "The user non_existent_user does not exist"
104   end
105
106   ##
107   # test the reply action
108   def test_reply
109     # Check that the message reply page requires us to login
110     get :reply, :message_id => messages(:read_message).id
111     assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:read_message).id))
112
113     # Login as the wrong user
114     session[:user] = users(:second_public_user).id
115     cookies["_osm_username"] = users(:second_public_user).display_name
116
117     # Check that we can't reply to somebody else's message
118     get :reply, :message_id => messages(:read_message).id
119     assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:read_message).id))
120     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]
121
122     # Login as the right user
123     session[:user] = users(:normal_user).id
124     cookies["_osm_username"] = users(:normal_user).display_name
125
126     # Check that the message reply page loads
127     get :reply, :message_id => messages(:read_message).id
128     assert_response :success
129     assert_template "new"
130     assert_select "title", "OpenStreetMap | Re: test message 2"
131     assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
132       assert_select "input#message_title[value='Re: test message 2']", :count => 1
133       assert_select "textarea#message_body", :count => 1
134       assert_select "input[type='submit'][value='Send']", :count => 1
135     end
136
137     # Asking to reply to a message with no ID should fail
138     get :reply
139     assert_response :not_found
140     assert_template "no_such_message"
141
142     # Asking to reply to a message with a bogus ID should fail
143     get :reply, :message_id => 99999
144     assert_response :not_found
145     assert_template "no_such_message"
146   end
147
148   ##
149   # test the read action
150   def test_read
151     # Check that the read message page requires us to login
152     get :read, :message_id => messages(:unread_message).id
153     assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
154
155     # Login as the wrong user
156     session[:user] = users(:second_public_user).id
157     cookies["_osm_username"] = users(:second_public_user).display_name
158
159     # Check that we can't read the message
160     get :read, :message_id => messages(:unread_message).id
161     assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
162     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]
163
164     # Login as the message sender
165     session[:user] = users(:normal_user).id
166     cookies["_osm_username"] = users(:normal_user).display_name
167
168     # Check that the message sender can read the message
169     get :read, :message_id => messages(:unread_message).id
170     assert_response :success
171     assert_template "read"
172     assert_equal false, Message.find(messages(:unread_message).id).message_read
173
174     # Login as the message recipient
175     session[:user] = users(:public_user).id
176     cookies["_osm_username"] = users(:public_user).display_name
177
178     # Check that the message recipient can read the message
179     get :read, :message_id => messages(:unread_message).id
180     assert_response :success
181     assert_template "read"
182     assert_equal true, Message.find(messages(:unread_message).id).message_read
183
184     # Asking to read a message with no ID should fail
185     get :read
186     assert_response :not_found
187     assert_template "no_such_message"
188
189     # Asking to read a message with a bogus ID should fail
190     get :read, :message_id => 99999
191     assert_response :not_found
192     assert_template "no_such_message"
193   end
194
195   ##
196   # test the inbox action
197   def test_inbox
198     # Check that the inbox page requires us to login
199     get :inbox, :display_name => users(:normal_user).display_name
200     assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
201
202     # Login
203     session[:user] = users(:normal_user).id
204     cookies["_osm_username"] = users(:normal_user).display_name
205
206     # Check that we can view our inbox when logged in
207     get :inbox, :display_name => users(:normal_user).display_name
208     assert_response :success
209     assert_template "inbox"
210     assert_select "table.messages", :count => 1 do
211       assert_select "tr", :count => 2
212       assert_select "tr#inbox-#{messages(:read_message).id}.inbox-row", :count => 1
213     end
214
215     # Check that we can't view somebody else's inbox when logged in
216     get :inbox, :display_name => users(:public_user).display_name
217     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
218   end
219
220   ##
221   # test the outbox action
222   def test_outbox
223     # Check that the outbox page requires us to login
224     get :outbox, :display_name => users(:normal_user).display_name
225     assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
226
227     # Login
228     session[:user] = users(:normal_user).id
229     cookies["_osm_username"] = users(:normal_user).display_name
230
231     # Check that we can view our outbox when logged in
232     get :outbox, :display_name => users(:normal_user).display_name
233     assert_response :success
234     assert_template "outbox"
235     assert_select "table.messages", :count => 1 do
236       assert_select "tr", :count => 2
237       assert_select "tr.inbox-row", :count => 1
238     end
239
240     # Check that we can't view somebody else's outbox when logged in
241     get :outbox, :display_name => users(:public_user).display_name
242     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
243   end
244
245   ##
246   # test the mark action
247   def test_mark
248     # Check that the marking a message requires us to login
249     post :mark, :message_id => messages(:unread_message).id
250     assert_response :forbidden
251
252     # Login as a user with no messages
253     session[:user] = users(:second_public_user).id
254     cookies["_osm_username"] = users(:second_public_user).display_name
255
256     # Check that marking a message we didn't send or receive fails
257     post :mark, :message_id => messages(:read_message).id
258     assert_response :not_found
259     assert_template "no_such_message"
260
261     # Login as the message recipient
262     session[:user] = users(:public_user).id
263     cookies["_osm_username"] = users(:public_user).display_name
264
265     # Check that the marking a message read works
266     post :mark, :message_id => messages(:unread_message).id, :mark => "read"
267     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
268     assert_equal true, Message.find(messages(:unread_message).id).message_read
269
270     # Check that the marking a message unread works
271     post :mark, :message_id => messages(:unread_message).id, :mark => "unread"
272     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
273     assert_equal false, Message.find(messages(:unread_message).id).message_read
274
275     # Check that the marking a message read via XHR works
276     xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "read"
277     assert_response :success
278     assert_template "mark"
279     assert_equal true, Message.find(messages(:unread_message).id).message_read
280
281     # Check that the marking a message unread via XHR works
282     xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "unread"
283     assert_response :success
284     assert_template "mark"
285     assert_equal false, Message.find(messages(:unread_message).id).message_read
286
287     # Asking to mark a message with no ID should fail
288     post :mark
289     assert_response :not_found
290     assert_template "no_such_message"
291
292     # Asking to mark a message with a bogus ID should fail
293     post :mark, :message_id => 99999
294     assert_response :not_found
295     assert_template "no_such_message"
296   end
297
298   ##
299   # test the delete action
300   def test_delete
301     # Check that the deleting a message requires us to login
302     post :delete, :message_id => messages(:read_message).id
303     assert_response :forbidden
304
305     # Login as a user with no messages
306     session[:user] = users(:second_public_user).id
307     cookies["_osm_username"] = users(:second_public_user).display_name
308
309     # Check that deleting a message we didn't send or receive fails
310     post :delete, :message_id => messages(:read_message).id
311     assert_response :not_found
312     assert_template "no_such_message"
313
314     # Login as the message recipient
315     session[:user] = users(:normal_user).id
316     cookies["_osm_username"] = users(:normal_user).display_name
317
318     # Check that the deleting a received message works
319     post :delete, :message_id => messages(:read_message).id
320     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
321     assert_equal "Message deleted", flash[:notice]
322     m = Message.find(messages(:read_message).id)
323     assert_equal true, m.from_user_visible
324     assert_equal false, m.to_user_visible
325
326     # Check that the deleting a sent message works
327     post :delete, :message_id => messages(:unread_message).id
328     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
329     assert_equal "Message deleted", flash[:notice]
330     m = Message.find(messages(:unread_message).id)
331     assert_equal false, m.from_user_visible
332     assert_equal true, m.to_user_visible
333
334     # Asking to delete a message with no ID should fail
335     post :delete
336     assert_response :not_found
337     assert_template "no_such_message"
338
339     # Asking to delete a message with a bogus ID should fail
340     post :delete, :message_id => 99999
341     assert_response :not_found
342     assert_template "no_such_message"
343   end
344 end