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
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 sending a message works
69 assert_difference "ActionMailer::Base.deliveries.size", 1 do
70 assert_difference "Message.count", 1 do
72 :display_name => users(:public_user).display_name,
73 :message => { :title => "Test Message", :body => "Test message body" }
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
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
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 "h1", "The user non_existent_user does not exist"
100 # test the reply action
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))
106 # Login as the wrong user
107 session[:user] = users(:second_public_user).id
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]
114 # Login as the right user
115 session[:user] = users(:public_user).id
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
127 assert_equal true, Message.find(messages(:unread_message).id).message_read
129 # Asking to reply to a message with no ID should fail
130 assert_raise ActionController::UrlGenerationError do
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"
141 # test the read action
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))
147 # Login as the wrong user
148 session[:user] = users(:second_public_user).id
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]
155 # Login as the message sender
156 session[:user] = users(:normal_user).id
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
164 # Login as the message recipient
165 session[:user] = users(:public_user).id
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
173 # Asking to read a message with no ID should fail
174 assert_raise ActionController::UrlGenerationError do
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"
185 # test the inbox action
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))
192 session[:user] = users(:normal_user).id
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
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)
209 # test the outbox action
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))
216 session[:user] = users(:normal_user).id
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
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)
233 # test the mark action
235 # Check that the marking a message requires us to login
236 post :mark, :message_id => messages(:unread_message).id
237 assert_response :forbidden
239 # Login as a user with no messages
240 session[:user] = users(:second_public_user).id
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"
247 # Login as the message recipient
248 session[:user] = users(:public_user).id
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
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
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
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
272 # Asking to mark a message with no ID should fail
273 assert_raise ActionController::UrlGenerationError do
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"
284 # test the delete action
286 # Check that the deleting a message requires us to login
287 post :delete, :message_id => messages(:read_message).id
288 assert_response :forbidden
290 # Login as a user with no messages
291 session[:user] = users(:second_public_user).id
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"
298 # Login as the message recipient
299 session[:user] = users(:normal_user).id
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
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
317 # Asking to delete a message with no ID should fail
318 assert_raise ActionController::UrlGenerationError do
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"