diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 154 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/helpers/public_body_helper_spec.rb | 50 | ||||
-rw-r--r-- | spec/integration/admin_public_body_edit_spec.rb | 71 | ||||
-rw-r--r-- | spec/integration/alaveteli_dsl.rb | 2 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/pdf_spec.rb | 17 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 333 | ||||
-rw-r--r-- | spec/views/public_body/show.html.erb_spec.rb | 21 |
8 files changed, 629 insertions, 25 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 7de292303..3ab58317a 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -44,6 +44,15 @@ describe AdminPublicBodyController, 'when showing the form for a new public body assigns[:public_body].should be_a(PublicBody) end + it "builds new translations for all locales" do + get :new + + translations = assigns[:public_body].translations.map{ |t| t.locale.to_s }.sort + available = I18n.available_locales.map{ |l| l.to_s }.sort + + expect(translations).to eq(available) + end + context 'when passed a change request id as a param' do render_views @@ -88,11 +97,13 @@ describe AdminPublicBodyController, "when creating a public body" do :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' }] - } + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Mi Nuevo Quango", + :short_name => "", + :request_email => 'newquango@localhost' } + } + } } PublicBody.count.should == n + 1 @@ -159,6 +170,12 @@ describe AdminPublicBodyController, "when editing a public body" do response.should render_template('edit') end + it "builds new translations if the body does not already have a translation in the specified locale" do + public_body = FactoryGirl.create(:public_body) + get :edit, :id => public_body.id + expect(assigns[:public_body].translations.map(&:locale)).to include(:fr) + end + context 'when passed a change request id as a param' do render_views @@ -196,6 +213,116 @@ describe AdminPublicBodyController, "when updating a public body" do pb.name.should == "Renamed" end + it 'adds a new translation' do + pb = public_bodies(:humpadink_public_body) + pb.translation_for(:es).destroy + pb.reload + + post :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :locale => "es", + :name => "El Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('El Department for Humpadinking') + end + end + + it 'adds new translations' do + pb = public_bodies(:humpadink_public_body) + pb.translation_for(:es).destroy + pb.reload + + post :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :locale => "es", + :name => "El Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' }, + 'fr' => { :locale => "fr", + :name => "Le Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('El Department for Humpadinking') + end + + I18n.with_locale(:fr) do + expect(pb.name).to eq('Le Department for Humpadinking') + end + end + + it 'updates an existing translation and adds a third translation' do + pb = public_bodies(:humpadink_public_body) + + put :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + # Update existing translation + 'es' => { :locale => "es", + :name => "Renamed Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' }, + # Add new translation + 'fr' => { :locale => "fr", + :name => "Le Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('Renamed Department for Humpadinking') + end + + I18n.with_locale(:fr) do + expect(pb.name).to eq('Le Department for Humpadinking') + end + + end + it "saves edits to a public body in another locale" do I18n.with_locale(:es) do pb = PublicBody.find(id=3) @@ -208,11 +335,11 @@ describe AdminPublicBodyController, "when updating a public body" do :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'} + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Renamed", + :short_name => "", + :request_email => 'edited@localhost' } } } } @@ -220,12 +347,15 @@ describe AdminPublicBodyController, "when updating a public body" do end pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + I18n.with_locale(:es) do - pb.name.should == "Renamed" + expect(pb.name).to eq('Renamed') end + I18n.with_locale(:en) do - pb.name.should == "Department for Humpadinking" + expect(pb.name).to eq('Department for Humpadinking') end + end context 'when the body is being updated as a result of a change request' do diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 28dac7b96..128a42556 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -291,4 +291,10 @@ describe GeneralController, 'when using xapian search' do response.body.should include('Track this search') end + it 'should not show high page offsets as these are extremely slow to generate' do + lambda { + get :search, :combined => 'bob/all', :page => 25 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end diff --git a/spec/helpers/public_body_helper_spec.rb b/spec/helpers/public_body_helper_spec.rb new file mode 100644 index 000000000..89a4d0641 --- /dev/null +++ b/spec/helpers/public_body_helper_spec.rb @@ -0,0 +1,50 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe PublicBodyHelper do + include PublicBodyHelper + + describe :public_body_not_requestable_reasons do + + before do + @body = FactoryGirl.build(:public_body) + end + + it 'returns an empty array if there are no reasons' do + expect(public_body_not_requestable_reasons(@body)).to eq([]) + end + + it 'includes a reason if the law does not apply to the authority' do + @body.tag_string = 'not_apply' + msg = 'Freedom of Information law does not apply to this authority, so you cannot make a request to it.' + expect(public_body_not_requestable_reasons(@body)).to include(msg) + end + + it 'includes a reason if the body no longer exists' do + @body.tag_string = 'defunct' + msg = 'This authority no longer exists, so you cannot make a request to it.' + expect(public_body_not_requestable_reasons(@body)).to include(msg) + end + + it 'links to the request page if the body has no contact email' do + @body.request_email = '' + msg = %Q(<a href="/new/#{ @body.url_name }" + class="link_button_green">Make + a request to this authority</a>).squish + + expect(public_body_not_requestable_reasons(@body)).to include(msg) + end + + it 'returns the reasons in order of importance' do + @body.tag_string = 'defunct not_apply' + @body.request_email = '' + + reasons = public_body_not_requestable_reasons(@body) + + expect(reasons[0]).to match(/no longer exists/) + expect(reasons[1]).to match(/does not apply/) + expect(reasons[2]).to match(/Make a request/) + end + + end + +end diff --git a/spec/integration/admin_public_body_edit_spec.rb b/spec/integration/admin_public_body_edit_spec.rb new file mode 100644 index 000000000..aeec3e65a --- /dev/null +++ b/spec/integration/admin_public_body_edit_spec.rb @@ -0,0 +1,71 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl') + +describe 'Editing a Public Body' do + before do + AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false) + + confirm(:admin_user) + @admin = login(:admin_user) + + PublicBody.create(:name => 'New Quango', + :short_name => '', + :request_email => 'newquango@localhost', + :last_edit_editor => 'test', + :last_edit_comment => 'testing') + + @body = PublicBody.find_by_name('New Quango') + end + + it 'can edit the default locale' do + @admin.visit edit_admin_body_path(@body) + @admin.fill_in 'public_body_name__en', :with => 'New Quango EN' + @admin.click_button 'Save' + + pb = @body.reload + expect(pb.name).to eq('New Quango EN') + end + + it 'can add a translation for a single locale' do + expect(@body.find_translation_by_locale('fr')).to be_nil + + @admin.visit edit_admin_body_path(@body) + @admin.fill_in 'public_body_translations_attributes_fr_name__fr', :with => 'New Quango FR' + @admin.click_button 'Save' + + pb = @body.reload + I18n.with_locale(:fr) do + expect(pb.name).to eq('New Quango FR') + end + end + + it 'can add a translation for multiple locales', :focus => true do + @admin.visit edit_admin_body_path(@body) + @admin.fill_in 'public_body_name__en', :with => 'New Quango EN' + @admin.click_button 'Save' + + # Add FR translation + expect(@body.find_translation_by_locale('fr')).to be_nil + @admin.visit edit_admin_body_path(@body) + @admin.fill_in 'public_body_translations_attributes_fr_name__fr', :with => 'New Quango FR' + @admin.click_button 'Save' + + # Add ES translation + expect(@body.find_translation_by_locale('es')).to be_nil + @admin.visit edit_admin_body_path(@body) + @admin.fill_in 'public_body_translations_attributes_es_name__es', :with => 'New Quango ES' + @admin.click_button 'Save' + + pb = @body.reload + + expect(pb.name).to eq('New Quango EN') + + I18n.with_locale(:fr) do + expect(pb.name).to eq('New Quango FR') + end + + I18n.with_locale(:es) do + expect(pb.name).to eq('New Quango ES') + end + end +end diff --git a/spec/integration/alaveteli_dsl.rb b/spec/integration/alaveteli_dsl.rb index b408bc4c6..370628d98 100644 --- a/spec/integration/alaveteli_dsl.rb +++ b/spec/integration/alaveteli_dsl.rb @@ -21,7 +21,7 @@ module AlaveteliDsl response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token) follow_redirect! response.should render_template("user/sign") - response.body.should match(/To send your FOI request, please sign in or make a new account./) + response.body.should match(/To send your FOI request, create an account or sign in/) end end diff --git a/spec/lib/attachment_to_html/adapters/pdf_spec.rb b/spec/lib/attachment_to_html/adapters/pdf_spec.rb index da79b2de0..f1ae4695c 100644 --- a/spec/lib/attachment_to_html/adapters/pdf_spec.rb +++ b/spec/lib/attachment_to_html/adapters/pdf_spec.rb @@ -15,7 +15,7 @@ describe AttachmentToHTML::Adapters::PDF do adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') adapter.tmpdir.should == '/tmp' end - + end describe :title do @@ -23,7 +23,14 @@ describe AttachmentToHTML::Adapters::PDF do it 'uses the attachment filename for the title' do adapter.title.should == attachment.display_filename end - + + it 'returns the title encoded as UTF-8' do + if RUBY_VERSION.to_f >= 1.9 + adapter.title.encoding.should == Encoding.find('UTF-8') + end + end + + end describe :body do @@ -38,6 +45,12 @@ describe AttachmentToHTML::Adapters::PDF do adapter.body end + it 'returns the body encoded as UTF-8' do + if RUBY_VERSION.to_f >= 1.9 + adapter.body.encoding.should == Encoding.find('UTF-8') + end + end + end 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 + diff --git a/spec/views/public_body/show.html.erb_spec.rb b/spec/views/public_body/show.html.erb_spec.rb index 0559fc8ef..917c0c793 100644 --- a/spec/views/public_body/show.html.erb_spec.rb +++ b/spec/views/public_body/show.html.erb_spec.rb @@ -2,10 +2,10 @@ require File.expand_path(File.join('..', '..', '..', 'spec_helper'), __FILE__) describe "public_body/show" do before do - @pb = mock_model(PublicBody, - :name => 'Test Quango', + @pb = mock_model(PublicBody, + :name => 'Test Quango', :short_name => 'tq', - :url_name => 'testquango', + :url_name => 'testquango', :notes => '', :type_of_authority => 'A public body', :eir_only? => nil, @@ -15,6 +15,7 @@ describe "public_body/show" do :calculated_home_page => '') @pb.stub!(:override_request_email).and_return(nil) @pb.stub!(:is_requestable?).and_return(true) + @pb.stub!(:special_not_requestable_reason?).and_return(false) @pb.stub!(:has_notes?).and_return(false) @pb.stub!(:has_tag?).and_return(false) @xap = mock(ActsAsXapian::Search, :matches_estimated => 2) @@ -69,7 +70,7 @@ describe "public_body/show" do response.should have_selector("div#header_right") do have_selector "a", :href => /www.charity-commission.gov.uk.*RegisteredCharityNumber=12345$/ end - end + end it "should link to Scottish Charity Regulator site if we have an SC number" do @pb.stub!(:has_tag?).and_return(true) @@ -79,7 +80,7 @@ describe "public_body/show" do response.should have_selector("div#header_right") do have_selector "a", :href => /www.oscr.org.uk.*id=SC1234$/ end - end + end it "should not link to Charity Commission site if we don't have number" do @@ -87,15 +88,15 @@ describe "public_body/show" do response.should have_selector("div#header_right") do have_selector "a", :href => /charity-commission.gov.uk/ end - end + end end -def mock_event - return mock_model(InfoRequestEvent, - :info_request => mock_model(InfoRequest, - :title => 'Title', +def mock_event + return mock_model(InfoRequestEvent, + :info_request => mock_model(InfoRequest, + :title => 'Title', :url_title => 'title', :display_status => 'waiting_response', :calculate_status => 'waiting_response', |