1 require File.dirname(__FILE__) + '/../test_helper'
3 class MessageControllerTest < ActionController::TestCase
4 fixtures :users, :messages
7 # test all routes which lead to this controller
10 { :path => "/user/username/inbox", :method => :get },
11 { :controller => "message", :action => "inbox", :display_name => "username" }
14 { :path => "/user/username/outbox", :method => :get },
15 { :controller => "message", :action => "outbox", :display_name => "username" }
18 { :path => "/message/new/username", :method => :get },
19 { :controller => "message", :action => "new", :display_name => "username" }
22 { :path => "/message/new/username", :method => :post },
23 { :controller => "message", :action => "new", :display_name => "username" }
26 { :path => "/message/read/1", :method => :get },
27 { :controller => "message", :action => "read", :message_id => "1" }
30 { :path => "/message/mark/1", :method => :post },
31 { :controller => "message", :action => "mark", :message_id => "1" }
34 { :path => "/message/reply/1", :method => :get },
35 { :controller => "message", :action => "reply", :message_id => "1" }
38 { :path => "/message/reply/1", :method => :post },
39 { :controller => "message", :action => "reply", :message_id => "1" }
42 { :path => "/message/delete/1", :method => :post },
43 { :controller => "message", :action => "delete", :message_id => "1" }
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))
54 # Login as a normal user
55 session[:user] = users(:normal_user).id
56 cookies["_osm_username"] = users(:normal_user).display_name
58 # Check that the new message page loads
59 get :new, :display_name => users(:public_user).display_name
60 assert_response :success
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
69 # Check that sending a message works
70 assert_difference "ActionMailer::Base.deliveries.size", 1 do
71 assert_difference "Message.count", 1 do
73 :display_name => users(:public_user).display_name,
74 :message => { :title => "Test Message", :body => "Test message body" }
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
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
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"
101 # test the reply action
103 # Check that the message reply page requires us to login
104 get :reply, :message_id => messages(:read_message).id
105 assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:read_message).id))
107 # Login as the wrong user
108 session[:user] = users(:second_public_user).id
109 cookies["_osm_username"] = users(:second_public_user).display_name
111 # Check that we can't reply to somebody else's message
112 get :reply, :message_id => messages(:read_message).id
113 assert_redirected_to login_path(:referer => reply_message_path(:message_id => messages(:read_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]
116 # Login as the right user
117 session[:user] = users(:normal_user).id
118 cookies["_osm_username"] = users(:normal_user).display_name
120 # Check that the message reply page loads
121 get :reply, :message_id => messages(:read_message).id
122 assert_response :success
123 assert_template "new"
124 assert_select "title", "OpenStreetMap | Re: test message 2"
125 assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do
126 assert_select "input#message_title[value='Re: test message 2']", :count => 1
127 assert_select "textarea#message_body", :count => 1
128 assert_select "input[type='submit'][value='Send']", :count => 1
131 # Asking to reply to a message with no ID should fail
132 assert_raise ActionController::RoutingError do
136 # Asking to reply to a message with a bogus ID should fail
137 get :reply, :message_id => 99999
138 assert_response :not_found
139 assert_template "no_such_message"
143 # test the read action
145 # Check that the read message page requires us to login
146 get :read, :message_id => messages(:unread_message).id
147 assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
149 # Login as the wrong user
150 session[:user] = users(:second_public_user).id
151 cookies["_osm_username"] = users(:second_public_user).display_name
153 # Check that we can't read the message
154 get :read, :message_id => messages(:unread_message).id
155 assert_redirected_to login_path(:referer => read_message_path(:message_id => messages(:unread_message).id))
156 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 # Login as the message sender
159 session[:user] = users(:normal_user).id
160 cookies["_osm_username"] = users(:normal_user).display_name
162 # Check that the message sender can read the message
163 get :read, :message_id => messages(:unread_message).id
164 assert_response :success
165 assert_template "read"
166 assert_equal false, Message.find(messages(:unread_message).id).message_read
168 # Login as the message recipient
169 session[:user] = users(:public_user).id
170 cookies["_osm_username"] = users(:public_user).display_name
172 # Check that the message recipient can read the message
173 get :read, :message_id => messages(:unread_message).id
174 assert_response :success
175 assert_template "read"
176 assert_equal true, Message.find(messages(:unread_message).id).message_read
178 # Asking to read a message with no ID should fail
179 assert_raise ActionController::RoutingError do
183 # Asking to read a message with a bogus ID should fail
184 get :read, :message_id => 99999
185 assert_response :not_found
186 assert_template "no_such_message"
190 # test the inbox action
192 # Check that the inbox page requires us to login
193 get :inbox, :display_name => users(:normal_user).display_name
194 assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
197 session[:user] = users(:normal_user).id
198 cookies["_osm_username"] = users(:normal_user).display_name
200 # Check that we can view our inbox when logged in
201 get :inbox, :display_name => users(:normal_user).display_name
202 assert_response :success
203 assert_template "inbox"
204 assert_select "table.messages", :count => 1 do
205 assert_select "tr", :count => 2
206 assert_select "tr#inbox-#{messages(:read_message).id}.inbox-row", :count => 1
209 # Check that we can't view somebody else's inbox when logged in
210 get :inbox, :display_name => users(:public_user).display_name
211 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
215 # test the outbox action
217 # Check that the outbox page requires us to login
218 get :outbox, :display_name => users(:normal_user).display_name
219 assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
222 session[:user] = users(:normal_user).id
223 cookies["_osm_username"] = users(:normal_user).display_name
225 # Check that we can view our outbox when logged in
226 get :outbox, :display_name => users(:normal_user).display_name
227 assert_response :success
228 assert_template "outbox"
229 assert_select "table.messages", :count => 1 do
230 assert_select "tr", :count => 2
231 assert_select "tr.inbox-row", :count => 1
234 # Check that we can't view somebody else's outbox when logged in
235 get :outbox, :display_name => users(:public_user).display_name
236 assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
240 # test the mark action
242 # Check that the marking a message requires us to login
243 post :mark, :message_id => messages(:unread_message).id
244 assert_response :forbidden
246 # Login as a user with no messages
247 session[:user] = users(:second_public_user).id
248 cookies["_osm_username"] = users(:second_public_user).display_name
250 # Check that marking a message we didn't send or receive fails
251 post :mark, :message_id => messages(:read_message).id
252 assert_response :not_found
253 assert_template "no_such_message"
255 # Login as the message recipient
256 session[:user] = users(:public_user).id
257 cookies["_osm_username"] = users(:public_user).display_name
259 # Check that the marking a message read works
260 post :mark, :message_id => messages(:unread_message).id, :mark => "read"
261 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
262 assert_equal true, Message.find(messages(:unread_message).id).message_read
264 # Check that the marking a message unread works
265 post :mark, :message_id => messages(:unread_message).id, :mark => "unread"
266 assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
267 assert_equal false, Message.find(messages(:unread_message).id).message_read
269 # Check that the marking a message read via XHR works
270 xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "read"
271 assert_response :success
272 assert_template "mark"
273 assert_equal true, Message.find(messages(:unread_message).id).message_read
275 # Check that the marking a message unread via XHR works
276 xhr :post, :mark, :message_id => messages(:unread_message).id, :mark => "unread"
277 assert_response :success
278 assert_template "mark"
279 assert_equal false, Message.find(messages(:unread_message).id).message_read
281 # Asking to mark a message with no ID should fail
282 assert_raise ActionController::RoutingError do
286 # Asking to mark a message with a bogus ID should fail
287 post :mark, :message_id => 99999
288 assert_response :not_found
289 assert_template "no_such_message"
293 # test the delete action
295 # Check that the deleting a message requires us to login
296 post :delete, :message_id => messages(:read_message).id
297 assert_response :forbidden
299 # Login as a user with no messages
300 session[:user] = users(:second_public_user).id
301 cookies["_osm_username"] = users(:second_public_user).display_name
303 # Check that deleting a message we didn't send or receive fails
304 post :delete, :message_id => messages(:read_message).id
305 assert_response :not_found
306 assert_template "no_such_message"
308 # Login as the message recipient
309 session[:user] = users(:normal_user).id
310 cookies["_osm_username"] = users(:normal_user).display_name
312 # Check that the deleting a received message works
313 post :delete, :message_id => messages(:read_message).id
314 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
315 assert_equal "Message deleted", flash[:notice]
316 m = Message.find(messages(:read_message).id)
317 assert_equal true, m.from_user_visible
318 assert_equal false, m.to_user_visible
320 # Check that the deleting a sent message works
321 post :delete, :message_id => messages(:unread_message).id
322 assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
323 assert_equal "Message deleted", flash[:notice]
324 m = Message.find(messages(:unread_message).id)
325 assert_equal false, m.from_user_visible
326 assert_equal true, m.to_user_visible
328 # Asking to delete a message with no ID should fail
329 assert_raise ActionController::RoutingError do
333 # Asking to delete a message with a bogus ID should fail
334 post :delete, :message_id => 99999
335 assert_response :not_found
336 assert_template "no_such_message"