aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/censor_rule_spec.rb206
-rw-r--r--spec/models/incoming_message_spec.rb33
-rw-r--r--spec/models/info_request_spec.rb311
-rw-r--r--spec/models/public_body_spec.rb64
-rw-r--r--spec/models/request_mailer_spec.rb2
5 files changed, 453 insertions, 163 deletions
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
index 44087c5a6..c11b05a03 100644
--- a/spec/models/censor_rule_spec.rb
+++ b/spec/models/censor_rule_spec.rb
@@ -1,25 +1,197 @@
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"
+describe CensorRule, "substituting things" do
+
+ 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
+
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"
+ describe "when using a regular expression rule" do
+
+ before do
+ @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
+ @body =
+<<BODY
+Some public information
+--PRIVATE
+Some private information
+--PRIVATE
+BODY
+ 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
+Hidden private info
+--REMOVED
+BODY
+ end
+
+ 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
- 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
+ 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 '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 a18a4bd1d..c55127992 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -1,8 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe InfoRequest do
+describe InfoRequest do
- describe "guessing a request from an email" do
+ describe "guessing a request from an email" do
before(:each) do
@im = incoming_messages(:useless_incoming_message)
@@ -17,7 +17,7 @@ describe InfoRequest do
@info_request.idhash.should_not == nil
end
- it 'should find a request based on an email with an intact id and a broken hash' do
+ it 'should find a request based on an email with an intact id and a broken hash' do
ir = info_requests(:fancy_dog_request)
id = ir.id
@im.mail.to = "request-#{id}-asdfg@example.com"
@@ -35,42 +35,42 @@ describe InfoRequest do
end
- describe "making up the URL title" do
+ describe "making up the URL title" do
before do
@info_request = InfoRequest.new
end
- it 'should remove spaces, and make lower case' do
+ it 'should remove spaces, and make lower case' do
@info_request.title = 'Something True'
@info_request.url_title.should == 'something_true'
end
- it 'should not allow a numeric title' do
+ it 'should not allow a numeric title' do
@info_request.title = '1234'
@info_request.url_title.should == 'request'
end
end
-
- describe "when asked for the last event id that needs description" do
-
+
+ describe "when asked for the last event id that needs description" do
+
before do
@info_request = InfoRequest.new
end
-
- it 'should return the last undescribed event id if there is one' do
+
+ it 'should return the last undescribed event id if there is one' do
last_mock_event = mock_model(InfoRequestEvent)
other_mock_event = mock_model(InfoRequestEvent)
@info_request.stub!(:events_needing_description).and_return([other_mock_event, last_mock_event])
@info_request.last_event_id_needing_description.should == last_mock_event.id
- end
-
+ end
+
it 'should return zero if there are no undescribed events' do
@info_request.stub!(:events_needing_description).and_return([])
@info_request.last_event_id_needing_description.should == 0
end
-
+
end
-
+
describe " when emailing" do
before do
@@ -116,7 +116,7 @@ describe InfoRequest do
hash_part = ir.incoming_email.match(/-[0-9a-f]+@/)[0]
break if hash_part.match(/1/)
end
-
+
# Make email with a 1 in the hash part changed to l
test_email = ir.incoming_email
new_hash_part = hash_part.gsub(/1/, "l")
@@ -134,7 +134,7 @@ describe InfoRequest do
end
it "should return nil when receiving email for a deleted request" do
- deleted_request_address = InfoRequest.magic_email_for_id("request-", 98765)
+ deleted_request_address = InfoRequest.magic_email_for_id("request-", 98765)
found_info_request = InfoRequest.find_by_incoming_email(deleted_request_address)
found_info_request.should be_nil
end
@@ -148,7 +148,7 @@ describe InfoRequest do
update_xapian_index
end
- end
+ end
describe "when calculating the status" do
@@ -169,22 +169,22 @@ describe InfoRequest do
end
it "isn't overdue on due date (20 working days after request sent)" do
- Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
+ Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
@ir.calculate_status.should == 'waiting_response'
end
it "is overdue a day after due date (20 working days after request sent)" do
- Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is still overdue 40 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59))
+ Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is very overdue the day after 40 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01))
@ir.calculate_status.should == 'waiting_response_very_overdue'
end
end
@@ -209,18 +209,18 @@ describe InfoRequest do
it "accepts extended states" do
# this time would normally be "overdue"
- Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
@ir.set_described_state("deadline_extended")
@ir.display_status.should == 'Deadline extended.'
@ir.date_deadline_extended
end
-
+
it "is not overdue if it's had the deadline extended" do
when_overdue = Time.utc(2007, 11, 10, 00, 01) + 16.days
- Time.stub!(:now).and_return(when_overdue)
+ Time.stub!(:now).and_return(when_overdue)
@ir.calculate_status.should == 'waiting_response_overdue'
end
-
+
end
@@ -245,160 +245,271 @@ describe InfoRequest do
end
it "isn't overdue on due date (20 working days after request sent)" do
- Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
+ Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
@ir.calculate_status.should == 'waiting_response'
end
it "is overdue a day after due date (20 working days after request sent)" do
- Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is still overdue 40 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59))
+ Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is still overdue the day after 40 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is still overdue 60 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2008, 01, 11, 23, 59))
+ Time.stub!(:now).and_return(Time.utc(2008, 01, 11, 23, 59))
@ir.calculate_status.should == 'waiting_response_overdue'
end
it "is very overdue the day after 60 working days after request sent" do
- Time.stub!(:now).and_return(Time.utc(2008, 01, 12, 00, 01))
+ Time.stub!(:now).and_return(Time.utc(2008, 01, 12, 00, 01))
@ir.calculate_status.should == 'waiting_response_very_overdue'
end
end
-
- describe 'when asked if a user is the owning user for this request' do
-
- before do
+
+ describe 'when asked if a user is the owning user for this request' do
+
+ before do
@mock_user = mock_model(User)
@info_request = InfoRequest.new(:user => @mock_user)
@other_mock_user = mock_model(User)
end
-
- it 'should return false if a nil object is passed to it' do
+
+ it 'should return false if a nil object is passed to it' do
@info_request.is_owning_user?(nil).should be_false
end
-
- it 'should return true if the user is the request\'s owner' do
+
+ it 'should return true if the user is the request\'s owner' do
@info_request.is_owning_user?(@mock_user).should be_true
end
-
- it 'should return false for a user that is not the owner and does not own every request' do
+
+ it 'should return false for a user that is not the owner and does not own every request' do
@other_mock_user.stub!(:owns_every_request?).and_return(false)
@info_request.is_owning_user?(@other_mock_user).should be_false
end
-
+
it 'should return true if the user is not the owner but owns every request' do
@other_mock_user.stub!(:owns_every_request?).and_return(true)
@info_request.is_owning_user?(@other_mock_user).should be_true
end
-
+
end
-
- describe 'when asked if it requires admin' do
-
- before do
+
+ describe 'when asked if it requires admin' do
+
+ before do
@info_request = InfoRequest.new
end
-
- it 'should return true if its described state is error_message' do
+
+ it 'should return true if its described state is error_message' do
@info_request.described_state = 'error_message'
@info_request.requires_admin?.should be_true
end
-
- it 'should return true if its described state is requires_admin' do
+
+ it 'should return true if its described state is requires_admin' do
@info_request.described_state = 'requires_admin'
@info_request.requires_admin?.should be_true
end
-
- it 'should return false if its described state is waiting_response' do
+
+ it 'should return false if its described state is waiting_response' do
@info_request.described_state = 'waiting_response'
@info_request.requires_admin?.should be_false
end
-
+
end
-
- describe 'when asked for old unclassified requests' do
-
- before do
+
+ describe 'when asked for old unclassified requests' do
+
+ before do
Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
end
-
- it 'should ask for requests using any limit param supplied' do
- InfoRequest.should_receive(:find).with(:all, {:select => anything,
- :order => anything,
- :conditions=> anything,
+
+ it 'should ask for requests using any limit param supplied' do
+ InfoRequest.should_receive(:find).with(:all, {:select => anything,
+ :order => anything,
+ :conditions=> anything,
:limit => 5})
InfoRequest.find_old_unclassified(:limit => 5)
end
-
- it 'should not limit the number of requests returned by default' do
- InfoRequest.should_not_receive(:find).with(:all, {:select => anything,
- :order => anything,
- :conditions=> anything,
+
+ it 'should not limit the number of requests returned by default' do
+ InfoRequest.should_not_receive(:find).with(:all, {:select => anything,
+ :order => anything,
+ :conditions=> anything,
:limit => anything})
InfoRequest.find_old_unclassified
- end
-
- it 'should add extra conditions if supplied' do
- InfoRequest.should_receive(:find).with(:all,
- {:select=> anything,
- :order=> anything,
- :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 prominence != 'backpage'",
+ end
+
+ it 'should add extra conditions if supplied' do
+ InfoRequest.should_receive(:find).with(:all,
+ {:select=> anything,
+ :order=> anything,
+ :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 and prominence != 'backpage'",
true, Time.now - 21.days]})
InfoRequest.find_old_unclassified({:conditions => ["prominence != 'backpage'"]})
end
-
- it 'should ask the database for requests that are awaiting description, have a last response older than 21 days old, are not the holding pen and are not backpaged' do
- InfoRequest.should_receive(:find).with(:all,
- {:select=>"*, (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) as last_response_time",
- :order=>"last_response_time",
- :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'",
+
+ it 'should ask the database for requests that are awaiting description, have a last response older than 21 days old, are not the holding pen and are not backpaged' do
+ InfoRequest.should_receive(:find).with(:all,
+ {:select=>"*, (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) as last_response_time",
+ :order=>"last_response_time",
+ :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",
true, Time.now - 21.days]})
InfoRequest.find_old_unclassified
end
-
+
end
-
- describe 'when an instance is asked if it is old and unclassified' do
-
- before do
+
+ describe 'when an instance is asked if it is old and unclassified' 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')
- @info_request = InfoRequest.new(:prominence => 'normal',
- :awaiting_description => true,
+ @info_request = InfoRequest.new(:prominence => 'normal',
+ :awaiting_description => true,
:info_request_events => [@mock_response_event, @mock_comment_event])
end
-
- it 'should return false if it is the holding pen' do
+
+ it 'should return false if it is the holding pen' do
@info_request.stub!(:url_title).and_return('holding_pen')
@info_request.is_old_unclassified?.should be_false
end
-
- it 'should return false if it is not awaiting description' do
+
+ it 'should return false if it is not awaiting description' do
@info_request.stub!(:awaiting_description).and_return(false)
@info_request.is_old_unclassified?.should be_false
end
-
- it 'should return false if its last response event occurred less than 21 days ago' do
+
+ it 'should return false if its last response event occurred less than 21 days ago' do
@mock_response_event.stub!(:created_at).and_return(Time.now - 20.days)
@info_request.is_old_unclassified?.should be_false
end
-
- it 'should return true if it is awaiting description, isn\'t the holding pen and hasn\'t had an event in 21 days' do
- @info_request.is_old_unclassified?.should be_true
+
+ it 'should return true if it is awaiting description, isn\'t the holding pen and hasn\'t had an event in 21 days' do
+ (@info_request.is_external? || @info_request.is_old_unclassified?).should be_true
+ end
+
+ 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
+
+ before do
+ @info_request = InfoRequest.new
+ end
+
+ it 'should return true if its prominence is normal' do
+ @info_request.prominence = 'normal'
+ @info_request.all_can_view?.should == true
+ end
+
+ it 'should return true if its prominence is backpage' do
+ @info_request.prominence = 'backpage'
+ @info_request.all_can_view?.should == true
+ end
+
+ it 'should return false if its prominence is hidden' do
+ @info_request.prominence = 'hidden'
+ @info_request.all_can_view?.should == false
+ end
+
+ it 'should return false if its prominence is requester_only' do
+ @info_request.prominence = 'requester_only'
+ @info_request.all_can_view?.should == false
+ end
+
end
-
+
end
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index e30916dff..8ff6afde3 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe PublicBody, " using tags" do
+describe PublicBody, " using tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
:short_name => 'AMS',
@@ -9,7 +9,7 @@ describe PublicBody, " using tags" do
:last_edit_comment => '')
end
- it 'should correctly convert a tag string into tags' do
+ it 'should correctly convert a tag string into tags' do
@public_body.tag_string = 'stilton emmental'
@public_body.tag_string.should == 'stilton emmental'
@@ -56,7 +56,7 @@ describe PublicBody, " using tags" do
end
end
-describe PublicBody, " using machine tags" do
+describe PublicBody, " using machine tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
:short_name => 'AMS',
@@ -114,22 +114,22 @@ describe PublicBody, "when finding_by_tags" do
end
end
-describe PublicBody, " when making up the URL name" do
+describe PublicBody, " when making up the URL name" do
before do
@public_body = PublicBody.new
end
- it 'should remove spaces, and make lower case' do
+ it 'should remove spaces, and make lower case' do
@public_body.name = 'Some Authority'
@public_body.url_name.should == 'some_authority'
end
- it 'should not allow a numeric name' do
+ it 'should not allow a numeric name' do
@public_body.name = '1234'
@public_body.url_name.should == 'body'
end
end
-
+
describe PublicBody, " when saving" do
before do
@public_body = PublicBody.new
@@ -157,14 +157,14 @@ describe PublicBody, " when saving" do
@public_body.last_edit_comment = "This is a test"
@public_body.save!
end
-
+
it "should update first_letter" do
@public_body.name = "Testing Public Body"
@public_body.short_name = "TPB"
@public_body.request_email = "request@localhost"
@public_body.last_edit_editor = "*test*"
@public_body.last_edit_comment = "This is a test"
-
+
@public_body.first_letter.should be_nil
@public_body.save!
@public_body.first_letter.should == 'T'
@@ -208,14 +208,14 @@ describe PublicBody, "when searching" do
body.id.should == 3
body.class.to_s.should == 'PublicBody'
end
-
+
it "should cope with same url_name across multiple locales" do
PublicBody.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
- body.save!
-
+ body.save!
+
# now try to retrieve it
body = PublicBody.find_by_url_name_with_historic('tgq')
body.id.should == public_bodies(:geraldine_public_body).id
@@ -242,7 +242,7 @@ describe PublicBody, " when loading CSV files" do
# depending on the tag used. By accessing it here before every test, it doesn't disturb our checks later on
PublicBody.internal_admin_body
end
-
+
it "should import even if no email is provided" do
errors, notes = PublicBody.import_csv("1,aBody", '', 'replace', true, 'someadmin') # true means dry run
errors.should == []
@@ -250,7 +250,7 @@ describe PublicBody, " when loading CSV files" do
notes[0].should == "line 1: creating new authority 'aBody' (locale: en):\n\t{\"name\":\"aBody\"}"
notes[1].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
end
-
+
it "should do a dry run successfully" do
original_count = PublicBody.count
@@ -259,8 +259,8 @@ describe PublicBody, " when loading CSV files" do
errors.should == []
notes.size.should == 4
notes[0..2].should == [
- "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
- "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
"line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
]
notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
@@ -276,8 +276,8 @@ describe PublicBody, " when loading CSV files" do
errors.should == []
notes.size.should == 4
notes[0..2].should == [
- "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
- "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
"line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
]
notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
@@ -287,14 +287,14 @@ describe PublicBody, " when loading CSV files" do
it "should do imports without a tag successfully" do
original_count = PublicBody.count
-
+
csv_contents = load_file_fixture("fake-authority-type.csv")
errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run
errors.should == []
notes.size.should == 4
notes[0..2].should == [
- "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
- "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
"line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
]
notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
@@ -309,15 +309,15 @@ describe PublicBody, " when loading CSV files" do
errors.should == []
notes.size.should == 4
notes[0..2].should == [
- "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"\}",
- "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"\}",
+ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"\}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"\}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"\}",
]
notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
PublicBody.count.should == original_count
end
-
+
it "should import tags successfully when the import tag is not set" do
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run
@@ -344,11 +344,11 @@ describe PublicBody, " when loading CSV files" do
PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['fake']
PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['fake', 'scottish']
PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['aTag', 'fake']
-
+
# Import again to check the 'replace' tag functionality works
new_tags_file = load_file_fixture('fake-authority-add-tags.rb')
errors, notes = PublicBody.import_csv(new_tags_file, 'fake', 'replace', false, 'someadmin') # false means real run
-
+
# Check tags were added successfully
PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['aTag']
PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['aTag']
@@ -363,8 +363,8 @@ describe PublicBody, " when loading CSV files" do
errors.should == []
notes.size.should == 7
notes[0..5].should == [
- "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
- "line 2: creating new authority 'North West Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad del Nordeste\"}",
+ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
+ "line 2: creating new authority 'North West Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad del Nordeste\"}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad Escocesa\"}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}",
@@ -373,8 +373,8 @@ describe PublicBody, " when loading CSV files" do
notes[6].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
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_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')
@@ -387,13 +387,13 @@ describe PublicBody, " when loading CSV files" do
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
# Depending on the runtime environment (Ruby version? OS?) the list of available locales
# is made of strings or symbols, so we use 'en' here as a string to test both scenarios.
- # See https://github.com/sebbacon/alaveteli/issues/193
+ # See https://github.com/mysociety/alaveteli/issues/193
errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin', ['en', :xx]) # true means dry run
errors.should == []
notes.size.should == 4
notes[0..2].should == [
"line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
- "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}",
]
notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/
diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb
index 64ac35cf7..ea75ec765 100644
--- a/spec/models/request_mailer_spec.rb
+++ b/spec/models/request_mailer_spec.rb
@@ -223,7 +223,7 @@ describe RequestMailer, "when sending reminders to requesters to classify a resp
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_params = {: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'",
+ expected_params = {: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",
true, Time.now() - 7.days ],
:include => [ :user ],
:order => "info_requests.id"}