diff options
Diffstat (limited to 'spec/controllers')
20 files changed, 1183 insertions, 82 deletions
diff --git a/spec/controllers/admin_censor_rule_controller_spec.rb b/spec/controllers/admin_censor_rule_controller_spec.rb index 37ffd9764..68eaecd6a 100644 --- a/spec/controllers/admin_censor_rule_controller_spec.rb +++ b/spec/controllers/admin_censor_rule_controller_spec.rb @@ -1,19 +1,590 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe AdminCensorRuleController do + before(:each) { basic_auth_login(@request) } + + describe 'GET new' do + + context 'request_id param' do + + before do + @info_request = FactoryGirl.create(:info_request) + get :new, :request_id => @info_request.id, :name_prefix => 'request_' + end + + it 'returns a successful response' do + expect(response).to be_success + end + + it 'initializes a new censor rule' do + expect(assigns[:censor_rule]).to be_new_record + end + + it 'renders the correct template' do + expect(response).to render_template('new') + end + + it 'finds an info request if the request_id param is supplied' do + expect(assigns[:info_request]).to eq(@info_request) + end + + it 'associates the info request with the new censor rule' do + expect(assigns[:censor_rule].info_request).to eq(@info_request) + end + + it 'sets the URL for the form to POST to' do + expect(assigns[:form_url]).to eq(admin_request_censor_rules_path(@info_request)) + end + + end + + context 'user_id param' do + + before do + @user = FactoryGirl.create(:user) + get :new, :user_id => @user.id, :name_prefix => 'user_' + end + + it 'returns a successful response' do + expect(response).to be_success + end + + it 'initializes a new censor rule' do + expect(assigns[:censor_rule]).to be_new_record + end + + it 'renders the correct template' do + expect(response).to render_template('new') + end + + it 'finds a user if the user_id param is supplied' do + expect(assigns[:censor_user]).to eq(@user) + end + + it 'associates the user with the new censor rule' do + expect(assigns[:censor_rule].user).to eq(@user) + end + + it 'sets the URL for the form to POST to' do + expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(@user)) + end + + end + + end + + describe 'POST create' do + + context 'request_id param' 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) + @info_request = FactoryGirl.create(:info_request) + post :create, :request_id => @info_request.id, + :censor_rule => @censor_rule_params, + :name_prefix => 'request_' + end + + it 'sets the last_edit_editor to the current admin' do + expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*') + end + + it 'finds an info request if the request_id param is supplied' do + expect(assigns[:info_request]).to eq(@info_request) + end + + it 'associates the info request with the new censor rule' do + expect(assigns[:censor_rule].info_request).to eq(@info_request) + end + + it 'sets the URL for the form to POST to' do + expect(assigns[:form_url]).to eq(admin_request_censor_rules_path(@info_request)) + end + + context 'successfully saving the censor rule' do + + it 'persists the censor rule' do + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + expect(assigns[:censor_rule]).to be_persisted + end + + it 'confirms the censor rule is created' do + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + msg = 'CensorRule was successfully created.' + expect(flash[:notice]).to eq(msg) + end + + it 'purges the cache for the info request' do + @controller.should_receive(:expire_for_request). + with(@info_request) + + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + end + + it 'redirects to the associated info request' do + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + expect(response).to redirect_to( + admin_request_path(assigns[:censor_rule].info_request) + ) + 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, + :request_id => @info_request.id, + :name_prefix => 'request_' + expect(assigns[:censor_rule]).to be_new_record + end + + it 'renders the form' do + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + expect(response).to render_template('new') + end + + end + end + + context 'user_id param' 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) + @user = FactoryGirl.create(:user) + post :create, :user_id => @user.id, + :censor_rule => @censor_rule_params, + :name_prefix => 'user_' + end + + it 'sets the last_edit_editor to the current admin' do + expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*') + end + + it 'finds a user if the user_id param is supplied' do + expect(assigns[:censor_user]).to eq(@user) + end + + it 'associates the user with the new censor rule' do + expect(assigns[:censor_rule].user).to eq(@user) + end + + it 'sets the URL for the form to POST to' do + expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(@user)) + end + + context 'successfully saving the censor rule' do + + it 'purges the cache for the info request' do + censor_rule = CensorRule.new(@censor_rule_params) + @controller.should_receive(:expire_requests_for_user). + with(@user) + + post :create, :censor_rule => @censor_rule_params, + :user_id => @user.id, + :name_prefix => 'user_' + end + + it 'redirects to the associated info request' do + post :create, :censor_rule => @censor_rule_params, + :user_id => @user.id, + :name_prefix => 'user_' + expect(response).to redirect_to( + admin_user_path(assigns[:censor_rule].user) + ) + 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, + :user_id => @user.id, + :name_prefix => 'user_' + expect(assigns[:censor_rule]).to be_new_record + end + + it 'renders the form' do + post :create, :censor_rule => @censor_rule_params, + :user_id => @user.id, + :name_prefix => 'user_' + expect(response).to render_template('new') + end + + end + + end + + end + + describe 'GET edit' do + + context 'a CensorRule with an associated InfoRequest' do + + before(:each) do + @censor_rule = FactoryGirl.create(:info_request_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 + + context 'a CensorRule with an associated User' do + + before(:each) do + @censor_rule = FactoryGirl.create(:user_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 + + context 'when editing a global rule' do + + before(:each) do + @censor_rule = FactoryGirl.create(:global_censor_rule) + end + + it 'shows an error notice' do + get :edit, :id => @censor_rule.id + flash[:notice].should == 'Only user and request censor rules can be edited' + end + + it 'redirects to the admin index' do + get :edit, :id => @censor_rule.id + expect(response).to redirect_to(admin_general_index_path) + end + + end + + end + + describe 'PUT update' do + + context 'a global CensorRule' do + + before(:each) do + @censor_rule = FactoryGirl.create(:global_censor_rule) + end + + it 'shows an error notice' do + get :edit, :id => @censor_rule.id + flash[:notice].should == 'Only user and request censor rules can be edited' + end + + it 'redirects to the admin index' do + get :edit, :id => @censor_rule.id + expect(response).to redirect_to(admin_general_index_path) + end + + end + + context 'a CensorRule with an associated InfoRequest' do + + before(:each) do + @censor_rule = FactoryGirl.create(:info_request_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 + + it 'updates the censor rule' do + 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 + put :update, :id => @censor_rule.id, + :censor_rule => { :text => 'different text' } + msg = 'CensorRule was successfully updated.' + expect(flash[:notice]).to eq(msg) + 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_path(assigns[:censor_rule].info_request) + ) + 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 + + context 'a CensorRule with an associated User' do + + before(:each) do + @censor_rule = FactoryGirl.create(:user_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 + + it 'updates the censor rule' do + 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 + put :update, :id => @censor_rule.id, + :censor_rule => { :text => 'different text' } + msg = 'CensorRule was successfully updated.' + expect(flash[:notice]).to eq(msg) + 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_path(assigns[:censor_rule].user) + ) + 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 + + end + + describe 'DELETE destroy' do + + context 'a global CensorRule' do + + before(:each) do + @censor_rule = FactoryGirl.create(:global_censor_rule) + end + + it 'shows an error notice' do + get :edit, :id => @censor_rule.id + flash[:notice].should == 'Only user and request censor rules can be edited' + end + + it 'redirects to the admin index' do + get :edit, :id => @censor_rule.id + expect(response).to redirect_to(admin_general_index_path) + end + + end + + context 'a CensorRule with an associated InfoRequest' do + + before(:each) do + @censor_rule = FactoryGirl.create(:info_request_censor_rule) + end + + it 'finds the correct censor rule to destroy' do + delete :destroy, :id => @censor_rule.id + expect(assigns[:censor_rule]).to eq(@censor_rule) + end + + it 'confirms the censor rule is destroyed in all cases' do + delete :destroy, :id => @censor_rule.id + msg = 'CensorRule was successfully destroyed.' + expect(flash[:notice]).to eq(msg) + end + + it 'purges the cache for the info request' do + @controller.should_receive(:expire_for_request).with(@censor_rule.info_request) + delete :destroy, :id => @censor_rule.id + end + + it 'redirects to the associated info request' do + delete :destroy, :id => @censor_rule.id + expect(response).to redirect_to(admin_request_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 'finds the correct censor rule to destroy' do + delete :destroy, :id => @censor_rule.id + expect(assigns[:censor_rule]).to eq(@censor_rule) + end + + it 'confirms the censor rule is destroyed in all cases' do + delete :destroy, :id => @censor_rule.id + msg = 'CensorRule was successfully destroyed.' + expect(flash[:notice]).to eq(msg) + end + + it 'purges the cache for the user' do + @controller.should_receive(:expire_requests_for_user).with(@censor_rule.user) + delete :destroy, :id => @censor_rule.id + end + + it 'redirects to the associated info request' do + delete :destroy, :id => @censor_rule.id + expect(response).to redirect_to(admin_user_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 } - + it "should create a censor rule and purge the corresponding request from varnish" do - ir = info_requests(:fancy_dog_request) - post :create, :censor_rule => { + ir = info_requests(:fancy_dog_request) + post :create, :request_id => ir.id, + :name_prefix => 'request_', + :censor_rule => { :text => "meat", :replacement => "tofu", - :last_edit_comment => "none", - :info_request_id => ir + :last_edit_comment => "none" } PurgeRequest.all().first.model_id.should == ir.id end - end diff --git a/spec/controllers/admin_comment_controller_spec.rb b/spec/controllers/admin_comment_controller_spec.rb new file mode 100644 index 000000000..f87231e3b --- /dev/null +++ b/spec/controllers/admin_comment_controller_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminCommentController do + + describe :edit do + + before do + @comment = FactoryGirl.create(:comment) + get :edit, :id => @comment.id + end + + it 'renders the edit template' do + expect(response).to render_template('edit') + end + + it 'gets the comment' do + assigns[:comment].should == @comment + end + + end + + describe :update do + + context 'on valid data submission' do + + before do + @comment = FactoryGirl.create(:comment) + atts = FactoryGirl.attributes_for(:comment, :body => 'I am new') + put :update, :id => @comment.id, :comment => atts + end + + it 'gets the comment' do + assigns[:comment].should == @comment + end + + it 'updates the comment' do + Comment.find(@comment.id).body.should == 'I am new' + end + + it 'logs the update event' do + most_recent_event = Comment.find(@comment.id).info_request_events.last + most_recent_event.event_type.should == 'edit_comment' + most_recent_event.comment_id.should == @comment.id + end + + it 'shows a success notice' do + flash[:notice].should == "Comment successfully updated." + end + + it 'redirects to the request page' do + response.should redirect_to(admin_request_path(@comment.info_request)) + end + end + + context 'on invalid data submission' do + + it 'renders the edit template' do + @comment = FactoryGirl.create(:comment) + put :update, :id => @comment.id, :comment => {:body => ''} + response.should render_template('edit') + end + + end + end + +end diff --git a/spec/controllers/admin_holiday_imports_controller_spec.rb b/spec/controllers/admin_holiday_imports_controller_spec.rb new file mode 100644 index 000000000..dd23a022f --- /dev/null +++ b/spec/controllers/admin_holiday_imports_controller_spec.rb @@ -0,0 +1,73 @@ +require 'spec_helper' + +describe AdminHolidayImportsController do + + describe :new do + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + + it 'creates an import' do + get :new + assigns[:holiday_import].should be_instance_of(HolidayImport) + end + + describe 'if the import is valid' do + + it 'populates the import' do + mock_import = mock(HolidayImport, :valid? => true, + :populate => nil) + HolidayImport.stub!(:new).and_return(mock_import) + mock_import.should_receive(:populate) + get :new + end + + end + + end + + describe :create do + + it 'creates an import' do + post :create + assigns[:holiday_import].should be_instance_of(HolidayImport) + end + + describe 'if the import can be saved' do + + before do + mock_import = mock(HolidayImport, :save => true) + HolidayImport.stub!(:new).and_return(mock_import) + post :create + end + + it 'should show a success notice' do + flash[:notice].should == 'Holidays successfully imported' + end + + it 'should redirect to the index' do + response.should redirect_to(admin_holidays_path) + end + + end + + describe 'if the import cannot be saved' do + + before do + mock_import = mock(HolidayImport, :save => false) + HolidayImport.stub!(:new).and_return(mock_import) + post :create + end + + it 'should render the new template' do + expect(response).to render_template('new') + end + + end + + end + + +end diff --git a/spec/controllers/admin_holidays_controller_spec.rb b/spec/controllers/admin_holidays_controller_spec.rb new file mode 100644 index 000000000..21cb51d29 --- /dev/null +++ b/spec/controllers/admin_holidays_controller_spec.rb @@ -0,0 +1,192 @@ +require 'spec_helper' + +describe AdminHolidaysController do + + describe :index do + + before do + @holiday_one = FactoryGirl.create(:holiday, :day => Date.new(2010, 1, 1)) + @holiday_two = FactoryGirl.create(:holiday, :day => Date.new(2011, 2, 2)) + @holiday_three = FactoryGirl.create(:holiday, :day => Date.new(2011, 3, 3)) + end + + it 'gets a hash of holidays keyed by year' do + get :index + assigns(:holidays_by_year)[2010].should include(@holiday_one) + assigns(:holidays_by_year)[2011].should include(@holiday_two) + assigns(:holidays_by_year)[2011].should include(@holiday_three) + end + + it 'gets a list of years with holidays' do + get :index + assigns(:years).should include(2010) + assigns(:years).should include(2011) + end + + it 'renders the index template' do + get :index + expect(response).to render_template('index') + end + + end + + describe :new do + + + describe 'when not using ajax' do + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + + end + + describe 'when using ajax' do + + it 'renders the new form partial' do + xhr :get, :new + expect(response).to render_template('new_form') + end + end + + it 'creates a new holiday' do + get :new + assigns[:holiday].should be_instance_of(Holiday) + end + + end + + describe :create do + + before do + @holiday_params = { :description => "New Year's Day", + 'day(1i)' => '2010', + 'day(2i)' => '1', + 'day(3i)' => '1' } + post :create, :holiday => @holiday_params + end + + it 'creates a new holiday' do + assigns(:holiday).description.should == @holiday_params[:description] + assigns(:holiday).day.should == Date.new(2010, 1, 1) + assigns(:holiday).should be_persisted + end + + it 'shows the admin a success message' do + flash[:notice].should == 'Holiday successfully created.' + end + + it 'redirects to the index' do + response.should redirect_to admin_holidays_path + end + + context 'when there are errors' do + + before do + Holiday.any_instance.stub(:save).and_return(false) + post :create, :holiday => @holiday_params + end + + it 'renders the new template' do + expect(response).to render_template('new') + end + end + + end + + describe :edit do + + before do + @holiday = FactoryGirl.create(:holiday) + end + + describe 'when not using ajax' do + + it 'renders the edit template' do + get :edit, :id => @holiday.id + expect(response).to render_template('edit') + end + + end + + describe 'when using ajax' do + + it 'renders the edit form partial' do + xhr :get, :edit, :id => @holiday.id + expect(response).to render_template('edit_form') + end + + end + + it 'gets the holiday in the id param' do + get :edit, :id => @holiday.id + assigns[:holiday].should == @holiday + end + + end + + describe :update do + + before do + @holiday = FactoryGirl.create(:holiday, :day => Date.new(2010, 1, 1), + :description => "Test Holiday") + put :update, :id => @holiday.id, :holiday => { :description => 'New Test Holiday' } + end + + it 'gets the holiday in the id param' do + assigns[:holiday].should == @holiday + end + + it 'updates the holiday' do + holiday = Holiday.find(@holiday.id).description.should == 'New Test Holiday' + end + + it 'shows the admin a success message' do + flash[:notice].should == 'Holiday successfully updated.' + end + + it 'redirects to the index' do + response.should redirect_to admin_holidays_path + end + + context 'when there are errors' do + + before do + Holiday.any_instance.stub(:update_attributes).and_return(false) + put :update, :id => @holiday.id, :holiday => { :description => 'New Test Holiday' } + end + + it 'renders the edit template' do + expect(response).to render_template('edit') + end + end + + end + + describe :destroy do + + before(:each) do + @holiday = FactoryGirl.create(:holiday) + delete :destroy, :id => @holiday.id + end + + it 'finds the holiday to destroy' do + assigns(:holiday).should == @holiday + end + + it 'destroys the holiday' do + assigns(:holiday).should be_destroyed + end + + it 'tells the admin the holiday has been destroyed' do + msg = "Holiday successfully destroyed" + flash[:notice].should == msg + end + + it 'redirects to the index action' do + expect(response).to redirect_to(admin_holidays_path) + end + end + + end diff --git a/spec/controllers/admin_incoming_message_controller_spec.rb b/spec/controllers/admin_incoming_message_controller_spec.rb index 21c744e5b..24a526ca4 100644 --- a/spec/controllers/admin_incoming_message_controller_spec.rb +++ b/spec/controllers/admin_incoming_message_controller_spec.rb @@ -17,19 +17,19 @@ describe AdminIncomingMessageController, "when administering incoming messages" it "destroys the raw email file" do raw_email = @im.raw_email.filepath assert_equal File.exists?(raw_email), true - post :destroy, :incoming_message_id => @im.id + post :destroy, :id => @im.id assert_equal File.exists?(raw_email), false end it 'asks the incoming message to fully destroy itself' do IncomingMessage.stub!(:find).and_return(@im) @im.should_receive(:fully_destroy) - post :destroy, :incoming_message_id => @im.id + post :destroy, :id => @im.id end it 'expires the file cache for the associated info_request' do @controller.should_receive(:expire_for_request).with(@im.info_request) - post :destroy, :incoming_message_id => @im.id + post :destroy, :id => @im.id end end @@ -46,7 +46,7 @@ describe AdminIncomingMessageController, "when administering incoming messages" destination_info_request = info_requests(:naughty_chicken_request) incoming_message = incoming_messages(:useless_incoming_message) @controller.should_receive(:expire_for_request).with(current_info_request) - post :redeliver, :redeliver_incoming_message_id => incoming_message.id, + post :redeliver, :id => incoming_message.id, :url_title => destination_info_request.url_title end @@ -56,7 +56,7 @@ describe AdminIncomingMessageController, "when administering incoming messages" current_info_request = info_requests(:fancy_dog_request) destination_info_request = info_requests(:naughty_chicken_request) incoming_message = incoming_messages(:useless_incoming_message) - post :redeliver, :redeliver_incoming_message_id => incoming_message.id, + post :redeliver, :id => incoming_message.id, :url_title => destination_info_request.url_title end @@ -130,7 +130,7 @@ describe AdminIncomingMessageController, "when administering incoming messages" it 'should redirect to the admin info request view' do make_request - response.should redirect_to admin_request_show_url(@incoming.info_request) + response.should redirect_to admin_request_url(@incoming.info_request) end it 'should show a message that the incoming message has been updated' do diff --git a/spec/controllers/admin_info_request_event_controller_spec.rb b/spec/controllers/admin_info_request_event_controller_spec.rb new file mode 100644 index 000000000..23300a0b8 --- /dev/null +++ b/spec/controllers/admin_info_request_event_controller_spec.rb @@ -0,0 +1,41 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminInfoRequestEventController do + + describe :update do + + describe 'when handling valid data' do + + before do + @info_request_event = FactoryGirl.create(:info_request_event) + put :update, :id => @info_request_event + end + + it 'gets the info request event' do + assigns[:info_request_event].should == @info_request_event + end + + it 'sets the described and calculated states on the event' do + event = InfoRequestEvent.find(@info_request_event.id) + event.described_state.should == 'waiting_clarification' + event.calculated_state.should == 'waiting_clarification' + end + + it 'shows a success notice' do + flash[:notice].should == 'Old response marked as having been a clarification' + end + + it 'redirects to the request admin page' do + response.should redirect_to(admin_request_url(@info_request_event.info_request)) + end + end + + it 'raises an exception if the event is not a response' do + @info_request_event = FactoryGirl.create(:sent_event) + lambda{ put :update, :id => @info_request_event }.should raise_error + end + + end + +end diff --git a/spec/controllers/admin_outgoing_message_controller_spec.rb b/spec/controllers/admin_outgoing_message_controller_spec.rb index 0dde53b86..a46a077da 100644 --- a/spec/controllers/admin_outgoing_message_controller_spec.rb +++ b/spec/controllers/admin_outgoing_message_controller_spec.rb @@ -79,7 +79,7 @@ describe AdminOutgoingMessageController do it 'should redirect to the admin info request view' do make_request - response.should redirect_to admin_request_show_url(@info_request) + response.should redirect_to admin_request_url(@info_request) end it 'should show a message that the incoming message has been updated' do diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb index 35454990d..4c641bd75 100644 --- a/spec/controllers/admin_public_body_categories_controller_spec.rb +++ b/spec/controllers/admin_public_body_categories_controller_spec.rb @@ -6,6 +6,7 @@ describe AdminPublicBodyCategoriesController do it 'shows the index page' do get :index + expect(response).to be_success end end @@ -14,6 +15,12 @@ describe AdminPublicBodyCategoriesController 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 @@ -44,7 +51,6 @@ describe AdminPublicBodyCategoriesController do category.public_body_headings.should == [heading] end - it 'creates a new public body category with multiple locales' do n = PublicBodyCategory.count post :create, { @@ -69,6 +75,12 @@ describe AdminPublicBodyCategoriesController do 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 @@ -82,14 +94,21 @@ describe AdminPublicBodyCategoriesController do render_views - it "edits a public body category" do + it "finds the requested category" do get :edit, :id => @category.id + expect(assigns[:category]).to eq(@category) + end + + it "renders the edit template" do + 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} + get :edit, { :id => @category.id, :locale => :en } - # When editing a body, the controller returns all available translations + # 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 @@ -161,7 +180,9 @@ describe AdminPublicBodyCategoriesController 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') + + 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 @@ -175,18 +196,39 @@ describe AdminPublicBodyCategoriesController do 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 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 - 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_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 095d23245..7de292303 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -272,7 +272,7 @@ describe AdminPublicBodyController, "when destroying a public body" do it "destroys a public body" do n = PublicBody.count post :destroy, { :id => public_bodies(:forlorn_public_body).id } - response.should redirect_to(:controller=>'admin_public_body', :action=>'list') + response.should redirect_to admin_bodies_path PublicBody.count.should == n - 1 end @@ -286,7 +286,7 @@ describe AdminPublicBodyController, "when assigning public body tags" do n = PublicBody.joins(:translations).where([condition, "en"]).count post :mass_tag_add, { :new_tag => "department", :table_name => "substring" } request.flash[:notice].should == "Added tag to table of bodies." - response.should redirect_to(:action=>'list') + response.should redirect_to admin_bodies_path PublicBody.find_by_tag("department").count.should == n end end diff --git a/spec/controllers/admin_public_body_headings_controller_spec.rb b/spec/controllers/admin_public_body_headings_controller_spec.rb index 31517d238..afbe0fa30 100644 --- a/spec/controllers/admin_public_body_headings_controller_spec.rb +++ b/spec/controllers/admin_public_body_headings_controller_spec.rb @@ -7,6 +7,11 @@ describe AdminPublicBodyHeadingsController 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 @@ -45,6 +50,12 @@ describe AdminPublicBodyHeadingsController do 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 @@ -54,8 +65,14 @@ describe AdminPublicBodyHeadingsController do render_views - it "edits a public body heading" do + 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 @@ -96,6 +113,21 @@ describe AdminPublicBodyHeadingsController 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 @@ -104,16 +136,19 @@ describe AdminPublicBodyHeadingsController do @heading = FactoryGirl.create(:public_body_heading) end - it "does not destroy a public body heading that has associated categories" do + 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(edit_admin_heading_path(@heading)) - PublicBodyHeading.count.should == n + 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 diff --git a/spec/controllers/admin_raw_email_controller_spec.rb b/spec/controllers/admin_raw_email_controller_spec.rb new file mode 100644 index 000000000..77c57c38b --- /dev/null +++ b/spec/controllers/admin_raw_email_controller_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminRawEmailController do + + describe :show do + + before do + @raw_email = FactoryGirl.create(:incoming_message).raw_email + end + + describe 'html version' do + + it 'renders the show template' do + get :show, :id => @raw_email.id + end + + end + + describe 'text version' do + + it 'sends the email as an RFC-822 attachment' do + get :show, :id => @raw_email.id, :format => 'txt' + response.content_type.should == 'message/rfc822' + response.body.should == @raw_email.data + end + end + + end + +end diff --git a/spec/controllers/admin_request_controller_spec.rb b/spec/controllers/admin_request_controller_spec.rb index 7c5253f49..4eb463963 100644 --- a/spec/controllers/admin_request_controller_spec.rb +++ b/spec/controllers/admin_request_controller_spec.rb @@ -57,12 +57,12 @@ describe AdminRequestController, "when administering requests" do it 'expires the file cache for that request' do info_request = info_requests(:badger_request) @controller.should_receive(:expire_for_request).with(info_request) - get :fully_destroy, { :id => info_request } + get :destroy, { :id => info_request } end it 'uses a different flash message to avoid trying to fetch a non existent user record' do info_request = info_requests(:external_request) - post :fully_destroy, { :id => info_request.id } + post :destroy, { :id => info_request.id } request.flash[:notice].should include('external') end @@ -77,34 +77,6 @@ describe AdminRequestController, "when administering the holding pen" do load_raw_emails_data end - it "shows a rejection reason for an incoming message from an invalid address" do - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") - get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_public_response.raw_email.id - response.should contain "Only the authority can reply to this request" - end - - it "guesses a misdirected request" do - ir = info_requests(:fancy_dog_request) - ir.handle_rejected_responses = 'holding_pen' - ir.allow_new_responses_from = 'authority_only' - ir.save! - mail_to = "request-#{ir.id}-asdfg@example.com" - receive_incoming_mail('incoming-request-plain.email', mail_to) - interesting_email = InfoRequest.holding_pen_request.get_last_public_response.raw_email.id - # now we add another message to the queue, which we're not interested in - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") - InfoRequest.holding_pen_request.incoming_messages.length.should == 2 - get :show_raw_email, :id => interesting_email - response.should contain "Could not identify the request" - assigns[:info_requests][0].should == ir - end - - - it "shows a suitable default 'your email has been hidden' message" do ir = info_requests(:fancy_dog_request) get :show, :id => ir.id @@ -119,7 +91,7 @@ describe AdminRequestController, "when administering the holding pen" do it "hides requests and sends a notification email that it has done so" do ir = info_requests(:fancy_dog_request) - post :hide_request, :id => ir.id, :explanation => "Foo", :reason => "vexatious" + post :hide, :id => ir.id, :explanation => "Foo", :reason => "vexatious" ir.reload ir.prominence.should == "requester_only" ir.described_state.should == "vexatious" @@ -132,7 +104,7 @@ describe AdminRequestController, "when administering the holding pen" do it 'expires the file cache for the request' do ir = info_requests(:fancy_dog_request) @controller.should_receive(:expire_for_request).with(ir) - post :hide_request, :id => ir.id, :explanation => "Foo", :reason => "vexatious" + post :hide, :id => ir.id, :explanation => "Foo", :reason => "vexatious" end describe 'when hiding an external request' do @@ -153,7 +125,7 @@ describe AdminRequestController, "when administering the holding pen" do end def make_request(params=@default_params) - post :hide_request, params + post :hide, params end it 'should redirect the the admin page for the request' do diff --git a/spec/controllers/admin_track_controller_spec.rb b/spec/controllers/admin_track_controller_spec.rb index f2de6c0d3..d29db4966 100644 --- a/spec/controllers/admin_track_controller_spec.rb +++ b/spec/controllers/admin_track_controller_spec.rb @@ -1,9 +1,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe AdminTrackController, "when administering tracks" do - render_views - - it "shows the list page" do - get :list + + it "shows the index page" do + get :index end end diff --git a/spec/controllers/admin_user_controller_spec.rb b/spec/controllers/admin_user_controller_spec.rb index 8b89506f9..e979355cf 100644 --- a/spec/controllers/admin_user_controller_spec.rb +++ b/spec/controllers/admin_user_controller_spec.rb @@ -2,13 +2,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe AdminUserController, "when administering users" do render_views - - it "shows the index/list page" do + + it "shows the index page" do get :index end it "searches for 'bob'" do - get :list, :query => "bob" + get :index, :query => "bob" assigns[:admin_users].should == [ users(:bob_smith_user) ] end @@ -51,7 +51,7 @@ describe AdminUserController do before(:each) do @user = FactoryGirl.create(:user) - request.env["HTTP_REFERER"] = admin_user_show_path(@user) + request.env["HTTP_REFERER"] = admin_user_path(@user) end it 'redirects to the page the admin was previously on' do @@ -61,7 +61,7 @@ describe AdminUserController do :comment_ids => comment.id, :hide_selected => 'hidden' } - response.should redirect_to(admin_user_show_path(@user)) + response.should redirect_to(admin_user_path(@user)) end it 'sets the given comments visibility to hidden' do diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index c0a9d57d3..28dac7b96 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -53,6 +53,18 @@ describe GeneralController, 'when getting the blog feed' do end end + context 'when the blog has entries' do + + render_views + + it 'should escape any javascript from the entries' do + controller.stub!(:quietly_try_to_open).and_return(load_file_fixture("blog_feed.atom")) + get :blog + response.body.should_not include('<script>alert("exciting!")</script>') + end + + end + end describe GeneralController, "when showing the frontpage" do @@ -126,6 +138,35 @@ describe GeneralController, "when showing the frontpage" do end + describe 'when handling logged-in users' do + + before do + @user = FactoryGirl.create(:user) + session[:user_id] = @user.id + end + + it 'should set a time to live on a non "remember me" session' do + get :frontpage + response.body.should match @user.name + session[:ttl].should be_within(1).of(Time.now) + end + + it 'should not set a time to live on a "remember me" session' do + session[:remember_me] = true + get :frontpage + response.body.should match @user.name + session[:ttl].should be_nil + end + + it 'should end a logged-in session whose ttl has expired' do + session[:ttl] = Time.now - 4.hours + get :frontpage + response.should redirect_to signin_path + session[:user_id].should be_nil + 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_change_requests_controller_spec.rb b/spec/controllers/public_body_change_requests_controller_spec.rb index 8fe7befeb..4053b2f40 100644 --- a/spec/controllers/public_body_change_requests_controller_spec.rb +++ b/spec/controllers/public_body_change_requests_controller_spec.rb @@ -28,6 +28,7 @@ describe PublicBodyChangeRequestsController, "creating a change request" do it "should send an email to the site contact address" do post :create, {:public_body_change_request => @change_request_params} + change_request_id = assigns[:change_request].id deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] @@ -37,8 +38,8 @@ describe PublicBodyChangeRequestsController, "creating a change request" do mail.body.should include('new_body@example.com') mail.body.should include('New Body') mail.body.should include("Please") - mail.body.should include('http://test.host/admin/body/new?change_request_id=') - mail.body.should include('http://test.host/admin/change_request/edit/') + mail.body.should include("http://test.host/admin/bodies/new?change_request_id=#{change_request_id}") + mail.body.should include("http://test.host/admin/change_requests/#{change_request_id}/edit") end it 'should show a notice' do @@ -83,6 +84,7 @@ describe PublicBodyChangeRequestsController, "creating a change request" do it 'should send an email to the site contact address' do post :create, {:public_body_change_request => @change_request_params} + change_request_id = assigns[:change_request].id deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] @@ -92,8 +94,8 @@ describe PublicBodyChangeRequestsController, "creating a change request" do mail.body.should include('new_body@example.com') mail.body.should include(@public_body.name) mail.body.should include("Please") - mail.body.should include("http://test.host/admin/body/edit/#{@public_body.id}?change_request_id=") - mail.body.should include('http://test.host/admin/change_request/edit/') + mail.body.should include("http://test.host/admin/bodies/#{@public_body.id}/edit?change_request_id=#{change_request_id}") + mail.body.should include("http://test.host/admin/change_requests/#{change_request_id}/edit") end it 'should show a notice' do diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index fc7143522..840b4bb28 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -7,7 +7,6 @@ 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 @@ -76,10 +75,6 @@ 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 diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 6c0f4573e..2d3ccfa63 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -596,6 +596,18 @@ describe RequestController, "when showing one request" do response.status.should == 303 end + it "should sanitise HTML attachments" do + incoming_message = FactoryGirl.create(:incoming_message_with_html_attachment) + get :get_attachment, :incoming_message_id => incoming_message.id, + :id => incoming_message.info_request.id, + :part => 2, + :file_name => 'interesting.html', + :skip_cache => 1 + response.body.should_not match("script") + response.body.should_not match("interesting") + response.body.should match('dull') + end + it "should censor attachments downloaded as binary" do ir = info_requests(:fancy_dog_request) @@ -2380,6 +2392,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 @@ -2430,7 +2459,7 @@ describe RequestController, "when caching fragments" do :info_request_id => 132, :id => 44, :get_attachments_for_display => nil, - :html_mask_stuff! => nil, + :apply_masks! => nil, :user_can_view? => true, :all_can_view? => true) attachment = FactoryGirl.build(:body_text, :filename => long_name) @@ -2643,7 +2672,7 @@ describe RequestController, "#select_authorities" do end - context 'when asked for JSON', :focus => true do + context 'when asked for JSON' do it 'should be successful' do get :select_authorities, {:public_body_query => "Quan", :format => 'json'}, {:user_id => @user.id} diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index 14731f090..248c97ad4 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -60,21 +60,25 @@ describe ServicesController, "when returning a message for people in other count response.should be_success response.body.should == 'Hello! We have an <a href="/help/alaveteli?country_name=Deutschland">important message</a> for visitors outside Deutschland' end + it "should default to no message if the country_from_ip domain doesn't exist" do AlaveteliConfiguration.stub!(:gaze_url).and_return('http://12123sdf14qsd.com') get :other_country_message response.should be_success response.body.should == '' end + it "should default to no message if the country_from_ip service doesn't exist" do AlaveteliConfiguration.stub!(:gaze_url).and_return('http://www.google.com') get :other_country_message response.should be_success response.body.should == '' end - it "should default to no message if the country_from_ip service returns an error" do + + it "should default to no message and log the error with url if the country_from_ip service returns an error" do FakeWeb.register_uri(:get, %r|500.com|, :body => "Error", :status => ["500", "Error"]) AlaveteliConfiguration.stub!(:gaze_url).and_return('http://500.com') + Rails.logger.should_receive(:warn).with /500\.com.*500 Error/ get :other_country_message response.should be_success response.body.should == '' |