1 # frozen_string_literal: true
 
   6   class RepliesControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/messages/1/reply/new", :method => :get },
 
  12         { :controller => "messages/replies", :action => "new", :message_id => "1" }
 
  18       recipient_user = create(:user)
 
  19       other_user = create(:user)
 
  20       message = create(:message, :unread, :sender => user, :recipient => recipient_user)
 
  22       # Check that the message reply page requires us to login
 
  23       get new_message_reply_path(message)
 
  24       assert_redirected_to login_path(:referer => new_message_reply_path(message))
 
  26       # Login as the wrong user
 
  27       session_for(other_user)
 
  29       # Check that we can't reply to somebody else's message
 
  30       get new_message_reply_path(message)
 
  31       assert_redirected_to login_path(:referer => new_message_reply_path(message))
 
  32       assert_equal "You are logged in as '#{other_user.display_name}' but the message you have asked to reply to was not sent to that user. Please log in as the correct user in order to reply.", flash[:notice]
 
  34       # Login as the right user
 
  35       session_for(recipient_user)
 
  37       # Check that the message reply page loads
 
  38       get new_message_reply_path(message)
 
  39       assert_response :success
 
  41       assert_select "title", "Re: #{message.title} | OpenStreetMap"
 
  42       assert_select "form[action='/messages']", :count => 1 do
 
  43         assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']"
 
  44         assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
 
  45         assert_select "textarea#message_body", :count => 1
 
  46         assert_select "input[type='submit'][value='Send']", :count => 1
 
  48       assert Message.find(message.id).message_read
 
  50       # Login as the sending user
 
  53       # Check that the message reply page loads
 
  54       get new_message_reply_path(message)
 
  55       assert_response :success
 
  57       assert_select "title", "Re: #{message.title} | OpenStreetMap"
 
  58       assert_select "form[action='/messages']", :count => 1 do
 
  59         assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']"
 
  60         assert_select "input#message_title[value='Re: #{message.title}']", :count => 1
 
  61         assert_select "textarea#message_body", :count => 1
 
  62         assert_select "input[type='submit'][value='Send']", :count => 1
 
  65       # Asking to reply to a message with a bogus ID should fail
 
  66       get new_message_reply_path(99999)
 
  67       assert_response :not_found
 
  68       assert_template "no_such_message"