aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/admin_public_body_categories_controller_spec.rb192
-rw-r--r--spec/controllers/admin_public_body_headings_controller_spec.rb240
-rw-r--r--spec/controllers/admin_spam_addresses_controller_spec.rb4
-rw-r--r--spec/controllers/health_checks_controller_spec.rb30
-rw-r--r--spec/controllers/public_body_controller_spec.rb22
-rw-r--r--spec/controllers/request_controller_spec.rb20
-rw-r--r--spec/controllers/user_controller_spec.rb12
-rw-r--r--spec/factories/outgoing_messages.rb19
-rw-r--r--spec/factories/public_body_categories.rb8
-rw-r--r--spec/factories/public_body_category_links.rb6
-rw-r--r--spec/factories/public_body_headings.rb5
-rw-r--r--spec/fixtures/info_request_events.yml4
-rw-r--r--spec/helpers/health_checks_helper_spec.rb15
-rw-r--r--spec/integration/localisation_spec.rb29
-rw-r--r--spec/lib/health_checks/checks/days_ago_check_spec.rb66
-rw-r--r--spec/lib/health_checks/health_checkable_spec.rb128
-rw-r--r--spec/lib/health_checks/health_checks_spec.rb77
-rw-r--r--spec/lib/public_body_categories_spec.rb42
-rw-r--r--spec/models/censor_rule_spec.rb33
-rw-r--r--spec/models/change_email_validator_spec.rb124
-rw-r--r--spec/models/info_request_spec.rb11
-rw-r--r--spec/models/public_body_category/category_collection_spec.rb81
-rw-r--r--spec/models/public_body_category_link_spec.rb53
-rw-r--r--spec/models/public_body_category_spec.rb65
-rw-r--r--spec/models/public_body_heading_spec.rb68
-rw-r--r--spec/models/public_body_spec.rb52
-rw-r--r--spec/spec_helper.rb8
27 files changed, 1333 insertions, 81 deletions
diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb
new file mode 100644
index 000000000..35454990d
--- /dev/null
+++ b/spec/controllers/admin_public_body_categories_controller_spec.rb
@@ -0,0 +1,192 @@
+require 'spec_helper'
+
+describe AdminPublicBodyCategoriesController do
+ context 'when showing the index of categories and headings' do
+ render_views
+
+ it 'shows the index page' do
+ get :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
+ get :new
+ assigns[:category].should be_a(PublicBodyCategory)
+ 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
+
+ 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]
+ 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
+
+ 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"
+ end
+ I18n.with_locale(:es) do
+ category.title.should == "Mi Nuevo Category"
+ end
+
+ response.should redirect_to(admin_categories_path)
+ end
+ end
+
+ context 'when editing a public body category' do
+ before do
+ @category = FactoryGirl.create(:public_body_category)
+ I18n.with_locale('es') do
+ @category.title = 'Los category'
+ @category.save!
+ end
+ end
+
+ render_views
+
+ it "edits a public body category" do
+ get :edit, :id => @category.id
+ end
+
+ it "edits a public body in another locale" do
+ get :edit, {:id => @category.id, :locale => :en}
+
+ # 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')
+ end
+ end
+
+ context 'when updating a public body category' do
+
+ before do
+ @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,
+ :category_display_order => 0)
+ @tag = @category.category_tag
+ I18n.with_locale('es') do
+ @category.title = 'Los category'
+ @category.save!
+ end
+ end
+
+ render_views
+
+ 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"
+ end
+
+ it "saves edits to a public body category's heading associations" do
+ @category.public_body_headings.should == [@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"}
+ }
+ }
+ }
+ request.flash[:notice].should include('successful')
+ end
+
+ pbc = PublicBodyCategory.find(@category.id)
+ I18n.with_locale(:es) do
+ pbc.title.should == "Renamed"
+ end
+ I18n.with_locale(:en) do
+ pbc.title.should == "Category"
+ 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" } }
+ request.flash[:notice].should include('can\'t')
+ pbc = PublicBodyCategory.find(@category.id)
+ pbc.category_tag.should == @tag
+ 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
+ end
+
+ context 'when destroying a public body category' do
+
+ it "destroys a public body category" 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
+ end
+ end
+
+
+end
diff --git a/spec/controllers/admin_public_body_headings_controller_spec.rb b/spec/controllers/admin_public_body_headings_controller_spec.rb
new file mode 100644
index 000000000..31517d238
--- /dev/null
+++ b/spec/controllers/admin_public_body_headings_controller_spec.rb
@@ -0,0 +1,240 @@
+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
+ get :new
+ assigns[:heading].should be_a(PublicBodyHeading)
+ 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
+
+ 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"
+ end
+ I18n.with_locale(:es) do
+ heading.name.should == "Mi Nuevo Heading"
+ end
+
+ response.should redirect_to(admin_categories_path)
+ end
+ end
+
+ context 'when editing a public body heading' do
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ end
+
+ render_views
+
+ it "edits a public body heading" do
+ get :edit, :id => @heading.id
+ 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"
+ 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')
+ end
+
+ heading = PublicBodyHeading.find(@heading.id)
+ I18n.with_locale(:es) do
+ heading.name.should == "Renamed"
+ end
+ I18n.with_locale(:en) do
+ heading.name.should == @name
+ end
+ end
+ end
+
+ context 'when destroying a public body heading' do
+
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ end
+
+ it "does not destroy a public body heading that has associated categories" do
+ category = FactoryGirl.create(:public_body_category)
+ link = FactoryGirl.create(:public_body_category_link,
+ :public_body_category => category,
+ :public_body_heading => @heading,
+ :category_display_order => 0)
+ n = PublicBodyHeading.count
+ post :destroy, { :id => @heading.id }
+ response.should redirect_to(edit_admin_heading_path(@heading))
+ PublicBodyHeading.count.should == n
+ 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
+ end
+ end
+
+ context 'when reordering public body headings' do
+
+ render_views
+
+ before do
+ @first = FactoryGirl.create(:public_body_heading, :display_order => 0)
+ @second = FactoryGirl.create(:public_body_heading, :display_order => 1)
+ @default_params = { :headings => [@second.id, @first.id] }
+ end
+
+ def make_request(params=@default_params)
+ post :reorder, params
+ end
+
+ context 'when handling valid input' do
+
+ it 'should reorder headings according to their position in the submitted params' do
+ make_request
+ PublicBodyHeading.find(@second.id).display_order.should == 0
+ PublicBodyHeading.find(@first.id).display_order.should == 1
+ end
+
+ it 'should return a "success" status' do
+ make_request
+ response.should be_success
+ end
+ end
+
+ context 'when handling invalid input' do
+
+ before do
+ @params = { :headings => [@second.id, @first.id, @second.id + 1]}
+ end
+
+ it 'should return an "unprocessable entity" status and an error message' do
+ make_request(@params)
+ assert_response :unprocessable_entity
+ response.body.should match("Couldn't find PublicBodyHeading with id")
+ end
+
+ it 'should not reorder headings' do
+ make_request(@params)
+ PublicBodyHeading.find(@first.id).display_order.should == 0
+ PublicBodyHeading.find(@second.id).display_order.should == 1
+ end
+
+ end
+ end
+
+ context 'when reordering public body categories' do
+
+ render_views
+
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ @first_category = FactoryGirl.create(:public_body_category)
+ @first_link = FactoryGirl.create(:public_body_category_link,
+ :public_body_category => @first_category,
+ :public_body_heading => @heading,
+ :category_display_order => 0)
+ @second_category = FactoryGirl.create(:public_body_category)
+ @second_link = FactoryGirl.create(:public_body_category_link,
+ :public_body_category => @second_category,
+ :public_body_heading => @heading,
+ :category_display_order => 1)
+ @default_params = { :categories => [@second_category.id, @first_category.id],
+ :id => @heading }
+ @old_order = [@first_category, @second_category]
+ @new_order = [@second_category, @first_category]
+ end
+
+ def make_request(params=@default_params)
+ post :reorder_categories, params
+ end
+
+ context 'when handling valid input' do
+
+ it 'should reorder categories for the heading according to their position \
+ in the submitted params' do
+
+ @heading.public_body_categories.should == @old_order
+ make_request
+ @heading.public_body_categories(reload=true).should == @new_order
+ end
+
+ it 'should return a success status' do
+ make_request
+ response.should be_success
+ end
+ end
+
+ context 'when handling invalid input' do
+
+ before do
+ @new_category = FactoryGirl.create(:public_body_category)
+ @params = @default_params.merge(:categories => [@second_category.id,
+ @first_category.id,
+ @new_category.id])
+ end
+
+ it 'should return an "unprocessable entity" status and an error message' do
+ make_request(@params)
+ assert_response :unprocessable_entity
+ response.body.should match("Couldn't find PublicBodyCategoryLink")
+ end
+
+ it 'should not reorder the categories for the heading' do
+ make_request(@params)
+ @heading.public_body_categories(reload=true).should == @old_order
+ end
+ end
+
+ end
+end
diff --git a/spec/controllers/admin_spam_addresses_controller_spec.rb b/spec/controllers/admin_spam_addresses_controller_spec.rb
index da1e9bb5a..a1e434159 100644
--- a/spec/controllers/admin_spam_addresses_controller_spec.rb
+++ b/spec/controllers/admin_spam_addresses_controller_spec.rb
@@ -37,7 +37,7 @@ describe AdminSpamAddressesController do
it 'redirects to the index action if successful' do
SpamAddress.any_instance.stub(:save).and_return(true)
post :create, :spam_address => spam_params
- expect(response).to redirect_to(spam_addresses_path)
+ expect(response).to redirect_to(admin_spam_addresses_path)
end
it 'notifies the admin the spam address has been created' do
@@ -83,7 +83,7 @@ describe AdminSpamAddressesController do
end
it 'redirects to the index action' do
- expect(response).to redirect_to(spam_addresses_path)
+ expect(response).to redirect_to(admin_spam_addresses_path)
end
end
diff --git a/spec/controllers/health_checks_controller_spec.rb b/spec/controllers/health_checks_controller_spec.rb
new file mode 100644
index 000000000..f7ad6d6a4
--- /dev/null
+++ b/spec/controllers/health_checks_controller_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe HealthChecksController do
+
+ describe :index do
+
+ describe :index do
+
+ it 'returns a 200 if all health checks pass' do
+ HealthChecks.stub(:ok? => true)
+ get :index
+ expect(response.status).to eq(200)
+ end
+
+ it 'returns a 500 if the health check fails' do
+ HealthChecks.stub(:ok? => false)
+ get :index
+ expect(response.status).to eq(500)
+ end
+
+ it 'does not render a layout' do
+ get :index
+ expect(response).to render_template(:layout => false)
+ end
+
+ end
+
+ end
+
+end
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index f64975580..fc7143522 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -7,6 +7,7 @@ describe PublicBodyController, "when showing a body" do
render_views
before(:each) do
+ PublicBodyCategory.stub!(:load_categories)
load_raw_emails_data
get_fixtures_xapian_index
end
@@ -75,6 +76,10 @@ end
describe PublicBodyController, "when listing bodies" do
render_views
+ before(:each) do
+ PublicBodyCategory.stub!(:load_categories)
+ end
+
it "should be successful" do
get :list
response.should be_success
@@ -204,16 +209,19 @@ describe PublicBodyController, "when listing bodies" do
end
end
- it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do
- load_test_categories
-
- public_bodies(:humpadink_public_body).tag_string = "foo local_council"
+ it "should list a tagged thing on the appropriate list page, and others on the other page,
+ and all still on the all page" do
+ category = FactoryGirl.create(:public_body_category)
+ heading = FactoryGirl.create(:public_body_heading)
+ PublicBodyCategoryLink.create(:public_body_heading_id => heading.id,
+ :public_body_category_id => category.id)
+ public_bodies(:humpadink_public_body).tag_string = category.category_tag
- get :list, :tag => "local_council"
+ get :list, :tag => category.category_tag
response.should render_template('list')
assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ]
- assigns[:tag].should == "local_council"
- assigns[:description].should == "in the category ‘Local councils’"
+ assigns[:tag].should == category.category_tag
+ assigns[:description].should == "in the category ‘#{category.title}’"
get :list, :tag => "other"
response.should render_template('list')
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index f7c935af3..6c0f4573e 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -1827,7 +1827,15 @@ describe RequestController, "when sending a followup message" do
# make the followup
session[:user_id] = users(:bob_smith_user).id
- post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1
+
+ post :show_response,
+ :outgoing_message => {
+ :body => "What a useless response! You suck.",
+ :what_doing => 'normal_sort'
+ },
+ :id => info_requests(:fancy_dog_request).id,
+ :incoming_message_id => incoming_messages(:useless_incoming_message),
+ :submitted_followup => 1
# check it worked
deliveries = ActionMailer::Base.deliveries
@@ -1982,7 +1990,15 @@ describe RequestController, "sending overdue request alerts" do
:info_request_id => chicken_request.id,
:body => 'Some text',
:what_doing => 'normal_sort')
- outgoing_message.send_message
+
+ outgoing_message.sendable?
+ mail_message = OutgoingMailer.followup(
+ outgoing_message.info_request,
+ outgoing_message,
+ outgoing_message.incoming_message_followup
+ ).deliver
+ outgoing_message.record_email_delivery(mail_message.to_addrs.join(', '), mail_message.message_id)
+
outgoing_message.save!
chicken_request = InfoRequest.find(chicken_request.id)
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb
index e4854fe6b..413d395c5 100644
--- a/spec/controllers/user_controller_spec.rb
+++ b/spec/controllers/user_controller_spec.rb
@@ -21,7 +21,8 @@ describe UserController, "when redirecting a show request to a canonical url" do
it 'should not redirect a long canonical name that has a numerical suffix' do
User.stub!(:find).with(:first, anything()).and_return(mock_model(User,
:url_name => 'bob_smithbob_smithbob_smithbob_s_2',
- :name => 'Bob Smith Bob Smith Bob Smith Bob Smith'))
+ :name => 'Bob Smith Bob Smith Bob Smith Bob Smith',
+ :info_requests => []))
User.stub!(:find).with(:all, anything()).and_return([])
get :show, :url_name => 'bob_smithbob_smithbob_smithbob_s_2'
response.should be_success
@@ -107,6 +108,15 @@ describe UserController, "when showing a user" do
]
end
+ it 'filters by the given request status' do
+ get :show, :url_name => 'bob_smith',
+ :user_query => 'money',
+ :request_latest_status => 'waiting_response'
+ assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ [
+ info_requests(:naughty_chicken_request)
+ ]
+ end
+
it "should not show unconfirmed users" do
begin
get :show, :url_name => "unconfirmed_user"
diff --git a/spec/factories/outgoing_messages.rb b/spec/factories/outgoing_messages.rb
index d1ed25093..e11cbdfb9 100644
--- a/spec/factories/outgoing_messages.rb
+++ b/spec/factories/outgoing_messages.rb
@@ -1,6 +1,8 @@
FactoryGirl.define do
factory :outgoing_message do
+ info_request
+
factory :initial_request do
ignore do
status 'ready'
@@ -8,7 +10,9 @@ FactoryGirl.define do
body 'Some information please'
what_doing 'normal_sort'
end
+
end
+
factory :internal_review_request do
ignore do
status 'ready'
@@ -16,14 +20,27 @@ FactoryGirl.define do
body 'I want a review'
what_doing 'internal_review'
end
+
end
+
+ # FIXME: This here because OutgoingMessage has an after_initialize,
+ # which seems to call everything in the app! FactoryGirl calls new with
+ # no parameters and then uses the assignment operator of each attribute
+ # to update it. Because after_initialize executes before assigning the
+ # attributes, loads of stuff fails because whatever after_initialize is
+ # doing expects some of the attributes to be there.
initialize_with { OutgoingMessage.new({ :status => status,
:message_type => message_type,
:body => body,
:what_doing => what_doing }) }
+
after_create do |outgoing_message|
- outgoing_message.send_message
+ outgoing_message.sendable?
+ outgoing_message.record_email_delivery(
+ 'test@example.com',
+ 'ogm-14+537f69734b97c-1ebd@localhost')
end
+
end
end
diff --git a/spec/factories/public_body_categories.rb b/spec/factories/public_body_categories.rb
new file mode 100644
index 000000000..baa474c6b
--- /dev/null
+++ b/spec/factories/public_body_categories.rb
@@ -0,0 +1,8 @@
+
+FactoryGirl.define do
+ factory :public_body_category do
+ sequence(:title) { |n| "Example Public Body Category #{n}" }
+ sequence(:category_tag) { |n| "example_tag_#{n}" }
+ sequence(:description) { |n| "Example Public body Description #{n}" }
+ end
+end
diff --git a/spec/factories/public_body_category_links.rb b/spec/factories/public_body_category_links.rb
new file mode 100644
index 000000000..7663b1f52
--- /dev/null
+++ b/spec/factories/public_body_category_links.rb
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :public_body_category_link do
+ association :public_body_category
+ association :public_body_heading
+ end
+end
diff --git a/spec/factories/public_body_headings.rb b/spec/factories/public_body_headings.rb
new file mode 100644
index 000000000..ed54ddada
--- /dev/null
+++ b/spec/factories/public_body_headings.rb
@@ -0,0 +1,5 @@
+FactoryGirl.define do
+ factory :public_body_heading do
+ sequence(:name) { |n| "Example Public Body Heading #{n}" }
+ end
+end
diff --git a/spec/fixtures/info_request_events.yml b/spec/fixtures/info_request_events.yml
index b2f40cc37..23ef80cc2 100644
--- a/spec/fixtures/info_request_events.yml
+++ b/spec/fixtures/info_request_events.yml
@@ -31,8 +31,10 @@ silly_outgoing_message_event:
info_request_id: 103
event_type: sent
created_at: 2007-10-14 10:41:12.686264
- described_state:
outgoing_message_id: 2
+ calculated_state: waiting_response
+ described_state: waiting_response
+ last_described_at: 2007-10-14 10:41:12.686264
useless_incoming_message_event:
id: 902
params_yaml: "--- \n\
diff --git a/spec/helpers/health_checks_helper_spec.rb b/spec/helpers/health_checks_helper_spec.rb
new file mode 100644
index 000000000..7d4083da5
--- /dev/null
+++ b/spec/helpers/health_checks_helper_spec.rb
@@ -0,0 +1,15 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe HealthChecksHelper do
+ include HealthChecksHelper
+
+ describe :check_status do
+
+ it 'warns that the check is failing' do
+ check = double(:message => 'Failed', :ok? => false)
+ expect(check_status(check)).to include('red')
+ end
+
+ end
+
+end
diff --git a/spec/integration/localisation_spec.rb b/spec/integration/localisation_spec.rb
index 4f6b61ae1..037603ad5 100644
--- a/spec/integration/localisation_spec.rb
+++ b/spec/integration/localisation_spec.rb
@@ -24,14 +24,29 @@ describe "when generating urls" do
response.should_not contain @home_link_regex
end
- it 'should redirect requests for a public body in a locale to the canonical name in that locale' do
- get('/es/body/dfh')
- response.should redirect_to "/es/body/edfh"
- end
+ context 'when handling public body requests' do
+
+ before do
+ AlaveteliLocalization.set_locales(available_locales='es en', default_locale='en')
+ body = FactoryGirl.create(:public_body, :short_name => 'english_short')
+ I18n.with_locale(:es) do
+ body.short_name = 'spanish_short'
+ body.save!
+ end
+ end
+
+ it 'should redirect requests for a public body in a locale to the
+ canonical name in that locale' do
+ get('/es/body/english_short')
+ response.should redirect_to "/es/body/spanish_short"
+ end
- it 'should remember a filter view when redirecting a public body request to the canonical name' do
- get('/es/body/tgq/successful')
- response.should redirect_to "/es/body/etgq/successful"
+ it 'should remember a filter view when redirecting a public body
+ request to the canonical name' do
+ AlaveteliLocalization.set_locales(available_locales='es en', default_locale='en')
+ get('/es/body/english_short/successful')
+ response.should redirect_to "/es/body/spanish_short/successful"
+ end
end
describe 'when there is more than one locale' do
diff --git a/spec/lib/health_checks/checks/days_ago_check_spec.rb b/spec/lib/health_checks/checks/days_ago_check_spec.rb
new file mode 100644
index 000000000..33b4642cd
--- /dev/null
+++ b/spec/lib/health_checks/checks/days_ago_check_spec.rb
@@ -0,0 +1,66 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
+
+describe HealthChecks::Checks::DaysAgoCheck do
+ include HealthChecks::Checks
+
+ it { should be_kind_of(HealthChecks::HealthCheckable) }
+
+ it 'defaults to comparing to one day ago' do
+ check = HealthChecks::Checks::DaysAgoCheck.new
+ expect(check.days).to eq(1)
+ end
+
+ it 'accepts a custom number of days' do
+ check = HealthChecks::Checks::DaysAgoCheck.new(:days => 4)
+ expect(check.days).to eq(4)
+ end
+
+ describe :check do
+
+ it 'is successful if the subject is in the last day' do
+ check = HealthChecks::Checks::DaysAgoCheck.new { Time.now }
+ expect(check.check).to be_true
+ end
+
+ it 'fails if the subject is over a day ago' do
+ check = HealthChecks::Checks::DaysAgoCheck.new { 2.days.ago }
+ expect(check.check).to be_false
+ end
+
+ end
+
+ describe :failure_message do
+
+ it 'includes the check subject in the default message' do
+ subject = 2.days.ago
+ check = HealthChecks::Checks::DaysAgoCheck.new { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ it 'includes the check subject in a custom message' do
+ params = { :failure_message => 'This check failed' }
+ subject = 2.days.ago
+ check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ end
+
+ describe :success_message do
+
+ it 'includes the check subject in the default message' do
+ subject = Time.now
+ check = HealthChecks::Checks::DaysAgoCheck.new { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ it 'includes the check subject in a custom message' do
+ params = { :success_message => 'This check succeeded' }
+ subject = Time.now
+ check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject }
+ expect(check.success_message).to include(subject.to_s)
+ end
+
+ end
+
+end
diff --git a/spec/lib/health_checks/health_checkable_spec.rb b/spec/lib/health_checks/health_checkable_spec.rb
new file mode 100644
index 000000000..abfeb5c21
--- /dev/null
+++ b/spec/lib/health_checks/health_checkable_spec.rb
@@ -0,0 +1,128 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe HealthChecks::HealthCheckable do
+
+ before(:each) do
+ class MockCheck
+ include HealthChecks::HealthCheckable
+ end
+ @subject = MockCheck.new
+ end
+
+ describe :initialize do
+
+ it 'allows a custom failure message to be set' do
+ @subject = MockCheck.new(:failure_message => 'F')
+ expect(@subject.failure_message).to eq('F')
+ end
+
+ it 'allows a custom success message to be set' do
+ @subject = MockCheck.new(:success_message => 'S')
+ expect(@subject.success_message).to eq('S')
+ end
+
+ end
+
+ describe :name do
+
+ it 'returns the name of the check' do
+ expect(@subject.name).to eq('MockCheck')
+ end
+
+ end
+
+ describe :check do
+
+ it 'is intended to be overridden by the includer' do
+ expect{ @subject.check }.to raise_error(NotImplementedError)
+ end
+
+ end
+
+ describe :ok? do
+
+ it 'returns true if the check was successful' do
+ @subject.stub(:check => true)
+ expect(@subject.ok?).to be_true
+ end
+
+ it 'returns false if the check failed' do
+ @subject.stub(:check => false)
+ expect(@subject.ok?).to be_false
+ end
+
+ end
+
+ describe :failure_message do
+
+ it 'returns a default message if one has not been set' do
+ expect(@subject.failure_message).to eq('Failed')
+ end
+
+ end
+
+ describe :failure_message= do
+
+ it 'allows a custom failure message to be set' do
+ @subject.failure_message = 'F'
+ expect(@subject.failure_message).to eq('F')
+ end
+
+ end
+
+ describe :success_message do
+
+ it 'returns a default message if one has not been set' do
+ expect(@subject.success_message).to eq('Success')
+ end
+
+ end
+
+ describe :success_message= do
+
+ it 'allows a custom success message to be set' do
+ @subject.success_message = 'S'
+ expect(@subject.success_message).to eq('S')
+ end
+
+ end
+
+ describe :message do
+
+ context 'if the check succeeds' do
+
+ before(:each) do
+ @subject.stub(:check => true)
+ end
+
+ it 'returns the default success message' do
+ expect(@subject.message).to eq('Success')
+ end
+
+ it 'returns a custom success message if one has been set' do
+ @subject.success_message = 'Custom Success'
+ expect(@subject.message).to eq('Custom Success')
+ end
+
+ end
+
+ context 'if the check fails' do
+
+ before(:each) do
+ @subject.stub(:check => false)
+ end
+
+ it 'returns the default failure message' do
+ expect(@subject.message).to eq('Failed')
+ end
+
+ it 'returns a custom failure message if one has been set' do
+ @subject.failure_message = 'Custom Failed'
+ expect(@subject.message).to eq('Custom Failed')
+ end
+
+ end
+
+ end
+
+end
diff --git a/spec/lib/health_checks/health_checks_spec.rb b/spec/lib/health_checks/health_checks_spec.rb
new file mode 100644
index 000000000..c7037b813
--- /dev/null
+++ b/spec/lib/health_checks/health_checks_spec.rb
@@ -0,0 +1,77 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe HealthChecks do
+ include HealthChecks
+
+ describe :add do
+
+ it 'adds a check to the collection and returns the check' do
+ check = double('MockCheck', :check => true)
+ expect(add(check)).to eq(check)
+ end
+
+ it 'does not add checks that do not define the check method' do
+ check = double('BadCheck')
+ expect(add(check)).to eq(false)
+ end
+
+ end
+
+ describe :all do
+
+ it 'returns all the checks' do
+ check1 = double('MockCheck', :check => true)
+ check2 = double('AnotherCheck', :check => false)
+ add(check1)
+ add(check2)
+ expect(all).to include(check1, check2)
+ end
+
+ end
+
+ describe :each do
+
+ it 'iterates over each check' do
+ expect(subject).to respond_to(:each)
+ end
+
+ end
+
+ describe :ok? do
+
+ it 'returns true if all checks are ok' do
+ checks = [
+ double('MockCheck', :ok? => true),
+ double('FakeCheck', :ok? => true),
+ double('TestCheck', :ok? => true)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_true
+ end
+
+ it 'returns false if all checks fail' do
+ checks = [
+ double('MockCheck', :ok? => false),
+ double('FakeCheck', :ok? => false),
+ double('TestCheck', :ok? => false)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_false
+ end
+
+ it 'returns false if a single check fails' do
+ checks = [
+ double('MockCheck', :ok? => true),
+ double('FakeCheck', :ok? => false),
+ double('TestCheck', :ok? => true)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_false
+ end
+
+ end
+
+end
diff --git a/spec/lib/public_body_categories_spec.rb b/spec/lib/public_body_categories_spec.rb
deleted file mode 100644
index e53d9a028..000000000
--- a/spec/lib/public_body_categories_spec.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe PublicBodyCategories do
-
- before do
- load_test_categories
- end
-
- describe 'when asked for categories with headings' do
-
- it 'should return a list of headings as plain strings, each followed by n tag specifications as
- lists in the form:
- ["tag_to_use_as_category", "Sub category title", "Instance description"]' do
- expected_categories = ["Local and regional", ["local_council",
- "Local councils",
- "a local council"],
- "Miscellaneous", ["other",
- "Miscellaneous",
- "miscellaneous"]]
- PublicBodyCategories::get().with_headings().should == expected_categories
- end
-
- end
-
- describe 'when asked for headings' do
-
- it 'should return a list of headings' do
- PublicBodyCategories::get().headings().should == ['Local and regional', 'Miscellaneous']
- end
-
- end
-
- describe 'when asked for tags by headings' do
-
- it 'should return a hash of tags keyed by heading' do
- PublicBodyCategories::get().by_heading().should == {'Local and regional' => ['local_council'],
- 'Miscellaneous' => ['other']}
- end
-
- end
-
-end \ No newline at end of file
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
index 5b41cc0d4..4ecd2d3e1 100644
--- a/spec/models/censor_rule_spec.rb
+++ b/spec/models/censor_rule_spec.rb
@@ -90,17 +90,32 @@ end
describe 'when validating rules' do
- it 'should be invalid without text' do
+ it 'must have the text to redact' do
censor_rule = CensorRule.new
- censor_rule.valid?.should == false
- censor_rule.errors[:text].should == ["can't be blank"]
+ expect(censor_rule).to have(1).error_on(:text)
+ expect(censor_rule.errors[:text]).to eql(["can't be blank"])
+ end
+
+ it 'must have a replacement' do
+ expect(CensorRule.new).to have(1).error_on(:replacement)
+ end
+
+ it 'must have a last_edit_editor' do
+ expect(CensorRule.new).to have(1).error_on(:last_edit_editor)
+ end
+
+ it 'must have a last_edit_comment' do
+ expect(CensorRule.new).to have(1).error_on(:last_edit_comment)
end
describe 'when validating a regexp rule' do
before do
@censor_rule = CensorRule.new(:regexp => true,
- :text => '*')
+ :text => '*',
+ :replacement => '---',
+ :last_edit_comment => 'test',
+ :last_edit_editor => 'rspec')
end
it 'should try to create a regexp from the text' do
@@ -133,7 +148,10 @@ describe 'when validating rules' do
describe 'when the allow_global flag has been set' do
before do
- @censor_rule = CensorRule.new(:text => 'some text')
+ @censor_rule = CensorRule.new(:text => 'some text',
+ :replacement => '---',
+ :last_edit_comment => 'test',
+ :last_edit_editor => 'rspec')
@censor_rule.allow_global = true
end
@@ -146,7 +164,10 @@ describe 'when validating rules' do
describe 'when the allow_global flag has not been set' do
before do
- @censor_rule = CensorRule.new(:text => '/./')
+ @censor_rule = CensorRule.new(:text => '/./',
+ :replacement => '---',
+ :last_edit_comment => 'test',
+ :last_edit_editor => 'rspec')
end
it 'should not allow a global text censor rule (without user_id, request_id or public_body_id)' do
diff --git a/spec/models/change_email_validator_spec.rb b/spec/models/change_email_validator_spec.rb
new file mode 100644
index 000000000..b667a23d1
--- /dev/null
+++ b/spec/models/change_email_validator_spec.rb
@@ -0,0 +1,124 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+def validator_with_user_and_params(user, params = {})
+ validator = ChangeEmailValidator.new(params)
+ validator.logged_in_user = user
+ validator
+end
+
+describe ChangeEmailValidator do
+
+ let(:user) { FactoryGirl.create(:user) }
+
+ describe :old_email do
+
+ it 'must have an old email' do
+ params = { :old_email => nil,
+ :new_email => 'new@example.com',
+ :user_circumstance => 'change_email',
+ :password => 'jonespassword' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = 'Please enter your old email address'
+ expect(validator.errors_on(:old_email)).to include(msg)
+ end
+
+ it 'must be a valid email' do
+ params = { :old_email => 'old',
+ :new_email => 'new@example.com',
+ :user_circumstance => 'change_email',
+ :password => 'jonespassword' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = "Old email doesn't look like a valid address"
+ expect(validator.errors_on(:old_email)).to include(msg)
+ end
+
+ it 'must have the same email as the logged in user' do
+ params = { :old_email => user.email,
+ :new_email => 'new@example.com',
+ :user_circumstance => 'change_email',
+ :password => 'jonespassword' }
+ validator = validator_with_user_and_params(user, params)
+ validator.logged_in_user = FactoryGirl.build(:user)
+
+ msg = "Old email address isn't the same as the address of the account you are logged in with"
+ expect(validator.errors_on(:old_email)).to include(msg)
+ end
+
+ end
+
+ describe :new_email do
+
+ it 'must have a new email' do
+ params = { :old_email => user.email,
+ :new_email => nil,
+ :user_circumstance => 'change_email',
+ :password => 'jonespassword' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = 'Please enter your new email address'
+ expect(validator.errors_on(:new_email)).to include(msg)
+ end
+
+ it 'must be a valid email' do
+ params = { :old_email => user.email,
+ :new_email => 'new',
+ :user_circumstance => 'change_email',
+ :password => 'jonespassword' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = "New email doesn't look like a valid address"
+ expect(validator.errors_on(:new_email)).to include(msg)
+ end
+
+ end
+
+ describe :password do
+
+ pending 'password_and_format_of_email validation fails when password is nil' do
+ it 'must have a password' do
+ params = { :old_email => user.email,
+ :new_email => 'new@example.com',
+ :password => nil }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = 'Please enter your password'
+ expect(validator.errors_on(:password)).to include(msg)
+ end
+ end
+
+ it 'does not require a password if changing email' do
+ params = { :old_email => user.email,
+ :new_email => 'new@example.com',
+ :user_circumstance => 'change_email',
+ :password => '' }
+ validator = validator_with_user_and_params(user, params)
+
+ expect(validator).to have(0).errors_on(:password)
+ end
+
+ it 'must have a password if not changing email' do
+ params = { :old_email => user.email,
+ :new_email => 'new@example.com',
+ :user_circumstance => 'unknown',
+ :password => '' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = 'Please enter your password'
+ expect(validator.errors_on(:password)).to include(msg)
+ end
+
+ it 'must be the correct password' do
+ params = { :old_email => user.email,
+ :new_email => 'new@example.com',
+ :password => 'incorrectpass' }
+ validator = validator_with_user_and_params(user, params)
+
+ msg = 'Password is not correct'
+ expect(validator.errors_on(:password)).to include(msg)
+ end
+
+ end
+
+end
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index afb8e0949..9ad616ea5 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -848,9 +848,11 @@ describe InfoRequest do
context "a series of events on a request" do
it "should have sensible events after the initial request has been made" do
# An initial request is sent
- # The logic that changes the status when a message is sent is mixed up
- # in OutgoingMessage#send_message. So, rather than extract it (or call it)
- # let's just duplicate what it does here for the time being.
+ # FIXME: The logic that changes the status when a message
+ # is sent is mixed up in
+ # OutgoingMessage#record_email_delivery. So, rather than
+ # extract it (or call it) let's just duplicate what it does
+ # here for the time being.
request.log_event('sent', {})
request.set_described_state('waiting_response')
@@ -919,7 +921,8 @@ describe InfoRequest do
request.log_event("status_update", {})
request.set_described_state("waiting_response")
# A normal follow up is sent
- # This is normally done in OutgoingMessage#send_message
+ # This is normally done in
+ # OutgoingMessage#record_email_delivery
request.log_event('followup_sent', {})
request.set_described_state('waiting_response')
diff --git a/spec/models/public_body_category/category_collection_spec.rb b/spec/models/public_body_category/category_collection_spec.rb
new file mode 100644
index 000000000..1fbcbe739
--- /dev/null
+++ b/spec/models/public_body_category/category_collection_spec.rb
@@ -0,0 +1,81 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe PublicBodyCategory::CategoryCollection do
+ context "requesting data" do
+
+ before do
+ data = [ "Local and regional",
+ [ "local_council", "Local councils", "a local council" ],
+ "Miscellaneous",
+ [ "other", "Miscellaneous", "miscellaneous" ] ]
+ @categories = PublicBodyCategory::CategoryCollection.new
+ data.each { |item| @categories << item }
+ end
+
+ describe 'when asked for headings' do
+
+ it 'should return a list of headings' do
+ @categories.headings().should == ['Local and regional', 'Miscellaneous']
+ end
+
+ end
+
+ describe 'when asked for categories with headings' do
+ it 'should return a list of headings as plain strings, each followed by n tag specifications as
+ lists in the form:
+ ["tag_to_use_as_category", "Sub category title", "Instance description"]' do
+ expected_categories = ["Local and regional", ["local_council",
+ "Local councils",
+ "a local council"],
+ "Miscellaneous", ["other",
+ "Miscellaneous",
+ "miscellaneous"]]
+ @categories.with_headings().should == expected_categories
+ end
+ end
+
+
+
+ describe 'when asked for tags by headings' do
+ it 'should return a hash of tags keyed by heading' do
+ @categories.by_heading().should == {'Local and regional' => ['local_council'],
+ 'Miscellaneous' => ['other']}
+ end
+ end
+
+ describe 'when asked for categories with description' do
+ it 'should return a list of tag specifications as lists in the form:
+ ["tag_to_use_as_category", "Sub category title", "Instance description"]' do
+ expected_categories = [
+ ["local_council", "Local councils", "a local council"],
+ ["other", "Miscellaneous", "miscellaneous"]
+ ]
+ @categories.with_description().should == expected_categories
+ end
+ end
+
+ describe 'when asked for tags' do
+ it 'should return a list of tags' do
+ @categories.tags().should == ["local_council", "other"]
+ end
+ end
+
+ describe 'when asked for categories by tag' do
+ it 'should return a hash of categories keyed by tag' do
+ @categories.by_tag().should == {
+ "local_council" => "Local councils",
+ "other" => "Miscellaneous"
+ }
+ end
+ end
+
+ describe 'when asked for singular_by_tag' do
+ it 'should return a hash of category descriptions keyed by tag' do
+ @categories.singular_by_tag().should == {
+ "local_council" => "a local council",
+ "other" => "miscellaneous"
+ }
+ end
+ end
+ end
+end
diff --git a/spec/models/public_body_category_link_spec.rb b/spec/models/public_body_category_link_spec.rb
new file mode 100644
index 000000000..8d91f02d5
--- /dev/null
+++ b/spec/models/public_body_category_link_spec.rb
@@ -0,0 +1,53 @@
+# == Schema Information
+#
+# Table name: public_body_category_link
+#
+# public_body_category_id :integer not null
+# public_body_heading_id :integer not null
+# category_display_order :integer
+#
+
+require 'spec_helper'
+
+describe PublicBodyHeading, 'when validating' do
+
+ it 'should set a default display order based on the next available display order' do
+ heading = FactoryGirl.create(:public_body_heading)
+ category = FactoryGirl.create(:public_body_category)
+ category_link = PublicBodyCategoryLink.new(:public_body_heading => heading,
+ :public_body_category => category)
+ category_link.valid?
+ category_link.category_display_order.should == PublicBodyCategoryLink.next_display_order(heading)
+ end
+
+ it 'should be invalid without a category' do
+ category_link = PublicBodyCategoryLink.new
+ category_link.should_not be_valid
+ category_link.errors[:public_body_category].should == ["can't be blank"]
+ end
+
+ it 'should be invalid without a heading' do
+ category_link = PublicBodyCategoryLink.new
+ category_link.should_not be_valid
+ category_link.errors[:public_body_heading].should == ["can't be blank"]
+ end
+
+end
+
+describe PublicBodyCategoryLink, 'when setting a category display order' do
+
+ it 'should return 0 if there are no public body headings' do
+ heading = FactoryGirl.create(:public_body_heading)
+ PublicBodyCategoryLink.next_display_order(heading).should == 0
+ end
+
+ it 'should return one more than the highest display order if there are public body headings' do
+ heading = FactoryGirl.create(:public_body_heading)
+ category = FactoryGirl.create(:public_body_category)
+ category_link = PublicBodyCategoryLink.create(:public_body_heading_id => heading.id,
+ :public_body_category_id => category.id)
+
+ PublicBodyCategoryLink.next_display_order(heading).should == 1
+ end
+
+end
diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb
new file mode 100644
index 000000000..2d39a7376
--- /dev/null
+++ b/spec/models/public_body_category_spec.rb
@@ -0,0 +1,65 @@
+# == Schema Information
+#
+# Table name: public_body_categories
+#
+# id :integer not null, primary key
+# locale :string
+# title :text not null
+# category_tag :text not null
+# description :text not null
+# display_order :integer
+#
+
+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
+
+ it 'should require a title' do
+ category = PublicBodyCategory.new
+ category.should_not be_valid
+ category.errors[:title].should == ["Title can't be blank"]
+ end
+
+ it 'should require a category tag' do
+ category = PublicBodyCategory.new
+ category.should_not be_valid
+ category.errors[:category_tag].should == ["Tag can't be blank"]
+ end
+
+ it 'should require a unique tag' do
+ existing = FactoryGirl.create(:public_body_category)
+ PublicBodyCategory.new(:email => existing.category_tag).should_not be_valid
+ end
+ end
+end
diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb
new file mode 100644
index 000000000..add2cac60
--- /dev/null
+++ b/spec/models/public_body_heading_spec.rb
@@ -0,0 +1,68 @@
+# == Schema Information
+#
+# Table name: public_body_headings
+#
+# id :integer not null, primary key
+# locale :string
+# name :text not null
+# display_order :integer
+#
+
+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
+ heading = PublicBodyHeading.new
+ heading.should_not be_valid
+ heading.errors[:name].should == ["Name can't be blank"]
+ end
+
+ it 'should require a unique name' do
+ heading = FactoryGirl.create(:public_body_heading)
+ new_heading = PublicBodyHeading.new(:name => heading.name)
+ new_heading.should_not be_valid
+ new_heading.errors[:name].should == ["Name is already taken"]
+ end
+
+ it 'should set a default display order based on the next available display order' do
+ heading = PublicBodyHeading.new
+ heading.valid?
+ heading.display_order.should == PublicBodyHeading.next_display_order
+ end
+ end
+
+ context 'when setting a display order' do
+
+ it 'should return 0 if there are no public body headings' do
+ PublicBodyHeading.next_display_order.should == 0
+ end
+
+ it 'should return one more than the highest display order if there are public body headings' do
+ heading = FactoryGirl.create(:public_body_heading)
+ PublicBodyHeading.next_display_order.should == 1
+ end
+ end
+end
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index a7544c218..225958cac 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -546,6 +546,58 @@ CSV
errors.should include("error: line 3: Url name URL name is already taken for authority 'Foobar Test'")
end
+ it 'has a default list of fields to import' do
+ expected_fields = [
+ ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'],
+ ['short_name', '(i18n)'],
+ ['request_email', '(i18n)'],
+ ['notes', '(i18n)'],
+ ['publication_scheme', '(i18n)'],
+ ['disclosure_log', '(i18n)'],
+ ['home_page', ''],
+ ['tag_string', '(tags separated by spaces)'],
+ ]
+
+ expect(PublicBody.csv_import_fields).to eq(expected_fields)
+ end
+
+ it 'allows you to override the default list of fields to import' do
+ old_csv_import_fields = PublicBody.csv_import_fields.clone
+ expected_fields = [
+ ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'],
+ ['short_name', '(i18n)'],
+ ]
+
+ PublicBody.csv_import_fields = expected_fields
+
+ expect(PublicBody.csv_import_fields).to eq(expected_fields)
+
+ # Reset our change so that we don't affect other specs
+ PublicBody.csv_import_fields = old_csv_import_fields
+ end
+
+ it 'allows you to append to the default list of fields to import' do
+ old_csv_import_fields = PublicBody.csv_import_fields.clone
+ expected_fields = [
+ ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'],
+ ['short_name', '(i18n)'],
+ ['request_email', '(i18n)'],
+ ['notes', '(i18n)'],
+ ['publication_scheme', '(i18n)'],
+ ['disclosure_log', '(i18n)'],
+ ['home_page', ''],
+ ['tag_string', '(tags separated by spaces)'],
+ ['a_new_field', ''],
+ ]
+
+ PublicBody.csv_import_fields << ['a_new_field', '']
+
+ expect(PublicBody.csv_import_fields).to eq(expected_fields)
+
+ # Reset our change so that we don't affect other specs
+ PublicBody.csv_import_fields = old_csv_import_fields
+ end
+
end
describe PublicBody do
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 0e3fe35c7..74a4891c2 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -201,14 +201,6 @@ Spork.prefork do
I18n.default_locale = original_default_locale
end
- def load_test_categories
- PublicBodyCategories.add(:en, [
- "Local and regional",
- [ "local_council", "Local councils", "a local council" ],
- "Miscellaneous",
- [ "other", "Miscellaneous", "miscellaneous" ],])
- end
-
def basic_auth_login(request, username = nil, password = nil)
username = AlaveteliConfiguration::admin_username if username.nil?
password = AlaveteliConfiguration::admin_password if password.nil?