aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers')
-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/public_body_controller_spec.rb22
-rw-r--r--spec/controllers/user_controller_spec.rb12
4 files changed, 458 insertions, 8 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/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/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"