aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/general_controller_spec.rb29
-rw-r--r--spec/controllers/request_controller_spec.rb2
-rw-r--r--spec/controllers/services_controller_spec.rb6
-rw-r--r--spec/lib/alaveteli_text_masker_spec.rb146
-rw-r--r--spec/mailers/outgoing_mailer_spec.rb12
-rw-r--r--spec/models/incoming_message_spec.rb179
-rw-r--r--spec/models/info_request_spec.rb2
7 files changed, 246 insertions, 130 deletions
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index c0a9d57d3..4a7a0bb48 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -126,6 +126,35 @@ describe GeneralController, "when showing the frontpage" do
end
+ describe 'when handling logged-in users' do
+
+ before do
+ @user = FactoryGirl.create(:user)
+ session[:user_id] = @user.id
+ end
+
+ it 'should set a time to live on a non "remember me" session' do
+ get :frontpage
+ response.body.should match @user.name
+ session[:ttl].should be_within(1).of(Time.now)
+ end
+
+ it 'should not set a time to live on a "remember me" session' do
+ session[:remember_me] = true
+ get :frontpage
+ response.body.should match @user.name
+ session[:ttl].should be_nil
+ end
+
+ it 'should end a logged-in session whose ttl has expired' do
+ session[:ttl] = Time.now - 4.hours
+ get :frontpage
+ response.should redirect_to signin_path
+ session[:user_id].should be_nil
+ end
+
+ end
+
end
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index 4d0070470..ba558cc93 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -2447,7 +2447,7 @@ describe RequestController, "when caching fragments" do
:info_request_id => 132,
:id => 44,
:get_attachments_for_display => nil,
- :html_mask_stuff! => nil,
+ :apply_masks! => nil,
:user_can_view? => true,
:all_can_view? => true)
attachment = FactoryGirl.build(:body_text, :filename => long_name)
diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb
index 14731f090..248c97ad4 100644
--- a/spec/controllers/services_controller_spec.rb
+++ b/spec/controllers/services_controller_spec.rb
@@ -60,21 +60,25 @@ describe ServicesController, "when returning a message for people in other count
response.should be_success
response.body.should == 'Hello! We have an <a href="/help/alaveteli?country_name=Deutschland">important message</a> for visitors outside Deutschland'
end
+
it "should default to no message if the country_from_ip domain doesn't exist" do
AlaveteliConfiguration.stub!(:gaze_url).and_return('http://12123sdf14qsd.com')
get :other_country_message
response.should be_success
response.body.should == ''
end
+
it "should default to no message if the country_from_ip service doesn't exist" do
AlaveteliConfiguration.stub!(:gaze_url).and_return('http://www.google.com')
get :other_country_message
response.should be_success
response.body.should == ''
end
- it "should default to no message if the country_from_ip service returns an error" do
+
+ it "should default to no message and log the error with url if the country_from_ip service returns an error" do
FakeWeb.register_uri(:get, %r|500.com|, :body => "Error", :status => ["500", "Error"])
AlaveteliConfiguration.stub!(:gaze_url).and_return('http://500.com')
+ Rails.logger.should_receive(:warn).with /500\.com.*500 Error/
get :other_country_message
response.should be_success
response.body.should == ''
diff --git a/spec/lib/alaveteli_text_masker_spec.rb b/spec/lib/alaveteli_text_masker_spec.rb
new file mode 100644
index 000000000..1a4782a83
--- /dev/null
+++ b/spec/lib/alaveteli_text_masker_spec.rb
@@ -0,0 +1,146 @@
+# -*- coding: utf-8 -*-
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe AlaveteliTextMasker do
+ include AlaveteliTextMasker
+
+ describe :apply_masks! do
+
+ describe 'when applying censor rules' do
+
+ before do
+ @cheese_censor_rule = FactoryGirl.build(:censor_rule, :text => 'Stilton',
+ :replacement => 'Jarlsberg')
+ @colour_censor_rule = FactoryGirl.build(:censor_rule, :text => 'blue',
+ :replacement => 'yellow')
+ @regex_censor_rule = FactoryGirl.build(:censor_rule, :text => 'm[a-z][a-z][a-z]e',
+ :replacement => 'cat',
+ :regexp => true)
+ @censor_rules = [@cheese_censor_rule, @colour_censor_rule, @regex_censor_rule]
+ end
+
+ it "should do nothing to a JPEG" do
+ data = "There was a mouse called Stilton, he wished that he was blue."
+ apply_masks!(data, "image/jpeg", :censor_rules => @censor_rules)
+ data.should == "There was a mouse called Stilton, he wished that he was blue."
+ end
+
+ it "should replace censor text in Word documents" do
+ data = "There was a mouse called Stilton, he wished that he was blue."
+ apply_masks!(data, "application/vnd.ms-word", :censor_rules => @censor_rules)
+ data.should == "There was a xxxxx called xxxxxxx, he wished that he was xxxx."
+ end
+
+ it 'should handle multibyte characters correctly' do
+ data = 'á mouse'
+ @regex_censor_rule.text = 'á'
+ apply_masks!(data, "application/octet-stream", :censor_rules => @censor_rules).should == 'x mouse'
+ end
+
+ it "should apply censor rules to HTML files" do
+ data = "There was a mouse called Stilton, he wished that he was blue."
+ apply_masks!(data, 'text/html', :censor_rules => @censor_rules)
+ data.should == "There was a cat called Jarlsberg, he wished that he was yellow."
+ end
+
+ end
+
+ it "should replace ASCII email addresses in Word documents" do
+ data = "His email was foo@bar.com"
+ expected = "His email was xxx@xxx.xxx"
+ apply_masks!(data, "application/vnd.ms-word")
+ data.should == expected
+ end
+
+
+ it "should replace UCS-2 addresses in Word documents" do
+ data = "His email was f\000o\000o\000@\000b\000a\000r\000.\000c\000o\000m\000, indeed"
+ apply_masks!(data, "application/vnd.ms-word")
+ data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed"
+ end
+
+ def pdf_replacement_test(use_ghostscript_compression)
+ config = MySociety::Config.load_default()
+ previous = config['USE_GHOSTSCRIPT_COMPRESSION']
+ config['USE_GHOSTSCRIPT_COMPRESSION'] = use_ghostscript_compression
+ orig_pdf = load_file_fixture('tfl.pdf')
+ pdf = orig_pdf.dup
+
+ orig_text = MailHandler.get_attachment_text_one_file('application/pdf', pdf)
+ orig_text.should match(/foi@tfl.gov.uk/)
+
+ apply_masks!(pdf, "application/pdf")
+
+ masked_text = MailHandler.get_attachment_text_one_file('application/pdf', pdf)
+ masked_text.should_not match(/foi@tfl.gov.uk/)
+ masked_text.should match(/xxx@xxx.xxx.xx/)
+ config['USE_GHOSTSCRIPT_COMPRESSION'] = previous
+ end
+
+ it "should replace everything in PDF files using pdftk" do
+ pdf_replacement_test(false)
+ end
+
+ it "should replace everything in PDF files using ghostscript" do
+ pdf_replacement_test(true)
+ end
+
+ it "should not produce zero length output if pdftk silently fails" do
+ orig_pdf = load_file_fixture('psni.pdf')
+ pdf = orig_pdf.dup
+ apply_masks!(pdf, "application/pdf")
+ pdf.should_not == ""
+ end
+
+ it "should apply hard-coded privacy rules to HTML files" do
+ data = "http://test.host/c/cheese"
+ apply_masks!(data, 'text/html')
+ data.should == "[Alaveteli login link]"
+ end
+
+ it 'should replace a simple email address' do
+ expected = "the address is [email address]"
+ apply_masks!("the address is test@example.com", 'text/html', {}).should == expected
+ end
+
+ it 'should replace a mobile phone number prefixed with "Mobile"' do
+ expected = "the mobile is [mobile number]"
+ apply_masks!("the mobile is Mobile 55555 555555", 'text/html', {}).should == expected
+ end
+
+ it 'should replace a mobile phone number prefixed with "Mob Tel"' do
+ expected = "the mobile is [mobile number]"
+ apply_masks!("the mobile is Mob Tel: 55555 555 555", 'text/html', {}).should == expected
+ end
+
+ it 'should replace a mobile phone number prefixed with "Mob/Fax:"' do
+ expected = "the mobile is [mobile number]"
+ apply_masks!("the mobile is Mob/Fax: 55555 555555", 'text/html', {}).should == expected
+ end
+
+ it "should replace an Alaveteli login link" do
+ expected = "the login link is [Alaveteli login link]"
+ apply_masks!("the login link is http://test.host/c/ekfmsdfkm", 'text/html', {}).should == expected
+ end
+
+ it "should replace a https Alaveteli login link" do
+ expected = "the login link is [Alaveteli login link]"
+ apply_masks!("the login link is https://test.host/c/ekfmsdfkm", 'text/html', {}).should == expected
+ end
+
+ it "should apply censor rules to text" do
+ censor_rule = FactoryGirl.build(:censor_rule, :text => 'mouse', :replacement => 'cat')
+ expected = "here is a cat"
+ apply_masks!("here is a mouse", 'text/html', {:censor_rules => [ censor_rule ]}).should == expected
+ end
+
+ it 'should apply extra masks to text' do
+ mask = {:to_replace => 'mouse', :replacement => 'cat'}
+ expected = "here is a cat"
+ apply_masks!("here is a mouse", 'text/html', {:masks => [ mask ]}).should == expected
+ end
+
+ end
+
+end
+
diff --git a/spec/mailers/outgoing_mailer_spec.rb b/spec/mailers/outgoing_mailer_spec.rb
index a11d56dd3..3df5018fe 100644
--- a/spec/mailers/outgoing_mailer_spec.rb
+++ b/spec/mailers/outgoing_mailer_spec.rb
@@ -75,14 +75,14 @@ describe OutgoingMailer, "when working out follow up subjects" 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?"
+ ir.email_subject_request(:html => false).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?"
+ OutgoingMailer.subject_for_followup(ir, om, :html => false).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
@@ -91,7 +91,7 @@ describe OutgoingMailer, "when working out follow up subjects" do
om = outgoing_messages(:useless_outgoing_message)
om.incoming_message_followup = im
- OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421"
+ OutgoingMailer.subject_for_followup(ir, om, :html => false).should == "Re: Geraldine FOI Code AZXB421"
end
it "should not add Re: prefix if there already is such a prefix" do
@@ -101,7 +101,7 @@ describe OutgoingMailer, "when working out follow up subjects" do
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"
+ OutgoingMailer.subject_for_followup(ir, om, :html => false).should == "Re: Geraldine FOI Code AZXB421"
end
it "should not add Re: prefix if there already is a lower case re: prefix" do
@@ -113,7 +113,7 @@ describe OutgoingMailer, "when working out follow up subjects" do
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"
+ OutgoingMailer.subject_for_followup(ir, om, :html => false).should == "re: Geraldine FOI Code AZXB421"
end
it "should use 'Re:' and initial request subject when replying to failed delivery notifications" do
@@ -126,7 +126,7 @@ describe OutgoingMailer, "when working out follow up subjects" do
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?"
+ OutgoingMailer.subject_for_followup(ir, om, :html => false).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?"
end
end
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index 3b6887f76..f6e524de3 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -423,127 +423,50 @@ describe IncomingMessage, " checking validity to reply to with real emails" do
end
-describe IncomingMessage, " when censoring data" do
-
- before(:each) do
- @test_data = "There was a mouse called Stilton, he wished that he was blue."
-
- @im = incoming_messages(:useless_incoming_message)
-
- @censor_rule_1 = CensorRule.new()
- @censor_rule_1.text = "Stilton"
- @censor_rule_1.replacement = "Jarlsberg"
- @censor_rule_1.last_edit_editor = "unknown"
- @censor_rule_1.last_edit_comment = "none"
- @im.info_request.censor_rules << @censor_rule_1
-
- @censor_rule_2 = CensorRule.new()
- @censor_rule_2.text = "blue"
- @censor_rule_2.replacement = "yellow"
- @censor_rule_2.last_edit_editor = "unknown"
- @censor_rule_2.last_edit_comment = "none"
- @im.info_request.censor_rules << @censor_rule_2
-
- @regex_censor_rule = CensorRule.new()
- @regex_censor_rule.text = 'm[a-z][a-z][a-z]e'
- @regex_censor_rule.regexp = true
- @regex_censor_rule.replacement = 'cat'
- @regex_censor_rule.last_edit_editor = 'unknown'
- @regex_censor_rule.last_edit_comment = 'none'
- @im.info_request.censor_rules << @regex_censor_rule
- load_raw_emails_data
- end
-
- it "should do nothing to a JPEG" do
- data = @test_data.dup
- @im.binary_mask_stuff!(data, "image/jpeg")
- data.should == @test_data
- end
-
- it "should replace censor text in Word documents" do
- data = @test_data.dup
- @im.binary_mask_stuff!(data, "application/vnd.ms-word")
- data.should == "There was a xxxxx called xxxxxxx, he wished that he was xxxx."
- end
-
- it "should replace ASCII email addresses in Word documents" do
- orig_data = "His email was foo@bar.com"
- data = orig_data.dup
- @im.binary_mask_stuff!(data, "application/vnd.ms-word")
- data.should == "His email was xxx@xxx.xxx"
- end
-
- it "should replace UCS-2 addresses in Word documents" do
- orig_data = "His email was f\000o\000o\000@\000b\000a\000r\000.\000c\000o\000m\000, indeed"
- data = orig_data.dup
- @im.binary_mask_stuff!(data, "application/vnd.ms-word")
- data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed"
- end
-
- it 'should handle multibyte characters correctly' do
- orig_data = 'á'
- data = orig_data.dup
- @regex_censor_rule = CensorRule.new()
- @regex_censor_rule.text = 'á'
- @regex_censor_rule.regexp = true
- @regex_censor_rule.replacement = 'cat'
- @regex_censor_rule.last_edit_editor = 'unknown'
- @regex_censor_rule.last_edit_comment = 'none'
- @im.info_request.censor_rules << @regex_censor_rule
- lambda{ @im.binary_mask_stuff!(data, "text/plain") }.should_not raise_error
- end
- def pdf_replacement_test(use_ghostscript_compression)
- config = MySociety::Config.load_default()
- previous = config['USE_GHOSTSCRIPT_COMPRESSION']
- config['USE_GHOSTSCRIPT_COMPRESSION'] = use_ghostscript_compression
- orig_pdf = load_file_fixture('tfl.pdf')
- pdf = orig_pdf.dup
-
- orig_text = MailHandler.get_attachment_text_one_file('application/pdf', pdf)
- orig_text.should match(/foi@tfl.gov.uk/)
-
- @im.binary_mask_stuff!(pdf, "application/pdf")
-
- masked_text = MailHandler.get_attachment_text_one_file('application/pdf', pdf)
- masked_text.should_not match(/foi@tfl.gov.uk/)
- masked_text.should match(/xxx@xxx.xxx.xx/)
- config['USE_GHOSTSCRIPT_COMPRESSION'] = previous
- end
-
- it "should replace everything in PDF files using pdftk" do
- pdf_replacement_test(false)
- end
-
- it "should replace everything in PDF files using ghostscript" do
- pdf_replacement_test(true)
- end
-
- it "should not produce zero length output if pdftk silently fails" do
- orig_pdf = load_file_fixture('psni.pdf')
- pdf = orig_pdf.dup
- @im.binary_mask_stuff!(pdf, "application/pdf")
- pdf.should_not == ""
- end
-
- it "should apply censor rules to HTML files" do
- data = @test_data.dup
- @im.html_mask_stuff!(data)
- data.should == "There was a cat called Jarlsberg, he wished that he was yellow."
- end
-
- it "should apply hard-coded privacy rules to HTML files" do
- data = "http://#{AlaveteliConfiguration::domain}/c/cheese"
- @im.html_mask_stuff!(data)
- data.should == "[WDTK login link]"
- end
+describe IncomingMessage, " when censoring data" do
- it "should apply censor rules to From: addresses" do
- @im.stub!(:mail_from).and_return("Stilton Mouse")
- @im.stub!(:last_parsed).and_return(Time.now)
- safe_mail_from = @im.safe_mail_from
- safe_mail_from.should == "Jarlsberg Mouse"
- end
+ before(:each) do
+ @test_data = "There was a mouse called Stilton, he wished that he was blue."
+
+ @im = incoming_messages(:useless_incoming_message)
+
+ @censor_rule_1 = CensorRule.new()
+ @censor_rule_1.text = "Stilton"
+ @censor_rule_1.replacement = "Jarlsberg"
+ @censor_rule_1.last_edit_editor = "unknown"
+ @censor_rule_1.last_edit_comment = "none"
+ @im.info_request.censor_rules << @censor_rule_1
+
+ @censor_rule_2 = CensorRule.new()
+ @censor_rule_2.text = "blue"
+ @censor_rule_2.replacement = "yellow"
+ @censor_rule_2.last_edit_editor = "unknown"
+ @censor_rule_2.last_edit_comment = "none"
+ @im.info_request.censor_rules << @censor_rule_2
+
+ @regex_censor_rule = CensorRule.new()
+ @regex_censor_rule.text = 'm[a-z][a-z][a-z]e'
+ @regex_censor_rule.regexp = true
+ @regex_censor_rule.replacement = 'cat'
+ @regex_censor_rule.last_edit_editor = 'unknown'
+ @regex_censor_rule.last_edit_comment = 'none'
+ @im.info_request.censor_rules << @regex_censor_rule
+ load_raw_emails_data
+ end
+
+ it "should replace censor text" do
+ data = "There was a mouse called Stilton, he wished that he was blue."
+ @im.apply_masks!(data, "application/vnd.ms-word")
+ data.should == "There was a xxxxx called xxxxxxx, he wished that he was xxxx."
+ end
+
+ it "should apply censor rules to From: addresses" do
+ @im.stub!(:mail_from).and_return("Stilton Mouse")
+ @im.stub!(:last_parsed).and_return(Time.now)
+ safe_mail_from = @im.safe_mail_from
+ safe_mail_from.should == "Jarlsberg Mouse"
+ end
end
@@ -565,15 +488,16 @@ describe IncomingMessage, " when censoring whole users" do
it "should apply censor rules to HTML files" do
data = @test_data.dup
- @im.html_mask_stuff!(data)
+ @im.apply_masks!(data, 'text/html')
data.should == "There was a mouse called Gorgonzola, he wished that he was blue."
end
it "should replace censor text to Word documents" do
data = @test_data.dup
- @im.binary_mask_stuff!(data, "application/vnd.ms-word")
+ @im.apply_masks!(data, "application/vnd.ms-word")
data.should == "There was a mouse called xxxxxxx, he wished that he was blue."
end
+
end
@@ -770,3 +694,16 @@ describe IncomingMessage, "when extracting attachments" do
end
end
+
+describe IncomingMessage, 'when getting the body of a message for html display' do
+
+ it 'should replace any masked email addresses with a link to the help page' do
+ incoming_message = IncomingMessage.new
+ body_text = 'there was an [email address] here'
+ incoming_message.stub!(:get_main_body_text_folded).and_return(body_text)
+ incoming_message.stub!(:get_main_body_text_unfolded).and_return(body_text)
+ expected = 'there was an [<a href="/help/officers#mobiles">email address</a>] here'
+ incoming_message.get_body_for_html_display.should == expected
+ end
+
+end
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index 9ad616ea5..70947584b 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -824,7 +824,7 @@ describe InfoRequest do
im = mock_model(IncomingMessage,
:subject => nil,
:valid_to_reply_to? => true)
- subject = ir.email_subject_followup im
+ subject = ir.email_subject_followup(:incoming_message => im, :html => false)
subject.should match(/^Re: Freedom of Information request.*fancy dog/)
end