aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers')
-rw-r--r--spec/controllers/admin_censor_rule_controller_spec.rb471
-rw-r--r--spec/controllers/admin_general_controller_spec.rb7
-rw-r--r--spec/controllers/admin_public_body_categories_controller_spec.rb234
-rw-r--r--spec/controllers/admin_public_body_change_requests_controller_spec.rb35
-rw-r--r--spec/controllers/admin_public_body_headings_controller_spec.rb275
-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/help_controller_spec.rb9
-rw-r--r--spec/controllers/public_body_controller_spec.rb19
-rw-r--r--spec/controllers/request_controller_spec.rb37
-rw-r--r--spec/controllers/user_controller_spec.rb12
11 files changed, 1104 insertions, 29 deletions
diff --git a/spec/controllers/admin_censor_rule_controller_spec.rb b/spec/controllers/admin_censor_rule_controller_spec.rb
index 37ffd9764..4df56a92b 100644
--- a/spec/controllers/admin_censor_rule_controller_spec.rb
+++ b/spec/controllers/admin_censor_rule_controller_spec.rb
@@ -1,5 +1,476 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+describe AdminCensorRuleController do
+ before(:each) { basic_auth_login(@request) }
+
+ describe 'GET new' do
+
+ it 'returns a successful response' do
+ get :new
+ expect(response).to be_success
+ end
+
+ it 'initializes a new censor rule' do
+ get :new
+ expect(assigns[:censor_rule]).to be_new_record
+ end
+
+ it 'renders the correct template' do
+ get :new
+ expect(response).to render_template('new')
+ end
+
+ it 'sets the URL for the form to POST to' do
+ get :new
+ expect(assigns[:form_url]).to eq(admin_rule_create_path)
+ end
+
+ context 'info_request_id param' do
+
+ it 'finds an info request if the info_request_id param is supplied' do
+ info_request = FactoryGirl.create(:info_request)
+ get :new, :info_request_id => info_request.id
+ expect(assigns[:info_request]).to eq(info_request)
+ end
+
+ it 'associates the info request with the new censor rule' do
+ info_request = FactoryGirl.create(:info_request)
+ get :new, :info_request_id => info_request.id
+ expect(assigns[:censor_rule].info_request).to eq(info_request)
+ end
+
+ it 'sets the URL for the form to POST to' do
+ info_request = FactoryGirl.create(:info_request)
+ get :new, :info_request_id => info_request.id
+ expect(assigns[:form_url]).to eq(admin_info_request_censor_rules_path(info_request))
+ end
+
+ it 'does not find an info request if no info_request_id param is supplied' do
+ get :new
+ expect(assigns[:info_request]).to be_nil
+ end
+
+ end
+
+ context 'user_id param' do
+
+ it 'finds a user if the user_id param is supplied' do
+ user = FactoryGirl.create(:user)
+ get :new, :user_id => user.id
+ expect(assigns[:censor_user]).to eq(user)
+ end
+
+ it 'associates the user with the new censor rule' do
+ user = FactoryGirl.create(:user)
+ get :new, :user_id => user.id
+ expect(assigns[:censor_rule].user).to eq(user)
+ end
+
+ it 'sets the URL for the form to POST to' do
+ user = FactoryGirl.create(:user)
+ get :new, :user_id => user.id
+ expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(user))
+ end
+
+ it 'does not find a user if no user_id param is supplied' do
+ get :new
+ expect(assigns[:censor_user]).to be_nil
+ end
+
+ end
+
+ end
+
+ describe 'POST create' do
+
+ before(:each) do
+ @censor_rule_params = FactoryGirl.build(:global_censor_rule).serializable_hash
+ # last_edit_editor gets set in the controller
+ @censor_rule_params.delete(:last_edit_editor)
+ end
+
+ it 'sets the last_edit_editor to the current admin' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*')
+ end
+
+ it 'sets the URL for the form to POST to' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:form_url]).to eq(admin_rule_create_path)
+ end
+
+ context 'info_request_id param' do
+
+ it 'finds an info request if the info_request_id param is supplied' do
+ info_request = FactoryGirl.create(:info_request)
+ post :create, :info_request_id => info_request.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:info_request]).to eq(info_request)
+ end
+
+ it 'associates the info request with the new censor rule' do
+ info_request = FactoryGirl.create(:info_request)
+ post :create, :info_request_id => info_request.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:censor_rule].info_request).to eq(info_request)
+ end
+
+ it 'sets the URL for the form to POST to' do
+ info_request = FactoryGirl.create(:info_request)
+ post :create, :info_request_id => info_request.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:form_url]).to eq(admin_info_request_censor_rules_path(info_request))
+ end
+
+ it 'does not find an info request if no info_request_id param is supplied' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:info_request]).to be_nil
+ end
+
+ end
+
+ context 'user_id param' do
+
+ it 'finds a user if the user_id param is supplied' do
+ user = FactoryGirl.create(:user)
+ post :create, :user_id => user.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:censor_user]).to eq(user)
+ end
+
+ it 'associates the user with the new censor rule' do
+ user = FactoryGirl.create(:user)
+ post :create, :user_id => user.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:censor_rule].user).to eq(user)
+ end
+
+ it 'sets the URL for the form to POST to' do
+ user = FactoryGirl.create(:user)
+ post :create, :user_id => user.id,
+ :censor_rule => @censor_rule_params
+ expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(user))
+ end
+
+ it 'does not find a user if no user_id param is supplied' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:censor_user]).to be_nil
+ end
+
+ end
+
+ context 'successfully saving the censor rule' do
+
+ before(:each) do
+ CensorRule.any_instance.stub(:save).and_return(true)
+ end
+
+ it 'persists the censor rule' do
+ pending("This raises an internal error in most cases")
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:censor_rule]).to be_persisted
+ end
+
+ it 'confirms the censor rule is created' do
+ pending("This raises an internal error in most cases")
+ post :create, :censor_rule => @censor_rule_params
+ msg = 'CensorRule was successfully created.'
+ expect(flash[:notice]).to eq(msg)
+ end
+
+ it 'raises an error after creating the rule' do
+ expect {
+ post :create, :censor_rule => @censor_rule_params
+ }.to raise_error 'internal error'
+ end
+
+ context 'a CensorRule with an associated InfoRequest' do
+
+ before(:each) do
+ @censor_rule_params = FactoryGirl.build(:info_request_censor_rule).serializable_hash
+ # last_edit_editor gets set in the controller
+ @censor_rule_params.delete(:last_edit_editor)
+ end
+
+ it 'purges the cache for the info request' do
+ censor_rule = CensorRule.new(@censor_rule_params)
+ @controller.should_receive(:expire_for_request).
+ with(censor_rule.info_request)
+
+ post :create, :censor_rule => @censor_rule_params
+ end
+
+ it 'redirects to the associated info request' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(response).to redirect_to(
+ admin_request_show_path(assigns[:censor_rule].info_request)
+ )
+ end
+
+ end
+
+ context 'a CensorRule with an associated User' do
+
+ before(:each) do
+ @censor_rule_params = FactoryGirl.build(:user_censor_rule).serializable_hash
+ # last_edit_editor gets set in the controller
+ @censor_rule_params.delete(:last_edit_editor)
+ end
+
+ it 'purges the cache for the info request' do
+ censor_rule = CensorRule.new(@censor_rule_params)
+ @controller.should_receive(:expire_requests_for_user).
+ with(censor_rule.user)
+
+ post :create, :censor_rule => @censor_rule_params
+ end
+
+ it 'redirects to the associated info request' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(response).to redirect_to(
+ admin_user_show_path(assigns[:censor_rule].user)
+ )
+ end
+
+ end
+
+ end
+
+ context 'unsuccessfully saving the censor rule' do
+
+ before(:each) do
+ CensorRule.any_instance.stub(:save).and_return(false)
+ end
+
+ it 'does not persist the censor rule' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(assigns[:censor_rule]).to be_new_record
+ end
+
+ it 'renders the form' do
+ post :create, :censor_rule => @censor_rule_params
+ expect(response).to render_template('new')
+ end
+
+ end
+
+ end
+
+ describe 'GET edit' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:global_censor_rule)
+ end
+
+ it 'returns a successful response' do
+ get :edit, :id => @censor_rule.id
+ expect(response).to be_success
+ end
+
+ it 'renders the correct template' do
+ get :edit, :id => @censor_rule.id
+ expect(response).to render_template('edit')
+ end
+
+ it 'finds the correct censor rule to edit' do
+ get :edit, :id => @censor_rule.id
+ expect(assigns[:censor_rule]).to eq(@censor_rule)
+ end
+
+ end
+
+ describe 'PUT update' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:global_censor_rule)
+ end
+
+ it 'finds the correct censor rule to edit' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ expect(assigns[:censor_rule]).to eq(@censor_rule)
+ end
+
+ it 'sets the last_edit_editor to the current admin' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*')
+ end
+
+ context 'successfully saving the censor rule' do
+
+ before(:each) do
+ CensorRule.any_instance.stub(:save).and_return(true)
+ end
+
+ it 'updates the censor rule' do
+ pending("This raises an internal error in most cases")
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+ @censor_rule.reload
+ expect(@censor_rule.text).to eq('different text')
+ end
+
+ it 'confirms the censor rule is updated' do
+ pending("This raises an internal error in most cases")
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ msg = 'CensorRule was successfully updated.'
+ expect(flash[:notice]).to eq(msg)
+ end
+
+ it 'raises an error after updating the rule' do
+ expect {
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+ }.to raise_error 'internal error'
+ end
+
+ context 'a CensorRule with an associated InfoRequest' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:info_request_censor_rule)
+ end
+
+ it 'purges the cache for the info request' do
+ @controller.should_receive(:expire_for_request).
+ with(@censor_rule.info_request)
+
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+ end
+
+ it 'redirects to the associated info request' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ expect(response).to redirect_to(
+ admin_request_show_path(assigns[:censor_rule].info_request)
+ )
+ end
+
+ end
+
+ context 'a CensorRule with an associated User' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:user_censor_rule)
+ end
+
+ it 'purges the cache for the info request' do
+ @controller.should_receive(:expire_requests_for_user).
+ with(@censor_rule.user)
+
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+ end
+
+ it 'redirects to the associated info request' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ expect(response).to redirect_to(
+ admin_user_show_path(assigns[:censor_rule].user)
+ )
+ end
+
+ end
+
+ end
+
+ context 'unsuccessfully saving the censor rule' do
+
+ before(:each) do
+ CensorRule.any_instance.stub(:save).and_return(false)
+ end
+
+ it 'does not update the censor rule' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+ @censor_rule.reload
+ expect(@censor_rule.text).to eq('some text to redact')
+ end
+
+ it 'renders the form' do
+ put :update, :id => @censor_rule.id,
+ :censor_rule => { :text => 'different text' }
+
+ expect(response).to render_template('edit')
+ end
+
+ end
+
+ end
+
+ describe 'DELETE destroy' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:global_censor_rule)
+ end
+
+ it 'finds the correct censor rule to destroy' do
+ pending("This raises an internal error in most cases")
+ # TODO: Replace :censor_rule_id with :id
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ expect(assigns[:censor_rule]).to eq(@censor_rule)
+ end
+
+ it 'raises an error after destroying the rule' do
+ expect {
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ }.to raise_error 'internal error'
+ end
+
+ it 'confirms the censor rule is destroyed in all cases' do
+ pending("This actually raises an internal error anyway")
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ msg = 'CensorRule was successfully destroyed.'
+ expect(flash[:notice]).to eq(msg)
+ end
+
+ context 'a CensorRule with an associated InfoRequest' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:info_request_censor_rule)
+ end
+
+ it 'purges the cache for the info request' do
+ @controller.should_receive(:expire_for_request).with(@censor_rule.info_request)
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ end
+
+ it 'redirects to the associated info request' do
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ expect(response).to redirect_to(admin_request_show_path(@censor_rule.info_request))
+ end
+
+ end
+
+ context 'a CensorRule with an associated User' do
+
+ before(:each) do
+ @censor_rule = FactoryGirl.create(:user_censor_rule)
+ end
+
+ it 'purges the cache for the user' do
+ @controller.should_receive(:expire_requests_for_user).with(@censor_rule.user)
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ end
+
+ it 'redirects to the associated info request' do
+ delete :destroy, :censor_rule_id => @censor_rule.id
+ expect(response).to redirect_to(admin_user_show_path(@censor_rule.user))
+ end
+
+ end
+
+ end
+
+end
+
describe AdminCensorRuleController, "when making censor rules from the admin interface" do
render_views
before { basic_auth_login @request }
diff --git a/spec/controllers/admin_general_controller_spec.rb b/spec/controllers/admin_general_controller_spec.rb
index 971960762..cc2ec41b4 100644
--- a/spec/controllers/admin_general_controller_spec.rb
+++ b/spec/controllers/admin_general_controller_spec.rb
@@ -8,13 +8,8 @@ describe AdminGeneralController do
before { basic_auth_login @request }
it "should render the front page" do
- get :index, :suppress_redirect => 1
- response.should render_template('index')
- end
-
- it "should redirect to include trailing slash" do
get :index
- response.should redirect_to admin_general_index_url(:trailing_slash => true)
+ response.should render_template('index')
end
end
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..4c641bd75
--- /dev/null
+++ b/spec/controllers/admin_public_body_categories_controller_spec.rb
@@ -0,0 +1,234 @@
+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
+ expect(response).to be_success
+ 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
+
+ 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
+
+ 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
+
+ it "renders the form if creating the record was unsuccessful" do
+ post :create, :public_body_category => { :title => '' }
+ expect(response).to render_template('new')
+ 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 "finds the requested category" do
+ get :edit, :id => @category.id
+ expect(assigns[:category]).to eq(@category)
+ end
+
+ it "renders the edit template" do
+ get :edit, :id => @category.id
+ expect(assigns[:category]).to render_template('edit')
+ 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" } }
+
+ 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
+ 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" } }
+
+ 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 => '' } }
+
+ 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
+ end
+
+ it "destroys non-empty public body categories" do
+ 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
+ end
+ end
+end
diff --git a/spec/controllers/admin_public_body_change_requests_controller_spec.rb b/spec/controllers/admin_public_body_change_requests_controller_spec.rb
index b478e851d..003510e60 100644
--- a/spec/controllers/admin_public_body_change_requests_controller_spec.rb
+++ b/spec/controllers/admin_public_body_change_requests_controller_spec.rb
@@ -15,21 +15,36 @@ describe AdminPublicBodyChangeRequestsController, 'updating a change request' do
before do
@change_request = FactoryGirl.create(:add_body_request)
- post :update, { :id => @change_request.id,
- :response => 'Thanks but no',
- :subject => 'Your request' }
end
it 'should close the change request' do
+ post :update, { :id => @change_request.id }
PublicBodyChangeRequest.find(@change_request.id).is_open.should == false
end
- it 'should send a response email to the user who requested the change' do
- deliveries = ActionMailer::Base.deliveries
- deliveries.size.should == 1
- mail = deliveries[0]
- mail.subject.should == 'Your request'
- mail.to.should == [@change_request.get_user_email]
- mail.body.should =~ /Thanks but no/
+ context 'when a response and subject are passed' do
+
+ it 'should send a response email to the user who requested the change' do
+ post :update, { :id => @change_request.id,
+ :response => 'Thanks but no',
+ :subject => 'Your request' }
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.subject.should == 'Your request'
+ mail.to.should == [@change_request.get_user_email]
+ mail.body.should =~ /Thanks but no/
+ end
+
+ end
+
+ context 'when no response or subject are passed' do
+
+ it 'should send a response email to the user who requested the change' do
+ post :update, { :id => @change_request.id }
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 0
+ 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..afbe0fa30
--- /dev/null
+++ b/spec/controllers/admin_public_body_headings_controller_spec.rb
@@ -0,0 +1,275 @@
+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
+
+ 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
+
+ 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
+
+ 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 'when editing a public body heading' do
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ end
+
+ render_views
+
+ it "finds the requested heading" do
+ get :edit, :id => @heading.id
+ expect(assigns[:heading]).to eq(@heading)
+ end
+
+ it "renders the edit template" do
+ get :edit, :id => @heading.id
+ expect(assigns[:heading]).to render_template('edit')
+ 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
+
+ it "redirects to the edit page after a successful update" do
+ post :update, { :id => @heading.id,
+ :public_body_heading => { :name => "Renamed" } }
+
+ expect(response).to redirect_to(edit_admin_heading_path(@heading))
+ end
+
+ it "re-renders the edit form after an unsuccessful update" do
+ post :update, { :id => @heading.id,
+ :public_body_heading => { :name => '' } }
+
+ expect(response).to render_template('edit')
+ end
+
+ end
+
+ context 'when destroying a public body heading' do
+
+ before do
+ @heading = FactoryGirl.create(:public_body_heading)
+ end
+
+ it "destroys 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
+ 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
+ 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/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb
index f92323f50..9453c9461 100644
--- a/spec/controllers/help_controller_spec.rb
+++ b/spec/controllers/help_controller_spec.rb
@@ -4,6 +4,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe HelpController do
render_views
+ describe :index do
+
+ it 'redirects to the about page' do
+ get :index
+ expect(response).to redirect_to(help_about_path)
+ end
+
+ end
+
describe :about do
it 'shows the about page' do
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index f64975580..840b4bb28 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -204,16 +204,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"
-
- get :list, :tag => "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 => 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..4d0070470 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)
@@ -2364,6 +2380,23 @@ describe RequestController, "when doing type ahead searches" do
get :search_typeahead, :q => "dog -chicken"
assigns[:xapian_requests].results.size.should == 1
end
+
+ it 'can filter search results by public body' do
+ get :search_typeahead, :q => 'boring', :requested_from => 'dfh'
+ expect(assigns[:query]).to eq('requested_from:dfh boring')
+ end
+
+ it 'defaults to 25 results per page' do
+ get :search_typeahead, :q => 'boring'
+ expect(assigns[:per_page]).to eq(25)
+ end
+
+ it 'can limit the number of searches returned' do
+ get :search_typeahead, :q => 'boring', :per_page => '1'
+ expect(assigns[:per_page]).to eq(1)
+ expect(assigns[:xapian_requests].results.size).to eq(1)
+ end
+
end
describe RequestController, "when showing similar requests" do
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"