diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 57 | ||||
-rw-r--r-- | spec/fixtures/files/fake-authority-add-tags.csv (renamed from spec/fixtures/files/fake-authority-add-tags.rb) | 0 | ||||
-rw-r--r-- | spec/fixtures/files/multiple-locales-same-name.csv | 2 | ||||
-rw-r--r-- | spec/helpers/public_body_helper_spec.rb | 141 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/pdf_spec.rb | 17 | ||||
-rw-r--r-- | spec/models/about_me_validator_spec.rb | 53 | ||||
-rw-r--r-- | spec/models/public_body_category_spec.rb | 29 | ||||
-rw-r--r-- | spec/models/public_body_heading_spec.rb | 20 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 524 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 18 | ||||
-rw-r--r-- | spec/spec_helper.rb | 1 | ||||
-rw-r--r-- | spec/views/public_body/show.html.erb_spec.rb | 51 |
12 files changed, 711 insertions, 202 deletions
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 413d395c5..443856cf3 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -1,6 +1,63 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe UserController do + + describe :set_profile_photo do + + context 'user is banned' do + + before(:each) do + @user = FactoryGirl.create(:user, :ban_text => 'Causing trouble') + session[:user_id] = @user.id + @uploadedfile = fixture_file_upload("/files/parrot.png") + + post :set_profile_photo, :id => @user.id, + :file => @uploadedfile, + :submitted_draft_profile_photo => 1, + :automatically_crop => 1 + end + + it 'redirects to the profile page' do + expect(response).to redirect_to(set_profile_photo_path) + end + + it 'renders an error message' do + msg = 'Banned users cannot edit their profile' + expect(flash[:error]).to eq(msg) + end + + end + + end + + describe :set_profile_about_me do + + context 'user is banned' do + + before(:each) do + @user = FactoryGirl.create(:user, :ban_text => 'Causing trouble') + session[:user_id] = @user.id + + post :set_profile_about_me, :submitted_about_me => '1', + :about_me => 'Bad stuff' + end + + it 'redirects to the profile page' do + expect(response).to redirect_to(set_profile_about_me_path) + end + + it 'renders an error message' do + msg = 'Banned users cannot edit their profile' + expect(flash[:error]).to eq(msg) + end + + end + + end + +end + # TODO: Use route_for or params_from to check /c/ links better # http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Example/ControllerExampleGroup.html describe UserController, "when redirecting a show request to a canonical url" do diff --git a/spec/fixtures/files/fake-authority-add-tags.rb b/spec/fixtures/files/fake-authority-add-tags.csv index a5612d87f..a5612d87f 100644 --- a/spec/fixtures/files/fake-authority-add-tags.rb +++ b/spec/fixtures/files/fake-authority-add-tags.csv diff --git a/spec/fixtures/files/multiple-locales-same-name.csv b/spec/fixtures/files/multiple-locales-same-name.csv new file mode 100644 index 000000000..43505f6a6 --- /dev/null +++ b/spec/fixtures/files/multiple-locales-same-name.csv @@ -0,0 +1,2 @@ +"#id","request_email","name","name.es","tag_string","home_page" +23842,"test@test.es","Test","Test",37,"http://www.test.es/" diff --git a/spec/helpers/public_body_helper_spec.rb b/spec/helpers/public_body_helper_spec.rb new file mode 100644 index 000000000..0bf55abb4 --- /dev/null +++ b/spec/helpers/public_body_helper_spec.rb @@ -0,0 +1,141 @@ +# encoding: UTF-8 +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 + + + describe :type_of_authority do + + it 'falls back to "A public authority"' do + public_body = FactoryGirl.build(:public_body) + expect(type_of_authority(public_body)).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(type_of_authority(public_body)).to eq('<a href="/body/list/spec">Ünicode category</a>') + 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') + expected = '<a href="/body/list/spec_0">Spec category 0</a> and <a href="/body/list/spec_2">spec category 2</a>' + expect(type_of_authority(public_body)).to eq(expected) + end + + + 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(type_of_authority(public_body)).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(type_of_authority(public_body)).to eq(description) + end + + end + + context 'when in a non-default locale' do + + it 'creates the anchor href in the correct locale' do + # Activate the routing filter, normally turned off for helper tests + RoutingFilter.active = true + 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="/es/body/list/spec">Spec category</a>) + I18n.with_locale(:es) { expect(type_of_authority(public_body) +).to eq(anchor) } + end + + end + + 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/about_me_validator_spec.rb b/spec/models/about_me_validator_spec.rb new file mode 100644 index 000000000..5610cead8 --- /dev/null +++ b/spec/models/about_me_validator_spec.rb @@ -0,0 +1,53 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AboutMeValidator do + + describe :new do + + it 'sets each supported attribute on the instance' do + params = { :about_me => 'My description' } + validator = AboutMeValidator.new(params) + expect(validator.about_me).to eq('My description') + end + + end + + describe :valid? do + + it 'is valid if about_me is =< 500' do + params = { :about_me => 'a'*500 } + validator = AboutMeValidator.new(params) + expect(validator).to be_valid + end + + it 'is valid if about_me is blank' do + params = { :about_me => '' } + validator = AboutMeValidator.new(params) + expect(validator).to be_valid + end + + it 'is valid if about_me is nil' do + params = { :about_me => nil } + validator = AboutMeValidator.new(params) + expect(validator).to be_valid + end + + it 'is invalid if about_me is > 500' do + params = { :about_me => 'a'*501 } + validator = AboutMeValidator.new(params) + expect(validator).to have(1).error_on(:about_me) + end + + end + + describe :about_me do + + it 'has an attribute accessor' do + params = { :about_me => 'My description' } + validator = AboutMeValidator.new(params) + expect(validator.about_me).to eq('My description') + end + + end + +end diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb index 96fe5686b..c16c9b8a1 100644 --- a/spec/models/public_body_category_spec.rb +++ b/spec/models/public_body_category_spec.rb @@ -9,35 +9,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe PublicBodyCategory do - describe 'when loading the data' do - it 'should use the display_order field to preserve the original data order' do - PublicBodyCategory.add(:en, [ - "Local and regional", - [ "local_council", "Local councils", "a local council" ], - "Miscellaneous", - [ "other", "Miscellaneous", "miscellaneous" ], - [ "aardvark", "Aardvark", "daft test"],]) - - headings = PublicBodyHeading.all - cat_group1 = headings[0].public_body_categories - cat_group1.count.should eq 1 - cat_group1[0].title.should eq "Local councils" - - cat_group2 = headings[1].public_body_categories - cat_group2.count.should eq 2 - cat_group2[0].title.should eq "Miscellaneous" - cat_group2[0].public_body_category_links.where( - :public_body_heading_id => headings[1].id). - first. - category_display_order.should eq 0 - - cat_group2[1].title.should eq "Aardvark" - cat_group2[1].public_body_category_links.where( - :public_body_heading_id => headings[1].id). - first. - category_display_order.should eq 1 - end - end context 'when validating' do diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb index 9372e0a07..620f7da9c 100644 --- a/spec/models/public_body_heading_spec.rb +++ b/spec/models/public_body_heading_spec.rb @@ -10,26 +10,6 @@ require 'spec_helper' describe PublicBodyHeading do - context 'when loading the data' do - - before do - PublicBodyCategory.add(:en, [ - "Local and regional", - [ "local_council", "Local councils", "a local council" ], - "Miscellaneous", - [ "other", "Miscellaneous", "miscellaneous" ],]) - end - - it 'should use the display_order field to preserve the original data order' do - headings = PublicBodyHeading.all - headings[0].name.should eq 'Local and regional' - headings[0].display_order.should eq 0 - headings[1].name.should eq 'Miscellaneous' - headings[1].display_order.should eq 1 - end - - end - context 'when validating' do it 'should require a name' do diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index a9e801bfd..d91021315 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -30,112 +30,6 @@ 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 @@ -654,7 +548,7 @@ describe PublicBody, " when loading CSV files" do PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['aTag', 'fake'] # Import again to check the 'add' tag functionality works - new_tags_file = load_file_fixture('fake-authority-add-tags.rb') + new_tags_file = load_file_fixture('fake-authority-add-tags.csv') errors, notes = PublicBody.import_csv(new_tags_file, '', 'add', false, 'someadmin') # false means real run # Check tags were added successfully @@ -673,15 +567,284 @@ describe PublicBody, " when loading CSV files" do 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') + new_tags_file = load_file_fixture('fake-authority-add-tags.csv') 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'] + PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['aTag', 'fake'] + PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['aTag', 'fake'] PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['aTag', 'fake'] end + + context 'when the import tag is set' do + + context 'with a new body' do + + it 'appends the import tag when no tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'add', false, 'someadmin') + + expected = %W(imported) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'appends the import tag when a tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,first_tag second_tag,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'add', false, 'someadmin') + + expected = %W(first_tag imported second_tag) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'replaces with the import tag when no tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'replace', false, 'someadmin') + + expected = %W(imported) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'replaces with the import tag and tag_string when a tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,first_tag second_tag,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'replace', false, 'someadmin') + + expected = %W(first_tag imported second_tag) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + end + + context 'an existing body without tags' do + + before do + @body = FactoryGirl.create(:public_body, :name => 'Existing Body') + end + + it 'will not import if there is an existing body without the tag' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }",,#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + errors, notes = PublicBody.import_csv(csv, 'imported', 'add', false, 'someadmin') + + expected = %W(imported) + errors.should include("error: line 2: Name Name is already taken for authority 'Existing Body'") + end + + end + + context 'an existing body with tags' do + + before do + @body = FactoryGirl.create(:public_body, :tag_string => 'imported first_tag second_tag') + end + + it 'created with tags, different tags in csv, add import tag' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }","first_tag new_tag",#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'add', false, 'someadmin') + expected = %W(first_tag imported new_tag second_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + it 'created with tags, different tags in csv, replace import tag' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }","first_tag new_tag",#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, 'imported', 'replace', false, 'someadmin') + + expected = %W(first_tag imported new_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + end + + end + + context 'when the import tag is not set' do + + context 'with a new body' do + + it 'it is empty if no tag_string is set' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'add', false, 'someadmin') + + expected = [] + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'uses the specified tag_string' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,first_tag,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'add', false, 'someadmin') + + expected = %W(first_tag) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'replaces with empty if no tag_string is set' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'replace', false, 'someadmin') + + expected = [] + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + it 'replaces with the specified tag_string' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + ,q@localhost,Quango,first_tag,http://example.org + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'replace', false, 'someadmin') + + expected = %W(first_tag) + expect(PublicBody.find_by_name('Quango').tag_array_for_search).to eq(expected) + end + + end + + context 'with an existing body without tags' do + + before do + @body = FactoryGirl.create(:public_body) + end + + it 'appends when no tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }",,#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'add', false, 'someadmin') + + expected = [] + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + it 'appends when a tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }",new_tag,#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'add', false, 'someadmin') + + expected = %W(new_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + it 'replaces when no tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }",,#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'replace', false, 'someadmin') + + expected = [] + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + it 'replaces when a tag_string is specified' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }",new_tag,#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'replace', false, 'someadmin') + + expected = %W(new_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + end + + describe 'with an existing body with tags' do + + before do + @body = FactoryGirl.create(:public_body, :tag_string => 'first_tag second_tag') + end + + it 'created with tags, different tags in csv, add tags' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }","first_tag new_tag",#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'add', false, 'someadmin') + + expected = %W(first_tag new_tag second_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + it 'created with tags, different tags in csv, replace' do + csv = <<-CSV.strip_heredoc + #id,request_email,name,tag_string,home_page + #{ @body.id },#{ @body.request_email },"#{ @body.name }","first_tag new_tag",#{ @body.home_page } + CSV + + # csv, tag, tag_behaviour, dry_run, editor + PublicBody.import_csv(csv, '', 'replace', false, 'someadmin') + + expected = %W(first_tag new_tag) + expect(PublicBody.find(@body.id).tag_array_for_search).to eq(expected) + end + + end + + end + it "should create bodies with names in multiple locales" do original_count = PublicBody.count @@ -806,6 +969,22 @@ CSV PublicBody.csv_import_fields = old_csv_import_fields end + it "should import translations for fields whose values are the same as the default locale's" do + original_count = PublicBody.count + + csv_contents = load_file_fixture("multiple-locales-same-name.csv") + + errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin', ['en', 'es']) # true means dry run + errors.should == [] + notes.size.should == 3 + notes[0..1].should == [ + "line 2: creating new authority 'Test' (locale: en):\n\t{\"name\":\"Test\",\"request_email\":\"test@test.es\",\"home_page\":\"http://www.test.es/\",\"tag_string\":\"37\"}", + "line 2: creating new authority 'Test' (locale: es):\n\t{\"name\":\"Test\"}", + ] + notes[2].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ + + PublicBody.count.should == original_count + end end describe PublicBody do @@ -868,6 +1047,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 @@ -960,3 +1186,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/models/user_spec.rb b/spec/models/user_spec.rb index 7dcd3ab8a..2245a024f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -369,3 +369,21 @@ describe User, "when calculating if a user has exceeded the request limit" do end + +describe User do + + describe :banned? do + + it 'is banned if the user has ban_text' do + user = FactoryGirl.build(:user, :ban_text => 'banned') + expect(user).to be_banned + end + + it 'is not banned if the user has no ban_text' do + user = FactoryGirl.build(:user, :ban_text => '') + expect(user).to_not be_banned + end + + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74a4891c2..93bcfa1ba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ SimpleCov.start('rails') do add_filter 'lib/has_tag_string' add_filter 'lib/acts_as_xapian' add_filter 'lib/themes' + add_filter '.bundle' end Spork.prefork do diff --git a/spec/views/public_body/show.html.erb_spec.rb b/spec/views/public_body/show.html.erb_spec.rb index 0559fc8ef..6ebc39caa 100644 --- a/spec/views/public_body/show.html.erb_spec.rb +++ b/spec/views/public_body/show.html.erb_spec.rb @@ -2,12 +2,12 @@ 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', + :tags => [], :eir_only? => nil, :info_requests => [1, 2, 3, 4], # out of sync with Xapian :publication_scheme => '', @@ -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) @@ -43,7 +44,7 @@ describe "public_body/show" do it "should tell total number of requests" do render - response.should match "4 Freedom of Information requests" + response.should match "4 requests" end it "should cope with no results" do @@ -58,44 +59,12 @@ describe "public_body/show" do response.should match "The search index is currently offline" end - it "should link to Charity Commission site if we have numbers to do so" do - @pb.stub!(:has_tag?).and_return(true) - @pb.stub!(:get_tag_values).and_return(['98765', '12345']) - - render - response.should have_selector("div#header_right") do - have_selector "a", :href => /charity-commission.gov.uk.*RegisteredCharityNumber=98765$/ - end - response.should have_selector("div#header_right") do - have_selector "a", :href => /www.charity-commission.gov.uk.*RegisteredCharityNumber=12345$/ - end - end - - it "should link to Scottish Charity Regulator site if we have an SC number" do - @pb.stub!(:has_tag?).and_return(true) - @pb.stub!(:get_tag_values).and_return(['SC1234']) - - render - response.should have_selector("div#header_right") do - have_selector "a", :href => /www.oscr.org.uk.*id=SC1234$/ - end - end - - - it "should not link to Charity Commission site if we don't have number" do - render - response.should have_selector("div#header_right") do - have_selector "a", :href => /charity-commission.gov.uk/ - 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', |