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