aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/public_body_spec.rb333
1 files changed, 333 insertions, 0 deletions
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index 0337293e8..b90696c25 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -28,6 +28,214 @@
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
+
+ it 'takes the correct code path for a Hash' do
+ attrs = {}
+ attrs.should_receive(:each_value)
+ PublicBody.new().translations_attributes = attrs
+ end
+
+ it 'updates an existing translation' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+ params = { 'es' => { :locale => 'es',
+ :name => 'Renamed' } }
+
+ body.translations_attributes = params
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :name => 'Le Geraldine Quango' }
+ }
+
+ expect(body.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(body.name).to eq('Le Geraldine Quango') }
+ end
+
+ it 'skips empty translations' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(body.translations.size).to eq(2)
+ end
+
+ end
+
+ context 'translation_attrs is an Array' do
+
+ it 'takes the correct code path for an Array' do
+ attrs = []
+ attrs.should_receive(:each)
+ PublicBody.new().translations_attributes = attrs
+ end
+
+ it 'creates a new translation' do
+ body = public_bodies(:geraldine_public_body)
+ body.translation_for(:es).destroy
+ body.reload
+
+ expect(body.translations.size).to eq(1)
+
+ body.translations_attributes = [ {
+ :locale => 'es',
+ :name => 'Renamed'
+ }
+ ]
+
+ expect(body.translations.size).to eq(2)
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ end
+
+ it 'skips empty translations' do
+ body = public_bodies(:geraldine_public_body)
+ body.translation_for(:es).destroy
+ body.reload
+
+ expect(body.translations.size).to eq(1)
+
+ body.translations_attributes = [
+ { :locale => 'empty' }
+ ]
+
+ expect(body.translations.size).to eq(1)
+ end
+
+ end
+
+ end
+
+end
+
describe PublicBody, " using tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
@@ -945,6 +1153,53 @@ describe PublicBody do
end
+ describe :has_request_email? do
+
+ before do
+ @body = PublicBody.new(:request_email => 'test@example.com')
+ end
+
+ it 'should return false if request_email is nil' do
+ @body.request_email = nil
+ @body.has_request_email?.should == false
+ end
+
+ it 'should return false if the request email is "blank"' do
+ @body.request_email = 'blank'
+ @body.has_request_email?.should == false
+ end
+
+ it 'should return false if the request email is an empty string' do
+ @body.request_email = ''
+ @body.has_request_email?.should == false
+ end
+
+ it 'should return true if the request email is an email address' do
+ @body.has_request_email?.should == true
+ end
+ end
+
+ describe :special_not_requestable_reason do
+
+ before do
+ @body = PublicBody.new
+ end
+
+ it 'should return true if the body is defunct' do
+ @body.stub!(:defunct?).and_return(true)
+ @body.special_not_requestable_reason?.should == true
+ end
+
+ it 'should return true if FOI does not apply' do
+ @body.stub!(:not_apply?).and_return(true)
+ @body.special_not_requestable_reason?.should == true
+ end
+
+ it 'should return false if the body is not defunct and FOI applies' do
+ @body.special_not_requestable_reason?.should == false
+ end
+ end
+
end
describe PublicBody, " when override all public body request emails set" do
@@ -1037,3 +1292,81 @@ describe PublicBody, 'when asked for popular bodies' do
end
end
+
+describe PublicBody do
+
+ describe :is_requestable? do
+
+ before do
+ @body = PublicBody.new(:request_email => 'test@example.com')
+ end
+
+ it 'should return false if the body is defunct' do
+ @body.stub!(:defunct?).and_return true
+ @body.is_requestable?.should == false
+ end
+
+ it 'should return false if FOI does not apply' do
+ @body.stub!(:not_apply?).and_return true
+ @body.is_requestable?.should == false
+ end
+
+ it 'should return false there is no request_email' do
+ @body.stub!(:has_request_email?).and_return false
+ @body.is_requestable?.should == false
+ end
+
+ it 'should return true if the request email is an email address' do
+ @body.is_requestable?.should == true
+ end
+
+ end
+
+ describe :is_followupable? do
+
+ before do
+ @body = PublicBody.new(:request_email => 'test@example.com')
+ end
+
+ it 'should return false there is no request_email' do
+ @body.stub!(:has_request_email?).and_return false
+ @body.is_followupable?.should == false
+ end
+
+ it 'should return true if the request email is an email address' do
+ @body.is_followupable?.should == true
+ end
+
+ end
+
+ describe :not_requestable_reason do
+
+ before do
+ @body = PublicBody.new(:request_email => 'test@example.com')
+ end
+
+ it 'should return "defunct" if the body is defunct' do
+ @body.stub!(:defunct?).and_return true
+ @body.not_requestable_reason.should == 'defunct'
+ end
+
+ it 'should return "not_apply" if FOI does not apply' do
+ @body.stub!(:not_apply?).and_return true
+ @body.not_requestable_reason.should == 'not_apply'
+ end
+
+
+ it 'should return "bad_contact" there is no request_email' do
+ @body.stub!(:has_request_email?).and_return false
+ @body.not_requestable_reason.should == 'bad_contact'
+ end
+
+ it 'should raise an error if the body is not defunct, FOI applies and has an email address' do
+ expected_error = "not_requestable_reason called with type that has no reason"
+ lambda{ @body.not_requestable_reason }.should raise_error(expected_error)
+ end
+
+ end
+
+end
+