aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/holiday_import_spec.rb157
-rw-r--r--spec/models/holiday_spec.rb133
-rw-r--r--spec/models/incoming_message_spec.rb179
-rw-r--r--spec/models/info_request_spec.rb2
-rw-r--r--spec/models/public_body_category_link_spec.rb9
-rw-r--r--spec/models/public_body_category_spec.rb8
-rw-r--r--spec/models/public_body_heading_spec.rb4
-rw-r--r--spec/models/public_body_spec.rb106
8 files changed, 402 insertions, 196 deletions
diff --git a/spec/models/holiday_import_spec.rb b/spec/models/holiday_import_spec.rb
new file mode 100644
index 000000000..21061f63f
--- /dev/null
+++ b/spec/models/holiday_import_spec.rb
@@ -0,0 +1,157 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe HolidayImport do
+
+ it 'validates the presence of a feed if the source is a feed' do
+ holiday_import = HolidayImport.new(:source => 'feed')
+ holiday_import.valid?.should be_false
+ holiday_import.errors[:ical_feed_url].should == ["can't be blank"]
+ end
+
+ it 'does not validate the presence of a feed if the source is suggestions' do
+ holiday_import = HolidayImport.new(:source => 'suggestions')
+ holiday_import.valid?.should be_true
+ end
+
+ it 'validates that the source is either "feed" or "suggestions"' do
+ holiday_import = HolidayImport.new(:source => 'something')
+ holiday_import.valid?.should be_false
+ holiday_import.errors[:source].should == ["is not included in the list"]
+ end
+
+ it 'validates that all holidays create from attributes are valid' do
+ holiday_import = HolidayImport.new(:source => 'suggestions',
+ :holidays_attributes => {"0" => {:description => '',
+ "day(1i)"=>"",
+ "day(2i)"=>"",
+ "day(3i)"=>""}})
+ holiday_import.valid?.should be_false
+ holiday_import.errors[:base].should == ["These holidays could not be imported"]
+ end
+
+ it 'validates that all holidays to import are valid' do
+ holiday_import = HolidayImport.new
+ holiday_import.holidays = [ Holiday.new ]
+ holiday_import.valid?.should be_false
+ holiday_import.errors[:base].should == ['These holidays could not be imported']
+ end
+
+ it 'defaults to importing holidays for the current year' do
+ holiday_import = HolidayImport.new
+ holiday_import.start_year.should == Time.now.year
+ holiday_import.end_year.should == Time.now.year
+ end
+
+ it 'allows the start and end year to be set' do
+ holiday_import = HolidayImport.new(:start_year => 2011, :end_year => 2012)
+ holiday_import.start_year.should == 2011
+ holiday_import.end_year.should == 2012
+ end
+
+ it 'sets the start and end dates to the beginning and end of the year' do
+ holiday_import = HolidayImport.new(:start_year => 2011, :end_year => 2012)
+ holiday_import.start_date.should == Date.new(2011, 1, 1)
+ holiday_import.end_date.should == Date.new(2012, 12, 31)
+ end
+
+ it 'sets a default source of suggestions' do
+ holiday_import = HolidayImport.new
+ holiday_import.source.should == 'suggestions'
+ end
+
+ it 'allows the source to be set' do
+ holiday_import = HolidayImport.new(:source => 'feed')
+ holiday_import.source.should == 'feed'
+ end
+
+ it 'allows an iCal feed URL to be set' do
+ holiday_import = HolidayImport.new(:ical_feed_url => 'http://www.example.com')
+ holiday_import.ical_feed_url.should == 'http://www.example.com'
+ end
+
+ it 'sets a default populated flag to false' do
+ holiday_import = HolidayImport.new
+ holiday_import.populated.should == false
+ end
+
+ it 'returns a readable description of the period for multiple years' do
+ HolidayImport.new(:start_year => 2011, :end_year => 2012).period.should == '2011-2012'
+ end
+
+ it 'returns a readable description of the period for a single year' do
+ HolidayImport.new(:start_year => 2011, :end_year => 2011).period.should == '2011'
+ end
+
+ it 'returns the country name for which suggestions are generated' do
+ HolidayImport.new.suggestions_country_name.should == 'Germany'
+ end
+
+ describe 'when populating a set of holidays to import from suggestions' do
+
+ before do
+ holidays = [ { :date => Date.new(2014, 1, 1), :name => "New Year's Day", :regions => [:gb] } ]
+ Holidays.stub!(:between).and_return(holidays)
+ @holiday_import = HolidayImport.new(:source => 'suggestions')
+ @holiday_import.populate
+ end
+
+ it 'should populate holidays from the suggestions' do
+ @holiday_import.holidays.size.should == 1
+ holiday = @holiday_import.holidays.first
+ holiday.description.should == "New Year's Day"
+ holiday.day.should == Date.new(2014, 1, 1)
+ end
+
+ it 'should return a flag that it has been populated' do
+ @holiday_import.populated.should == true
+ end
+
+ end
+
+ describe 'when populating a set of holidays to import from a feed' do
+
+ before do
+ @holiday_import = HolidayImport.new(:source => 'feed',
+ :ical_feed_url => 'http://www.example.com',
+ :start_year => 2014,
+ :end_year => 2014)
+ end
+
+ it 'should populate holidays from the feed that are between the dates' do
+ @holiday_import.stub!(:open).and_return(load_file_fixture('ical-holidays.ics'))
+ @holiday_import.populate
+ @holiday_import.holidays.size.should == 1
+ holiday = @holiday_import.holidays.first
+ holiday.description.should == "New Year's Day"
+ holiday.day.should == Date.new(2014, 1, 1)
+ end
+
+ it 'should add an error if the calendar cannot be parsed' do
+ @holiday_import.stub!(:open).and_return('some invalid data')
+ @holiday_import.populate
+ expected = ["Sorry, there's a problem with the format of that feed."]
+ @holiday_import.errors[:ical_feed_url].should == expected
+ end
+
+ it 'should add an error if the calendar cannot be found' do
+ @holiday_import.stub!(:open).and_raise Errno::ENOENT.new('No such file or directory')
+ @holiday_import.populate
+ expected = ["Sorry we couldn't find that feed."]
+ @holiday_import.errors[:ical_feed_url].should == expected
+ end
+
+ end
+
+ describe 'when saving' do
+
+ it 'saves all holidays' do
+ holiday = Holiday.new
+ holiday_import = HolidayImport.new
+ holiday_import.holidays = [ holiday ]
+ holiday.should_receive(:save)
+ holiday_import.save
+ end
+
+ end
+
+end
diff --git a/spec/models/holiday_spec.rb b/spec/models/holiday_spec.rb
index 89849abb7..2f8eeabd9 100644
--- a/spec/models/holiday_spec.rb
+++ b/spec/models/holiday_spec.rb
@@ -9,87 +9,98 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe Holiday, " when calculating due date" do
+describe Holiday do
- def due_date(ymd)
- return Holiday.due_date_from_working_days(Date.strptime(ymd), 20).strftime("%F")
- end
+ describe :new do
- context "in working days" do
- it "handles no holidays" do
- due_date('2008-10-01').should == '2008-10-29'
+ it 'should require a day' do
+ holiday = Holiday.new
+ holiday.valid?.should be_false
+ holiday.errors[:day].should == ["can't be blank"]
end
+ end
- it "handles non leap years" do
- due_date('2007-02-01').should == '2007-03-01'
- end
+ describe " when calculating due date" do
- it "handles leap years" do
- due_date('2008-02-01').should == '2008-02-29'
+ def due_date(ymd)
+ return Holiday.due_date_from_working_days(Date.strptime(ymd), 20).strftime("%F")
end
- it "handles Thursday start" do
- due_date('2009-03-12').should == '2009-04-14'
- end
+ context "in working days" do
+ it "handles no holidays" do
+ due_date('2008-10-01').should == '2008-10-29'
+ end
- it "handles Friday start" do
- due_date('2009-03-13').should == '2009-04-15'
- end
+ it "handles non leap years" do
+ due_date('2007-02-01').should == '2007-03-01'
+ end
- # Delivery at the weekend ends up the same due day as if it had arrived on
- # the Friday before. This is because the next working day (Monday) counts
- # as day 1.
- # See http://www.whatdotheyknow.com/help/officers#days
- it "handles Saturday start" do
- due_date('2009-03-14').should == '2009-04-15'
- end
- it "handles Sunday start" do
- due_date('2009-03-15').should == '2009-04-15'
- end
+ it "handles leap years" do
+ due_date('2008-02-01').should == '2008-02-29'
+ end
- it "handles Monday start" do
- due_date('2009-03-16').should == '2009-04-16'
- end
+ it "handles Thursday start" do
+ due_date('2009-03-12').should == '2009-04-14'
+ end
- it "handles Time objects" do
- Holiday.due_date_from_working_days(Time.utc(2009, 03, 16, 12, 0, 0), 20).strftime('%F').should == '2009-04-16'
- end
- end
+ it "handles Friday start" do
+ due_date('2009-03-13').should == '2009-04-15'
+ end
- context "in calendar days" do
- it "handles no holidays" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 1), 20).should == Date.new(2008, 10, 21)
- end
+ # Delivery at the weekend ends up the same due day as if it had arrived on
+ # the Friday before. This is because the next working day (Monday) counts
+ # as day 1.
+ # See http://www.whatdotheyknow.com/help/officers#days
+ it "handles Saturday start" do
+ due_date('2009-03-14').should == '2009-04-15'
+ end
+ it "handles Sunday start" do
+ due_date('2009-03-15').should == '2009-04-15'
+ end
- it "handles the due date falling on a Friday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 4), 20).should == Date.new(2008, 10, 24)
- end
+ it "handles Monday start" do
+ due_date('2009-03-16').should == '2009-04-16'
+ end
- # If the due date would fall on a Saturday it should in fact fall on the next day that isn't a weekend
- # or a holiday
- it "handles the due date falling on a Saturday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 5), 20).should == Date.new(2008, 10, 27)
+ it "handles Time objects" do
+ Holiday.due_date_from_working_days(Time.utc(2009, 03, 16, 12, 0, 0), 20).strftime('%F').should == '2009-04-16'
+ end
end
- it "handles the due date falling on a Sunday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 6), 20).should == Date.new(2008, 10, 27)
- end
+ context "in calendar days" do
+ it "handles no holidays" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 1), 20).should == Date.new(2008, 10, 21)
+ end
- it "handles the due date falling on a Monday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 10, 7), 20).should == Date.new(2008, 10, 27)
- end
+ it "handles the due date falling on a Friday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 4), 20).should == Date.new(2008, 10, 24)
+ end
- it "handles the due date falling on a day before a Holiday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 12, 4), 20).should == Date.new(2008, 12, 24)
- end
+ # If the due date would fall on a Saturday it should in fact fall on the next day that isn't a weekend
+ # or a holiday
+ it "handles the due date falling on a Saturday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 5), 20).should == Date.new(2008, 10, 27)
+ end
- it "handles the due date falling on a Holiday" do
- Holiday.due_date_from_calendar_days(Date.new(2008, 12, 5), 20).should == Date.new(2008, 12, 29)
- end
+ it "handles the due date falling on a Sunday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 6), 20).should == Date.new(2008, 10, 27)
+ end
+
+ it "handles the due date falling on a Monday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 10, 7), 20).should == Date.new(2008, 10, 27)
+ end
+
+ it "handles the due date falling on a day before a Holiday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 12, 4), 20).should == Date.new(2008, 12, 24)
+ end
- it "handles Time objects" do
- Holiday.due_date_from_calendar_days(Time.utc(2009, 03, 17, 12, 0, 0), 20).should == Date.new(2009, 4, 6)
+ it "handles the due date falling on a Holiday" do
+ Holiday.due_date_from_calendar_days(Date.new(2008, 12, 5), 20).should == Date.new(2008, 12, 29)
+ end
+
+ it "handles Time objects" do
+ Holiday.due_date_from_calendar_days(Time.utc(2009, 03, 17, 12, 0, 0), 20).should == Date.new(2009, 4, 6)
+ end
end
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
diff --git a/spec/models/public_body_category_link_spec.rb b/spec/models/public_body_category_link_spec.rb
index 8d91f02d5..fd5608480 100644
--- a/spec/models/public_body_category_link_spec.rb
+++ b/spec/models/public_body_category_link_spec.rb
@@ -1,10 +1,11 @@
# == Schema Information
#
-# Table name: public_body_category_link
+# Table name: public_body_category_links
#
-# public_body_category_id :integer not null
-# public_body_heading_id :integer not null
-# category_display_order :integer
+# public_body_category_id :integer not null
+# public_body_heading_id :integer not null
+# category_display_order :integer
+# id :integer not null, primary key
#
require 'spec_helper'
diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb
index c185a3169..96fe5686b 100644
--- a/spec/models/public_body_category_spec.rb
+++ b/spec/models/public_body_category_spec.rb
@@ -2,12 +2,8 @@
#
# Table name: public_body_categories
#
-# id :integer not null, primary key
-# locale :string
-# title :text not null
-# category_tag :text not null
-# description :text not null
-# display_order :integer
+# id :integer not null, primary key
+# category_tag :text not null
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb
index add2cac60..9372e0a07 100644
--- a/spec/models/public_body_heading_spec.rb
+++ b/spec/models/public_body_heading_spec.rb
@@ -2,9 +2,7 @@
#
# Table name: public_body_headings
#
-# id :integer not null, primary key
-# locale :string
-# name :text not null
+# id :integer not null, primary key
# display_order :integer
#
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index f12582f21..a9e801bfd 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -30,6 +30,112 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe PublicBody do
+ describe :type_of_authority do
+
+ it 'falls back to "A public authority"' do
+ public_body = FactoryGirl.build(:public_body)
+ expect(public_body.type_of_authority).to eq('A public authority')
+ end
+
+ it 'handles Unicode' do
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec',
+ :description => 'ünicode category')
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.add_category(category)
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec')
+
+
+ expect(public_body.type_of_authority).to eq('Ünicode category')
+ end
+
+ it 'constructs the correct string if there are tags which are not categories' do
+ heading = FactoryGirl.create(:public_body_heading)
+ 3.times do |i|
+ category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}",
+ :description => "spec category #{i}")
+ heading.add_category(category)
+ end
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_2 unknown')
+
+ expect(public_body.type_of_authority).to eq('Spec category 0 and spec category 2')
+ end
+
+ context 'when associated with one category' do
+
+ it 'returns the capitalised category description' do
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec',
+ :description => 'spec category')
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.add_category(category)
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec')
+
+ expect(public_body.type_of_authority).to eq('Spec category')
+ end
+
+ end
+
+ context 'when associated with several categories' do
+
+ it 'joins the category descriptions and capitalizes the first letter' do
+ heading = FactoryGirl.create(:public_body_heading)
+ 3.times do |i|
+ category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}",
+ :description => "spec category #{i}")
+ heading.add_category(category)
+ end
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_1 spec_2')
+
+ description = 'Spec category 0, spec category 1 and spec category 2'
+ expect(public_body.type_of_authority).to eq(description)
+ end
+
+ end
+
+ context 'when the html parameter is true' do
+
+ context 'when associated with one category' do
+
+ it 'returns the description wrapped in an anchor tag' do
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec',
+ :description => 'spec category')
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.add_category(category)
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec')
+
+ anchor = %Q(<a href="/body/list/spec">Spec category</a>)
+ expect(public_body.type_of_authority(true)).to eq(anchor)
+ end
+
+ end
+
+ context 'when associated with several categories' do
+
+ it 'joins the category descriptions and capitalizes the first letter' do
+ heading = FactoryGirl.create(:public_body_heading)
+ 3.times do |i|
+ category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}",
+ :description => "spec category #{i}")
+ heading.add_category(category)
+ end
+ public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_1 spec_2')
+
+ description = [
+ %Q(<a href="/body/list/spec_0">Spec category 0</a>),
+ ', ',
+ %Q(<a href="/body/list/spec_1">spec category 1</a>),
+ ' and ',
+ %Q(<a href="/body/list/spec_2">spec category 2</a>)
+ ].join('')
+
+ expect(public_body.type_of_authority(true)).to eq(description)
+ end
+
+ end
+
+ end
+
+ end
+
describe :translations_attributes= do
context 'translation_attrs is a Hash' do