aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/fcgi_fixes_spec.rb14
-rw-r--r--spec/lib/format_spec.rb53
-rw-r--r--spec/lib/sendmail_return_path_spec.rb82
-rw-r--r--spec/lib/timezone_fixes_spec.rb81
-rw-r--r--spec/lib/tmail_extensions_spec.rb26
5 files changed, 256 insertions, 0 deletions
diff --git a/spec/lib/fcgi_fixes_spec.rb b/spec/lib/fcgi_fixes_spec.rb
new file mode 100644
index 000000000..92b954645
--- /dev/null
+++ b/spec/lib/fcgi_fixes_spec.rb
@@ -0,0 +1,14 @@
+# This is a test of the monkey patches in lib/fcgi_fixes.rb
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require 'railties/lib/fcgi_handler.rb'
+
+describe "when doing FastCGI" do
+
+ it "should have fixed dynamic FastCGI bug" do
+ RailsFCGIHandler::SIGNALS['TERM'].should == :exit
+ end
+
+end
+
diff --git a/spec/lib/format_spec.rb b/spec/lib/format_spec.rb
new file mode 100644
index 000000000..8f0ebbed9
--- /dev/null
+++ b/spec/lib/format_spec.rb
@@ -0,0 +1,53 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe "when making clickable" do
+
+ it "should make URLs into links" do
+ text = "Hello http://www.flourish.org goodbye"
+ text = CGI.escapeHTML(text)
+ formatted = MySociety::Format.make_clickable(text)
+ formatted.should == "Hello <a href='http://www.flourish.org'>http://www.flourish.org</a> goodbye"
+ end
+
+ it "should make wrapped URLs in angle brackets clickable" do
+ text = """<http://www.flou
+rish.org/bl
+og>
+
+More stuff and then another angle bracket >"""
+ text = CGI.escapeHTML(text)
+
+ formatted = MySociety::Format.make_clickable(text)
+
+ formatted.should == "&lt;<a href='http://www.flourish.org/blog'>http://www.flourish.org/blog</a>&gt;\n\nMore stuff and then another angle bracket &gt;"
+ end
+
+ it "should make wrapped URLs in angle brackets clickable" do
+ text = """<https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&URL=h
+ttp%3a%2f%2fwww.ico.gov.uk%2fupload%2fdocuments%2flibrary%2ffreedom_of_infor
+mation%2fdetailed_specialist_guides%2fname_of_applicant_fop083_v1.pdf> Valid
+request - name and address for correspondence
+
+If we can be of any further assistance please contact our Helpline on 08456
+30 60 60 or 01625 545745 if you would prefer to call a national rate number,
+quoting your case reference number. You may also find some useful
+information on our website at
+<https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&URL=h
+ttp%3a%2f%2fwww.ico.gov.uk%2f> www.ico.gov.uk."""
+ text = CGI.escapeHTML(text)
+ formatted = MySociety::Format.make_clickable(text)
+
+ expected_formatted = """&lt;<a href='https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&amp;URL=http%3a%2f%2fwww.ico.gov.uk%2fupload%2fdocuments%2flibrary%2ffreedom_of_information%2fdetailed_specialist_guides%2fname_of_applicant_fop083_v1.pdf'>https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&amp;URL=http%3a%2f%2fwww.ico.gov.uk%2fupload%2fdocuments%2flibrary%2ffreedom_of_information%2fdetailed_specialist_guides%2fname_of_applicant_fop083_v1.pdf</a>&gt; Valid
+request - name and address for correspondence
+
+If we can be of any further assistance please contact our Helpline on 08456
+30 60 60 or 01625 545745 if you would prefer to call a national rate number,
+quoting your case reference number. You may also find some useful
+information on our website at
+&lt;<a href='https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&amp;URL=http%3a%2f%2fwww.ico.gov.uk%2f'>https://web.nhs.net/owa/redir.aspx?C=25a8af7e66054d62a435313f7f3d4694&amp;URL=http%3a%2f%2fwww.ico.gov.uk%2f</a>&gt; <a href='http://www.ico.gov.uk'>www.ico.gov.uk</a>."""
+
+ formatted.should == expected_formatted
+ end
+
+
+end
diff --git a/spec/lib/sendmail_return_path_spec.rb b/spec/lib/sendmail_return_path_spec.rb
new file mode 100644
index 000000000..6556d0888
--- /dev/null
+++ b/spec/lib/sendmail_return_path_spec.rb
@@ -0,0 +1,82 @@
+# This is a test of the monkey patches in sendmail_return_path.rb
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe "when sending email with an altered return path" do
+
+ it "should default to delivery method test" do
+ ActionMailer::Base.delivery_method.should == :test
+ end
+
+ it "should let the helper change the method" do
+ with_delivery_method :smtp do
+ ActionMailer::Base.delivery_method.should == :smtp
+ end
+ ActionMailer::Base.delivery_method.should == :test
+ end
+
+ # Documentation for fancy mock functions: http://rspec.info/documentation/mocks/message_expectations.html
+ it "should set the return path when sending email using SMTP" do
+ mock_smtp = mock("smtp")
+ mock_smtp_session = mock("smtp_session")
+
+ mock_smtp.should_receive(:start).once.and_yield(mock_smtp_session)
+ # the second parameter to the SMTP session is the sender (return path)
+ mock_smtp_session.should_receive(:sendmail).once.with(anything(), ["test@localhost"], anything())
+
+ Net::SMTP.stub!(:new).and_return(mock_smtp)
+
+ with_delivery_method :smtp do
+ ContactMailer.deliver_message(
+ "Mr. Test", "test@localhost", "Test script spec/lib/sendmail_return_path_spec.rb",
+ "This is just a test for a test script", nil, nil, nil
+ )
+ end
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 0
+ end
+
+ it "should set the return path when sending email using sendmail" do
+ with_stub_popen do
+ IO.should_receive(:popen).once.with('/usr/sbin/sendmail -i -t -f "test@localhost"', "w+")
+ with_delivery_method :sendmail do
+ ContactMailer.deliver_message(
+ "Mr. Test", "test@localhost", "Test script spec/lib/sendmail_return_path_spec.rb",
+ "This is just a test for a test script", nil, nil, nil
+ )
+ end
+ end
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 0
+ end
+
+
+ protected
+ # Change the way Rails delivers memory, just for current scope
+ def with_delivery_method(new_delivery_method)
+ old_delivery_method, ActionMailer::Base.delivery_method = ActionMailer::Base.delivery_method, new_delivery_method
+ yield
+ ensure
+ ActionMailer::Base.delivery_method = old_delivery_method
+ end
+
+ # By default, we can't stub popen, presumably because it is a builtin written in C.
+ # Replace it entirely with a normal method that just calls the C one, so we can stub it -
+ # this leaves IO working afterwards (for other tests that run in the same instance).
+ def with_stub_popen()
+ IO.class_eval "@orig_popen = self.method(:popen); def self.popen(a, b, &c); @orig_popen.call(a, b, &c); end"
+ begin
+ yield
+ ensure
+ # in theory would undo the popen alterations and return IO to a pristine state, but
+ # don't know how to (much fiddling with alias bind and the like didn't help). It
+ # doesn't matter - the new popen should behave just the same.
+ end
+ end
+
+
+end
+
+
diff --git a/spec/lib/timezone_fixes_spec.rb b/spec/lib/timezone_fixes_spec.rb
new file mode 100644
index 000000000..9ceb58a13
--- /dev/null
+++ b/spec/lib/timezone_fixes_spec.rb
@@ -0,0 +1,81 @@
+# This is a test of the monkey patches in timezone_fixes.rb
+
+# We use EximLogDone here just as a totally random model that has a datetime type.
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe "when doing things with timezones" do
+
+ it "should preserve time objects with local time conversion to default timezone UTC" do
+ with_env_tz 'America/New_York' do
+ with_active_record_default_timezone :utc do
+ time = Time.local(2000)
+ exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy')
+ saved_time = EximLogDone.find(exim_log_done.id).last_stat
+ assert_equal time, saved_time
+ assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "EST"], time.to_a
+ assert_equal [0, 0, 5, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a
+ end
+ end
+ end
+
+ it "should preserve time objects with time with zone conversion to default timezone UTC" do
+ with_env_tz 'America/New_York' do
+ with_active_record_default_timezone :utc do
+ Time.use_zone 'Central Time (US & Canada)' do
+ time = Time.zone.local(2000)
+ exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy')
+ saved_time = EximLogDone.find(exim_log_done.id).last_stat
+ assert_equal time, saved_time
+ assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a
+ assert_equal [0, 0, 6, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a
+ end
+ end
+ end
+ end
+
+ # XXX Couldn't get this test to work - but the other tests seem to detect presence of
+ # the monkey patch, so they will do for now.
+ #it "should preserve time objects with UTC time conversion to default timezone local" do
+ # with_env_tz 'America/New_York' do
+ # time = Time.utc(2000)
+ # exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy')
+ # saved_time = EximLogDone.find(exim_log_done.id).last_stat
+ # assert_equal time, saved_time
+ # assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a
+ # assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a
+ # end
+ #end
+
+ it "should preserve time objects with time with zone conversion to default timezone local" do
+ with_env_tz 'America/New_York' do
+ with_active_record_default_timezone :local do
+ Time.use_zone 'Central Time (US & Canada)' do
+ time = Time.zone.local(2000)
+ exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy')
+ saved_time = EximLogDone.find(exim_log_done.id).last_stat
+ assert_equal time, saved_time
+ assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a
+ assert_equal [0, 0, 1, 1, 1, 2000, 6, 1, false, "EST"], saved_time.to_a
+ end
+ end
+ end
+ end
+
+ protected
+ def with_env_tz(new_tz = 'US/Eastern')
+ old_tz, ENV['TZ'] = ENV['TZ'], new_tz
+ yield
+ ensure
+ old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
+ end
+
+ def with_active_record_default_timezone(zone)
+ old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
+ yield
+ ensure
+ ActiveRecord::Base.default_timezone = old_zone
+ end
+end
+
+
diff --git a/spec/lib/tmail_extensions_spec.rb b/spec/lib/tmail_extensions_spec.rb
new file mode 100644
index 000000000..1c913283a
--- /dev/null
+++ b/spec/lib/tmail_extensions_spec.rb
@@ -0,0 +1,26 @@
+# This is a test of the set_content_type monkey patch in
+# lib/tmail_extensions.rb
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe "when using TMail" do
+
+ 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
+
+end
+