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