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