forked from openstreetmap/openstreetmap-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use resourceful route for message reply
- Loading branch information
1 parent
d38f648
commit 1c3605d
Showing
8 changed files
with
127 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Messages | ||
class RepliesController < ApplicationController | ||
layout "site" | ||
|
||
before_action :authorize_web | ||
before_action :set_locale | ||
|
||
authorize_resource :class => Message | ||
|
||
before_action :check_database_readable | ||
before_action :check_database_writable | ||
|
||
allow_thirdparty_images | ||
|
||
# Allow the user to reply to another message. | ||
def new | ||
message = Message.find(params[:message_id]) | ||
|
||
if message.recipient == current_user | ||
message.update(:message_read => true) | ||
|
||
@message = Message.new( | ||
:recipient => message.sender, | ||
:title => "Re: #{message.title.sub(/^Re:\s*/, '')}", | ||
:body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" | ||
) | ||
|
||
@title = @message.title | ||
|
||
render "messages/new" | ||
elsif message.sender == current_user | ||
@message = Message.new( | ||
:recipient => message.recipient, | ||
:title => "Re: #{message.title.sub(/^Re:\s*/, '')}", | ||
:body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" | ||
) | ||
|
||
@title = @message.title | ||
|
||
render "messages/new" | ||
else | ||
flash[:notice] = t ".wrong_user", :user => current_user.display_name | ||
redirect_to login_path(:referer => request.fullpath) | ||
end | ||
rescue ActiveRecord::RecordNotFound | ||
@title = t "messages.no_such_message.title" | ||
render "messages/no_such_message", :status => :not_found | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "test_helper" | ||
|
||
module Messages | ||
class RepliesControllerTest < ActionDispatch::IntegrationTest | ||
## | ||
# test all routes which lead to this controller | ||
def test_routes | ||
assert_routing( | ||
{ :path => "/messages/1/reply/new", :method => :get }, | ||
{ :controller => "messages/replies", :action => "new", :message_id => "1" } | ||
) | ||
end | ||
|
||
def test_new | ||
user = create(:user) | ||
recipient_user = create(:user) | ||
other_user = create(:user) | ||
message = create(:message, :unread, :sender => user, :recipient => recipient_user) | ||
|
||
# Check that the message reply page requires us to login | ||
get new_message_reply_path(message) | ||
assert_redirected_to login_path(:referer => new_message_reply_path(message)) | ||
|
||
# Login as the wrong user | ||
session_for(other_user) | ||
|
||
# Check that we can't reply to somebody else's message | ||
get new_message_reply_path(message) | ||
assert_redirected_to login_path(:referer => new_message_reply_path(message)) | ||
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] | ||
|
||
# Login as the right user | ||
session_for(recipient_user) | ||
|
||
# Check that the message reply page loads | ||
get new_message_reply_path(message) | ||
assert_response :success | ||
assert_template "new" | ||
assert_select "title", "Re: #{message.title} | OpenStreetMap" | ||
assert_select "form[action='/messages']", :count => 1 do | ||
assert_select "input[type='hidden'][name='display_name'][value='#{user.display_name}']" | ||
assert_select "input#message_title[value='Re: #{message.title}']", :count => 1 | ||
assert_select "textarea#message_body", :count => 1 | ||
assert_select "input[type='submit'][value='Send']", :count => 1 | ||
end | ||
assert Message.find(message.id).message_read | ||
|
||
# Login as the sending user | ||
session_for(user) | ||
|
||
# Check that the message reply page loads | ||
get new_message_reply_path(message) | ||
assert_response :success | ||
assert_template "new" | ||
assert_select "title", "Re: #{message.title} | OpenStreetMap" | ||
assert_select "form[action='/messages']", :count => 1 do | ||
assert_select "input[type='hidden'][name='display_name'][value='#{recipient_user.display_name}']" | ||
assert_select "input#message_title[value='Re: #{message.title}']", :count => 1 | ||
assert_select "textarea#message_body", :count => 1 | ||
assert_select "input[type='submit'][value='Send']", :count => 1 | ||
end | ||
|
||
# Asking to reply to a message with a bogus ID should fail | ||
get new_message_reply_path(99999) | ||
assert_response :not_found | ||
assert_template "no_such_message" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters