diff options
Diffstat (limited to 'spec/mailers')
-rw-r--r-- | spec/mailers/outgoing_mailer_spec.rb | 111 | ||||
-rw-r--r-- | spec/mailers/track_mailer_spec.rb | 16 |
2 files changed, 62 insertions, 65 deletions
diff --git a/spec/mailers/outgoing_mailer_spec.rb b/spec/mailers/outgoing_mailer_spec.rb index 5d1ea2dfb..a11d56dd3 100644 --- a/spec/mailers/outgoing_mailer_spec.rb +++ b/spec/mailers/outgoing_mailer_spec.rb @@ -1,73 +1,66 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe OutgoingMailer, " when working out follow up addresses" do - # This is done with fixtures as the code is a bit tangled with the way it - # calls TMail. XXX untangle it and make these tests spread out and using - # mocks. Put parts of the tests in spec/lib/tmail_extensions.rb - before(:each) do - load_raw_emails_data +describe OutgoingMailer, " when working out follow up names and addresses" do + + before do + @info_request = mock_model(InfoRequest, + :recipient_name_and_email => 'test <test@example.com>', + :recipient_email => 'test@example.com') + @info_request.stub_chain(:public_body, :name).and_return("Test Authority") + @incoming_message = mock_model(IncomingMessage, + :from_email => 'specific@example.com', + :safe_mail_from => 'Specific Person') end - it "should parse them right" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "FOI Person <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" + def expect_address(info_request, incoming_message, expected_result) + mail = create_message_from(from_line) + name = MailHandler.get_from_name(mail) + email = MailHandler.get_from_address(mail) + address = MailHandler.address_from_name_and_email(name, email).to_s + [name, email, address].should == expected_result end - it "should work when there is only an email address" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] + describe 'if there is no incoming message being replied to' do - im.raw_email.data = im.raw_email.data.sub("\"FOI Person\" <foiperson@localhost>", "foiperson@localhost") - im.parse_raw_email! true + it 'should return the name and email address of the public body' do + OutgoingMailer.name_and_email_for_followup(@info_request, nil).should == 'test <test@example.com>' + OutgoingMailer.name_for_followup(@info_request, nil).should == 'Test Authority' + OutgoingMailer.email_for_followup(@info_request, nil).should == 'test@example.com' + end - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "foiperson@localhost" - OutgoingMailer.name_for_followup(ir, im).should == "Geraldine Quango" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" end - it "should quote funny characters" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] + describe 'if the incoming message being replied to is not valid to reply to' do - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI [ Person") - im.parse_raw_email! true + before do + @incoming_message.stub!(:valid_to_reply_to?).and_return(false) + end - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI [ Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI [ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" + it 'should return the safe name and email address of the public body' do + OutgoingMailer.name_and_email_for_followup(@info_request, @incoming_message).should == 'test <test@example.com>' + OutgoingMailer.name_for_followup(@info_request, @incoming_message).should == 'Test Authority' + OutgoingMailer.email_for_followup(@info_request, @incoming_message).should == 'test@example.com' + end end - it "should quote quotes" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] + describe 'if the incoming message is valid to reply to' do - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI \\\" Person") - im.parse_raw_email! true + before do + @incoming_message.stub!(:valid_to_reply_to?).and_return(true) + end - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI \\\" Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI \" Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end + it 'should return the name and email address from the incoming message' do + OutgoingMailer.name_and_email_for_followup(@info_request, @incoming_message).should == 'Specific Person <specific@example.com>' + OutgoingMailer.name_for_followup(@info_request, @incoming_message).should == 'Specific Person' + OutgoingMailer.email_for_followup(@info_request, @incoming_message).should == 'specific@example.com' + end - it "should quote @ signs" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] + it 'should return the name of the public body if the incoming message does not have + a safe name' do + @incoming_message.stub!(:safe_mail_from).and_return(nil) + OutgoingMailer.name_for_followup(@info_request, @incoming_message).should == 'Test Authority' + end - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI @ Person") - im.parse_raw_email! true - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI @ Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI @ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" end end @@ -79,21 +72,21 @@ describe OutgoingMailer, "when working out follow up subjects" do end it "should prefix the title with 'Freedom of Information request -' for initial requests" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) im = ir.incoming_messages[0] ir.email_subject_request.should == "Freedom of Information request - Why do you have & such a fancy dog?" end it "should use 'Re:' and inital request subject for followups which aren't replies to particular messages" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) om = outgoing_messages(:useless_outgoing_message) OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?" end it "should prefix with Re: the subject of the message being replied to" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) im = ir.incoming_messages[0] om = outgoing_messages(:useless_outgoing_message) om.incoming_message_followup = im @@ -102,7 +95,7 @@ describe OutgoingMailer, "when working out follow up subjects" do end it "should not add Re: prefix if there already is such a prefix" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) im = ir.incoming_messages[0] om = outgoing_messages(:useless_outgoing_message) om.incoming_message_followup = im @@ -112,19 +105,19 @@ describe OutgoingMailer, "when working out follow up subjects" do end it "should not add Re: prefix if there already is a lower case re: prefix" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) im = ir.incoming_messages[0] om = outgoing_messages(:useless_outgoing_message) om.incoming_message_followup = im im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: re: Geraldine FOI Code AZXB421") im.parse_raw_email! true - + OutgoingMailer.subject_for_followup(ir, om).should == "re: Geraldine FOI Code AZXB421" end it "should use 'Re:' and initial request subject when replying to failed delivery notifications" do - ir = info_requests(:fancy_dog_request) + ir = info_requests(:fancy_dog_request) im = ir.incoming_messages[0] om = outgoing_messages(:useless_outgoing_message) om.incoming_message_followup = im diff --git a/spec/mailers/track_mailer_spec.rb b/spec/mailers/track_mailer_spec.rb index 9537e6b14..e8094b692 100644 --- a/spec/mailers/track_mailer_spec.rb +++ b/spec/mailers/track_mailer_spec.rb @@ -13,7 +13,7 @@ describe TrackMailer do it 'should ask for all the users whose last daily track email was sent more than a day ago' do expected_conditions = [ "last_daily_track_email < ?", Time.utc(2007, 11, 11, 23, 59)] - User.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) + User.should_receive(:find_each).with(:conditions => expected_conditions) TrackMailer.alert_tracks end @@ -26,7 +26,7 @@ describe TrackMailer do :url_name => 'test-name', :get_locale => 'en', :should_be_emailed? => true) - User.stub!(:find).and_return([@user]) + User.stub!(:find_each).and_yield(@user) @user.stub!(:receive_email_alerts).and_return(true) @user.stub!(:no_xapian_reindex=) end @@ -69,11 +69,15 @@ describe TrackMailer do @xapian_search = mock('xapian search', :results => []) @found_event = mock_model(InfoRequestEvent, :described_at => @track_thing.created_at + 1.day) @search_result = {:model => @found_event} - InfoRequest.stub!(:full_search).and_return(@xapian_search) + ActsAsXapian::Search.stub!(:new).and_return(@xapian_search) end it 'should ask for the events returned by the tracking query' do - InfoRequest.should_receive(:full_search).with([InfoRequestEvent], 'test query', 'described_at', true, nil, 100, 1).and_return(@xapian_search) + ActsAsXapian::Search.should_receive(:new).with([InfoRequestEvent], 'test query', + :sort_by_prefix => 'described_at', + :sort_by_ascending => true, + :collapse_by_prefix => nil, + :limit => 100).and_return(@xapian_search) TrackMailer.alert_tracks end @@ -124,7 +128,7 @@ describe TrackMailer do :save! => true, :url_name => 'test-name', :should_be_emailed? => false) - User.stub!(:find).and_return([@user]) + User.stub!(:find_each).and_yield(@user) @user.stub!(:receive_email_alerts).and_return(true) @user.stub!(:no_xapian_reindex=) end @@ -195,7 +199,7 @@ describe TrackMailer do context "force ssl is off" do # Force SSL is off in the tests. Since the code that selectively switches the protocols # is in the initialiser for Rails it's hard to test. Hmmm... - # We could Configuration.stub!(:force_ssl).and_return(true) but the config/environment.rb + # We could AlaveteliConfiguration.stub!(:force_ssl).and_return(true) but the config/environment.rb # wouldn't get reloaded it "should have http links in the email" do |