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
57 # Check that the new message page loads
58 get :new, :display_name => users(:public_user).display_name
59 assert_response :success
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
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
72 :display_name => users(:public_user).display_name,
73 :message => { :title => "Test Message", :body => "" }
76 assert_response :success
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]"
83 assert_select "textarea#message_body", :text => "", :count => 1
84 assert_select "input[type='submit'][value='Send']", :count => 1
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
91 :display_name => users(:public_user).display_name,
92 :message => { :title => "", :body => "Test message body" }
95 assert_response :success
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=]"
102 assert_select "textarea#message_body", :text => "Test message body", :count => 1
103 assert_select "input[type='submit'][value='Send']", :count => 1
106 # Check that sending a message works
107 assert_difference "ActionMailer::Base.deliveries.size", 1 do
108 assert_difference "Message.count", 1 do
110 :display_name => users(:public_user).display_name,
111 :message => { :title => "Test Message", :body => "Test message body" }
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
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
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"
138 # test the reply action
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))
144 # Login as the wrong user
145 session[:user] = users(:second_public_user).id
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]
152 # Login as the right user
153 session[:user] = users(:public_user).id
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
165 assert_equal true, Message.find(messages(:unread_message).id).message_read
167 # Asking to reply to a message with no ID should fail
168 assert_raise ActionController::UrlGenerationError do
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"
179 # test the read action
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))
185 # Login as the wrong user
186 session[:user] = users(:second_public_user).id
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]
193 # Login as the message sender
194 session[:user] = users(:normal_user).id
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
202 # Login as the message recipient
203 session[:user] = users(:public_user).id
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
211 # Asking to read a message with no ID should fail
212 assert_raise ActionController::UrlGenerationError do
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"
223 # test the inbox action
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))
230 session[:user] = users(:normal_user).id
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
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)
247 # test the outbox action
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))
254 session[:user] = users(:normal_user).id
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
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)
271 # test the mark action
273 # Check that the marking a message requires us to login
274 post :mark, :message_id => messages(:unread_message).id
275 assert_response :forbidden
277 # Login as a user with no messages
278 session[:user] = users(:second_public_user).id
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"
285 # Login as the message recipient
286 session[:user] = users(:public_user).id
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
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
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
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
310 # Asking to mark a message with no ID should fail
311 assert_raise ActionController::UrlGenerationError do
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"
322 # test the delete action
324 # Check that the deleting a message requires us to login
325 post :delete, :message_id => messages(:read_message).id
326 assert_response :forbidden
328 # Login as a user with no messages
329 session[:user] = users(:second_public_user).id
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"
336 # Login as the message recipient
337 session[:user] = users(:normal_user).id
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
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
355 # Asking to delete a message with no ID should fail
356 assert_raise ActionController::UrlGenerationError do
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"