diff options
-rw-r--r-- | app/models/incoming_message.rb | 7 | ||||
-rw-r--r-- | app/views/request/_correspondence.rhtml | 9 | ||||
-rw-r--r-- | spec/fixtures/files/autoresponse-header.email | 22 | ||||
-rw-r--r-- | spec/fixtures/files/empty-return-path.email | 21 | ||||
-rw-r--r-- | spec/models/incoming_message_spec.rb | 60 |
5 files changed, 108 insertions, 11 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index 16ae38b92..edc395ff4 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -1308,7 +1308,12 @@ class IncomingMessage < ActiveRecord::Base if !prefix.nil? && prefix.downcase.match(/^(postmaster|mailer-daemon|auto_reply|donotreply|no.reply)$/) return false end - + if !self.mail['return-path'].nil? && self.mail['return-path'].addr == "<>" + return false + end + if !self.mail['auto-submitted'].nil? && !self.mail['auto-submitted'].keys.empty? + return false + end return true end diff --git a/app/views/request/_correspondence.rhtml b/app/views/request/_correspondence.rhtml index 4e46347b8..9500591ea 100644 --- a/app/views/request/_correspondence.rhtml +++ b/app/views/request/_correspondence.rhtml @@ -25,8 +25,11 @@ if not incoming_message.nil? <%= link_to "Admin", admin_url("request/show_raw_email/" + incoming_message.raw_email_id.to_s) %> | <% end %> <%= link_to _("Link to this"), incoming_message_url(incoming_message) %> | - <%= link_to _("Reply to this message"), show_response_url(:id => incoming_message.info_request.id, :incoming_message_id => incoming_message.id) + "#followup" %> - + <% if incoming_message.valid_to_reply_to? %> + <%= link_to _("Reply to this message"), show_response_url(:id => incoming_message.info_request.id, :incoming_message_id => incoming_message.id) + "#followup" %> + <% else %> + <%= link_to _("Send follow up to the main FOI contact"), show_response_no_followup_url(:id => outgoing_message.info_request.id, :incoming_message_id => nil) + "#followup" %> + <% end %> </p> </div> <% @@ -55,7 +58,7 @@ elsif [ 'sent', 'followup_sent' ].include?(info_request_event.event_type) --> <%= link_to _("Link to this"), outgoing_message_url(outgoing_message) %> | - <%= link_to _("Send follow up"), show_response_no_followup_url(:id => outgoing_message.info_request.id, :incoming_message_id => nil) + "#followup" %> + <%= link_to _("Send follow up to the main FOI contact"), show_response_no_followup_url(:id => outgoing_message.info_request.id, :incoming_message_id => nil) + "#followup" %> </p> </div> <% elsif [ 'resent', 'followup_resent' ].include?(info_request_event.event_type) %> diff --git a/spec/fixtures/files/autoresponse-header.email b/spec/fixtures/files/autoresponse-header.email new file mode 100644 index 000000000..ecd292961 --- /dev/null +++ b/spec/fixtures/files/autoresponse-header.email @@ -0,0 +1,22 @@ +From: EMAIL_FROM +To: FOI Person <EMAIL_TO> +Delivery-date: Fri, 01 Aug 2008 14:35:58 +0100 +Return-path: <me@cheese.com> +X-Failed-Recipients: enquiries@cheese.com +Auto-Submitted: auto-replied +From: Mail Delivery System <Mailer-Daemon@sandwich.com> +Subject: Mail delivery failed: returning message to sender +Message-Id: <E1KOunW-000dXv-C6@sandwich.com> +Date: Fri, 01 Aug 2008 14:35:58 +0100 + +This message was created automatically by mail delivery software. + +A message that you sent could not be delivered to one or more of its +recipients. This is a permanent error. The following address(es) failed: + + enquiries@cheese.com + Unrouteable address + +------ This is a copy of the message, including all the headers. ------ + +Hello
\ No newline at end of file diff --git a/spec/fixtures/files/empty-return-path.email b/spec/fixtures/files/empty-return-path.email new file mode 100644 index 000000000..b01e96de8 --- /dev/null +++ b/spec/fixtures/files/empty-return-path.email @@ -0,0 +1,21 @@ +From: EMAIL_FROM +To: FOI Person <EMAIL_TO> +Return-path: <> +Delivery-date: Fri, 01 Aug 2008 14:35:58 +0100 +X-Failed-Recipients: enquiries@cheese.com +From: Mail Delivery System <Mailer-Daemon@sandwich.com> +Subject: Mail delivery failed: returning message to sender +Message-Id: <E1KOunW-000dXv-C6@sandwich.com> +Date: Fri, 01 Aug 2008 14:35:58 +0100 + +This message was created automatically by mail delivery software. + +A message that you sent could not be delivered to one or more of its +recipients. This is a permanent error. The following address(es) failed: + + enquiries@cheese.com + Unrouteable address + +------ This is a copy of the message, including all the headers. ------ + +Hello
\ No newline at end of file diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 183a258af..d6923da21 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -79,11 +79,21 @@ describe IncomingMessage, " folding quoted parts of emails" do end describe IncomingMessage, " checking validity to reply to" do - def test_email(email, result) + def test_email(result, email, return_path, autosubmitted) @address = mock(TMail::Address) @address.stub!(:spec).and_return(email) + + @return_path = mock(TMail::ReturnPathHeader) + @return_path.stub!(:addr).and_return(return_path) + + @autosubmitted = mock(TMail::KeywordsHeader) + @autosubmitted.stub!(:keys).and_return(autosubmitted) + @mail = mock(TMail::Mail) @mail.stub!(:from_addrs).and_return( [ @address ] ) + @mail.stub!(:[]).with("return-path").and_return(@return_path) + @mail.stub!(:[]).with("auto-submitted").and_return(@autosubmitted) + @incoming_message = IncomingMessage.new() @incoming_message.stub!(:mail).and_return(@mail) @@ -91,27 +101,63 @@ describe IncomingMessage, " checking validity to reply to" do end it "says a valid email is fine" do - test_email("team@mysociety.org", true) + test_email(true, "team@mysociety.org", nil, []) end it "says postmaster email is bad" do - test_email("postmaster@mysociety.org", false) + test_email(false, "postmaster@mysociety.org", nil, []) end it "says Mailer-Daemon email is bad" do - test_email("Mailer-Daemon@mysociety.org", false) + test_email(false, "Mailer-Daemon@mysociety.org", nil, []) end it "says case mangled MaIler-DaemOn email is bad" do - test_email("MaIler-DaemOn@mysociety.org", false) + test_email(false, "MaIler-DaemOn@mysociety.org", nil, []) end it "says Auto_Reply email is bad" do - test_email("Auto_Reply@mysociety.org", false) + test_email(false, "Auto_Reply@mysociety.org", nil, []) end it "says DoNotReply email is bad" do - test_email("DoNotReply@tube.tfl.gov.uk", false) + test_email(false, "DoNotReply@tube.tfl.gov.uk", nil, []) + end + + it "says a filled-out return-path is fine" do + test_email(true, "team@mysociety.org", "Return-path: <foo@baz.com>", []) + end + + it "says an empty return-path is bad" do + test_email(false, "team@mysociety.org", "<>", []) + end + + it "says an auto-submitted keyword is bad" do + test_email(false, "team@mysociety.org", nil, ["auto-replied"]) + end + +end + +describe IncomingMessage, " checking validity to reply to with real emails" do + fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users + + after(:all) do + ActionMailer::Base.deliveries.clear + end + it "should allow a reply to plain emails" do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) + ir.incoming_messages[1].valid_to_reply_to?.should == true + end + it "should not allow a reply to emails with empty return-paths" do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('empty-return-path.email', ir.incoming_email) + ir.incoming_messages[1].valid_to_reply_to?.should == false + end + it "should not allow a reply to emails with autoresponse headers" do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('autoresponse-header.email', ir.incoming_email) + ir.incoming_messages[1].valid_to_reply_to?.should == false end end |