3 class MessageControllerTest < ActionController::TestCase
 
   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 new action message limit
 
 140     # Login as a normal user
 
 141     session[:user] = users(:normal_user).id
 
 143     # Check that sending a message fails when the message limit is hit
 
 144     assert_no_difference "ActionMailer::Base.deliveries.size" do
 
 145       assert_no_difference "Message.count" do
 
 146         with_message_limit(0) do
 
 148                :display_name => users(:public_user).display_name,
 
 149                :message => { :title => "Test Message", :body => "Test message body" }
 
 150           assert_response :success
 
 151           assert_template "new"
 
 152           assert_select ".error", /wait a while/
 
 159   # test the reply action
 
 161     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
 
 163     # Check that the message reply page requires us to login
 
 164     get :reply, :message_id => unread_message.id
 
 165     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
 
 167     # Login as the wrong user
 
 168     session[:user] = users(:second_public_user).id
 
 170     # Check that we can't reply to somebody else's message
 
 171     get :reply, :message_id => unread_message.id
 
 172     assert_redirected_to login_path(:referer => reply_message_path(:message_id => unread_message.id))
 
 173     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]
 
 175     # Login as the right user
 
 176     session[:user] = users(:public_user).id
 
 178     # Check that the message reply page loads
 
 179     get :reply, :message_id => unread_message.id
 
 180     assert_response :success
 
 181     assert_template "new"
 
 182     assert_select "title", "OpenStreetMap | Re: #{unread_message.title}"
 
 183     assert_select "form[action='#{new_message_path(:display_name => users(:normal_user).display_name)}']", :count => 1 do
 
 184       assert_select "input#message_title[value='Re: #{unread_message.title}']", :count => 1
 
 185       assert_select "textarea#message_body", :count => 1
 
 186       assert_select "input[type='submit'][value='Send']", :count => 1
 
 188     assert_equal true, Message.find(unread_message.id).message_read
 
 190     # Asking to reply to a message with no ID should fail
 
 191     assert_raise ActionController::UrlGenerationError do
 
 195     # Asking to reply to a message with a bogus ID should fail
 
 196     get :reply, :message_id => 99999
 
 197     assert_response :not_found
 
 198     assert_template "no_such_message"
 
 202   # test the read action
 
 204     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
 
 206     # Check that the read message page requires us to login
 
 207     get :read, :message_id => unread_message.id
 
 208     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
 
 210     # Login as the wrong user
 
 211     session[:user] = users(:second_public_user).id
 
 213     # Check that we can't read the message
 
 214     get :read, :message_id => unread_message.id
 
 215     assert_redirected_to login_path(:referer => read_message_path(:message_id => unread_message.id))
 
 216     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]
 
 218     # Login as the message sender
 
 219     session[:user] = users(:normal_user).id
 
 221     # Check that the message sender can read the message
 
 222     get :read, :message_id => unread_message.id
 
 223     assert_response :success
 
 224     assert_template "read"
 
 225     assert_equal false, Message.find(unread_message.id).message_read
 
 227     # Login as the message recipient
 
 228     session[:user] = users(:public_user).id
 
 230     # Check that the message recipient can read the message
 
 231     get :read, :message_id => unread_message.id
 
 232     assert_response :success
 
 233     assert_template "read"
 
 234     assert_equal true, Message.find(unread_message.id).message_read
 
 236     # Asking to read a message with no ID should fail
 
 237     assert_raise ActionController::UrlGenerationError do
 
 241     # Asking to read a message with a bogus ID should fail
 
 242     get :read, :message_id => 99999
 
 243     assert_response :not_found
 
 244     assert_template "no_such_message"
 
 248   # test the inbox action
 
 250     read_message = create(:message, :read, :recipient => users(:normal_user))
 
 251     # Check that the inbox page requires us to login
 
 252     get :inbox, :display_name => users(:normal_user).display_name
 
 253     assert_redirected_to login_path(:referer => inbox_path(:display_name => users(:normal_user).display_name))
 
 256     session[:user] = users(:normal_user).id
 
 258     # Check that we can view our inbox when logged in
 
 259     get :inbox, :display_name => users(:normal_user).display_name
 
 260     assert_response :success
 
 261     assert_template "inbox"
 
 262     assert_select "table.messages", :count => 1 do
 
 263       assert_select "tr", :count => 2
 
 264       assert_select "tr#inbox-#{read_message.id}.inbox-row", :count => 1
 
 267     # Check that we can't view somebody else's inbox when logged in
 
 268     get :inbox, :display_name => users(:public_user).display_name
 
 269     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
 
 273   # test the outbox action
 
 275     create(:message, :sender => users(:normal_user))
 
 277     # Check that the outbox page requires us to login
 
 278     get :outbox, :display_name => users(:normal_user).display_name
 
 279     assert_redirected_to login_path(:referer => outbox_path(:display_name => users(:normal_user).display_name))
 
 282     session[:user] = users(:normal_user).id
 
 284     # Check that we can view our outbox when logged in
 
 285     get :outbox, :display_name => users(:normal_user).display_name
 
 286     assert_response :success
 
 287     assert_template "outbox"
 
 288     assert_select "table.messages", :count => 1 do
 
 289       assert_select "tr", :count => 2
 
 290       assert_select "tr.inbox-row", :count => 1
 
 293     # Check that we can't view somebody else's outbox when logged in
 
 294     get :outbox, :display_name => users(:public_user).display_name
 
 295     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
 
 299   # test the mark action
 
 301     unread_message = create(:message, :unread, :sender => users(:normal_user), :recipient => users(:public_user))
 
 303     # Check that the marking a message requires us to login
 
 304     post :mark, :message_id => unread_message.id
 
 305     assert_response :forbidden
 
 307     # Login as a user with no messages
 
 308     session[:user] = users(:second_public_user).id
 
 310     # Check that marking a message we didn't send or receive fails
 
 311     post :mark, :message_id => unread_message.id
 
 312     assert_response :not_found
 
 313     assert_template "no_such_message"
 
 315     # Login as the message recipient
 
 316     session[:user] = users(:public_user).id
 
 318     # Check that the marking a message read works
 
 319     post :mark, :message_id => unread_message.id, :mark => "read"
 
 320     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
 
 321     assert_equal true, Message.find(unread_message.id).message_read
 
 323     # Check that the marking a message unread works
 
 324     post :mark, :message_id => unread_message.id, :mark => "unread"
 
 325     assert_redirected_to inbox_path(:display_name => users(:public_user).display_name)
 
 326     assert_equal false, Message.find(unread_message.id).message_read
 
 328     # Check that the marking a message read via XHR works
 
 329     xhr :post, :mark, :message_id => unread_message.id, :mark => "read"
 
 330     assert_response :success
 
 331     assert_template "mark"
 
 332     assert_equal true, Message.find(unread_message.id).message_read
 
 334     # Check that the marking a message unread via XHR works
 
 335     xhr :post, :mark, :message_id => unread_message.id, :mark => "unread"
 
 336     assert_response :success
 
 337     assert_template "mark"
 
 338     assert_equal false, Message.find(unread_message.id).message_read
 
 340     # Asking to mark a message with no ID should fail
 
 341     assert_raise ActionController::UrlGenerationError do
 
 345     # Asking to mark a message with a bogus ID should fail
 
 346     post :mark, :message_id => 99999
 
 347     assert_response :not_found
 
 348     assert_template "no_such_message"
 
 352   # test the delete action
 
 354     read_message = create(:message, :read, :recipient => users(:normal_user), :sender => users(:public_user))
 
 355     sent_message = create(:message, :unread, :recipient => users(:public_user), :sender => users(:normal_user))
 
 357     # Check that the deleting a message requires us to login
 
 358     post :delete, :message_id => read_message.id
 
 359     assert_response :forbidden
 
 361     # Login as a user with no messages
 
 362     session[:user] = users(:second_public_user).id
 
 364     # Check that deleting a message we didn't send or receive fails
 
 365     post :delete, :message_id => read_message.id
 
 366     assert_response :not_found
 
 367     assert_template "no_such_message"
 
 369     # Login as the message recipient
 
 370     session[:user] = users(:normal_user).id
 
 372     # Check that the deleting a received message works
 
 373     post :delete, :message_id => read_message.id
 
 374     assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name)
 
 375     assert_equal "Message deleted", flash[:notice]
 
 376     m = Message.find(read_message.id)
 
 377     assert_equal true, m.from_user_visible
 
 378     assert_equal false, m.to_user_visible
 
 380     # Check that the deleting a sent message works
 
 381     post :delete, :message_id => sent_message.id, :referer => outbox_path(:display_name => users(:normal_user).display_name)
 
 382     assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name)
 
 383     assert_equal "Message deleted", flash[:notice]
 
 384     m = Message.find(sent_message.id)
 
 385     assert_equal false, m.from_user_visible
 
 386     assert_equal true, m.to_user_visible
 
 388     # Asking to delete a message with no ID should fail
 
 389     assert_raise ActionController::UrlGenerationError do
 
 393     # Asking to delete a message with a bogus ID should fail
 
 394     post :delete, :message_id => 99999
 
 395     assert_response :not_found
 
 396     assert_template "no_such_message"
 
 401   def with_message_limit(value)
 
 402     max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
 
 403     Object.const_set("MAX_MESSAGES_PER_HOUR", value)
 
 407     Object.send("remove_const", "MAX_MESSAGES_PER_HOUR")
 
 408     Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour)