aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/mail_handler/mail_handler_spec.rb23
-rw-r--r--spec/lib/tmail_extensions_spec.rb45
-rw-r--r--spec/models/incoming_message_spec.rb56
-rw-r--r--spec/models/info_request_event_spec.rb47
-rw-r--r--spec/models/request_mailer_spec.rb2
-rw-r--r--spec/models/track_mailer_spec.rb2
-rw-r--r--spec/script/handle-mail-replies_spec.rb10
-rw-r--r--spec/spec_helper.rb4
8 files changed, 100 insertions, 89 deletions
diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb
new file mode 100644
index 000000000..a3fba0698
--- /dev/null
+++ b/spec/lib/mail_handler/mail_handler_spec.rb
@@ -0,0 +1,23 @@
+# coding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
+
+describe 'when creating a mail object from raw data' do
+
+ it 'should correctly parse a multipart email with a linebreak in the boundary' do
+ mail = get_fixture_mail('space-boundary.email')
+ mail.parts.size.should == 2
+ mail.multipart?.should == true
+ end
+
+ it 'should parse multiple to addresses with unqoted display names' do
+ mail = get_fixture_mail('multiple-unquoted-display-names.email')
+ mail.to.should == ["request-66666-caa77777@whatdotheyknow.com", "foi@example.com"]
+ end
+
+ it 'should convert an iso8859 email to utf8' do
+ mail = get_fixture_mail('iso8859_2_raw_email.email')
+ mail.subject.should have_text(/gjatë/u)
+ mail.body.is_utf8?.should == true
+ end
+
+end
diff --git a/spec/lib/tmail_extensions_spec.rb b/spec/lib/tmail_extensions_spec.rb
deleted file mode 100644
index bd89e6a84..000000000
--- a/spec/lib/tmail_extensions_spec.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-# coding: utf-8
-# This is a test of the set_content_type monkey patch in
-# lib/tmail_extensions.rb
-
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe "when using TMail" do
-
- before(:each) do
- ActionMailer::Base.deliveries.clear
- end
-
- it "should load an email with funny MIME settings" do
- # just send it to the holding pen
- InfoRequest.holding_pen_request.incoming_messages.size.should == 0
- receive_incoming_mail("humberside-police-odd-mime-type.email", 'dummy')
- InfoRequest.holding_pen_request.incoming_messages.size.should == 1
-
- # clear the notification of new message in holding pen
- deliveries = ActionMailer::Base.deliveries
- deliveries.size.should == 1
- deliveries.clear
-
- incoming_message = InfoRequest.holding_pen_request.incoming_messages[0]
-
- # This will raise an error if the bug in TMail hasn't been fixed
- incoming_message.get_body_for_html_display()
- end
-
- it 'should parse multiple to addresses with unqoted display names' do
- mail = TMail::Mail.parse(load_file_fixture('multiple-unquoted-display-names.email'))
- mail.to.should == ["request-66666-caa77777@whatdotheyknow.com", "foi@example.com"]
- end
-
- it 'should convert to utf8' do
- # NB this isn't actually a TMail extension, but is core TMail;
- # this was just a convenient place to assert the UTF8
- # conversion is working
- mail = TMail::Mail.parse(load_file_fixture('iso8859_2_raw_email.email'))
- mail.subject.should have_text(/gjatë/u)
- mail.body.is_utf8?.should == true
- end
-
-end
-
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index b038c43d9..fdbcd1e23 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -85,6 +85,26 @@ describe IncomingMessage, " when dealing with incoming mail" do
end
end
+
+ it "should load an email with funny MIME settings" do
+ ActionMailer::Base.deliveries.clear
+ # just send it to the holding pen
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 0
+ receive_incoming_mail("humberside-police-odd-mime-type.email", 'dummy')
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 1
+
+ # clear the notification of new message in holding pen
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ deliveries.clear
+
+ incoming_message = InfoRequest.holding_pen_request.incoming_messages[0]
+
+ # This will raise an error if the bug in TMail hasn't been fixed
+ incoming_message.get_body_for_html_display()
+ end
+
+
end
describe IncomingMessage, "when parsing HTML mail" do
@@ -399,14 +419,8 @@ end
describe IncomingMessage, " when uudecoding bad messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should be able to do it at all" do
- mail_body = load_file_fixture('incoming-request-bad-uuencoding.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-bad-uuencoding.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
im.extract_attachments!
@@ -418,9 +432,7 @@ describe IncomingMessage, " when uudecoding bad messages" do
end
it "should apply censor rules" do
- mail_body = load_file_fixture('incoming-request-bad-uuencoding.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-bad-uuencoding.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -443,14 +455,8 @@ end
describe IncomingMessage, "when messages are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-attach-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-attach-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -468,14 +474,8 @@ end
describe IncomingMessage, "when Outlook messages are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-oft-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-oft-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -490,14 +490,8 @@ end
describe IncomingMessage, "when TNEF attachments are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-tnef-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-tnef-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb
index 7352f3be0..796f8b840 100644
--- a/spec/models/info_request_event_spec.rb
+++ b/spec/models/info_request_event_spec.rb
@@ -54,36 +54,71 @@ describe InfoRequestEvent do
end
- describe "doing search/index stuff" do
+ describe "doing search/index stuff" do
before(:each) do
load_raw_emails_data
parse_all_incoming_messages
end
- it 'should get search text for outgoing messages' do
+ it 'should get search text for outgoing messages' do
event = info_request_events(:useless_outgoing_message_event)
message = outgoing_messages(:useless_outgoing_message).body
event.search_text_main.should == message + "\n\n"
end
- it 'should get search text for incoming messages' do
+ it 'should get search text for incoming messages' do
event = info_request_events(:useless_incoming_message_event)
event.search_text_main.strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
end
- it 'should get clipped text for incoming messages, and cache it too' do
+ it 'should get clipped text for incoming messages, and cache it too' do
event = info_request_events(:useless_incoming_message_event)
-
+
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded = nil
event.search_text_main(true).strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded.should_not == nil
end
-
end
+ describe 'when asked if it has the same email as a previous send' do
+
+ before do
+ @info_request_event = InfoRequestEvent.new
+ end
+
+ it 'should return true if the email in its params and the previous email the request was sent to are both nil' do
+ @info_request_event.stub!(:params).and_return({})
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ it 'should return false if one email address exists and the other does not' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
+ @info_request_event.same_email_as_previous_send?.should be_false
+ end
+ it 'should return true if the addresses are identical' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ it 'should return false if the addresses are different' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('different@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_false
+ end
+
+ it 'should return true if the addresses have different formats' do
+ @info_request_event.stub!(:params).and_return(:email => 'A Test <test@example.com>')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ end
end
diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb
index 906756784..0f09e6926 100644
--- a/spec/models/request_mailer_spec.rb
+++ b/spec/models/request_mailer_spec.rb
@@ -98,7 +98,7 @@ describe RequestMailer, " when receiving incoming mail" do
mail.multipart?.should == true
mail.parts.size.should == 2
message_part = mail.parts[0].to_s
- bounced_mail = TMail::Mail.parse(mail.parts[1].body)
+ bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body, decode=false)
bounced_mail.to.should == [ ir.incoming_email ]
bounced_mail.from.should == [ 'geraldinequango@localhost' ]
bounced_mail.body.include?("That's so totally a rubbish question").should be_true
diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb
index 1bf77dab5..9bf03c3d0 100644
--- a/spec/models/track_mailer_spec.rb
+++ b/spec/models/track_mailer_spec.rb
@@ -169,7 +169,7 @@ describe TrackMailer do
it 'should deliver one email, with right headers' do
@user = mock_model(User,
- :name_and_email => TMail::Address.address_from_name_and_email('Tippy Test', 'tippy@localhost'),
+ :name_and_email => MailHandler.address_from_name_and_email('Tippy Test', 'tippy@localhost'),
:url_name => 'tippy_test'
)
diff --git a/spec/script/handle-mail-replies_spec.rb b/spec/script/handle-mail-replies_spec.rb
index 406af9ee3..90a8de27c 100644
--- a/spec/script/handle-mail-replies_spec.rb
+++ b/spec/script/handle-mail-replies_spec.rb
@@ -5,7 +5,7 @@ def mail_reply_test(email_filename)
Dir.chdir Rails.root do
xc = ExternalCommand.new("script/handle-mail-replies", "--test")
xc.run(load_file_fixture(email_filename))
-
+
xc.err.should == ""
return xc
end
@@ -14,7 +14,7 @@ end
describe "When filtering" do
it "should not fail when not in test mode" do
xc = ExternalCommand.new("script/handle-mail-replies")
- xc.run(load_file_fixture("track-response-exim-bounce.email"))
+ xc.run(load_file_fixture("track-response-exim-bounce.email"))
xc.err.should == ""
end
@@ -23,19 +23,19 @@ describe "When filtering" do
r.status.should == 1
r.out.should == "user@example.com\n"
end
-
+
it "should detect a WebShield delivery error message" do
r = mail_reply_test("track-response-webshield-bounce.email")
r.status.should == 1
r.out.should == "failed.user@example.co.uk\n"
end
-
+
it "should detect a MS Exchange non-permanent delivery error message" do
r = mail_reply_test("track-response-ms-bounce.email")
r.status.should == 1
r.out.should == ""
end
-
+
it "should pass on a non-bounce message" do
r = mail_reply_test("incoming-request-bad-uuencoding.email")
r.status.should == 0
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 248dff70e..e6d81540b 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -208,6 +208,10 @@ def load_raw_emails_data
end
end
+def get_fixture_mail(filename)
+ MailHandler.mail_from_raw_email(load_file_fixture(filename))
+end
+
def parse_all_incoming_messages
IncomingMessage.find(:all).each{|x| x.parse_raw_email!}
end