aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/admin_public_body_categories_controller_spec.rb700
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb443
-rw-r--r--spec/controllers/admin_public_body_headings_controller_spec.rb517
-rw-r--r--spec/controllers/general_controller_spec.rb6
-rw-r--r--spec/controllers/user_controller_spec.rb57
-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.csv2
-rw-r--r--spec/helpers/public_body_helper_spec.rb141
-rw-r--r--spec/integration/admin_public_body_category_edit_spec.rb59
-rw-r--r--spec/integration/admin_public_body_edit_spec.rb71
-rw-r--r--spec/integration/admin_public_body_heading_edit_spec.rb58
-rw-r--r--spec/integration/alaveteli_dsl.rb2
-rw-r--r--spec/lib/attachment_to_html/adapters/pdf_spec.rb17
-rw-r--r--spec/models/about_me_validator_spec.rb53
-rw-r--r--spec/models/public_body_category_spec.rb169
-rw-r--r--spec/models/public_body_heading_spec.rb128
-rw-r--r--spec/models/public_body_spec.rb509
-rw-r--r--spec/models/user_spec.rb18
-rw-r--r--spec/spec_helper.rb1
-rw-r--r--spec/views/public_body/show.html.erb_spec.rb51
20 files changed, 2579 insertions, 423 deletions
diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb
index 4c641bd75..1131b3c0b 100644
--- a/spec/controllers/admin_public_body_categories_controller_spec.rb
+++ b/spec/controllers/admin_public_body_categories_controller_spec.rb
@@ -1,120 +1,268 @@
require 'spec_helper'
describe AdminPublicBodyCategoriesController do
- context 'when showing the index of categories and headings' do
- render_views
- it 'shows the index page' do
+ describe :index do
+
+ it 'responds successfully' do
get :index
expect(response).to be_success
end
+
+ it 'uses the current locale by default' do
+ get :index
+ expect(assigns(:locale)).to eq(I18n.locale.to_s)
+ end
+
+ it 'sets the locale if the show_locale param is passed' do
+ get :index, :show_locale => 'es'
+ expect(assigns(:locale)).to eq('es')
+ end
+
+ it 'finds all category headings' do
+ PublicBodyHeading.destroy_all
+
+ headings = [FactoryGirl.create(:public_body_heading),
+ FactoryGirl.create(:public_body_heading)]
+
+ get :index
+
+ expect(assigns(:category_headings)).to eq(headings)
+ end
+
+ it 'finds all categories without their headings' do
+ PublicBodyHeading.destroy_all
+ PublicBodyCategory.destroy_all
+
+ without_heading = FactoryGirl.create(:public_body_category)
+
+ heading = FactoryGirl.create(:public_body_heading)
+ with_heading = FactoryGirl.create(:public_body_category)
+ PublicBodyCategoryLink.create!(:public_body_heading_id => heading.id,
+ :public_body_category_id => with_heading.id)
+
+
+ get :index
+ expect(assigns(:without_heading)).to eq([without_heading])
+ end
+
+ it 'renders the index template' do
+ get :index
+ expect(response).to render_template('index')
+ end
+
end
- context 'when showing the form for a new public body category' do
- it 'should assign a new public body category to the view' do
+ describe :new do
+
+ it 'responds successfully' do
get :new
- assigns[:category].should be_a(PublicBodyCategory)
+ expect(response).to be_success
end
- it 'renders the new template' do
+ it 'builds a new PublicBodyCategory' do
get :new
- expect(response).to render_template('new')
+ expect(assigns(:category)).to be_new_record
end
+ it 'builds new translations for all locales' do
+ get :new
+
+ translations = assigns(:category).translations.map{ |t| t.locale.to_s }.sort
+ available = I18n.available_locales.map{ |l| l.to_s }.sort
+
+ expect(translations).to eq(available)
+ end
+
+ it 'renders the new template' do
+ get :new
+ expect(response).to render_template('new')
+ end
+
end
- context 'when creating a public body category' do
- it "creates a new public body category in one locale" do
- n = PublicBodyCategory.count
- post :create, {
- :public_body_category => {
- :title => 'New Category',
- :category_tag => 'new_test_category',
- :description => 'New category for testing stuff'
- }
- }
- PublicBodyCategory.count.should == n + 1
+ describe :create do
+
+ context 'on success' do
+
+ before(:each) do
+ PublicBodyCategory.destroy_all
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'New Category',
+ :description => 'New category for testing stuff' }
+ } }
+ end
+
+ it 'creates a new category in the default locale' do
+ PublicBodyCategory.destroy_all
+
+ expect {
+ post :create, :public_body_category => @params
+ }.to change{ PublicBodyCategory.count }.from(0).to(1)
+ end
+
+ it "saves the public body category's heading associations" do
+ heading = FactoryGirl.create(:public_body_heading)
+ params = FactoryGirl.attributes_for(:public_body_category)
+
+ post :create, :public_body_category => @params,
+ :headings => { "heading_#{ heading.id }" => heading.id }
+
+ category = PublicBodyCategory.find_by_title(@params[:translations_attributes]['en'][:title])
+ expect(category.public_body_headings).to eq([heading])
+ end
+
+ it 'notifies the admin that the category was created' do
+ post :create, :public_body_category => @params
+ expect(flash[:notice]).to eq('Category was successfully created.')
+ end
+
+ it 'redirects to the categories index' do
+ post :create, :public_body_category => @params
+ expect(response).to redirect_to(admin_categories_path)
+ end
- category = PublicBodyCategory.find_by_title("New Category")
- response.should redirect_to(admin_categories_path)
end
- it "saves the public body category's heading associations" do
- heading = FactoryGirl.create(:public_body_heading)
- category_attributes = FactoryGirl.attributes_for(:public_body_category)
- post :create, {
- :public_body_category => category_attributes,
- :headings => {"heading_#{heading.id}" => heading.id}
- }
- request.flash[:notice].should include('successful')
- category = PublicBodyCategory.find_by_title(category_attributes[:title])
- category.public_body_headings.should == [heading]
+ context 'on success for multiple locales' do
+
+ before(:each) do
+ PublicBodyCategory.destroy_all
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'New Category',
+ :description => 'New category for testing stuff' },
+ 'es' => { :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'saves the category' do
+ expect {
+ post :create, :public_body_category => @params
+ }.to change{ PublicBodyCategory.count }.from(0).to(1)
+ end
+
+ it 'saves the default locale translation' do
+ post :create, :public_body_category => @params
+
+ category = PublicBodyCategory.find_by_title('New Category')
+
+ I18n.with_locale(:en) do
+ expect(category.title).to eq('New Category')
+ end
+ end
+
+ it 'saves the alternative locale translation' do
+ post :create, :public_body_category => @params
+
+ category = PublicBodyCategory.find_by_title('New Category')
+
+ I18n.with_locale(:es) do
+ expect(category.title).to eq('Mi Nuevo Category')
+ end
+ end
+
end
- it 'creates a new public body category with multiple locales' do
- n = PublicBodyCategory.count
- post :create, {
- :public_body_category => {
- :title => 'New Category',
- :category_tag => 'new_test_category',
- :description => 'New category for testing stuff',
- :translated_versions => [{ :locale => "es",
- :title => "Mi Nuevo Category" }]
- }
- }
- PublicBodyCategory.count.should == n + 1
+ context 'on failure' do
- category = PublicBodyCategory.find_by_title("New Category")
- category.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"]
- I18n.with_locale(:en) do
- category.title.should == "New Category"
+ it 'renders the form if creating the record was unsuccessful' do
+ post :create, :public_body_category => { :title => '' }
+ expect(response).to render_template('new')
end
- I18n.with_locale(:es) do
- category.title.should == "Mi Nuevo Category"
+
+ it 'is rebuilt with the given params' do
+ post :create, :public_body_category => { :title => 'Need a description' }
+ expect(assigns(:category).title).to eq('Need a description')
end
- response.should redirect_to(admin_categories_path)
end
- it "renders the form if creating the record was unsuccessful" do
- post :create, :public_body_category => { :title => '' }
- expect(response).to render_template('new')
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :title => 'Need a description',
+ :description => nil },
+ 'es' => { :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :create, :public_body_category => @params
+ expect(assigns(:category).title).to eq('Need a description')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :create, :public_body_category => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:category).title).to eq('Mi Nuevo Category')
+ end
+ end
+
end
end
- context 'when editing a public body category' do
+ describe :edit do
+
before do
@category = FactoryGirl.create(:public_body_category)
I18n.with_locale('es') do
@category.title = 'Los category'
+ @category.description = 'ES Description'
@category.save!
end
end
- render_views
+ it 'responds successfully' do
+ get :edit, :id => @category.id
+ expect(response).to be_success
+ end
- it "finds the requested category" do
+ it 'finds the requested category' do
get :edit, :id => @category.id
expect(assigns[:category]).to eq(@category)
end
- it "renders the edit template" do
+ it 'builds new translations if the body does not already have a translation in the specified locale' do
get :edit, :id => @category.id
- expect(assigns[:category]).to render_template('edit')
+ expect(assigns[:category].translations.map(&:locale)).to include(:fr)
end
- it "edits a public body in another locale" do
- get :edit, { :id => @category.id, :locale => :en }
+ it 'finds the public bodies tagged with the category tag' do
+ # FIXME: I wanted to call PublicBody.destroy_all here so that we
+ # have a known DB state, but the constraints were preventing the
+ # deletion of the fixture data
+ FactoryGirl.create(:public_body, :tag_string => 'wont_be_found')
+
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec')
+ expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'),
+ FactoryGirl.create(:public_body, :tag_string => 'spec')]
- # When editing a body, the controller returns all available
- # translations
- assigns[:category].find_translation_by_locale("es").title.should == 'Los category'
- response.should render_template('edit')
+ get :edit, :id => category.id
+
+ expect(assigns(:tagged_public_bodies).sort).to eq(expected_bodies.sort)
end
+
+ it 'renders the edit template' do
+ get :edit, :id => @category.id
+ expect(response).to render_template('edit')
+ end
+
end
- context 'when updating a public body category' do
+ describe :update do
before do
@heading = FactoryGirl.create(:public_body_heading)
@@ -126,109 +274,389 @@ describe AdminPublicBodyCategoriesController do
@tag = @category.category_tag
I18n.with_locale('es') do
@category.title = 'Los category'
+ @category.description = 'ES Description'
@category.save!
end
+
+ @params = { :category_tag => @category.category_tag,
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => @category.title(:es),
+ :description => @category.description(:es) }
+ } }
end
- render_views
+ it 'finds the category to update' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+ expect(assigns(:category)).to eq(@category)
+ end
+
+ it 'finds the public bodies tagged with the category tag' do
+ # FIXME: I wanted to call PublicBody.destroy_all here so that we
+ # have a known DB state, but the constraints were preventing the
+ # deletion of the fixture data
+ FactoryGirl.create(:public_body, :tag_string => 'wont_be_found')
+
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'spec')
+ expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'),
+ FactoryGirl.create(:public_body, :tag_string => 'spec')]
+
+ post :update, :id => category.id,
+ :public_body_category => category.serializable_hash.except(:title, :description)
- it "saves edits to a public body category" do
- post :update, { :id => @category.id,
- :public_body_category => { :title => "Renamed" } }
- request.flash[:notice].should include('successful')
- pbc = PublicBodyCategory.find(@category.id)
- pbc.title.should == "Renamed"
+ expect(assigns(:tagged_public_bodies)).to eq(expected_bodies)
end
it "saves edits to a public body category's heading associations" do
- @category.public_body_headings.should == [@heading]
+ # We already have a heading from the before block. Here we're going
+ # to update to a new heading.
heading = FactoryGirl.create(:public_body_heading)
- post :update, { :id => @category.id,
- :public_body_category => { :title => "Renamed" },
- :headings => {"heading_#{heading.id}" => heading.id} }
- request.flash[:notice].should include('successful')
- pbc = PublicBodyCategory.find(@category.id)
- pbc.public_body_headings.should == [heading]
- end
-
- it "saves edits to a public body category in another locale" do
- I18n.with_locale(:es) do
- @category.title.should == 'Los category'
- post :update, {
- :id => @category.id,
- :public_body_category => {
- :title => "Category",
- :translated_versions => {
- @category.id => {:locale => "es",
- :title => "Renamed"}
+
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :title => 'Renamed' }
}
- }
- }
- request.flash[:notice].should include('successful')
+ },
+ :headings => { "heading_#{ heading.id }" => heading.id }
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.public_body_headings).to eq([heading])
+ end
+
+ context 'when the category has associated bodies' do
+
+ it 'does not save edits to category_tag' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.category_tag).to eq(@tag)
end
- pbc = PublicBodyCategory.find(@category.id)
- I18n.with_locale(:es) do
- pbc.title.should == "Renamed"
+ it 'notifies the user that the category_tag could not be updated' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+ msg = %Q(There are authorities associated with this category,
+ so the tag can't be renamed).squish
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ expect(flash[:error]).to eq(msg)
end
- I18n.with_locale(:en) do
- pbc.title.should == "Category"
+
+ it 'renders the edit action' do
+ body = FactoryGirl.create(:public_body, :tag_string => @tag)
+
+ post :update, :id => @category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ expect(response).to render_template('edit')
end
+
end
- it "does not save edits to category_tag if the category has associated bodies" do
- body = FactoryGirl.create(:public_body, :tag_string => @tag)
- post :update, { :id => @category.id,
- :public_body_category => { :category_tag => "renamed" } }
-
- msg = "There are authorities associated with this category, so the tag can't be renamed"
- request.flash[:error].should == msg
- pbc = PublicBodyCategory.find(@category.id)
- pbc.category_tag.should == @tag
+ context 'on success' do
+
+ before(:each) do
+ @params = { :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :title => 'Renamed' }
+ }
+ }
+ }
+ end
+
+ it 'saves edits to a public body category' do
+ post :update, @params
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.title).to eq('Renamed')
+ end
+
+ it 'notifies the admin that the category was created' do
+ post :update, @params
+ expect(flash[:notice]).to eq('Category was successfully updated.')
+ end
+
+ it 'redirects to the category edit page' do
+ post :update, @params
+ expect(response).to redirect_to(edit_admin_category_path(@category))
+ end
+
+ it 'saves edits to category_tag if the category has no associated bodies' do
+ category = FactoryGirl.create(:public_body_category, :category_tag => 'empty')
+
+ post :update, :id => category.id,
+ :public_body_category => { :category_tag => 'Renamed' }
+
+ category = PublicBodyCategory.find(category.id)
+ expect(category.category_tag).to eq('Renamed')
+ end
+
end
+ context 'on success for multiple locales' do
+
+ it "saves edits to a public body category in another locale" do
+ @category.title(:es).should == 'Los category'
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed',
+ :description => 'ES Description' }
+ }
+ }
+
+ category = PublicBodyCategory.find(@category.id)
+ expect(category.title(:es)).to eq('Renamed')
+ expect(category.title(:en)).to eq(@category.title(:en))
+ end
+
+ it 'adds a new translation' do
+ @category.translation_for(:es).destroy
+ @category.reload
+
+ put :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :locale => "es",
+ :title => "Example Public Body Category ES",
+ :description => @category.description(:es) }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Example Public Body Category ES')
+ end
+ end
+
+ it 'adds new translations' do
+ @category.translation_for(:es).destroy
+ @category.reload
+
+ post :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ 'es' => { :locale => "es",
+ :title => "Example Public Body Category ES",
+ :description => 'ES Description' },
+ 'fr' => { :locale => "fr",
+ :title => "Example Public Body Category FR",
+ :description => 'FR Description' }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Example Public Body Category ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(pbc.title).to eq('Example Public Body Category FR')
+ end
+ end
+
+ it 'updates an existing translation and adds a third translation' do
+ post :update, {
+ :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) },
+ # Update existing translation
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => "es",
+ :title => "Renamed Example Public Body Category ES",
+ :description => @category.description },
+ # Add new translation
+ 'fr' => { :locale => "fr",
+ :title => "Example Public Body Category FR",
+ :description => @category.description }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ pbc = PublicBodyCategory.find(@category.id)
+
+ I18n.with_locale(:es) do
+ expect(pbc.title).to eq('Renamed Example Public Body Category ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(pbc.title).to eq('Example Public Body Category FR')
+ end
+ end
+
+ it "redirects to the edit page after a successful update" do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => @category.title(:en),
+ :description => @category.description(:en) }
+ } }
+
+ expect(response).to redirect_to(edit_admin_category_path(@category))
+ end
- it "save edits to category_tag if the category has no associated bodies" do
- category = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-")
- post :update, { :id => category.id,
- :public_body_category => { :category_tag => "renamed" } }
- request.flash[:notice].should include('success')
- pbc = PublicBodyCategory.find(category.id)
- pbc.category_tag.should == "renamed"
end
- it "redirects to the edit page after a successful update" do
- post :update, { :id => @category.id,
- :public_body_category => { :title => "Renamed" } }
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => '',
+ :description => @category.description(:en) }
+ } }
+ expect(response).to render_template('edit')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :update, :id => @category.id,
+ :public_body_category => {
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => 'Need a description',
+ :description => '' }
+ } }
+ expect(assigns(:category).title).to eq('Need a description')
+ end
- expect(response).to redirect_to(edit_admin_category_path(@category))
end
- it "re-renders the edit form after an unsuccessful update" do
- post :update, { :id => @category.id,
- :public_body_category => { :title => '' } }
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :category_tag => 'new_test_category',
+ :translations_attributes => {
+ 'en' => { :id => @category.translation_for(:en).id,
+ :locale => 'en',
+ :title => 'Need a description',
+ :description => '' },
+ 'es' => { :id => @category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Mi Nuevo Category',
+ :description => 'ES Description' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+ expect(assigns(:category).title(:en)).to eq('Need a description')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :update, :id => @category.id,
+ :public_body_category => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:category).title).to eq('Mi Nuevo Category')
+ end
+ end
- expect(response).to render_template('edit')
end
end
- context 'when destroying a public body category' do
- it "destroys empty public body categories" do
- pbc = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-")
- n = PublicBodyCategory.count
- post :destroy, { :id => pbc.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyCategory.count.should == n - 1
+ describe :destroy do
+
+ it 'uses the current locale by default' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(assigns(:locale)).to eq(I18n.locale.to_s)
end
- it "destroys non-empty public body categories" do
+ it 'sets the locale if the show_locale param is passed' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id, :show_locale => 'es'
+ expect(assigns(:locale)).to eq('es')
+ end
+
+ it 'destroys empty public body categories' do
+ PublicBodyCategory.destroy_all
+
+ category = FactoryGirl.create(:public_body_category)
+
+ expect{
+ post :destroy, :id => category.id
+ }.to change{ PublicBodyCategory.count }.from(1).to(0)
+ end
+
+ it 'destroys non-empty public body categories' do
+ PublicBodyCategory.destroy_all
+
+ # FIXME: Couldn't create the PublicBodyCategory with a Factory
+ # because #authorities= doesn't exist?
+ # undefined method `authorities=' for
+ # #<PublicBodyCategory:0x7f55cbb84f70>
authority = FactoryGirl.create(:public_body)
- pbc = PublicBodyCategory.create(:title => "In-Use Category", :category_tag => "empty", :description => "-", :authorities => [authority])
- n = PublicBodyCategory.count
- post :destroy, { :id => pbc.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyCategory.count.should == n - 1
+ category = PublicBodyCategory.create(:title => "In-Use Category",
+ :category_tag => "empty",
+ :description => "-",
+ :authorities => [authority])
+
+ expect{
+ post :destroy, :id => category.id
+ }.to change{ PublicBodyCategory.count }.from(1).to(0)
+ end
+
+ it 'notifies the admin that the category was destroyed' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(flash[:notice]).to eq('Category was successfully destroyed.')
+ end
+
+ it 'redirects to the categories index' do
+ category = FactoryGirl.create(:public_body_category)
+ post :destroy, :id => category.id
+ expect(response).to redirect_to(admin_categories_path)
end
+
end
end
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index 7de292303..50a373d9d 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -39,9 +39,28 @@ end
describe AdminPublicBodyController, 'when showing the form for a new public body' do
+ it 'responds successfully' do
+ get :new
+ expect(response).to be_success
+ end
+
it 'should assign a new public body to the view' do
get :new
- assigns[:public_body].should be_a(PublicBody)
+ expect(assigns(:public_body)).to be_new_record
+ 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
+
+ it 'renders the new template' do
+ get :new
+ expect(response).to render_template('new')
end
context 'when passed a change request id as a param' do
@@ -67,49 +86,127 @@ end
describe AdminPublicBodyController, "when creating a public body" do
render_views
- it "creates a new public body in one locale" do
- n = PublicBody.count
- post :create, { :public_body => { :name => "New Quango",
- :short_name => "",
- :tag_string => "blah",
- :request_email => 'newquango@localhost',
- :last_edit_comment => 'From test code' } }
- PublicBody.count.should == n + 1
+ context 'on success' do
+
+ before(:each) do
+ @params = { :public_body => { :name => 'New Quango',
+ :short_name => 'nq',
+ :request_email => 'newquango@localhost',
+ :tag_string => 'spec',
+ :last_edit_comment => 'From test code' } }
+ end
+
+ it 'creates a new body in the default locale' do
+ # FIXME: Can't call PublicBody.destroy_all because database
+ # database contstraints prevent them being deleted.
+ existing = PublicBody.count
+ expected = existing + 1
+ expect {
+ post :create, @params
+ }.to change{ PublicBody.count }.from(existing).to(expected)
+ end
+
+ it 'notifies the admin that the body was created' do
+ post :create, @params
+ expect(flash[:notice]).to eq('PublicBody was successfully created.')
+ end
+
+ it 'redirects to the admin page of the body' do
+ post :create, @params
+ expect(response).to redirect_to(admin_body_path(assigns(:public_body)))
+ end
- body = PublicBody.find_by_name("New Quango")
- response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id)
end
- it "creates a new public body with multiple locales" do
- n = PublicBody.count
- post :create, {
- :public_body => { :name => "New Quango",
- :short_name => "",
- :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' }]
- }
- }
- PublicBody.count.should == n + 1
-
- body = PublicBody.find_by_name("New Quango")
- body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"]
- I18n.with_locale(:en) do
- body.name.should == "New Quango"
- body.url_name.should == "new_quango"
- body.first_letter.should == "N"
- end
- I18n.with_locale(:es) do
- body.name.should == "Mi Nuevo Quango"
- body.url_name.should == "mi_nuevo_quango"
- body.first_letter.should == "M"
- end
-
- response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id)
+ context 'on success for multiple locales' do
+
+ before(:each) do
+ @params = { :public_body => { :name => 'New Quango',
+ :short_name => 'nq',
+ :request_email => 'newquango@localhost',
+ :tag_string => 'spec',
+ :last_edit_comment => 'From test code',
+ :translations_attributes => {
+ 'es' => { :locale => 'es',
+ :name => 'Los Quango' }
+ } } }
+ end
+
+ it 'saves the body' do
+ # FIXME: Can't call PublicBody.destroy_all because database
+ # database contstraints prevent them being deleted.
+ existing = PublicBody.count
+ expected = existing + 1
+ expect {
+ post :create, @params
+ }.to change{ PublicBody.count }.from(existing).to(expected)
+ end
+
+ it 'saves the default locale translation' do
+ post :create, @params
+
+ body = PublicBody.find_by_name('New Quango')
+
+ I18n.with_locale(:en) do
+ expect(body.name).to eq('New Quango')
+ end
+ end
+
+ it 'saves the alternative locale translation' do
+ post :create, @params
+
+ body = PublicBody.find_by_name('New Quango')
+
+ I18n.with_locale(:es) do
+ expect(body.name).to eq('Los Quango')
+ end
+ end
+
+ end
+
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :create, :public_body => { :name => '',
+ :translations_attributes => {} }
+ expect(response).to render_template('new')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :create, :public_body => { :name => '',
+ :request_email => 'newquango@localhost',
+ :translations_attributes => {} }
+ expect(assigns(:public_body).request_email).to eq('newquango@localhost')
+ end
+
+ end
+
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :public_body => { :name => '',
+ :request_email => 'newquango@localhost',
+ :translations_attributes => {
+ 'es' => { :locale => 'es',
+ :name => 'Los Quango' }
+ } } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :create, @params
+ expect(assigns(:public_body)).to_not be_persisted
+ expect(assigns(:public_body).request_email).to eq('newquango@localhost')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :create, @params
+
+ expect(assigns(:public_body)).to_not be_persisted
+ I18n.with_locale(:es) do
+ expect(assigns(:public_body).name).to eq('Los Quango')
+ end
+ end
+
end
context 'when the body is being created as a result of a change request' do
@@ -146,10 +243,34 @@ end
describe AdminPublicBodyController, "when editing a public body" do
render_views
- it "edits a public body" do
- get :edit, :id => 2
+ before do
+ @body = FactoryGirl.create(:public_body)
+ I18n.with_locale('es') do
+ @body.name = 'Los Body'
+ @body.save!
+ end
+ end
+
+ it 'responds successfully' do
+ get :edit, :id => @body.id
+ expect(response).to be_success
end
+ it 'finds the requested body' do
+ get :edit, :id => @body.id
+ expect(assigns[:public_body]).to eq(@body)
+ end
+
+ it 'builds new translations if the body does not already have a translation in the specified locale' do
+ get :edit, :id => @body.id
+ expect(assigns[:public_body].translations.map(&:locale)).to include(:fr)
+ end
+
+ it 'renders the edit template' do
+ get :edit, :id => @body.id
+ expect(response).to render_template('edit')
+ end
+
it "edits a public body in another locale" do
get :edit, {:id => 3, :locale => :en}
@@ -184,48 +305,202 @@ end
describe AdminPublicBodyController, "when updating a public body" do
render_views
- it "saves edits to a public body" do
- public_bodies(:humpadink_public_body).name.should == "Department for Humpadinking"
- post :update, { :id => 3, :public_body => { :name => "Renamed",
- :short_name => "",
- :tag_string => "some tags",
- :request_email => 'edited@localhost',
- :last_edit_comment => 'From test code' } }
- request.flash[:notice].should include('successful')
- pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
- pb.name.should == "Renamed"
- end
-
- it "saves edits to a public body in another locale" do
- I18n.with_locale(:es) do
- pb = PublicBody.find(id=3)
- pb.name.should == "El Department for Humpadinking"
- post :update, {
- :id => 3,
- :public_body => {
- :name => "Department for Humpadinking",
- :short_name => "",
- :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'}
- }
- }
- }
- request.flash[:notice].should include('successful')
- end
-
- pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
- I18n.with_locale(:es) do
- pb.name.should == "Renamed"
- end
- I18n.with_locale(:en) do
- pb.name.should == "Department for Humpadinking"
+ before do
+ @body = FactoryGirl.create(:public_body)
+ I18n.with_locale('es') do
+ @body.name = 'Los Quango'
+ @body.save!
+ end
+
+ @params = { :id => @body.id,
+ :public_body => { :name => 'Renamed',
+ :short_name => @body.short_name,
+ :request_email => @body.request_email,
+ :tag_string => @body.tag_string,
+ :last_edit_comment => 'From test code',
+ :translations_attributes => {
+ 'es' => { :id => @body.translation_for(:es).id,
+ :locale => 'es',
+ :title => @body.name(:es) }
+ } } }
+ end
+
+ it 'finds the heading to update' do
+ post :update, @params
+ expect(assigns(:heading)).to eq(@heading)
+ end
+
+ context 'on success' do
+
+ it 'saves edits to a public body heading' do
+ post :update, @params
+ body = PublicBody.find(@body.id)
+ expect(body.name).to eq('Renamed')
+ end
+
+ it 'notifies the admin that the body was updated' do
+ post :update, @params
+ expect(flash[:notice]).to eq('PublicBody was successfully updated.')
end
+
+ it 'redirects to the admin body page' do
+ post :update, @params
+ expect(response).to redirect_to(admin_body_path(@body))
+ end
+
+ end
+
+ context 'on success for multiple locales' do
+
+ it 'saves edits to a public body heading in another locale' do
+ @body.name(:es).should == 'Los Quango'
+ post :update, :id => @body.id,
+ :public_body => {
+ :name => @body.name(:en),
+ :translations_attributes => {
+ 'es' => { :id => @body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' }
+ }
+ }
+
+ body = PublicBody.find(@body.id)
+ expect(body.name(:es)).to eq('Renamed')
+ expect(body.name(:en)).to eq(@body.name(:en))
+ end
+
+ it 'adds a new translation' do
+ @body.translation_for(:es).destroy
+ @body.reload
+
+ put :update, {
+ :id => @body.id,
+ :public_body => {
+ :name => @body.name(:en),
+ :translations_attributes => {
+ 'es' => { :locale => "es",
+ :name => "Example Public Body ES" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ body = PublicBody.find(@body.id)
+
+ I18n.with_locale(:es) do
+ expect(body.name).to eq('Example Public Body ES')
+ end
+ end
+
+ it 'adds new translations' do
+ @body.translation_for(:es).destroy
+ @body.reload
+
+ post :update, {
+ :id => @body.id,
+ :public_body => {
+ :name => @body.name(:en),
+ :translations_attributes => {
+ 'es' => { :locale => "es",
+ :name => "Example Public Body ES" },
+ 'fr' => { :locale => "fr",
+ :name => "Example Public Body FR" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ body = PublicBody.find(@body.id)
+
+ I18n.with_locale(:es) do
+ expect(body.name).to eq('Example Public Body ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(body.name).to eq('Example Public Body FR')
+ end
+ end
+
+ it 'updates an existing translation and adds a third translation' do
+ post :update, {
+ :id => @body.id,
+ :public_body => {
+ :name => @body.name(:en),
+ :translations_attributes => {
+ # Update existing translation
+ 'es' => { :id => @body.translation_for(:es).id,
+ :locale => "es",
+ :name => "Renamed Example Public Body ES" },
+ # Add new translation
+ 'fr' => { :locale => "fr",
+ :name => "Example Public Body FR" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ body = PublicBody.find(@body.id)
+
+ I18n.with_locale(:es) do
+ expect(body.name).to eq('Renamed Example Public Body ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(body.name).to eq('Example Public Body FR')
+ end
+ end
+
+ end
+
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :update, :id => @body.id,
+ :public_body => {
+ :name => '',
+ :translations_attributes => {}
+ }
+ expect(response).to render_template('edit')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :update, :id => @body.id,
+ :public_body => {
+ :name => '',
+ :request_email => 'updated@localhost',
+ :translations_attributes => {}
+ }
+ expect(assigns(:public_body).request_email).to eq('updated@localhost')
+ end
+
+ end
+
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :id => @body.id,
+ :public_body => { :name => '',
+ :translations_attributes => {
+ 'es' => { :id => @body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Mi Nuevo Body' }
+ } } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :update, @params
+ expect(assigns(:public_body).name(:en)).to eq('')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :update, @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:public_body).name).to eq('Mi Nuevo Body')
+ end
+ end
+
end
context 'when the body is being updated as a result of a change request' do
diff --git a/spec/controllers/admin_public_body_headings_controller_spec.rb b/spec/controllers/admin_public_body_headings_controller_spec.rb
index afbe0fa30..ccdfdecfb 100644
--- a/spec/controllers/admin_public_body_headings_controller_spec.rb
+++ b/spec/controllers/admin_public_body_headings_controller_spec.rb
@@ -2,161 +2,466 @@ require 'spec_helper'
describe AdminPublicBodyHeadingsController do
- context 'when showing the form for a new public body category' do
- it 'should assign a new public body heading to the view' do
+ describe :new do
+
+ it 'responds successfully' do
get :new
- assigns[:heading].should be_a(PublicBodyHeading)
+ expect(response).to be_success
end
- it 'renders the new template' do
+ it 'builds a new PublicBodyHeading' do
get :new
- expect(response).to render_template('new')
+ expect(assigns(:heading)).to be_new_record
end
+
+ it 'builds new translations for all locales' do
+ get :new
+
+ translations = assigns(:heading).translations.map{ |t| t.locale.to_s }.sort
+ available = I18n.available_locales.map{ |l| l.to_s }.sort
+
+ expect(translations).to eq(available)
+ end
+
+ it 'renders the new template' do
+ get :new
+ expect(response).to render_template('new')
+ end
+
end
- context 'when creating a public body heading' do
- it "creates a new public body heading in one locale" do
- n = PublicBodyHeading.count
- post :create, {
- :public_body_heading => {
- :name => 'New Heading'
- }
- }
- PublicBodyHeading.count.should == n + 1
-
- heading = PublicBodyHeading.find_by_name("New Heading")
- response.should redirect_to(admin_categories_path)
- end
+ describe :create do
- it 'creates a new public body heading with multiple locales' do
- n = PublicBodyHeading.count
- post :create, {
- :public_body_heading => {
- :name => 'New Heading',
- :translated_versions => [{ :locale => "es",
- :name => "Mi Nuevo Heading" }]
- }
- }
- PublicBodyHeading.count.should == n + 1
-
- heading = PublicBodyHeading.find_by_name("New Heading")
- heading.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"]
- I18n.with_locale(:en) do
- heading.name.should == "New Heading"
+ context 'on success' do
+
+ before(:each) do
+ PublicBodyHeading.destroy_all
+ @params = { :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :name => 'New Heading' }
+ } }
end
- I18n.with_locale(:es) do
- heading.name.should == "Mi Nuevo Heading"
+
+ it 'creates a new heading in the default locale' do
+ expect {
+ post :create, :public_body_heading => @params
+ }.to change{ PublicBodyHeading.count }.from(0).to(1)
end
- response.should redirect_to(admin_categories_path)
- end
+ it 'notifies the admin that the heading was created' do
+ post :create, :public_body_heading => @params
+ expect(flash[:notice]).to eq('Heading was successfully created.')
+ end
+
+ it 'redirects to the categories index' do
+ post :create, :public_body_heading => @params
+ expect(response).to redirect_to(admin_categories_path)
+ end
- it "renders the form if creating the record was unsuccessful" do
- post :create, :public_body_heading => { :name => '' }
- expect(response).to render_template('new')
end
- end
+ context 'on success for multiple locales' do
- context 'when editing a public body heading' do
- before do
- @heading = FactoryGirl.create(:public_body_heading)
- end
+ before(:each) do
+ PublicBodyHeading.destroy_all
+ @params = { :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :name => 'New Heading' },
+ 'es' => { :locale => 'es',
+ :name => 'Mi Nuevo Heading' }
+ } }
+ end
- render_views
+ it 'saves the heading' do
+ expect {
+ post :create, :public_body_heading => @params
+ }.to change{ PublicBodyHeading.count }.from(0).to(1)
+ end
- it "finds the requested heading" do
- get :edit, :id => @heading.id
- expect(assigns[:heading]).to eq(@heading)
- end
+ it 'saves the default locale translation' do
+ post :create, :public_body_heading => @params
- it "renders the edit template" do
- get :edit, :id => @heading.id
- expect(assigns[:heading]).to render_template('edit')
- end
- end
+ heading = PublicBodyHeading.find_by_name('New Heading')
+
+ I18n.with_locale(:en) do
+ expect(heading.name).to eq('New Heading')
+ end
+ end
+
+ it 'saves the alternative locale translation' do
+ post :create, :public_body_heading => @params
+
+ heading = PublicBodyHeading.find_by_name('New Heading')
+
+ I18n.with_locale(:es) do
+ expect(heading.name).to eq('Mi Nuevo Heading')
+ end
+ end
- context 'when updating a public body heading' do
- before do
- @heading = FactoryGirl.create(:public_body_heading)
- @name = @heading.name
end
- it "saves edits to a public body heading" do
- post :update, { :id => @heading.id,
- :public_body_heading => { :name => "Renamed" } }
- request.flash[:notice].should include('successful')
- found_heading = PublicBodyHeading.find(@heading.id)
- found_heading.name.should == "Renamed"
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :create, :public_body_heading => { :name => '' }
+ expect(response).to render_template('new')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :create, :public_body_heading => { :name => 'Need a description' }
+ expect(assigns(:heading).name).to eq('Need a description')
+ end
+
end
- it "saves edits to a public body heading in another locale" do
- I18n.with_locale(:es) do
- post :update, {
- :id => @heading.id,
- :public_body_heading => {
- :name => @name,
- :translated_versions => {
- @heading.id => {:locale => "es",
- :name => "Renamed"}
- }
- }
- }
- request.flash[:notice].should include('successful')
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :translations_attributes => {
+ 'en' => { :locale => 'en',
+ :name => 'Need a description' },
+ 'es' => { :locale => 'es',
+ :name => 'Mi Nuevo Heading' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :create, :public_body_heading => @params
+ expect(assigns(:heading).name).to eq('Need a description')
end
- heading = PublicBodyHeading.find(@heading.id)
- I18n.with_locale(:es) do
- heading.name.should == "Renamed"
+ it 'is rebuilt with the alternative locale translation' do
+ post :create, :public_body_heading => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:heading).name).to eq('Mi Nuevo Heading')
+ end
end
- I18n.with_locale(:en) do
- heading.name.should == @name
+
+ end
+
+ end
+
+ describe :edit do
+
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ I18n.with_locale('es') do
+ @heading.name = 'Los heading'
+ @heading.save!
end
end
- it "redirects to the edit page after a successful update" do
- post :update, { :id => @heading.id,
- :public_body_heading => { :name => "Renamed" } }
+ it 'responds successfully' do
+ get :edit, :id => @heading.id
+ expect(response).to be_success
+ end
- expect(response).to redirect_to(edit_admin_heading_path(@heading))
+ it 'finds the requested heading' do
+ get :edit, :id => @heading.id
+ expect(assigns[:heading]).to eq(@heading)
end
- it "re-renders the edit form after an unsuccessful update" do
- post :update, { :id => @heading.id,
- :public_body_heading => { :name => '' } }
+ it 'builds new translations if the body does not already have a translation in the specified locale' do
+ get :edit, :id => @heading.id
+ expect(assigns[:heading].translations.map(&:locale)).to include(:fr)
+ end
+ it 'renders the edit template' do
+ get :edit, :id => @heading.id
expect(response).to render_template('edit')
end
end
- context 'when destroying a public body heading' do
+ describe :update do
+
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ I18n.with_locale('es') do
+ @heading.name = 'Los heading'
+ @heading.save!
+ end
+ @params = { :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) },
+ 'es' => { :id => @heading.translation_for(:es).id,
+ :locale => 'es',
+ :title => @heading.name(:es) }
+ } }
+ end
+
+ it 'finds the heading to update' do
+ post :update, :id => @heading.id,
+ :public_body_category => @params
+ expect(assigns(:heading)).to eq(@heading)
+ end
+
+ context 'on success' do
+
+ before(:each) do
+ @params = { :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :name => 'Renamed' }
+ }
+ }
+ }
+ end
+
+ it 'saves edits to a public body heading' do
+ post :update, @params
+ heading = PublicBodyHeading.find(@heading.id)
+ expect(heading.name).to eq('Renamed')
+ end
+
+ it 'notifies the admin that the heading was updated' do
+ post :update, @params
+ expect(flash[:notice]).to eq('Heading was successfully updated.')
+ end
+
+ it 'redirects to the heading edit page' do
+ post :update, @params
+ expect(response).to redirect_to(edit_admin_heading_path(@heading))
+ end
+
+ end
+
+ context 'on success for multiple locales' do
+
+ it 'saves edits to a public body heading in another locale' do
+ @heading.name(:es).should == 'Los heading'
+ post :update, :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) },
+ 'es' => { :id => @heading.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' }
+ }
+ }
+
+ heading = PublicBodyHeading.find(@heading.id)
+ expect(heading.name(:es)).to eq('Renamed')
+ expect(heading.name(:en)).to eq(@heading.name(:en))
+ end
+
+ it 'adds a new translation' do
+ @heading.translation_for(:es).destroy
+ @heading.reload
+
+ put :update, {
+ :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) },
+ 'es' => { :locale => "es",
+ :name => "Example Public Body Heading ES" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ heading = PublicBodyHeading.find(@heading.id)
+
+ I18n.with_locale(:es) do
+ expect(heading.name).to eq('Example Public Body Heading ES')
+ end
+ end
+
+ it 'adds new translations' do
+ @heading.translation_for(:es).destroy
+ @heading.reload
+
+ post :update, {
+ :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) },
+ 'es' => { :locale => "es",
+ :name => "Example Public Body Heading ES" },
+ 'fr' => { :locale => "fr",
+ :name => "Example Public Body Heading FR" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ heading = PublicBodyHeading.find(@heading.id)
+
+ I18n.with_locale(:es) do
+ expect(heading.name).to eq('Example Public Body Heading ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(heading.name).to eq('Example Public Body Heading FR')
+ end
+ end
+
+ it 'updates an existing translation and adds a third translation' do
+ post :update, {
+ :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) },
+ # Update existing translation
+ 'es' => { :id => @heading.translation_for(:es).id,
+ :locale => "es",
+ :name => "Renamed Example Public Body Heading ES" },
+ # Add new translation
+ 'fr' => { :locale => "fr",
+ :name => "Example Public Body Heading FR" }
+ }
+ }
+ }
+
+ request.flash[:notice].should include('successful')
+
+ heading = PublicBodyHeading.find(@heading.id)
+
+ I18n.with_locale(:es) do
+ expect(heading.name).to eq('Renamed Example Public Body Heading ES')
+ end
+ I18n.with_locale(:fr) do
+ expect(heading.name).to eq('Example Public Body Heading FR')
+ end
+ end
+
+ it "redirects to the edit page after a successful update" do
+ post :update, :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => @heading.name(:en) }
+ } }
+
+ expect(response).to redirect_to(edit_admin_heading_path(@heading))
+ end
+
+ end
+
+ context 'on failure' do
+
+ it 'renders the form if creating the record was unsuccessful' do
+ post :update, :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => '' }
+ } }
+ expect(response).to render_template('edit')
+ end
+
+ it 'is rebuilt with the given params' do
+ post :update, :id => @heading.id,
+ :public_body_heading => {
+ :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => 'Need a description' }
+ } }
+ expect(assigns(:heading).name).to eq('Need a description')
+ end
+
+ end
+
+ context 'on failure for multiple locales' do
+
+ before(:each) do
+ @params = { :translations_attributes => {
+ 'en' => { :id => @heading.translation_for(:en).id,
+ :locale => 'en',
+ :name => '' },
+ 'es' => { :id => @heading.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Mi Nuevo Heading' }
+ } }
+ end
+
+ it 'is rebuilt with the default locale translation' do
+ post :update, :id => @heading.id,
+ :public_body_heading => @params
+ expect(assigns(:heading).name(:en)).to eq('')
+ end
+
+ it 'is rebuilt with the alternative locale translation' do
+ post :update, :id => @heading.id,
+ :public_body_heading => @params
+
+ I18n.with_locale(:es) do
+ expect(assigns(:heading).name).to eq('Mi Nuevo Heading')
+ end
+ end
+
+ end
+
+ end
+
+ describe :destroy do
+
+ it 'uses the current locale by default' do
+ heading = FactoryGirl.create(:public_body_heading)
+ post :destroy, :id => heading.id
+ expect(assigns(:locale)).to eq(I18n.locale.to_s)
+ end
- before do
- @heading = FactoryGirl.create(:public_body_heading)
+ it 'sets the locale if the show_locale param is passed' do
+ heading = FactoryGirl.create(:public_body_heading)
+ post :destroy, :id => heading.id, :show_locale => 'es'
+ expect(assigns(:locale)).to eq('es')
end
- it "destroys a public body heading that has associated categories" do
+ it 'destroys the public body heading' do
+ PublicBodyHeading.destroy_all
+
+ heading = FactoryGirl.create(:public_body_heading)
+
+ expect{
+ post :destroy, :id => heading.id
+ }.to change{ PublicBodyHeading.count }.from(1).to(0)
+ end
+
+ it 'destroys a heading that has associated categories' do
+ PublicBodyHeading.destroy_all
+ PublicBodyCategory.destroy_all
+
+ heading = FactoryGirl.create(:public_body_heading)
category = FactoryGirl.create(:public_body_category)
link = FactoryGirl.create(:public_body_category_link,
:public_body_category => category,
- :public_body_heading => @heading,
+ :public_body_heading => heading,
:category_display_order => 0)
- n = PublicBodyHeading.count
- n_links = PublicBodyCategoryLink.count
- post :destroy, { :id => @heading.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyHeading.count.should == n - 1
- PublicBodyCategoryLink.count.should == n_links - 1
+ expect{
+ post :destroy, :id => heading.id
+ }.to change{ PublicBodyHeading.count }.from(1).to(0)
+ end
+
+ it 'notifies the admin that the heading was destroyed' do
+ heading = FactoryGirl.create(:public_body_heading)
+ post :destroy, :id => heading.id
+ expect(flash[:notice]).to eq('Heading was successfully destroyed.')
end
- it "destroys an empty public body heading" do
- n = PublicBodyHeading.count
- post :destroy, { :id => @heading.id }
- response.should redirect_to(admin_categories_path)
- PublicBodyHeading.count.should == n - 1
+ it 'redirects to the categories index' do
+ heading = FactoryGirl.create(:public_body_heading)
+ post :destroy, :id => heading.id
+ expect(response).to redirect_to(admin_categories_path)
end
+
end
context 'when reordering public body headings' 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/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/integration/admin_public_body_category_edit_spec.rb b/spec/integration/admin_public_body_category_edit_spec.rb
new file mode 100644
index 000000000..043524189
--- /dev/null
+++ b/spec/integration/admin_public_body_category_edit_spec.rb
@@ -0,0 +1,59 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')
+
+describe 'Editing a Public Body Category' do
+ before do
+ AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false)
+
+ confirm(:admin_user)
+ @admin = login(:admin_user)
+ @category = FactoryGirl.create(:public_body_category)
+ end
+
+ it 'can edit the default locale' do
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_title__en', :with => 'New Category EN'
+ @admin.click_button 'Save'
+
+ @category.reload
+ expect(@category.title).to eq('New Category EN')
+ end
+
+ it 'can add a translation for a single locale' do
+ expect(@category.find_translation_by_locale('fr')).to be_nil
+
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_fr_title__fr', :with => 'New Category FR'
+ @admin.fill_in 'public_body_category_translations_attributes_fr_description__fr', :with => 'FR Description'
+ @admin.click_button 'Save'
+
+ @category.reload
+ I18n.with_locale(:fr) do
+ expect(@category.title).to eq('New Category FR')
+ end
+ end
+
+ it 'can add a translation for multiple locales' do
+ # Add FR translation
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_fr_title__fr', :with => 'New Category FR'
+ @admin.fill_in 'public_body_category_translations_attributes_fr_description__fr', :with => 'FR Description'
+ @admin.click_button 'Save'
+
+ # Add ES translation
+ @admin.visit edit_admin_category_path(@category)
+ @admin.fill_in 'public_body_category_translations_attributes_es_title__es', :with => 'New Category ES'
+ @admin.fill_in 'public_body_category_translations_attributes_es_description__es', :with => 'ES Description'
+ @admin.click_button 'Save'
+
+ @category.reload
+ I18n.with_locale(:fr) do
+ expect(@category.title).to eq('New Category FR')
+ end
+
+ I18n.with_locale(:es) do
+ expect(@category.title).to eq('New Category ES')
+ 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/admin_public_body_heading_edit_spec.rb b/spec/integration/admin_public_body_heading_edit_spec.rb
new file mode 100644
index 000000000..6c7a5a74b
--- /dev/null
+++ b/spec/integration/admin_public_body_heading_edit_spec.rb
@@ -0,0 +1,58 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')
+
+describe 'Editing a Public Body Heading' do
+ before do
+ AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false)
+
+ confirm(:admin_user)
+ @admin = login(:admin_user)
+ @heading = FactoryGirl.create(:public_body_heading)
+ end
+
+ it 'can edit the default locale' do
+ @admin.visit edit_admin_heading_path(@heading)
+ @admin.fill_in 'public_body_heading_name__en', :with => 'New Heading EN'
+ @admin.click_button 'Save'
+
+ @heading.reload
+ expect(@heading.name).to eq('New Heading EN')
+ end
+
+ it 'can add a translation for a single locale' do
+ expect(@heading.find_translation_by_locale('fr')).to be_nil
+
+ @admin.visit edit_admin_heading_path(@heading)
+ @admin.fill_in 'public_body_heading_translations_attributes_fr_name__fr', :with => 'New Heading FR'
+ @admin.click_button 'Save'
+
+ @heading.reload
+ I18n.with_locale(:fr) do
+ expect(@heading.name).to eq('New Heading FR')
+ end
+ end
+
+ it 'can add a translation for multiple locales' do
+ # Add FR translation
+ expect(@heading.find_translation_by_locale('fr')).to be_nil
+ @admin.visit edit_admin_heading_path(@heading)
+ @admin.fill_in 'public_body_heading_translations_attributes_fr_name__fr', :with => 'New Heading FR'
+ @admin.click_button 'Save'
+
+ # Add ES translation
+ expect(@heading.find_translation_by_locale('es')).to be_nil
+ @admin.visit edit_admin_heading_path(@heading)
+ @admin.fill_in 'public_body_heading_translations_attributes_es_name__es', :with => 'New Heading ES'
+ @admin.click_button 'Save'
+
+ @heading.reload
+ I18n.with_locale(:fr) do
+ expect(@heading.name).to eq('New Heading FR')
+ end
+
+ I18n.with_locale(:es) do
+ expect(@heading.name).to eq('New Heading 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/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..297bd096a 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
@@ -63,5 +34,145 @@ describe PublicBodyCategory do
category.should_not be_valid
category.errors[:description].should == ["Description can't be blank"]
end
+
+ it 'validates the translations' do
+ category = FactoryGirl.build(:public_body_category)
+ translation = category.translations.build
+ expect(category).to_not be_valid
+ end
+
+ it 'uses the base model validation for the default locale' do
+ category = PublicBodyCategory.new
+ translation = category.translations.build(:locale => 'en',
+ :description => 'No title')
+ category.valid?
+ translation.valid?
+
+ expect(category).to have(1).error_on(:title)
+ expect(translation).to have(0).errors_on(:title)
+ end
+
+ end
+
+ describe :save do
+
+ it 'saves translations' do
+ category = FactoryGirl.build(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+
+ category.save
+ expect(PublicBodyCategory.find(category.id).translations.size).to eq(2)
+ end
+
end
+
+ describe :translations_attributes= do
+
+ context 'translation_attrs is a Hash' do
+
+ it 'does not persist translations' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+
+ expect(PublicBodyCategory.find(category.id).translations.size).to eq(1)
+ end
+
+ it 'creates a new translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { :es => { :locale => 'es',
+ :title => 'El Category',
+ :description => 'Spanish description' } }
+ category.save
+ category.reload
+ expect(category.title(:es)).to eq('El Category')
+ end
+
+ it 'updates an existing translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations_attributes = { 'es' => { :locale => 'es',
+ :title => 'Name',
+ :description => 'Desc' } }
+ category.save
+
+ category.translations_attributes = { 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed',
+ :description => 'Desc' } }
+ category.save
+ expect(category.title(:es)).to eq('Renamed')
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations.create(:locale => 'es',
+ :title => 'Los Category',
+ :description => 'ES Description')
+
+ expect(category.translations.size).to eq(2)
+
+ category.translations_attributes = {
+ 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :title => 'Le Category' }
+ }
+
+ expect(category.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(category.title).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(category.title).to eq('Le Category') }
+ end
+
+ it 'skips empty translations' do
+ category = FactoryGirl.create(:public_body_category)
+ category.translations.create(:locale => 'es',
+ :title => 'Los Category',
+ :description => 'ES Description')
+
+ expect(category.translations.size).to eq(2)
+
+ category.translations_attributes = {
+ 'es' => { :id => category.translation_for(:es).id,
+ :locale => 'es',
+ :title => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(category.translations.size).to eq(2)
+ end
+
+ end
+ end
+
+end
+
+describe PublicBodyCategory::Translation do
+
+ it 'requires a locale' do
+ translation = PublicBodyCategory::Translation.new
+ translation.valid?
+ expect(translation.errors[:locale]).to eq(["can't be blank"])
+ end
+
+ it 'is valid if no required attributes are assigned' do
+ translation = PublicBodyCategory::Translation.new(:locale => I18n.default_locale)
+ expect(translation).to be_valid
+ end
+
+ it 'requires a title if another required attribute is assigned' do
+ translation = PublicBodyCategory::Translation.new(:description => 'spec')
+ translation.valid?
+ expect(translation.errors[:title]).to eq(["Title can't be blank"])
+ end
+
+ it 'requires a description if another required attribute is assigned' do
+ translation = PublicBodyCategory::Translation.new(:title => 'spec')
+ translation.valid?
+ expect(translation.errors[:description]).to eq(["Description can't be blank"])
+ end
+
end
diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb
index 9372e0a07..be3e7c7d2 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
@@ -50,6 +30,13 @@ describe PublicBodyHeading do
heading.valid?
heading.display_order.should == PublicBodyHeading.next_display_order
end
+
+ it 'validates the translations' do
+ heading = FactoryGirl.build(:public_body_heading)
+ translation = heading.translations.build
+ expect(heading).to_not be_valid
+ end
+
end
context 'when setting a display order' do
@@ -63,4 +50,105 @@ describe PublicBodyHeading do
PublicBodyHeading.next_display_order.should == 1
end
end
+
+ describe :save do
+
+ it 'saves translations' do
+ heading = FactoryGirl.build(:public_body_heading)
+ heading.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Heading' } }
+
+ heading.save
+ expect(PublicBodyHeading.find(heading.id).translations.size).to eq(2)
+ end
+
+ end
+
+ describe :translations_attributes= do
+
+ context 'translation_attrs is a Hash' do
+
+ it 'does not persist translations' do
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Heading' } }
+
+ expect(PublicBodyHeading.find(heading.id).translations.size).to eq(1)
+ end
+
+ it 'creates a new translation' do
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Heading' } }
+ heading.save
+ heading.reload
+ expect(heading.name(:es)).to eq('El Heading')
+ end
+
+ it 'updates an existing translation' do
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.translations_attributes = { 'es' => { :locale => 'es',
+ :name => 'Name' } }
+ heading.save
+
+ heading.translations_attributes = { 'es' => { :id => heading.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' } }
+ heading.save
+ expect(heading.name(:es)).to eq('Renamed')
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.translations.create(:locale => 'es',
+ :name => 'Los Heading')
+
+ expect(heading.translations.size).to eq(2)
+
+ heading.translations_attributes = {
+ 'es' => { :id => heading.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :name => 'Le Heading' }
+ }
+
+ expect(heading.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(heading.name).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(heading.name).to eq('Le Heading') }
+ end
+
+ it 'skips empty translations' do
+ heading = FactoryGirl.create(:public_body_heading)
+ heading.translations.create(:locale => 'es',
+ :name => 'Los Heading')
+
+ expect(heading.translations.size).to eq(2)
+
+ heading.translations_attributes = {
+ 'es' => { :id => heading.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(heading.translations.size).to eq(2)
+ end
+ end
+ end
+end
+
+describe PublicBodyHeading::Translation do
+
+ it 'requires a locale' do
+ translation = PublicBodyHeading::Translation.new
+ translation.valid?
+ expect(translation.errors[:locale]).to eq(["can't be blank"])
+ end
+
+ it 'is valid if all required attributes are assigned' do
+ translation = PublicBodyHeading::Translation.new(:locale => I18n.default_locale)
+ expect(translation).to be_valid
+ end
+
end
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index 225958cac..7b55efda1 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -28,6 +28,83 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+describe PublicBody do
+
+ describe :translations_attributes= do
+
+ context 'translation_attrs is a Hash' do
+
+ it 'does not persist translations' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Body' } }
+
+ expect(PublicBody.find(body.id).translations.size).to eq(1)
+ end
+
+ it 'creates a new translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Body' } }
+ body.save
+ body.reload
+ expect(body.name(:es)).to eq('El Body')
+ end
+
+ it 'updates an existing translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { 'es' => { :locale => 'es',
+ :name => 'El Body' } }
+ body.save
+
+ body.translations_attributes = { 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' } }
+ body.save
+ expect(body.name(:es)).to eq('Renamed')
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations.create(:locale => 'es',
+ :name => 'El Body')
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :name => 'Le Body' }
+ }
+
+ 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 Body') }
+ end
+
+ it 'skips empty translations' do
+ body = FactoryGirl.create(:public_body)
+ body.translations.create(:locale => 'es',
+ :name => 'El Body')
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(body.translations.size).to eq(2)
+ end
+ end
+ end
+end
+
+
describe PublicBody, " using tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
@@ -446,7 +523,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
@@ -465,15 +542,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
@@ -598,6 +944,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
@@ -660,6 +1022,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
@@ -752,3 +1161,95 @@ 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
+
+describe PublicBody::Translation do
+
+ it 'requires a locale' do
+ translation = PublicBody::Translation.new
+ translation.valid?
+ expect(translation.errors[:locale]).to eq(["can't be blank"])
+ end
+
+ it 'is valid if all required attributes are assigned' do
+ translation = PublicBody::Translation.new(:locale => I18n.default_locale)
+ expect(translation).to be_valid
+ 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',