aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb115
-rw-r--r--spec/models/censor_rule_spec.rb198
-rw-r--r--spec/models/incoming_message_spec.rb33
-rw-r--r--spec/models/info_request_spec.rb100
4 files changed, 360 insertions, 86 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index 6d6e7c1ba..be33802c5 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -50,7 +50,7 @@ describe AdminPublicBodyController, "when administering public bodies" do
response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id => id)
PublicBody.count.should == n
end
-
+
it "destroys a public body" do
n = PublicBody.count
post :destroy, { :id => public_bodies(:forlorn_public_body).id }
@@ -72,9 +72,82 @@ describe AdminPublicBodyController, "when administering public bodies" do
end
describe 'import_csv' do
- it 'should get the page successfully' do
- get :import_csv
- response.should be_success
+
+ describe 'when handling a GET request' do
+
+ it 'should get the page successfully' do
+ get :import_csv
+ response.should be_success
+ end
+
+ end
+
+ describe 'when handling a POST request' do
+
+ before do
+ PublicBody.stub!(:import_csv).and_return([[],[]])
+ @file_object = mock("a file upload", :read => 'some contents',
+ :original_filename => 'contents.txt')
+ end
+
+ it 'should handle a nil csv file param' do
+ post :import_csv, { :commit => 'Dry run' }
+ response.should be_success
+ end
+
+ describe 'if there is a csv file param' do
+
+ it 'should try to get the contents and original name of a csv file param' do
+ @file_object.should_receive(:read).and_return('some contents')
+ post :import_csv, { :csv_file => @file_object,
+ :commit => 'Dry run'}
+ end
+
+ it 'should assign the original filename to the view' do
+ post :import_csv, { :csv_file => @file_object,
+ :commit => 'Dry run'}
+ assigns[:original_csv_file].should == 'contents.txt'
+ end
+
+ end
+
+ describe 'if there is no csv file param, but there are temporary_csv_file and
+ original_csv_file params' do
+
+ it 'should try and get the file contents from a temporary file whose name
+ is passed as a param' do
+ @controller.should_receive(:retrieve_csv_data).with('csv_upload-2046-12-31-394')
+ post :import_csv, { :temporary_csv_file => 'csv_upload-2046-12-31-394',
+ :original_csv_file => 'original_contents.txt',
+ :commit => 'Dry run'}
+ end
+
+ it 'should raise an error on an invalid temp file name' do
+ params = { :temporary_csv_file => 'bad_name',
+ :original_csv_file => 'original_contents.txt',
+ :commit => 'Dry run'}
+ expected_error = "Invalid filename in upload_csv: bad_name"
+ lambda{ post :import_csv, params }.should raise_error(expected_error)
+ end
+
+ it 'should raise an error if the temp file does not exist' do
+ temp_name = "csv_upload-20461231-394"
+ params = { :temporary_csv_file => temp_name,
+ :original_csv_file => 'original_contents.txt',
+ :commit => 'Dry run'}
+ expected_error = "Missing file in upload_csv: csv_upload-20461231-394"
+ lambda{ post :import_csv, params }.should raise_error(expected_error)
+ end
+
+ it 'should assign the temporary filename to the view' do
+ post :import_csv, { :csv_file => @file_object,
+ :commit => 'Dry run'}
+ temporary_filename = assigns[:temporary_csv_file]
+ temporary_filename.should match(/csv_upload-#{Time.now.strftime("%Y%m%d")}-\d{1,5}/)
+ end
+
+ end
+
end
end
end
@@ -86,7 +159,7 @@ describe AdminPublicBodyController, "when administering public bodies and paying
before do
config = MySociety::Config.load_default()
config['SKIP_ADMIN_AUTH'] = false
- basic_auth_login @request
+ basic_auth_login @request
end
after do
config = MySociety::Config.load_default()
@@ -113,7 +186,7 @@ describe AdminPublicBodyController, "when administering public bodies and paying
PublicBody.count.should == n - 1
session[:using_admin].should == 1
end
-
+
it "doesn't let people with bad credentials log in" do
config = MySociety::Config.load_default()
config['SKIP_ADMIN_AUTH'] = false
@@ -166,7 +239,7 @@ end
describe AdminPublicBodyController, "when administering public bodies with i18n" do
integrate_views
-
+
it "shows the index page" do
get :index
end
@@ -182,7 +255,7 @@ describe AdminPublicBodyController, "when administering public bodies with i18n"
it "edits a public body" do
get :edit, {:id => 3, :locale => :en}
-
+
# When editing a body, the controller returns all available translations
assigns[:public_body].translation("es").name.should == 'El Department for Humpadinking'
assigns[:public_body].name.should == 'Department for Humpadinking'
@@ -193,20 +266,20 @@ describe AdminPublicBodyController, "when administering public bodies with i18n"
PublicBody.with_locale(:es) do
pb = PublicBody.find(id=3)
pb.name.should == "El Department for Humpadinking"
- post :update, {
- :id => 3,
- :public_body => {
- :name => "Department for Humpadinking",
- :short_name => "",
- :tag_string => "some tags",
- :request_email => 'edited@localhost',
+ post :update, {
+ :id => 3,
+ :public_body => {
+ :name => "Department for Humpadinking",
+ :short_name => "",
+ :tag_string => "some tags",
+ :request_email => 'edited@localhost',
:last_edit_comment => 'From test code',
:translated_versions => {
3 => {:locale => "es", :name => "Renamed",:short_name => "", :request_email => 'edited@localhost'}
}
}
}
- response.flash[:notice].should include('successful')
+ response.flash[:notice].should include('successful')
end
pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
@@ -228,7 +301,7 @@ end
describe AdminPublicBodyController, "when creating public bodies with i18n" do
integrate_views
-
+
before do
@old_filters = ActionController::Routing::Routes.filters
ActionController::Routing::Routes.filters = RoutingFilter::Chain.new
@@ -249,14 +322,14 @@ describe AdminPublicBodyController, "when creating public bodies with i18n" do
it "creates a new public body with multiple locales" do
n = PublicBody.count
- post :create, {
- :public_body => {
+ post :create, {
+ :public_body => {
:name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code',
:translated_versions => [{ :locale => "es", :name => "Mi Nuevo Quango", :short_name => "", :request_email => 'newquango@localhost' }]
}
}
PublicBody.count.should == n + 1
-
+
body = PublicBody.find_by_name("New Quango")
body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"]
PublicBody.with_locale(:en) do
@@ -269,7 +342,7 @@ describe AdminPublicBodyController, "when creating public bodies with i18n" do
body.url_name.should == "mi_nuevo_quango"
body.first_letter.should == "M"
end
-
+
response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id)
end
end
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
index d5797ec74..c11b05a03 100644
--- a/spec/models/censor_rule_spec.rb
+++ b/spec/models/censor_rule_spec.rb
@@ -1,49 +1,52 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe CensorRule, "substituting things" do
- before do
- @censor_rule = CensorRule.new
- @censor_rule.text = "goodbye"
- @censor_rule.replacement = "hello"
- end
+describe CensorRule, "substituting things" do
- it 'should do basic text substitution' do
- body = "I don't know why you say goodbye"
- @censor_rule.apply_to_text!(body)
- body.should == "I don't know why you say hello"
- end
+ describe 'when using a text rule' do
+
+ before do
+ @censor_rule = CensorRule.new
+ @censor_rule.text = "goodbye"
+ @censor_rule.replacement = "hello"
+ end
+
+ it 'should do basic text substitution' do
+ body = "I don't know why you say goodbye"
+ @censor_rule.apply_to_text!(body)
+ body.should == "I don't know why you say hello"
+ end
+
+ it 'should keep size same for binary substitution' do
+ body = "I don't know why you say goodbye"
+ orig_body = body.dup
+ @censor_rule.apply_to_binary!(body)
+ body.size.should == orig_body.size
+ body.should == "I don't know why you say xxxxxxx"
+ body.should_not == orig_body # be sure duplicated as expected
+ end
- it 'should keep size same for binary substitution' do
- body = "I don't know why you say goodbye"
- orig_body = body.dup
- @censor_rule.apply_to_binary!(body)
- body.size.should == orig_body.size
- body.should == "I don't know why you say xxxxxxx"
- body.should_not == orig_body # be sure duplicated as expected
end
- context "when regexp type" do
+ describe "when using a regular expression rule" do
+
before do
- CensorRule.delete_all
- CensorRule.create(:last_edit_editor => 1,
- :last_edit_comment => 'comment')
@censor_rule = CensorRule.new(:last_edit_editor => 1,
:last_edit_comment => 'comment')
@censor_rule.text = "--PRIVATE.*--PRIVATE"
@censor_rule.replacement = "--REMOVED\nHidden private info\n--REMOVED"
@censor_rule.regexp = true
- end
-
- it "replaces with the regexp" do
- body =
+ @body =
<<BODY
Some public information
--PRIVATE
Some private information
--PRIVATE
BODY
- @censor_rule.apply_to_text!(body)
- body.should ==
+ end
+
+ it "replaces the regexp with the replacement text when applied to text" do
+ @censor_rule.apply_to_text!(@body)
+ @body.should ==
<<BODY
Some public information
--REMOVED
@@ -52,14 +55,143 @@ Hidden private info
BODY
end
- it "validates without info_request, user or public body set" do
- @censor_rule.save.should be_true
+ it "replaces the regexp with the same number of 'x' characters as the text replaced
+ when applied to binary" do
+ @censor_rule.apply_to_binary!(@body)
+ @body.should ==
+<<BODY
+Some public information
+xxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxx
+BODY
+ end
+
+ end
+
+end
+
+describe 'when validating rules' do
+
+ describe '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"
+ end
+
+ describe 'when validating a regexp rule' do
+
+ before do
+ @censor_rule = CensorRule.new(:regexp => true,
+ :text => '*')
+ end
+
+ it 'should try to create a regexp from the text' do
+ Regexp.should_receive(:new).with('*', Regexp::MULTILINE)
+ @censor_rule.valid?
+ end
+
+ describe 'if a regexp error is produced' 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"
+ end
+
+ end
+
+ describe 'if no regexp error is produced' 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
+ end
+
+ end
+
+ end
+
+ describe 'when the allow_global flag has been set' do
+
+ before do
+ @censor_rule = CensorRule.new(:text => 'some text')
+ @censor_rule.allow_global = true
end
- it "has scope for regexps" do
- @censor_rule.save
- CensorRule.regexps.all.should == [@censor_rule]
+ it 'should allow a global censor rule (without user_id, request_id or public_body_id)' do
+ @censor_rule.valid?.should == true
end
+
end
+
+ describe 'when the allow_global flag has not been set' do
+
+ before do
+ @censor_rule = CensorRule.new(:text => '/./')
+ end
+
+ 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]
+ 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]
+ end
+
+ end
+
end
+describe 'when handling global rules' do
+
+ describe 'an instance without user_id, request_id or public_body_id' do
+
+ before do
+ @global_rule = CensorRule.new
+ end
+
+ it 'should return a value of true from is_global?' do
+ @global_rule.is_global?.should == true
+ end
+
+ end
+
+ describe 'the scope CensorRule.global.all' do
+
+ before do
+ @global_rule = CensorRule.create!(:allow_global => true,
+ :text => 'hide me',
+ :replacement => 'nothing to see here',
+ :last_edit_editor => 1,
+ :last_edit_comment => 'comment')
+ @user_rule = CensorRule.create!(:user_id => 1,
+ :text => 'hide me',
+ :replacement => 'nothing to see here',
+ :last_edit_editor => 1,
+ :last_edit_comment => 'comment')
+ end
+
+ it 'should include an instance without user_id, request_id or public_body_id' do
+ CensorRule.global.all.include?(@global_rule).should == true
+ end
+
+ it 'should not include a request with user_id' do
+ CensorRule.global.all.include?(@user_rule).should == false
+ end
+
+ after do
+ @global_rule.destroy if @global_rule
+ @user_rule.destroy if @user_rule
+ end
+ end
+
+end
+
+
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index c6658905c..bc73ef071 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -71,7 +71,7 @@ describe IncomingMessage, " when dealing with incoming mail" do
end
-describe IncomingMessage, "when parsing HTML mail" do
+describe IncomingMessage, "when parsing HTML mail" do
it "should display UTF-8 characters in the plain text version correctly" do
html = "<html><b>foo</b> është"
plain_text = IncomingMessage._get_attachment_text_internal_one_file('text/html', html)
@@ -79,15 +79,15 @@ describe IncomingMessage, "when parsing HTML mail" do
end
end
-describe IncomingMessage, "when getting the attachment text" do
+describe IncomingMessage, "when getting the attachment text" do
- it "should not raise an error if the expansion of a zip file raises an error" do
+ it "should not raise an error if the expansion of a zip file raises an error" do
mock_entry = mock('ZipFile entry', :file? => true)
mock_entry.stub!(:get_input_stream).and_raise("invalid distance too far back")
Zip::ZipFile.stub!(:open).and_return([mock_entry])
IncomingMessage._get_attachment_text_internal_one_file('application/zip', "some string")
end
-
+
end
@@ -196,17 +196,17 @@ describe IncomingMessage, " checking validity to reply to with real emails" do
ActionMailer::Base.deliveries.clear
end
it "should allow a reply to plain emails" do
- ir = info_requests(:fancy_dog_request)
+ ir = info_requests(:fancy_dog_request)
receive_incoming_mail('incoming-request-plain.email', ir.incoming_email)
ir.incoming_messages[1].valid_to_reply_to?.should == true
end
it "should not allow a reply to emails with empty return-paths" do
- ir = info_requests(:fancy_dog_request)
+ ir = info_requests(:fancy_dog_request)
receive_incoming_mail('empty-return-path.email', ir.incoming_email)
ir.incoming_messages[1].valid_to_reply_to?.should == false
end
it "should not allow a reply to emails with autoresponse headers" do
- ir = info_requests(:fancy_dog_request)
+ ir = info_requests(:fancy_dog_request)
receive_incoming_mail('autoresponse-header.email', ir.incoming_email)
ir.incoming_messages[1].valid_to_reply_to?.should == false
end
@@ -234,6 +234,13 @@ describe IncomingMessage, " when censoring data" do
@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
@@ -246,7 +253,7 @@ describe IncomingMessage, " when censoring data" do
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 mouse called xxxxxxx, he wished that he was xxxx."
+ 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
@@ -301,7 +308,7 @@ describe IncomingMessage, " when censoring data" do
it "should apply censor rules to HTML files" do
data = @test_data.dup
@im.html_mask_stuff!(data)
- data.should == "There was a mouse called Jarlsberg, he wished that he was yellow."
+ 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
@@ -312,8 +319,8 @@ describe IncomingMessage, " when censoring data" do
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)
+ @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
@@ -363,7 +370,7 @@ describe IncomingMessage, " when uudecoding bad messages" do
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
im.extract_attachments!
-
+
attachments = im.foi_attachments
attachments.size.should == 2
attachments[1].filename.should == 'moo.txt'
@@ -407,7 +414,7 @@ describe IncomingMessage, "when messages are attached to messages" do
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
-
+
im.extract_attachments!
attachments = im.get_attachments_for_display
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index 41d01c89a..c55127992 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -401,25 +401,87 @@ describe InfoRequest do
end
- context "with regexp censor rule" do
- before do
- Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
- @info_request = InfoRequest.create!(:prominence => 'normal',
- :awaiting_description => true,
- :title => 'title',
- :public_body => public_bodies(:geraldine_public_body),
- :user_id => 1)
- @censor_rule = CensorRule.create(:last_edit_editor => 1,
- :last_edit_comment => 'comment',
- :text => 'text',
- :replacement => 'replacement',
- :regexp => true)
- end
- it "applies regexp censor rule" do
- body = 'text'
- @info_request.apply_censor_rules_to_text!(body)
- body.should == 'replacement'
- end
+ describe 'when applying censor rules' do
+
+ before do
+ @global_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @user_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @request_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @body_rule = mock_model(CensorRule, :apply_to_text! => nil,
+ :apply_to_binary! => nil)
+ @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')
+ @info_request.stub!(:user).and_return(@user)
+ @info_request.stub!(:censor_rules).and_return([@request_rule])
+ @info_request.stub!(:public_body).and_return(@body)
+ @text = 'some text'
+ CensorRule.stub!(:global).and_return(mock('global context', :all => [@global_rule]))
+ end
+
+ context "when applying censor rules to text" do
+
+ it "should apply a global censor rule" do
+ @global_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ it 'should apply a user rule' do
+ @user_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ it 'should not raise an error if there is no user' do
+ @info_request.user_id = nil
+ lambda{ @info_request.apply_censor_rules_to_text!(@text) }.should_not raise_error
+ end
+
+ it 'should apply a rule from the body associated with the request' do
+ @body_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ it 'should apply a request rule' do
+ @request_rule.should_receive(:apply_to_text!).with(@text)
+ @info_request.apply_censor_rules_to_text!(@text)
+ end
+
+ end
+
+ context 'when applying censor rules to binary files' do
+
+ it "should apply a global censor rule" do
+ @global_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should apply a user rule' do
+ @user_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should not raise an error if there is no user' do
+ @info_request.user_id = nil
+ lambda{ @info_request.apply_censor_rules_to_binary!(@text) }.should_not raise_error
+ end
+
+ it 'should apply a rule from the body associated with the request' do
+ @body_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ it 'should apply a request rule' do
+ @request_rule.should_receive(:apply_to_binary!).with(@text)
+ @info_request.apply_censor_rules_to_binary!(@text)
+ end
+
+ end
+
end
describe 'when an instance is asked if all can view it' do