From 0d533ce1d56e1ec1446774cc6fff52451a844f3f Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sun, 9 Dec 2012 18:35:39 +1100 Subject: Switch to new method name --- spec/models/public_body_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index c2e0a6353..127391e79 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -210,7 +210,7 @@ describe PublicBody, "when searching" do end it "should cope with same url_name across multiple locales" do - PublicBody.with_locale(:es) do + PublicBody.with_locales(:es) do # use the unique spanish name to retrieve and edit body = PublicBody.find_by_url_name_with_historic('etgq') body.short_name = 'tgq' # Same as english version @@ -231,7 +231,7 @@ end describe PublicBody, " when dealing public body locales" do it "shouldn't fail if it internal_admin_body was created in a locale other than the default" do # first time, do it with the non-default locale - PublicBody.with_locale(:es) do + PublicBody.with_locales(:es) do PublicBody.internal_admin_body end @@ -378,7 +378,7 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 - # XXX Not sure why trying to do a PublicBody.with_locale fails here. Seems related to + # XXX Not sure why trying to do a PublicBody.with_locales fails here. Seems related to # the way categories are loaded every time from the PublicBody class. For now we just # test some translation was done. body = PublicBody.find_by_name('North West Fake Authority') -- cgit v1.2.3 From 1f8bc6560b5240a7aa84ac8d6bdc47eea5d2d781 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Mon, 10 Dec 2012 00:09:55 +1100 Subject: The with_locale has been removed in Globalize3 I think Globalize is supposed to pick up the locale from I18n anyway so I don't know if these are needed. I think I still haven't done the right thing but it's time to move on. --- spec/models/public_body_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 127391e79..f247f561c 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -210,7 +210,7 @@ describe PublicBody, "when searching" do end it "should cope with same url_name across multiple locales" do - PublicBody.with_locales(:es) do + I18n.with_locale(:es) do # use the unique spanish name to retrieve and edit body = PublicBody.find_by_url_name_with_historic('etgq') body.short_name = 'tgq' # Same as english version @@ -231,7 +231,7 @@ end describe PublicBody, " when dealing public body locales" do it "shouldn't fail if it internal_admin_body was created in a locale other than the default" do # first time, do it with the non-default locale - PublicBody.with_locales(:es) do + I18n.with_locale(:es) do PublicBody.internal_admin_body end @@ -378,7 +378,7 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 - # XXX Not sure why trying to do a PublicBody.with_locales fails here. Seems related to + # XXX Not sure why trying to do a PublicBody.with_locale fails here. Seems related to # the way categories are loaded every time from the PublicBody class. For now we just # test some translation was done. body = PublicBody.find_by_name('North West Fake Authority') -- cgit v1.2.3 From 6e63699743941457962d53827e683fc085555aae Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 13:47:12 +1100 Subject: safe_mock_model doesn't seem to be needed, also tested in Ruby 1.9 --- spec/models/info_request_spec.rb | 16 ++++++++-------- spec/models/user_spec.rb | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'spec/models') diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 544852f91..a1a6e6c27 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -426,8 +426,8 @@ describe InfoRequest do before do Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) - @mock_comment_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment') - @mock_response_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response') + @mock_comment_event = mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment') + @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response') @info_request = InfoRequest.new(:prominence => 'normal', :awaiting_description => true, :info_request_events => [@mock_response_event, @mock_comment_event]) @@ -457,16 +457,16 @@ describe InfoRequest do describe 'when applying censor rules' do before do - @global_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @global_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @user_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @user_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @request_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @request_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @body_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @body_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @user = safe_mock_model(User, :censor_rules => [@user_rule]) - @body = safe_mock_model(PublicBody, :censor_rules => [@body_rule]) + @user = mock_model(User, :censor_rules => [@user_rule]) + @body = mock_model(PublicBody, :censor_rules => [@body_rule]) @info_request = InfoRequest.new(:prominence => 'normal', :awaiting_description => true, :title => 'title') diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e31c3f1b5..651ba4b65 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -152,10 +152,10 @@ end describe User, "when reindexing referencing models" do before do - @request_event = safe_mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) - @request = safe_mock_model(InfoRequest, :info_request_events => [@request_event]) - @comment_event = safe_mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) - @comment = safe_mock_model(Comment, :info_request_events => [@comment_event]) + @request_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) + @request = mock_model(InfoRequest, :info_request_events => [@request_event]) + @comment_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) + @comment = mock_model(Comment, :info_request_events => [@comment_event]) @user = User.new(:comments => [@comment], :info_requests => [@request]) end -- cgit v1.2.3 From 8f29b77bfb54a9ce42c18087ecae06ca96412655 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 14:24:03 +1100 Subject: Update to new RSpec module names --- spec/models/incoming_message_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 70b323e9f..97bf78cb6 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -27,7 +27,7 @@ describe IncomingMessage, " when dealing with incoming mail" do end it "should correctly fold various types of footer" do - Dir.glob(File.join(Spec::Runner.configuration.fixture_path, "files", "email-folding-example-*.txt")).each do |file| + Dir.glob(File.join(RSpec.configuration.fixture_path, "files", "email-folding-example-*.txt")).each do |file| message = File.read(file) parsed = IncomingMessage.remove_quoted_sections(message) expected = File.read("#{file}.expected") -- cgit v1.2.3 From 1e7e8b5f73f5680b4c68063680ed5335c070987f Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 12 Dec 2012 16:38:30 +1100 Subject: The run_callbacks API has changed https://rails.lighthouseapp.com/projects/8994/tickets/5472-unable-to-manually-run-activerecord-object-callbacks --- spec/models/info_request_event_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 796f8b840..842246fd8 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -21,7 +21,7 @@ describe InfoRequestEvent do :event_type => 'sent', :params => {}) event.should_receive(:xapian_mark_needs_index) - event.run_callbacks(:after_save) + event.run_callbacks(:save) end end -- cgit v1.2.3 From 5f6a7a29bc1ea3784728a9c57edfa4be73d4196f Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Thu, 17 Jan 2013 05:26:54 +1100 Subject: Set locale with I18n rather than through globalize --- spec/models/public_body_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index c2e0a6353..0b1bfccd7 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -210,7 +210,7 @@ describe PublicBody, "when searching" do end it "should cope with same url_name across multiple locales" do - PublicBody.with_locale(:es) do + I18n.with_locale(:es) do # use the unique spanish name to retrieve and edit body = PublicBody.find_by_url_name_with_historic('etgq') body.short_name = 'tgq' # Same as english version @@ -231,7 +231,7 @@ end describe PublicBody, " when dealing public body locales" do it "shouldn't fail if it internal_admin_body was created in a locale other than the default" do # first time, do it with the non-default locale - PublicBody.with_locale(:es) do + I18n.with_locale(:es) do PublicBody.internal_admin_body end @@ -378,7 +378,7 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 - # XXX Not sure why trying to do a PublicBody.with_locale fails here. Seems related to + # XXX Not sure why trying to do a I18n.with_locale fails here. Seems related to # the way categories are loaded every time from the PublicBody class. For now we just # test some translation was done. body = PublicBody.find_by_name('North West Fake Authority') -- cgit v1.2.3 From bb3222c06d13161c00564c5d0d7c2dc2784279fe Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 23 Jan 2013 12:17:04 +1100 Subject: In 447e4af we started adding the error to each attribute so start testing each --- spec/models/censor_rule_spec.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'spec/models') diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index c11b05a03..c1abcd71a 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -134,15 +134,21 @@ describe 'when validating rules' do it 'should not allow a global text censor rule (without user_id, request_id or public_body_id)' do @censor_rule.valid?.should == false - @expected_error = 'Censor must apply to an info request a user or a body; is invalid' - @censor_rule.errors.full_messages.should == [@expected_error] + + expected_error = ["Rule must apply to an info request, a user or a body"] + @censor_rule.errors[:user].should == expected_error + @censor_rule.errors[:info_request].should == expected_error + @censor_rule.errors[:public_body].should == expected_error end it 'should not allow a global regex censor rule (without user_id, request_id or public_body_id)' do @censor_rule.regexp = true @censor_rule.valid?.should == false - @expected_error = 'Censor must apply to an info request a user or a body; is invalid' - @censor_rule.errors.full_messages.should == [@expected_error] + + expected_error = ["Rule must apply to an info request, a user or a body"] + @censor_rule.errors[:user].should == expected_error + @censor_rule.errors[:info_request].should == expected_error + @censor_rule.errors[:public_body].should == expected_error end end -- cgit v1.2.3 From b60b459a1bfea6d9c60e302eaeaa403020459cc1 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 23 Jan 2013 12:20:31 +1100 Subject: Change syntax - this looks like a test to me --- spec/models/censor_rule_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index c1abcd71a..d216ba91c 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -73,7 +73,7 @@ end describe 'when validating rules' do - describe 'should be invalid without text' do + it 'should be invalid without text' do censor_rule = CensorRule.new censor_rule.valid?.should == false censor_rule.errors.on(:text).should == "can't be blank" -- cgit v1.2.3 From 0a19af1e65cd48d170cf853169e4220542db15e4 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Wed, 23 Jan 2013 12:21:38 +1100 Subject: Fix deprecation warnings * Errors#on have been deprecated * Errors#[] now always returns an Array --- spec/models/censor_rule_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/models') diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index d216ba91c..3782cc630 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -76,7 +76,7 @@ describe 'when validating rules' do it 'should be invalid without text' do censor_rule = CensorRule.new censor_rule.valid?.should == false - censor_rule.errors.on(:text).should == "can't be blank" + censor_rule.errors[:text].should == ["can't be blank"] end describe 'when validating a regexp rule' do @@ -96,7 +96,7 @@ describe 'when validating rules' do it 'should add an error message to the text field with the regexp error message' do Regexp.stub!(:new).and_raise(RegexpError.new("very bad regexp")) @censor_rule.valid?.should == false - @censor_rule.errors.on(:text).should == "very bad regexp" + @censor_rule.errors[:text].should == ["very bad regexp"] end end @@ -106,7 +106,7 @@ describe 'when validating rules' do it 'should not add any error message to the text field' do Regexp.stub!(:new) @censor_rule.valid? - @censor_rule.errors.on(:text).should == nil + @censor_rule.errors[:text].should == [] end end -- cgit v1.2.3 From 6bdcd345b13ea94b3cbcf720619be8c4d36424c7 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Wed, 23 Jan 2013 16:54:39 +1100 Subject: Fixes to ApplicationMailer tests in Rails 3 --- spec/models/application_mailer_spec.rb | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'spec/models') diff --git a/spec/models/application_mailer_spec.rb b/spec/models/application_mailer_spec.rb index acf5f43bc..cc4609e5a 100644 --- a/spec/models/application_mailer_spec.rb +++ b/spec/models/application_mailer_spec.rb @@ -8,8 +8,7 @@ describe ApplicationMailer do def set_base_views ApplicationMailer.class_eval do @previous_view_paths = self.view_paths.dup - self.view_paths.clear - self.view_paths << File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'core') + self.view_paths = [File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'core')] end end @@ -27,13 +26,13 @@ describe ApplicationMailer do def prepend_theme_views(theme_name) ApplicationMailer.class_eval do - view_paths.unshift File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) + prepend_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) end end def append_theme_views(theme_name) ApplicationMailer.class_eval do - view_paths << File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) + append_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) end end @@ -45,11 +44,8 @@ describe ApplicationMailer do def create_multipart_method(method_name) ApplicationMailer.send(:define_method, method_name) do - attachment :content_type => 'message/rfc822', - :body => 'xxx', - :filename => "original.eml", - :transfer_encoding => '7bit', - :content_disposition => 'inline' + attachments['original.eml'] = 'xxx' + mail end end @@ -78,10 +74,10 @@ describe ApplicationMailer do @mail.body.should match('Core only') end - it 'should raise an error if the template is in neither core nor theme' do + it 'should render an empty body if the template is in neither core nor theme' do prepend_theme_views('theme_one') - expected_error = 'Missing template application_mailer/neither.erb in view path' - lambda{ ApplicationMailer.create_neither() }.should raise_error(/#{expected_error}/) + @mail = ApplicationMailer.create_neither() + @mail.body.should be_empty end it 'should render a multipart email using a theme template' do @@ -124,10 +120,10 @@ describe ApplicationMailer do @mail.body.should match('Core only') end - it 'should raise an error if the template is in neither core nor theme' do + it 'should render an empty body if the template is in neither core nor theme' do append_theme_views('theme_one') - expected_error = 'Missing template application_mailer/neither.erb in view path' - lambda{ ApplicationMailer.create_neither() }.should raise_error(/#{expected_error}/) + @mail = ApplicationMailer.create_neither() + @mail.body.should be_empty end it 'should render a multipart email using a core template' do -- cgit v1.2.3 From c799e4184c3f14e7770afa514621c72ebc408c52 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Mon, 28 Jan 2013 10:55:37 +1100 Subject: Calling TMail::Mail#base64_decode does not modify the mail object so doesn't do anything as used here --- spec/models/request_mailer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index 0f09e6926..5edc8edb6 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 = MailHandler.mail_from_raw_email(mail.parts[1].body, decode=false) + bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body) 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 -- cgit v1.2.3 From 8ef10e453fb0c87c3c58ce6eefdd54c1928a6590 Mon Sep 17 00:00:00 2001 From: Matthew Landauer Date: Fri, 1 Feb 2013 17:39:05 +1100 Subject: Move over to rails 3 mail api because there appear to be problems with sending attachments --- spec/models/request_mailer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index 5edc8edb6..f7eaa58f8 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 = MailHandler.mail_from_raw_email(mail.parts[1].body) + bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body.to_s) 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 -- cgit v1.2.3 From bfeadd9e2273ed8c09f841e1345fb826182d93a6 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Mon, 4 Feb 2013 15:05:31 +1100 Subject: Add a test that exposes a problem we have with renaming attributes --- spec/models/public_body_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 0b1bfccd7..bc693b4da 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -169,6 +169,14 @@ describe PublicBody, " when saving" do @public_body.save! @public_body.first_letter.should == 'T' end + + it "should save the name when renaming an existing public body" do + public_body = public_bodies(:geraldine_public_body) + public_body.name = "Mark's Public Body" + public_body.save! + + public_body.name.should == "Mark's Public Body" + end end describe PublicBody, "when searching" do -- cgit v1.2.3 From d38e035cced8bf225f1e3c94755f3ad14db0b302 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Fri, 8 Feb 2013 15:29:05 +1100 Subject: Mocking and stubbing work a bit differently, this allows the test to run --- spec/models/request_mailer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index f7eaa58f8..72fd808d7 100644 --- a/spec/models/request_mailer_spec.rb +++ b/spec/models/request_mailer_spec.rb @@ -243,7 +243,7 @@ describe RequestMailer, "when sending reminders to requesters to classify a resp query_params[:conditions].should == expected_conditions query_params[:include].should == [ :user ] query_params[:order].should == 'info_requests.id' - end + end.and_return [@mock_request] send_alerts end -- cgit v1.2.3 From 01f80bfcdcb055705552146cebccec0162caff49 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Mon, 25 Feb 2013 14:53:56 +1100 Subject: and_return with should_not_receive is deprecated. Fixes #826 --- spec/models/track_mailer_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/models') diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb index 9bf03c3d0..896f1c416 100644 --- a/spec/models/track_mailer_spec.rb +++ b/spec/models/track_mailer_spec.rb @@ -129,7 +129,7 @@ describe TrackMailer do it 'should not ask for any daily track things for the user' do expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) + TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions) TrackMailer.alert_tracks end @@ -137,7 +137,7 @@ describe TrackMailer do @user.stub(:should_be_emailed?).and_return(true) @user.stub(:receive_email_alerts).and_return(false) expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) + TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions) TrackMailer.alert_tracks end -- cgit v1.2.3 From 506b848bc60cdaa5c33db73fd897d53c8240fcf4 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Mon, 25 Feb 2013 16:28:59 +1100 Subject: Mailers have their own home in Rails 3 --- spec/models/application_mailer_spec.rb | 158 --------------- spec/models/contact_mailer_spec.rb | 8 - spec/models/outgoing_mailer_spec.rb | 140 ------------- spec/models/request_mailer_spec.rb | 359 --------------------------------- spec/models/track_mailer_spec.rb | 198 ------------------ spec/models/user_mailer_spec.rb | 8 - 6 files changed, 871 deletions(-) delete mode 100644 spec/models/application_mailer_spec.rb delete mode 100644 spec/models/contact_mailer_spec.rb delete mode 100644 spec/models/outgoing_mailer_spec.rb delete mode 100644 spec/models/request_mailer_spec.rb delete mode 100644 spec/models/track_mailer_spec.rb delete mode 100644 spec/models/user_mailer_spec.rb (limited to 'spec/models') diff --git a/spec/models/application_mailer_spec.rb b/spec/models/application_mailer_spec.rb deleted file mode 100644 index cc4609e5a..000000000 --- a/spec/models/application_mailer_spec.rb +++ /dev/null @@ -1,158 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - - -describe ApplicationMailer do - - context 'when using plugins' do - - def set_base_views - ApplicationMailer.class_eval do - @previous_view_paths = self.view_paths.dup - self.view_paths = [File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'core')] - end - end - - def add_mail_methods(method_names) - method_names.each{ |method_name| ApplicationMailer.send(:define_method, method_name){} } - end - - def remove_mail_methods(method_names) - method_names.each do |method_name| - if ApplicationMailer.respond_to?(method_name) - ApplicationMailer.send(:remove_method, method_name) - end - end - end - - def prepend_theme_views(theme_name) - ApplicationMailer.class_eval do - prepend_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) - end - end - - def append_theme_views(theme_name) - ApplicationMailer.class_eval do - append_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) - end - end - - def reset_views - ApplicationMailer.class_eval do - self.view_paths = @previous_view_paths - end - end - - def create_multipart_method(method_name) - ApplicationMailer.send(:define_method, method_name) do - attachments['original.eml'] = 'xxx' - mail - end - end - - before do - set_base_views - add_mail_methods(['simple', 'theme_only', 'core_only', 'neither']) - end - - describe 'when a plugin prepends its mail templates to the view paths' do - - it 'should render a theme template in preference to a core template' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_simple() - @mail.body.should match('Theme simple') - end - - it 'should render the template provided by the theme if no template is available in core' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_theme_only() - @mail.body.should match('Theme only') - end - - it 'should render the template provided by core if there is no theme template' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_core_only() - @mail.body.should match('Core only') - end - - it 'should render an empty body if the template is in neither core nor theme' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_neither() - @mail.body.should be_empty - end - - it 'should render a multipart email using a theme template' do - prepend_theme_views('theme_one') - create_multipart_method('multipart_theme_only') - @mail = ApplicationMailer.create_multipart_theme_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Theme multipart") - end - - it 'should render a multipart email using a core template' do - prepend_theme_views('theme_one') - create_multipart_method('multipart_core_only') - @mail = ApplicationMailer.create_multipart_core_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Core multipart") - end - - end - - describe 'when a plugin appends its mail templates to the view paths' do - - it 'should render a core template in preference to a theme template' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_simple() - @mail.body.should match('Core simple') - end - - it 'should render the template provided by the theme if no template is available in core' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_theme_only() - @mail.body.should match('Theme only') - end - - it 'should render the template provided by core if there is no theme template' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_core_only() - @mail.body.should match('Core only') - end - - it 'should render an empty body if the template is in neither core nor theme' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_neither() - @mail.body.should be_empty - end - - it 'should render a multipart email using a core template' do - append_theme_views('theme_one') - create_multipart_method('multipart_core_only') - @mail = ApplicationMailer.create_multipart_core_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Core multipart") - end - - it 'should render a multipart email using a theme template' do - append_theme_views('theme_one') - create_multipart_method('multipart_theme_only') - @mail = ApplicationMailer.create_multipart_theme_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Theme multipart") - end - - end - - after do - reset_views - remove_mail_methods(['simple', 'theme_only', 'core_only', 'neither', 'multipart']) - end - end - -end - - - diff --git a/spec/models/contact_mailer_spec.rb b/spec/models/contact_mailer_spec.rb deleted file mode 100644 index 202e45758..000000000 --- a/spec/models/contact_mailer_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe ContactMailer, " when blah" do - before do - end -end - - diff --git a/spec/models/outgoing_mailer_spec.rb b/spec/models/outgoing_mailer_spec.rb deleted file mode 100644 index 5d1ea2dfb..000000000 --- a/spec/models/outgoing_mailer_spec.rb +++ /dev/null @@ -1,140 +0,0 @@ -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 - 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 " - OutgoingMailer.name_for_followup(ir, im).should == "FOI Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should work when there is only an email address" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - im.raw_email.data = im.raw_email.data.sub("\"FOI Person\" ", "foiperson@localhost") - im.parse_raw_email! true - - # 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] - - 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\" " - OutgoingMailer.name_for_followup(ir, im).should == "FOI [ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should quote quotes" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - 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\" " - OutgoingMailer.name_for_followup(ir, im).should == "FOI \" Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should quote @ signs" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - 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\" " - OutgoingMailer.name_for_followup(ir, im).should == "FOI @ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - -end - -describe OutgoingMailer, "when working out follow up subjects" do - - before(:each) do - load_raw_emails_data - end - - it "should prefix the title with 'Freedom of Information request -' for initial requests" do - 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) - 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) - im = ir.incoming_messages[0] - om = outgoing_messages(:useless_outgoing_message) - om.incoming_message_followup = im - - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421" - end - - it "should not add Re: prefix if there already is such a prefix" do - 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") - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421" - end - - it "should not add Re: prefix if there already is a lower case re: prefix" do - 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) - 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("foiperson@localhost", "postmaster@localhost") - im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: Delivery Failed") - im.parse_raw_email! true - - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?" - end -end - - diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb deleted file mode 100644 index 72fd808d7..000000000 --- a/spec/models/request_mailer_spec.rb +++ /dev/null @@ -1,359 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe RequestMailer, " when receiving incoming mail" do - before(:each) do - load_raw_emails_data - ActionMailer::Base.deliveries = [] - end - - it "should append it to the appropriate request" do - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 2 # one more arrives - ir.info_request_events[-1].incoming_message_id.should_not be_nil - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'bob@localhost' ] # to the user who sent fancy_dog_request - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the email is not to any information request" do - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', 'dummy@localhost') - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event - last_event.params[:rejected_reason].should == "Could not identify the request from the email address" - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the from email is empty and only authorites can reply" do - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event - last_event.params[:rejected_reason].should =~ /there is no "From" address/ - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the from email is unknown and only authorites can reply" do - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event - last_event.params[:rejected_reason].should =~ /Only the authority can reply/ - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should return incoming mail to sender when a request is stopped fully for spam" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'bounce' - ir.save! - - # test what happens if something arrives - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 # nothing should arrive - - # should be a message back to sender - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'geraldinequango@localhost' ] - # check attached bounce is good copy of incoming-request-plain.email - mail.multipart?.should == true - mail.parts.size.should == 2 - message_part = mail.parts[0].to_s - bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body.to_s) - 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 - message_part.include?("marked to no longer receive responses").should be_true - deliveries.clear - end - - it "should return incoming mail to sender if not authority when a request is stopped for non-authority spam" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'bounce' - ir.save! - - # Test what happens if something arrives from authority domain (@localhost) - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "Geraldine ") - ir.incoming_messages.size.should == 2 # one more arrives - - # ... should get "responses arrived" message for original requester - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'bob@localhost' ] # to the user who sent fancy_dog_request - deliveries.clear - - # Test what happens if something arrives from another domain - ir.incoming_messages.size.should == 2 # in fixture and above - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "dummy-address@dummy.localhost") - ir.incoming_messages.size.should == 2 # nothing should arrive - - # ... should be a bounce message back to sender - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'dummy-address@dummy.localhost' ] - deliveries.clear - end - - it "should send all new responses to holding pen if a request is marked to do so" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - - # test what happens if something arrives - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 # arrives in holding pen - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event - last_event.params[:rejected_reason].should =~ /allow new responses from nobody/ - - # should be a message to admin regarding holding pen - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should destroy the messages sent to a request if marked to do so" do - ActionMailer::Base.deliveries.clear - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'blackhole' - ir.save! - - # test what happens if something arrives - should be nothing - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - - # should be no messages to anyone - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 0 - end - - - it "should not mutilate long URLs when trying to word wrap them" do - long_url = 'http://www.this.is.quite.a.long.url.flourish.org/there.is.no.way.it.is.short.whatsoever' - body = "This is a message with quite a long URL in it. It also has a paragraph, being this one that has quite a lot of text in it to. Enough to test the wrapping of itself. - -#{long_url} - -And a paragraph afterwards." - wrapped = MySociety::Format.wrap_email_body_by_paragraphs(body) - wrapped.should include(long_url) - end -end - - -describe RequestMailer, "when sending reminders to requesters to classify a response to their request" do - - before do - Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) - @mock_event = mock_model(InfoRequestEvent) - @mock_response = mock_model(IncomingMessage) - @mock_user = mock_model(User) - @mock_request = mock_model(InfoRequest, :get_last_response_event_id => @mock_event.id, - :get_last_response => @mock_response, - :user_id => 2, - :url_title => 'test_title', - :user => @mock_user) - InfoRequest.stub!(:find).and_return([@mock_request]) - RequestMailer.stub!(:deliver_new_response_reminder_alert) - @sent_alert = mock_model(UserInfoRequestSentAlert, :user= =>nil, - :info_request= => nil, - :alert_type= => nil, - :info_request_event_id= => nil, - :save! => true) - UserInfoRequestSentAlert.stub!(:new).and_return(@sent_alert) - end - - def send_alerts - RequestMailer.alert_new_response_reminders_internal(7, 'new_response_reminder_1') - end - - it 'should ask for all requests that are awaiting description and whose latest response is older - than the number of days given and that are not the holding pen' do - expected_conditions = [ "awaiting_description = ? - AND (SELECT created_at - FROM info_request_events - WHERE info_request_events.info_request_id = info_requests.id - AND info_request_events.event_type = 'response' - ORDER BY created_at desc LIMIT 1) < ? - AND url_title != 'holding_pen' - AND user_id IS NOT NULL".split(' ').join(' '), - true, Time.now() - 7.days ] - - # compare the query string ignoring any spacing differences - InfoRequest.should_receive(:find) do |all, query_params| - query_string = query_params[:conditions][0] - query_params[:conditions][0] = query_string.split(' ').join(' ') - query_params[:conditions].should == expected_conditions - query_params[:include].should == [ :user ] - query_params[:order].should == 'info_requests.id' - end.and_return [@mock_request] - - send_alerts - end - - it 'should raise an error if a request does not have a last response event id' do - @mock_request.stub!(:get_last_response_event_id).and_return(nil) - expected_message = "internal error, no last response while making alert new response reminder, request id #{@mock_request.id}" - lambda{ send_alerts }.should raise_error(expected_message) - end - - it 'should check to see if an alert matching the attributes of the one to be sent has already been sent' do - expected_params = {:conditions => [ "alert_type = ? and user_id = ? and info_request_id = ? and info_request_event_id = ?", - 'new_response_reminder_1', 2, @mock_request.id, @mock_event.id]} - UserInfoRequestSentAlert.should_receive(:find).with(:first, expected_params) - send_alerts - end - - describe 'if an alert matching the attributes of the reminder to be sent has already been sent' do - - before do - UserInfoRequestSentAlert.stub!(:find).and_return(mock_model(UserInfoRequestSentAlert)) - end - - it 'should not send the reminder' do - RequestMailer.should_not_receive(:deliver_new_response_reminder_alert) - send_alerts - end - - end - - describe 'if no alert matching the attributes of the reminder to be sent has already been sent' do - - before do - UserInfoRequestSentAlert.stub!(:find).and_return(nil) - end - - it 'should store the information that the reminder has been sent' do - mock_sent_alert = mock_model(UserInfoRequestSentAlert) - UserInfoRequestSentAlert.stub!(:new).and_return(mock_sent_alert) - mock_sent_alert.should_receive(:info_request=).with(@mock_request) - mock_sent_alert.should_receive(:user=).with(@mock_user) - mock_sent_alert.should_receive(:alert_type=).with('new_response_reminder_1') - mock_sent_alert.should_receive(:info_request_event_id=).with(@mock_request.get_last_response_event_id) - mock_sent_alert.should_receive(:save!) - send_alerts - end - - it 'should send the reminder' do - RequestMailer.should_receive(:deliver_new_response_reminder_alert) - send_alerts - end - end - -end - -describe RequestMailer, 'when sending mail when someone has updated an old unclassified request' do - - before do - @user = mock_model(User, :name_and_email => 'test name and email') - @public_body = mock_model(PublicBody, :name => 'Test public body') - @info_request = mock_model(InfoRequest, :user => @user, - :law_used_full => 'Freedom of Information', - :title => 'Test request', - :public_body => @public_body, - :display_status => 'Refused.', - :url_title => 'test_request') - @mail = RequestMailer.create_old_unclassified_updated(@info_request) - end - - it 'should have the subject "Someone has updated the status of your request"' do - @mail.subject.should == 'Someone has updated the status of your request' - end - - it 'should tell them what status was picked' do - @mail.body.should match(/"refused."/) - end - - it 'should contain the request path' do - @mail.body.should match(/request\/test_request/) - end - -end - -describe RequestMailer, 'requires_admin' do - before(:each) do - user = mock_model(User, :name_and_email => 'Bruce Jones', - :name => 'Bruce Jones') - @info_request = mock_model(InfoRequest, :user => user, - :described_state => 'error_message', - :title => 'Test request', - :url_title => 'test_request', - :law_used_short => 'FOI', - :id => 123) - end - - it 'body should contain the full admin URL' do - mail = RequestMailer.deliver_requires_admin(@info_request) - - mail.body.should include('http://test.host/en/admin/request/show/123') - end - - context 'has an ADMIN_BASE_URL set' do - before(:each) do - Configuration::should_receive(:admin_base_url).and_return('http://our.proxy.server/admin/alaveteli/') - end - - it 'body should contain the full admin URL' do - mail = RequestMailer.deliver_requires_admin(@info_request) - - mail.body.should include('http://our.proxy.server/admin/alaveteli/request/show/123') - end - end -end diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb deleted file mode 100644 index 896f1c416..000000000 --- a/spec/models/track_mailer_spec.rb +++ /dev/null @@ -1,198 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe TrackMailer do - - describe 'when sending email alerts for tracked things' do - - before do - TrackMailer.stub!(:deliver_event_digest) - Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) - end - - 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([]) - TrackMailer.alert_tracks - end - - describe 'for each user' do - - before do - @user = mock_model(User, :no_xapian_reindex= => false, - :last_daily_track_email= => true, - :save! => true, - :url_name => 'test-name', - :get_locale => 'en', - :should_be_emailed? => true) - User.stub!(:find).and_return([@user]) - @user.stub!(:receive_email_alerts).and_return(true) - @user.stub!(:no_xapian_reindex=) - end - - it 'should ask for any daily track things for the user' do - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) - TrackMailer.alert_tracks - end - - - it 'should set the no_xapian_reindex flag on the user' do - @user.should_receive(:no_xapian_reindex=).with(true) - TrackMailer.alert_tracks - end - - it 'should update the time of the user\'s last daily tracking email' do - @user.should_receive(:last_daily_track_email=).with(Time.now) - @user.should_receive(:save!) - TrackMailer.alert_tracks - end - it 'should return true' do - TrackMailer.alert_tracks.should == true - end - - - describe 'for each tracked thing' do - - before do - @track_things_sent_emails_array = [] - @track_things_sent_emails_array.stub!(:find).and_return([]) # this is for the date range find (created in last 14 days) - @track_thing = mock_model(TrackThing, :track_query => 'test query', - :track_things_sent_emails => @track_things_sent_emails_array, - :created_at => Time.utc(2007, 11, 9, 23, 59)) - TrackThing.stub!(:find).and_return([@track_thing]) - @track_things_sent_email = mock_model(TrackThingsSentEmail, :save! => true, - :track_thing_id= => true, - :info_request_event_id= => true) - TrackThingsSentEmail.stub!(:new).and_return(@track_things_sent_email) - @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) - 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) - TrackMailer.alert_tracks - end - - it 'should not include in the email any events that the user has already been sent a tracking email about' do - sent_email = mock_model(TrackThingsSentEmail, :info_request_event_id => @found_event.id) - @track_things_sent_emails_array.stub!(:find).and_return([sent_email]) # this is for the date range find (created in last 14 days) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_not_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should not include in the email any events not sent in a previous tracking email that were described before the track was set up' do - @found_event.stub!(:described_at).and_return(@track_thing.created_at - 1.day) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_not_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should include in the email any events that the user has not been sent a tracking email on that have been described since the track was set up' do - @found_event.stub!(:described_at).and_return(@track_thing.created_at + 1.day) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should raise an error if a non-event class is returned by the tracking query' do - @xapian_search.stub!(:results).and_return([{:model => 'string class'}]) - lambda{ TrackMailer.alert_tracks }.should raise_error('need to add other types to TrackMailer.alert_tracks (unalerted)') - end - - it 'should record that a tracking email has been sent for each event that has been included in the email' do - @xapian_search.stub!(:results).and_return([@search_result]) - sent_email = mock_model(TrackThingsSentEmail) - TrackThingsSentEmail.should_receive(:new).and_return(sent_email) - sent_email.should_receive(:track_thing_id=).with(@track_thing.id) - sent_email.should_receive(:info_request_event_id=).with(@found_event.id) - sent_email.should_receive(:save!) - TrackMailer.alert_tracks - end - end - - end - - describe 'when a user should not be emailed' do - before do - @user = mock_model(User, :no_xapian_reindex= => false, - :last_daily_track_email= => true, - :save! => true, - :url_name => 'test-name', - :should_be_emailed? => false) - User.stub!(:find).and_return([@user]) - @user.stub!(:receive_email_alerts).and_return(true) - @user.stub!(:no_xapian_reindex=) - end - - it 'should not ask for any daily track things for the user' do - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions) - TrackMailer.alert_tracks - end - - it 'should not ask for any daily track things for the user if they have receive_email_alerts off but could otherwise be emailed' do - @user.stub(:should_be_emailed?).and_return(true) - @user.stub(:receive_email_alerts).and_return(false) - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions) - TrackMailer.alert_tracks - end - - it 'should not set the no_xapian_reindex flag on the user' do - @user.should_not_receive(:no_xapian_reindex=).with(true) - TrackMailer.alert_tracks - end - - it 'should not update the time of the user\'s last daily tracking email' do - @user.should_not_receive(:last_daily_track_email=).with(Time.now) - @user.should_not_receive(:save!) - TrackMailer.alert_tracks - end - it 'should return false' do - TrackMailer.alert_tracks.should == false - end - end - - end - - describe 'delivering the email' do - - before do - @post_redirect = mock_model(PostRedirect, :save! => true, - :email_token => "token") - PostRedirect.stub!(:new).and_return(@post_redirect) - ActionMailer::Base.deliveries = [] - end - - it 'should deliver one email, with right headers' do - @user = mock_model(User, - :name_and_email => MailHandler.address_from_name_and_email('Tippy Test', 'tippy@localhost'), - :url_name => 'tippy_test' - ) - - TrackMailer.deliver_event_digest(@user, []) # no items in it email for minimal test - deliveries = ActionMailer::Base.deliveries - if deliveries.size > 1 # debugging if there is an error - deliveries.each do |d| - $stderr.puts "------------------------------" - $stderr.puts d.body - $stderr.puts "------------------------------" - end - end - deliveries.size.should == 1 - mail = deliveries[0] - - mail['Auto-Submitted'].to_s.should == 'auto-generated' - mail['Precedence'].to_s.should == 'bulk' - - deliveries.clear - end - end - -end - - - diff --git a/spec/models/user_mailer_spec.rb b/spec/models/user_mailer_spec.rb deleted file mode 100644 index 0792aaab2..000000000 --- a/spec/models/user_mailer_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe UserMailer, " when blah" do - before do - end -end - - -- cgit v1.2.3 From cbdff06aa95a7987b54c712dc6729e138f608eca Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sun, 3 Mar 2013 14:52:30 +1100 Subject: Rename Configuration class to avoid conflict with ActiveSupport::Configurable --- spec/models/incoming_message_spec.rb | 2 +- spec/models/mail_server_log_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 781212841..aa7826865 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -353,7 +353,7 @@ describe IncomingMessage, " when censoring data" do end it "should apply hard-coded privacy rules to HTML files" do - data = "http://#{Configuration::domain}/c/cheese" + data = "http://#{AlaveteliConfiguration::domain}/c/cheese" @im.html_mask_stuff!(data) data.should == "[WDTK login link]" end diff --git a/spec/models/mail_server_log_spec.rb b/spec/models/mail_server_log_spec.rb index d0a1d202f..2b44a4559 100644 --- a/spec/models/mail_server_log_spec.rb +++ b/spec/models/mail_server_log_spec.rb @@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe MailServerLog do describe ".load_file" do before :each do - Configuration.stub!(:incoming_email_domain).and_return("example.com") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") File.stub_chain(:stat, :mtime).and_return(DateTime.new(2012, 10, 10)) end @@ -64,8 +64,8 @@ describe MailServerLog do describe ".email_addresses_on_line" do before :each do - Configuration.stub!(:incoming_email_domain).and_return("example.com") - Configuration.stub!(:incoming_email_prefix).and_return("foi+") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") + AlaveteliConfiguration.stub!(:incoming_email_prefix).and_return("foi+") end it "recognises a single incoming email" do @@ -106,7 +106,7 @@ describe MailServerLog do # Postfix logs for a single email go over multiple lines. They are all tied together with the Queue ID. # See http://onlamp.com/onlamp/2004/01/22/postfix.html it "loads the postfix log and untangles seperate email transactions using the queue ID" do - Configuration.stub!(:incoming_email_domain).and_return("example.com") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") log.stub!(:rewind) ir1 = info_requests(:fancy_dog_request) ir2 = info_requests(:naughty_chicken_request) @@ -135,7 +135,7 @@ describe MailServerLog do describe ".scan_for_postfix_queue_ids" do it "returns the queue ids of interest with the connected email addresses" do - Configuration.stub!(:incoming_email_domain).and_return("example.com") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") MailServerLog.scan_for_postfix_queue_ids(log).should == { "CB55836EE58C" => ["request-14-e0e09f97@example.com"], "9634B16F7F7" => ["request-10-1234@example.com"] -- cgit v1.2.3 From 931109d4c756ef6fb31a8f47494c21c460a06902 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Mon, 4 Mar 2013 17:47:56 +1100 Subject: Reload cached object after extracting attachments --- spec/models/incoming_message_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index aa7826865..e22235298 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -405,6 +405,7 @@ describe IncomingMessage, " when uudecoding bad messages" do im.stub!(:mail).and_return(mail) im.extract_attachments! + im.reload attachments = im.foi_attachments attachments.size.should == 2 attachments[1].filename.should == 'moo.txt' -- cgit v1.2.3 From 004e683db2cc3abe51225d071db993f81f3a6742 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 13 May 2013 16:00:04 +0100 Subject: Add a new example for unconvertible-to-UTF-8 text part data The example file that was used for checking for text attachment data which could not be converted to UTF-8 is one that we *can* actually deal with by guessing the character set, since it's valid GB18030. So, this commit changes that test to check for the first few Chinese characters in that email, and introduces a replacement test with text from /dev/random, which should not be interpretable in any sensible way. --- spec/models/incoming_message_spec.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index e22235298..b97ee3ca8 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -59,12 +59,19 @@ describe IncomingMessage, " when dealing with incoming mail" do message.subject.should == "Câmara Responde: Banco de ideias" end - it 'should not error on display of a message which has no charset set on the body part and - is not good utf-8' do + it 'should deal with GB18030 text even if the charset is missing' do ir = info_requests(:fancy_dog_request) receive_incoming_mail('no-part-charset-bad-utf8.email', ir.incoming_email) message = ir.incoming_messages[1] message.parse_raw_email! + message.get_main_body_text_internal.should include("贵公司负责人") + end + + it 'should not error on display of a message which has no charset set on the body part and is not good UTF-8' do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('no-part-charset-random-data.email', ir.incoming_email) + message = ir.incoming_messages[1] + message.parse_raw_email! message.get_main_body_text_internal.should include("The above text was badly encoded") end -- cgit v1.2.3 From ac2d5ed7f249fe5bef5601b4a60141fa79095558 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Thu, 16 May 2013 15:10:54 +0100 Subject: Avoid an FoiAttachment validation failure under Rails 3 Under Rails 3, the uudecoded FoiAttachment in this test fails validation at the self.save! in IncomingMessage.parse_raw_email, although the FoiAttachment has been correctly created and saved to the database in _uudecode_and_save_attachments. Forcing a reload=true on self.foi_attachments fixes this. Thanks to Louise Crow for finding the fix for this problem. --- spec/models/incoming_message_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index b97ee3ca8..1d86c26ad 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -419,6 +419,17 @@ describe IncomingMessage, " when uudecoding bad messages" do im.get_attachments_for_display.size.should == 1 end + it "should still work when parsed from the raw email" do + raw_email = load_file_fixture 'inline-uuencode.email' + mail = MailHandler.mail_from_raw_email(raw_email) + im = incoming_messages :useless_incoming_message + im.stub!(:raw_email).and_return(raw_email) + im.stub!(:mail).and_return(mail) + im.parse_raw_email! + attachments = im.foi_attachments + attachments.size.should == 2 + end + it "should apply censor rules" do mail = get_fixture_mail('incoming-request-bad-uuencoding.email') -- cgit v1.2.3 From f06da9241f28b96cebcadddda09ef525eeb071a0 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 28 May 2013 18:28:14 +0100 Subject: Remove erroneous test name --- spec/models/foi_attachment_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb index 537a3363c..9b0115c44 100644 --- a/spec/models/foi_attachment_spec.rb +++ b/spec/models/foi_attachment_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe FoiAttachment, " when calculating due date" do +describe FoiAttachment do before(:each) do load_raw_emails_data -- cgit v1.2.3 From 65e9bd66ead001125a792ff3bf483cc341e66048 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 29 May 2013 11:13:12 +0100 Subject: When extracting attachments for an incoming message and getting the body of the main part in order to look for uuencoded text, make sure that we're getting that main part from the reparsed attachments, and not getting an obsolete attachment. Fixes #958. --- spec/models/incoming_message_spec.rb | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 1d86c26ad..03152f5ff 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -542,3 +542,46 @@ describe IncomingMessage, "when TNEF attachments are attached to messages" do end end +describe IncomingMessage, "when extracting attachments" do + + it 'handles the case where reparsing changes the body of the main part + and the cached attachment has been deleted' do + # original set of attachment attributes + attachment_attributes = { :url_part_number => 1, + :within_rfc822_subject => nil, + :content_type => "text/plain", + :charset => nil, + :body => "No way!\n", + :hexdigest => "0c8b1b0f5cb9c94ed15a180e73b5c7d1", + :filename => nil } + + # Make a small change in the body returned for the attachment + new_attachment_attributes = attachment_attributes.merge(:body => "No way!", + :hexdigest => "74d2c0a41e074f9cebe49324d5b47414") + + + # Simulate parsing with the original attachments + MailHandler.stub!(:get_attachment_attributes).and_return([attachment_attributes]) + incoming_message = incoming_messages(:useless_incoming_message) + + # Extract the attachments + incoming_message.extract_attachments! + + # delete the cached file for the main body part + main = incoming_message.get_main_body_text_part + main.delete_cached_file! + + # Simulate reparsing with the slightly changed body + MailHandler.stub!(:get_attachment_attributes).and_return([new_attachment_attributes]) + + # Re-extract the attachments + incoming_message.extract_attachments! + + attachments = incoming_message.foi_attachments + attachments.size.should == 1 + attachments.first.hexdigest.should == "74d2c0a41e074f9cebe49324d5b47414" + attachments.first.body.should == 'No way!' + end + +end + -- cgit v1.2.3 From e503bf89c973dad5bdbffb3e2ec4d15cf063bf91 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Mon, 3 Jun 2013 13:10:46 +0100 Subject: Parse the 'to' address as if on a real mail to trigger quoted string encoding. --- spec/models/info_request_event_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'spec/models') diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 842246fd8..eb0de8c86 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequestEvent do @@ -118,6 +119,13 @@ describe InfoRequestEvent do @info_request_event.same_email_as_previous_send?.should be_true end + it 'should handle non-ascii characters in the name input' do + address = "\"Someone’s name\" " + @info_request_event.stub!(:params).and_return(:email => address) + @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(address) + @info_request_event.same_email_as_previous_send?.should be_true + end + end end -- cgit v1.2.3