3 class MessagesControllerTest < ActionDispatch::IntegrationTest
 
   5   # test all routes which lead to this controller
 
   8       { :path => "/messages/new/username", :method => :get },
 
   9       { :controller => "messages", :action => "new", :display_name => "username" }
 
  12       { :path => "/messages", :method => :post },
 
  13       { :controller => "messages", :action => "create" }
 
  16       { :path => "/messages/1", :method => :get },
 
  17       { :controller => "messages", :action => "show", :id => "1" }
 
  20       { :path => "/messages/1", :method => :delete },
 
  21       { :controller => "messages", :action => "destroy", :id => "1" }
 
  26   # test fetching new message page when not logged in
 
  28     # Check that the new message page requires us to login
 
  30     get new_message_path(user)
 
  31     assert_redirected_to login_path(:referer => new_message_path(user))
 
  35   # test fetching new message page when logged in
 
  37     # Login as a normal user
 
  39     recipient_user = create(:user)
 
  42     # Check that the new message page loads
 
  43     get new_message_path(recipient_user)
 
  44     assert_response :success
 
  46     assert_select "title", "Send message | OpenStreetMap"
 
  47     assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
 
  48     assert_select "form[action='/messages']", :count => 1 do
 
  49       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
 
  50       assert_select "input#message_title", :count => 1
 
  51       assert_select "textarea#message_body", :count => 1
 
  52       assert_select "input[type='submit'][value='Send']", :count => 1
 
  57   # test fetching new message page with body and title
 
  58   def test_new_get_with_params
 
  59     # Login as a normal user
 
  61     recipient_user = create(:user)
 
  64     # Check that we can't send a message from a GET request
 
  65     assert_difference "ActionMailer::Base.deliveries.size", 0 do
 
  66       assert_difference "Message.count", 0 do
 
  67         perform_enqueued_jobs do
 
  68           get new_message_path(recipient_user, :message => { :title => "Test Message", :body => "Test message body" })
 
  72     assert_response :success
 
  74     assert_select "title", "Send message | OpenStreetMap"
 
  75     assert_select "form[action='/messages']", :count => 1 do
 
  76       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
 
  77       assert_select "input#message_title", :count => 1 do
 
  78         assert_select "[value='Test Message']"
 
  80       assert_select "textarea#message_body", :text => "Test message body", :count => 1
 
  81       assert_select "input[type='submit'][value='Send']", :count => 1
 
  86   # test posting new message page with no body
 
  87   def test_new_post_no_body
 
  88     # Login as a normal user
 
  90     recipient_user = create(:user)
 
  93     # Check that the subject is preserved over errors
 
  94     assert_difference "ActionMailer::Base.deliveries.size", 0 do
 
  95       assert_difference "Message.count", 0 do
 
  96         perform_enqueued_jobs do
 
  97           post messages_path(:display_name => recipient_user.display_name,
 
  98                              :message => { :title => "Test Message", :body => "" })
 
 102     assert_response :success
 
 103     assert_template "new"
 
 104     assert_select "title", "Send message | OpenStreetMap"
 
 105     assert_select "form[action='/messages']", :count => 1 do
 
 106       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
 
 107       assert_select "input#message_title", :count => 1 do
 
 108         assert_select "[value='Test Message']"
 
 110       assert_select "textarea#message_body", :text => "", :count => 1
 
 111       assert_select "input[type='submit'][value='Send']", :count => 1
 
 116   # test posting new message page with no title
 
 117   def test_new_post_no_title
 
 118     # Login as a normal user
 
 120     recipient_user = create(:user)
 
 123     # Check that the body text is preserved over errors
 
 124     assert_difference "ActionMailer::Base.deliveries.size", 0 do
 
 125       assert_difference "Message.count", 0 do
 
 126         perform_enqueued_jobs do
 
 127           post messages_path(:display_name => recipient_user.display_name,
 
 128                              :message => { :title => "", :body => "Test message body" })
 
 132     assert_response :success
 
 133     assert_template "new"
 
 134     assert_select "title", "Send message | OpenStreetMap"
 
 135     assert_select "form[action='/messages']", :count => 1 do
 
 136       assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
 
 137       assert_select "input#message_title", :count => 1 do
 
 138         assert_select "[value='']"
 
 140       assert_select "textarea#message_body", :text => "Test message body", :count => 1
 
 141       assert_select "input[type='submit'][value='Send']", :count => 1
 
 146   # test posting new message page sends message
 
 147   def test_new_post_send
 
 148     # Login as a normal user
 
 150     recipient_user = create(:user)
 
 153     # Check that sending a message works
 
 154     assert_difference "ActionMailer::Base.deliveries.size", 1 do
 
 155       assert_difference "Message.count", 1 do
 
 156         perform_enqueued_jobs do
 
 157           post messages_path(:display_name => recipient_user.display_name,
 
 158                              :message => { :title => "Test Message", :body => "Test message body" })
 
 162     assert_redirected_to messages_outbox_path
 
 163     assert_equal "Message sent", flash[:notice]
 
 164     e = ActionMailer::Base.deliveries.first
 
 165     assert_equal [recipient_user.email], e.to
 
 166     assert_equal "[OpenStreetMap] Test Message", e.subject
 
 167     assert_match(/Test message body/, e.text_part.decoded)
 
 168     assert_match(/Test message body/, e.html_part.decoded)
 
 169     assert_match %r{#{Settings.server_url}/messages/[0-9]+}, e.text_part.decoded
 
 172     assert_equal user.id, m.from_user_id
 
 173     assert_equal recipient_user.id, m.to_user_id
 
 174     assert_in_delta Time.now.utc, m.sent_on, 2
 
 175     assert_equal "Test Message", m.title
 
 176     assert_equal "Test message body", m.body
 
 177     assert_equal "markdown", m.body_format
 
 179     # Asking to send a message with a bogus user name should fail
 
 180     get new_message_path("non_existent_user")
 
 181     assert_response :not_found
 
 182     assert_template "users/no_such_user"
 
 183     assert_select "h1", "The user non_existent_user does not exist"
 
 187   # test the new action message limit
 
 189     # Login as a normal user
 
 191     recipient_user = create(:user)
 
 194     # Check that sending a message fails when the message limit is hit
 
 195     assert_no_difference "ActionMailer::Base.deliveries.size" do
 
 196       assert_no_difference "Message.count" do
 
 197         with_settings(:max_messages_per_hour => 0) do
 
 198           perform_enqueued_jobs do
 
 199             post messages_path(:display_name => recipient_user.display_name,
 
 200                                :message => { :title => "Test Message", :body => "Test message body" })
 
 201             assert_response :success
 
 202             assert_template "new"
 
 203             assert_select ".alert.alert-danger", /wait a while/
 
 211   # test the show action
 
 214     recipient_user = create(:user)
 
 215     other_user = create(:user)
 
 216     message = create(:message, :unread, :sender => user, :recipient => recipient_user)
 
 218     # Check that the show message page requires us to login
 
 219     get message_path(message)
 
 220     assert_redirected_to login_path(:referer => message_path(message))
 
 222     # Login as the wrong user
 
 223     session_for(other_user)
 
 225     # Check that we can't read the message
 
 226     get message_path(message)
 
 227     assert_redirected_to login_path(:referer => message_path(message))
 
 228     assert_equal "You are logged in as '#{other_user.display_name}' but the message you have asked to read was not sent by or to that user. Please log in as the correct user in order to read it.", flash[:notice]
 
 230     # Login as the message sender
 
 233     # Check that the message sender can read the message and that Reply button is available
 
 234     get message_path(message)
 
 235     assert_response :success
 
 236     assert_template "show"
 
 237     assert_select "a[href='#{user_path recipient_user}']", :text => recipient_user.display_name
 
 238     assert_select "a.btn.btn-primary", :text => "Reply"
 
 239     assert_not Message.find(message.id).message_read
 
 241     # Login as the message recipient
 
 242     session_for(recipient_user)
 
 244     # Check that the message recipient can read the message and that Reply button is available
 
 245     get message_path(message)
 
 246     assert_response :success
 
 247     assert_template "show"
 
 248     assert_select "a[href='#{user_path user}']", :text => user.display_name
 
 249     assert_select "a.btn.btn-primary", :text => "Reply"
 
 250     assert Message.find(message.id).message_read
 
 252     # Asking to read a message with a bogus ID should fail
 
 253     get message_path(99999)
 
 254     assert_response :not_found
 
 255     assert_template "no_such_message"
 
 259   # test the destroy action
 
 262     second_user = create(:user)
 
 263     other_user = create(:user)
 
 264     read_message = create(:message, :read, :recipient => user, :sender => second_user)
 
 265     sent_message = create(:message, :unread, :recipient => second_user, :sender => user)
 
 267     # Check that destroying a message requires us to login
 
 268     delete message_path(read_message)
 
 269     assert_response :forbidden
 
 271     # Login as a user with no messages
 
 272     session_for(other_user)
 
 274     # Check that destroying a message we didn't send or receive fails
 
 275     delete message_path(read_message)
 
 276     assert_response :not_found
 
 277     assert_template "no_such_message"
 
 279     # Login as the message recipient_user
 
 282     # Check that the destroy a received message works
 
 283     delete message_path(read_message)
 
 284     assert_redirected_to messages_inbox_path
 
 285     assert_equal "Message deleted", flash[:notice]
 
 286     m = Message.find(read_message.id)
 
 287     assert m.from_user_visible
 
 288     assert_not m.to_user_visible
 
 290     # Check that the destroying a sent message works
 
 291     delete message_path(sent_message, :referer => messages_outbox_path)
 
 292     assert_redirected_to messages_outbox_path
 
 293     assert_equal "Message deleted", flash[:notice]
 
 294     m = Message.find(sent_message.id)
 
 295     assert_not m.from_user_visible
 
 296     assert m.to_user_visible
 
 298     # Asking to destroy a message with a bogus ID should fail
 
 299     delete message_path(99999)
 
 300     assert_response :not_found
 
 301     assert_template "no_such_message"