diff options
Diffstat (limited to 'spec/controllers')
20 files changed, 2257 insertions, 735 deletions
diff --git a/spec/controllers/admin_censor_rule_controller_spec.rb b/spec/controllers/admin_censor_rule_controller_spec.rb index 4df56a92b..68eaecd6a 100644 --- a/spec/controllers/admin_censor_rule_controller_spec.rb +++ b/spec/controllers/admin_censor_rule_controller_spec.rb @@ -5,76 +5,68 @@ describe AdminCensorRuleController do describe 'GET new' do - it 'returns a successful response' do - get :new - expect(response).to be_success - end + context 'request_id param' do - it 'initializes a new censor rule' do - get :new - expect(assigns[:censor_rule]).to be_new_record - end + before do + @info_request = FactoryGirl.create(:info_request) + get :new, :request_id => @info_request.id, :name_prefix => 'request_' + end - it 'renders the correct template' do - get :new - expect(response).to render_template('new') - end + it 'returns a successful response' do + expect(response).to be_success + 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 + it 'initializes a new censor rule' do + expect(assigns[:censor_rule]).to be_new_record + end - context 'info_request_id param' do + it 'renders the correct template' do + expect(response).to render_template('new') + end - 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) + 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 - info_request = FactoryGirl.create(:info_request) - get :new, :info_request_id => info_request.id - expect(assigns[:censor_rule].info_request).to eq(info_request) + 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 + 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 - user = FactoryGirl.create(:user) - get :new, :user_id => user.id - expect(assigns[:censor_user]).to eq(user) + 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) + 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 + expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(@user)) end end @@ -83,256 +75,292 @@ describe AdminCensorRuleController do 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 + context 'request_id param' do - 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 + 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 - context 'info_request_id param' do + 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 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) + 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 - 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) + 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)) + expect(assigns[:form_url]).to eq(admin_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 + context 'successfully saving the censor rule' do - end + 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 - context 'user_id param' do + 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 '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 'purges the cache for the info request' do + @controller.should_receive(:expire_for_request). + with(@info_request) - 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 + post :create, :censor_rule => @censor_rule_params, + :request_id => @info_request.id, + :name_prefix => 'request_' + 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)) + 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 - 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 + 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 'successfully saving the censor rule' do + context 'user_id param' do before(:each) do - CensorRule.any_instance.stub(:save).and_return(true) + @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 '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 + it 'sets the last_edit_editor to the current admin' do + expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*') 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) + it 'finds a user if the user_id param is supplied' do + expect(assigns[:censor_user]).to eq(@user) end - it 'raises an error after creating the rule' do - expect { - post :create, :censor_rule => @censor_rule_params - }.to raise_error 'internal error' + it 'associates the user with the new censor rule' do + expect(assigns[:censor_rule].user).to eq(@user) end - context 'a CensorRule with an associated InfoRequest' do + it 'sets the URL for the form to POST to' do + expect(assigns[:form_url]).to eq(admin_user_censor_rules_path(@user)) + end - 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 + context 'successfully saving the censor rule' do - it 'purges the cache for the info request' do + 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) + @controller.should_receive(:expire_requests_for_user). + with(@user) - post :create, :censor_rule => @censor_rule_params + 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 + post :create, :censor_rule => @censor_rule_params, + :user_id => @user.id, + :name_prefix => 'user_' expect(response).to redirect_to( - admin_request_show_path(assigns[:censor_rule].info_request) + admin_user_path(assigns[:censor_rule].user) ) end end - context 'a CensorRule with an associated User' do + context 'unsuccessfully saving the censor rule' 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) + CensorRule.any_instance.stub(:save).and_return(false) 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 + 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 '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) - ) + 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 - context 'unsuccessfully saving the censor rule' do + end + + describe 'GET edit' do + + context 'a CensorRule with an associated InfoRequest' do before(:each) do - CensorRule.any_instance.stub(:save).and_return(false) + @censor_rule = FactoryGirl.create(:info_request_censor_rule) end - it 'does not persist the censor rule' do - post :create, :censor_rule => @censor_rule_params - expect(assigns[:censor_rule]).to be_new_record + it 'returns a successful response' do + get :edit, :id => @censor_rule.id + expect(response).to be_success end - it 'renders the form' do - post :create, :censor_rule => @censor_rule_params - expect(response).to render_template('new') + 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 - end + context 'a CensorRule with an associated User' do - describe 'GET edit' do + before(:each) do + @censor_rule = FactoryGirl.create(:user_censor_rule) + end - 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 '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 - 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) + 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 - before(:each) do - @censor_rule = FactoryGirl.create(:global_censor_rule) - end + context 'a global CensorRule' do - it 'finds the correct censor rule to edit' do - put :update, :id => @censor_rule.id, - :censor_rule => { :text => 'different text' } + before(:each) do + @censor_rule = FactoryGirl.create(:global_censor_rule) + end - expect(assigns[:censor_rule]).to eq(@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 'sets the last_edit_editor to the current admin' do - put :update, :id => @censor_rule.id, - :censor_rule => { :text => 'different text' } + it 'redirects to the admin index' do + get :edit, :id => @censor_rule.id + expect(response).to redirect_to(admin_general_index_path) + end - expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*') end - context 'successfully saving the censor rule' do + context 'a CensorRule with an associated InfoRequest' do before(:each) do - CensorRule.any_instance.stub(:save).and_return(true) + @censor_rule = FactoryGirl.create(:info_request_censor_rule) end - it 'updates the censor rule' do - pending("This raises an internal error in most cases") + it 'finds the correct censor rule to edit' do put :update, :id => @censor_rule.id, :censor_rule => { :text => 'different text' } - @censor_rule.reload - expect(@censor_rule.text).to eq('different text') + + expect(assigns[:censor_rule]).to eq(@censor_rule) end - it 'confirms the censor rule is updated' do - pending("This raises an internal error in most cases") + it 'sets the last_edit_editor to the current admin' do put :update, :id => @censor_rule.id, :censor_rule => { :text => 'different text' } - msg = 'CensorRule was successfully updated.' - expect(flash[:notice]).to eq(msg) + expect(assigns[:censor_rule].last_edit_editor).to eq('*unknown*') end - it 'raises an error after updating the rule' do - expect { + context 'successfully saving the censor rule' do + + it 'updates the censor rule' do 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 + @censor_rule.reload + expect(@censor_rule.text).to eq('different text') + end - before(:each) do - @censor_rule = FactoryGirl.create(:info_request_censor_rule) + 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 @@ -348,87 +376,137 @@ describe AdminCensorRuleController do :censor_rule => { :text => 'different text' } expect(response).to redirect_to( - admin_request_show_path(assigns[:censor_rule].info_request) + admin_request_path(assigns[:censor_rule].info_request) ) end end - context 'a CensorRule with an associated User' do + context 'unsuccessfully saving the censor rule' do before(:each) do - @censor_rule = FactoryGirl.create(:user_censor_rule) + CensorRule.any_instance.stub(:save).and_return(false) end - it 'purges the cache for the info request' do - @controller.should_receive(:expire_requests_for_user). - with(@censor_rule.user) - + 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 'redirects to the associated info request' do + it 'renders the form' 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) - ) + expect(response).to render_template('edit') end - end + end + end - context 'unsuccessfully saving the censor rule' do + context 'a CensorRule with an associated User' do before(:each) do - CensorRule.any_instance.stub(:save).and_return(false) + @censor_rule = FactoryGirl.create(:user_censor_rule) end - it 'does not update the censor rule' do + it 'finds the correct censor rule to edit' 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') + + expect(assigns[:censor_rule]).to eq(@censor_rule) end - it 'renders the form' do + it 'sets the last_edit_editor to the current admin' do put :update, :id => @censor_rule.id, :censor_rule => { :text => 'different text' } - expect(response).to render_template('edit') + 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 - before(:each) do - @censor_rule = FactoryGirl.create(:global_censor_rule) - end + context 'a global CensorRule' do - 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 + before(:each) do + @censor_rule = FactoryGirl.create(:global_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 '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 - 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 @@ -437,14 +515,25 @@ describe AdminCensorRuleController 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, :censor_rule_id => @censor_rule.id + delete :destroy, :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)) + delete :destroy, :id => @censor_rule.id + expect(response).to redirect_to(admin_request_path(@censor_rule.info_request)) end end @@ -455,14 +544,25 @@ describe AdminCensorRuleController 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, :censor_rule_id => @censor_rule.id + delete :destroy, :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)) + delete :destroy, :id => @censor_rule.id + expect(response).to redirect_to(admin_user_path(@censor_rule.user)) end end @@ -474,17 +574,17 @@ 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 4c641bd75..1131b3c0b 100644 --- a/spec/controllers/admin_public_body_categories_controller_spec.rb +++ b/spec/controllers/admin_public_body_categories_controller_spec.rb @@ -1,120 +1,268 @@ require 'spec_helper' describe AdminPublicBodyCategoriesController do - context 'when showing the index of categories and headings' do - render_views - it 'shows the index page' do + describe :index do + + it 'responds successfully' do get :index expect(response).to be_success end + + it 'uses the current locale by default' do + get :index + expect(assigns(:locale)).to eq(I18n.locale.to_s) + end + + it 'sets the locale if the show_locale param is passed' do + get :index, :show_locale => 'es' + expect(assigns(:locale)).to eq('es') + end + + it 'finds all category headings' do + PublicBodyHeading.destroy_all + + headings = [FactoryGirl.create(:public_body_heading), + FactoryGirl.create(:public_body_heading)] + + get :index + + expect(assigns(:category_headings)).to eq(headings) + end + + it 'finds all categories without their headings' do + PublicBodyHeading.destroy_all + PublicBodyCategory.destroy_all + + without_heading = FactoryGirl.create(:public_body_category) + + heading = FactoryGirl.create(:public_body_heading) + with_heading = FactoryGirl.create(:public_body_category) + PublicBodyCategoryLink.create!(:public_body_heading_id => heading.id, + :public_body_category_id => with_heading.id) + + + get :index + expect(assigns(:without_heading)).to eq([without_heading]) + end + + it 'renders the index template' do + get :index + expect(response).to render_template('index') + end + end - context 'when showing the form for a new public body category' do - it 'should assign a new public body category to the view' do + describe :new do + + it 'responds successfully' do get :new - assigns[:category].should be_a(PublicBodyCategory) + expect(response).to be_success end - it 'renders the new template' do + it 'builds a new PublicBodyCategory' do get :new - expect(response).to render_template('new') + expect(assigns(:category)).to be_new_record end + it 'builds new translations for all locales' do + get :new + + translations = assigns(:category).translations.map{ |t| t.locale.to_s }.sort + available = I18n.available_locales.map{ |l| l.to_s }.sort + + expect(translations).to eq(available) + end + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + end - context 'when creating a public body category' do - it "creates a new public body category in one locale" do - n = PublicBodyCategory.count - post :create, { - :public_body_category => { - :title => 'New Category', - :category_tag => 'new_test_category', - :description => 'New category for testing stuff' - } - } - PublicBodyCategory.count.should == n + 1 + describe :create do + + context 'on success' do + + before(:each) do + PublicBodyCategory.destroy_all + @params = { :category_tag => 'new_test_category', + :translations_attributes => { + 'en' => { :locale => 'en', + :title => 'New Category', + :description => 'New category for testing stuff' } + } } + end + + it 'creates a new category in the default locale' do + PublicBodyCategory.destroy_all + + expect { + post :create, :public_body_category => @params + }.to change{ PublicBodyCategory.count }.from(0).to(1) + end + + it "saves the public body category's heading associations" do + heading = FactoryGirl.create(:public_body_heading) + params = FactoryGirl.attributes_for(:public_body_category) + + post :create, :public_body_category => @params, + :headings => { "heading_#{ heading.id }" => heading.id } + + category = PublicBodyCategory.find_by_title(@params[:translations_attributes]['en'][:title]) + expect(category.public_body_headings).to eq([heading]) + end + + it 'notifies the admin that the category was created' do + post :create, :public_body_category => @params + expect(flash[:notice]).to eq('Category was successfully created.') + end + + it 'redirects to the categories index' do + post :create, :public_body_category => @params + expect(response).to redirect_to(admin_categories_path) + end - category = PublicBodyCategory.find_by_title("New Category") - response.should redirect_to(admin_categories_path) end - it "saves the public body category's heading associations" do - heading = FactoryGirl.create(:public_body_heading) - category_attributes = FactoryGirl.attributes_for(:public_body_category) - post :create, { - :public_body_category => category_attributes, - :headings => {"heading_#{heading.id}" => heading.id} - } - request.flash[:notice].should include('successful') - category = PublicBodyCategory.find_by_title(category_attributes[:title]) - category.public_body_headings.should == [heading] + context 'on success for multiple locales' do + + before(:each) do + PublicBodyCategory.destroy_all + @params = { :category_tag => 'new_test_category', + :translations_attributes => { + 'en' => { :locale => 'en', + :title => 'New Category', + :description => 'New category for testing stuff' }, + 'es' => { :locale => 'es', + :title => 'Mi Nuevo Category', + :description => 'ES Description' } + } } + end + + it 'saves the category' do + expect { + post :create, :public_body_category => @params + }.to change{ PublicBodyCategory.count }.from(0).to(1) + end + + it 'saves the default locale translation' do + post :create, :public_body_category => @params + + category = PublicBodyCategory.find_by_title('New Category') + + I18n.with_locale(:en) do + expect(category.title).to eq('New Category') + end + end + + it 'saves the alternative locale translation' do + post :create, :public_body_category => @params + + category = PublicBodyCategory.find_by_title('New Category') + + I18n.with_locale(:es) do + expect(category.title).to eq('Mi Nuevo Category') + end + end + end - it 'creates a new public body category with multiple locales' do - n = PublicBodyCategory.count - post :create, { - :public_body_category => { - :title => 'New Category', - :category_tag => 'new_test_category', - :description => 'New category for testing stuff', - :translated_versions => [{ :locale => "es", - :title => "Mi Nuevo Category" }] - } - } - PublicBodyCategory.count.should == n + 1 + context 'on failure' do - category = PublicBodyCategory.find_by_title("New Category") - category.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] - I18n.with_locale(:en) do - category.title.should == "New Category" + it 'renders the form if creating the record was unsuccessful' do + post :create, :public_body_category => { :title => '' } + expect(response).to render_template('new') end - I18n.with_locale(:es) do - category.title.should == "Mi Nuevo Category" + + it 'is rebuilt with the given params' do + post :create, :public_body_category => { :title => 'Need a description' } + expect(assigns(:category).title).to eq('Need a description') end - response.should redirect_to(admin_categories_path) end - it "renders the form if creating the record was unsuccessful" do - post :create, :public_body_category => { :title => '' } - expect(response).to render_template('new') + context 'on failure for multiple locales' do + + before(:each) do + @params = { :category_tag => 'new_test_category', + :translations_attributes => { + 'en' => { :locale => 'en', + :title => 'Need a description', + :description => nil }, + 'es' => { :locale => 'es', + :title => 'Mi Nuevo Category', + :description => 'ES Description' } + } } + end + + it 'is rebuilt with the default locale translation' do + post :create, :public_body_category => @params + expect(assigns(:category).title).to eq('Need a description') + end + + it 'is rebuilt with the alternative locale translation' do + post :create, :public_body_category => @params + + I18n.with_locale(:es) do + expect(assigns(:category).title).to eq('Mi Nuevo Category') + end + end + end end - context 'when editing a public body category' do + describe :edit do + before do @category = FactoryGirl.create(:public_body_category) I18n.with_locale('es') do @category.title = 'Los category' + @category.description = 'ES Description' @category.save! end end - render_views + it 'responds successfully' do + get :edit, :id => @category.id + expect(response).to be_success + end - it "finds the requested category" do + it 'finds the requested category' do get :edit, :id => @category.id expect(assigns[:category]).to eq(@category) end - it "renders the edit template" do + it 'builds new translations if the body does not already have a translation in the specified locale' do get :edit, :id => @category.id - expect(assigns[:category]).to render_template('edit') + expect(assigns[:category].translations.map(&:locale)).to include(:fr) end - it "edits a public body in another locale" do - get :edit, { :id => @category.id, :locale => :en } + it 'finds the public bodies tagged with the category tag' do + # FIXME: I wanted to call PublicBody.destroy_all here so that we + # have a known DB state, but the constraints were preventing the + # deletion of the fixture data + FactoryGirl.create(:public_body, :tag_string => 'wont_be_found') + + category = FactoryGirl.create(:public_body_category, :category_tag => 'spec') + expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'), + FactoryGirl.create(:public_body, :tag_string => 'spec')] - # When editing a body, the controller returns all available - # translations - assigns[:category].find_translation_by_locale("es").title.should == 'Los category' - response.should render_template('edit') + get :edit, :id => category.id + + expect(assigns(:tagged_public_bodies).sort).to eq(expected_bodies.sort) end + + it 'renders the edit template' do + get :edit, :id => @category.id + expect(response).to render_template('edit') + end + end - context 'when updating a public body category' do + describe :update do before do @heading = FactoryGirl.create(:public_body_heading) @@ -126,109 +274,389 @@ describe AdminPublicBodyCategoriesController do @tag = @category.category_tag I18n.with_locale('es') do @category.title = 'Los category' + @category.description = 'ES Description' @category.save! end + + @params = { :category_tag => @category.category_tag, + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) }, + 'es' => { :id => @category.translation_for(:es).id, + :locale => 'es', + :title => @category.title(:es), + :description => @category.description(:es) } + } } end - render_views + it 'finds the category to update' do + post :update, :id => @category.id, + :public_body_category => @params + expect(assigns(:category)).to eq(@category) + end + + it 'finds the public bodies tagged with the category tag' do + # FIXME: I wanted to call PublicBody.destroy_all here so that we + # have a known DB state, but the constraints were preventing the + # deletion of the fixture data + FactoryGirl.create(:public_body, :tag_string => 'wont_be_found') + + category = FactoryGirl.create(:public_body_category, :category_tag => 'spec') + expected_bodies = [FactoryGirl.create(:public_body, :tag_string => 'spec'), + FactoryGirl.create(:public_body, :tag_string => 'spec')] + + post :update, :id => category.id, + :public_body_category => category.serializable_hash.except(:title, :description) - it "saves edits to a public body category" do - post :update, { :id => @category.id, - :public_body_category => { :title => "Renamed" } } - request.flash[:notice].should include('successful') - pbc = PublicBodyCategory.find(@category.id) - pbc.title.should == "Renamed" + expect(assigns(:tagged_public_bodies)).to eq(expected_bodies) end it "saves edits to a public body category's heading associations" do - @category.public_body_headings.should == [@heading] + # We already have a heading from the before block. Here we're going + # to update to a new heading. heading = FactoryGirl.create(:public_body_heading) - post :update, { :id => @category.id, - :public_body_category => { :title => "Renamed" }, - :headings => {"heading_#{heading.id}" => heading.id} } - request.flash[:notice].should include('successful') - pbc = PublicBodyCategory.find(@category.id) - pbc.public_body_headings.should == [heading] - end - - it "saves edits to a public body category in another locale" do - I18n.with_locale(:es) do - @category.title.should == 'Los category' - post :update, { - :id => @category.id, - :public_body_category => { - :title => "Category", - :translated_versions => { - @category.id => {:locale => "es", - :title => "Renamed"} + + post :update, :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :title => 'Renamed' } } - } - } - request.flash[:notice].should include('successful') + }, + :headings => { "heading_#{ heading.id }" => heading.id } + + category = PublicBodyCategory.find(@category.id) + expect(category.public_body_headings).to eq([heading]) + end + + context 'when the category has associated bodies' do + + it 'does not save edits to category_tag' do + body = FactoryGirl.create(:public_body, :tag_string => @tag) + + post :update, :id => @category.id, + :public_body_category => { :category_tag => 'Renamed' } + + + category = PublicBodyCategory.find(@category.id) + expect(category.category_tag).to eq(@tag) end - pbc = PublicBodyCategory.find(@category.id) - I18n.with_locale(:es) do - pbc.title.should == "Renamed" + it 'notifies the user that the category_tag could not be updated' do + body = FactoryGirl.create(:public_body, :tag_string => @tag) + msg = %Q(There are authorities associated with this category, + so the tag can't be renamed).squish + + post :update, :id => @category.id, + :public_body_category => { :category_tag => 'Renamed' } + + expect(flash[:error]).to eq(msg) end - I18n.with_locale(:en) do - pbc.title.should == "Category" + + it 'renders the edit action' do + body = FactoryGirl.create(:public_body, :tag_string => @tag) + + post :update, :id => @category.id, + :public_body_category => { :category_tag => 'Renamed' } + + expect(response).to render_template('edit') end + end - it "does not save edits to category_tag if the category has associated bodies" do - body = FactoryGirl.create(:public_body, :tag_string => @tag) - post :update, { :id => @category.id, - :public_body_category => { :category_tag => "renamed" } } - - msg = "There are authorities associated with this category, so the tag can't be renamed" - request.flash[:error].should == msg - pbc = PublicBodyCategory.find(@category.id) - pbc.category_tag.should == @tag + context 'on success' do + + before(:each) do + @params = { :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :title => 'Renamed' } + } + } + } + end + + it 'saves edits to a public body category' do + post :update, @params + category = PublicBodyCategory.find(@category.id) + expect(category.title).to eq('Renamed') + end + + it 'notifies the admin that the category was created' do + post :update, @params + expect(flash[:notice]).to eq('Category was successfully updated.') + end + + it 'redirects to the category edit page' do + post :update, @params + expect(response).to redirect_to(edit_admin_category_path(@category)) + end + + it 'saves edits to category_tag if the category has no associated bodies' do + category = FactoryGirl.create(:public_body_category, :category_tag => 'empty') + + post :update, :id => category.id, + :public_body_category => { :category_tag => 'Renamed' } + + category = PublicBodyCategory.find(category.id) + expect(category.category_tag).to eq('Renamed') + end + end + context 'on success for multiple locales' do + + it "saves edits to a public body category in another locale" do + @category.title(:es).should == 'Los category' + post :update, :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) }, + 'es' => { :id => @category.translation_for(:es).id, + :locale => 'es', + :title => 'Renamed', + :description => 'ES Description' } + } + } + + category = PublicBodyCategory.find(@category.id) + expect(category.title(:es)).to eq('Renamed') + expect(category.title(:en)).to eq(@category.title(:en)) + end + + it 'adds a new translation' do + @category.translation_for(:es).destroy + @category.reload + + put :update, { + :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) }, + 'es' => { :locale => "es", + :title => "Example Public Body Category ES", + :description => @category.description(:es) } + } + } + } + + request.flash[:notice].should include('successful') + + pbc = PublicBodyCategory.find(@category.id) + + I18n.with_locale(:es) do + expect(pbc.title).to eq('Example Public Body Category ES') + end + end + + it 'adds new translations' do + @category.translation_for(:es).destroy + @category.reload + + post :update, { + :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) }, + 'es' => { :locale => "es", + :title => "Example Public Body Category ES", + :description => 'ES Description' }, + 'fr' => { :locale => "fr", + :title => "Example Public Body Category FR", + :description => 'FR Description' } + } + } + } + + request.flash[:notice].should include('successful') + + pbc = PublicBodyCategory.find(@category.id) + + I18n.with_locale(:es) do + expect(pbc.title).to eq('Example Public Body Category ES') + end + I18n.with_locale(:fr) do + expect(pbc.title).to eq('Example Public Body Category FR') + end + end + + it 'updates an existing translation and adds a third translation' do + post :update, { + :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) }, + # Update existing translation + 'es' => { :id => @category.translation_for(:es).id, + :locale => "es", + :title => "Renamed Example Public Body Category ES", + :description => @category.description }, + # Add new translation + 'fr' => { :locale => "fr", + :title => "Example Public Body Category FR", + :description => @category.description } + } + } + } + + request.flash[:notice].should include('successful') + + pbc = PublicBodyCategory.find(@category.id) + + I18n.with_locale(:es) do + expect(pbc.title).to eq('Renamed Example Public Body Category ES') + end + I18n.with_locale(:fr) do + expect(pbc.title).to eq('Example Public Body Category FR') + end + end + + it "redirects to the edit page after a successful update" do + post :update, :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => @category.title(:en), + :description => @category.description(:en) } + } } + + expect(response).to redirect_to(edit_admin_category_path(@category)) + end - it "save edits to category_tag if the category has no associated bodies" do - category = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-") - post :update, { :id => category.id, - :public_body_category => { :category_tag => "renamed" } } - request.flash[:notice].should include('success') - pbc = PublicBodyCategory.find(category.id) - pbc.category_tag.should == "renamed" end - it "redirects to the edit page after a successful update" do - post :update, { :id => @category.id, - :public_body_category => { :title => "Renamed" } } + context 'on failure' do + + it 'renders the form if creating the record was unsuccessful' do + post :update, :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => '', + :description => @category.description(:en) } + } } + expect(response).to render_template('edit') + end + + it 'is rebuilt with the given params' do + post :update, :id => @category.id, + :public_body_category => { + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => 'Need a description', + :description => '' } + } } + expect(assigns(:category).title).to eq('Need a description') + end - expect(response).to redirect_to(edit_admin_category_path(@category)) end - it "re-renders the edit form after an unsuccessful update" do - post :update, { :id => @category.id, - :public_body_category => { :title => '' } } + context 'on failure for multiple locales' do + + before(:each) do + @params = { :category_tag => 'new_test_category', + :translations_attributes => { + 'en' => { :id => @category.translation_for(:en).id, + :locale => 'en', + :title => 'Need a description', + :description => '' }, + 'es' => { :id => @category.translation_for(:es).id, + :locale => 'es', + :title => 'Mi Nuevo Category', + :description => 'ES Description' } + } } + end + + it 'is rebuilt with the default locale translation' do + post :update, :id => @category.id, + :public_body_category => @params + expect(assigns(:category).title(:en)).to eq('Need a description') + end + + it 'is rebuilt with the alternative locale translation' do + post :update, :id => @category.id, + :public_body_category => @params + + I18n.with_locale(:es) do + expect(assigns(:category).title).to eq('Mi Nuevo Category') + end + end - expect(response).to render_template('edit') end end - context 'when destroying a public body category' do - it "destroys empty public body categories" do - pbc = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-") - n = PublicBodyCategory.count - post :destroy, { :id => pbc.id } - response.should redirect_to(admin_categories_path) - PublicBodyCategory.count.should == n - 1 + describe :destroy do + + it 'uses the current locale by default' do + category = FactoryGirl.create(:public_body_category) + post :destroy, :id => category.id + expect(assigns(:locale)).to eq(I18n.locale.to_s) end - it "destroys non-empty public body categories" do + it 'sets the locale if the show_locale param is passed' do + category = FactoryGirl.create(:public_body_category) + post :destroy, :id => category.id, :show_locale => 'es' + expect(assigns(:locale)).to eq('es') + end + + it 'destroys empty public body categories' do + PublicBodyCategory.destroy_all + + category = FactoryGirl.create(:public_body_category) + + expect{ + post :destroy, :id => category.id + }.to change{ PublicBodyCategory.count }.from(1).to(0) + end + + it 'destroys non-empty public body categories' do + PublicBodyCategory.destroy_all + + # FIXME: Couldn't create the PublicBodyCategory with a Factory + # because #authorities= doesn't exist? + # undefined method `authorities=' for + # #<PublicBodyCategory:0x7f55cbb84f70> authority = FactoryGirl.create(:public_body) - pbc = PublicBodyCategory.create(:title => "In-Use Category", :category_tag => "empty", :description => "-", :authorities => [authority]) - n = PublicBodyCategory.count - post :destroy, { :id => pbc.id } - response.should redirect_to(admin_categories_path) - PublicBodyCategory.count.should == n - 1 + category = PublicBodyCategory.create(:title => "In-Use Category", + :category_tag => "empty", + :description => "-", + :authorities => [authority]) + + expect{ + post :destroy, :id => category.id + }.to change{ PublicBodyCategory.count }.from(1).to(0) + end + + it 'notifies the admin that the category was destroyed' do + category = FactoryGirl.create(:public_body_category) + post :destroy, :id => category.id + expect(flash[:notice]).to eq('Category was successfully destroyed.') + end + + it 'redirects to the categories index' do + category = FactoryGirl.create(:public_body_category) + post :destroy, :id => category.id + expect(response).to redirect_to(admin_categories_path) end + end end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index f176150da..50a373d9d 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -39,9 +39,14 @@ end describe AdminPublicBodyController, 'when showing the form for a new public body' do + it 'responds successfully' do + get :new + expect(response).to be_success + end + it 'should assign a new public body to the view' do get :new - assigns[:public_body].should be_a(PublicBody) + expect(assigns(:public_body)).to be_new_record end it "builds new translations for all locales" do @@ -53,6 +58,11 @@ describe AdminPublicBodyController, 'when showing the form for a new public body expect(translations).to eq(available) end + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + context 'when passed a change request id as a param' do render_views @@ -76,51 +86,127 @@ end describe AdminPublicBodyController, "when creating a public body" do render_views - it "creates a new public body in one locale" do - n = PublicBody.count - post :create, { :public_body => { :name => "New Quango", - :short_name => "", - :tag_string => "blah", - :request_email => 'newquango@localhost', - :last_edit_comment => 'From test code' } } - PublicBody.count.should == n + 1 + context 'on success' do + + before(:each) do + @params = { :public_body => { :name => 'New Quango', + :short_name => 'nq', + :request_email => 'newquango@localhost', + :tag_string => 'spec', + :last_edit_comment => 'From test code' } } + end + + it 'creates a new body in the default locale' do + # FIXME: Can't call PublicBody.destroy_all because database + # database contstraints prevent them being deleted. + existing = PublicBody.count + expected = existing + 1 + expect { + post :create, @params + }.to change{ PublicBody.count }.from(existing).to(expected) + end + + it 'notifies the admin that the body was created' do + post :create, @params + expect(flash[:notice]).to eq('PublicBody was successfully created.') + end + + it 'redirects to the admin page of the body' do + post :create, @params + expect(response).to redirect_to(admin_body_path(assigns(:public_body))) + end - body = PublicBody.find_by_name("New Quango") - response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) end - it "creates a new public body with multiple locales" do - n = PublicBody.count - post :create, { - :public_body => { :name => "New Quango", - :short_name => "", - :tag_string => "blah", - :request_email => 'newquango@localhost', - :last_edit_comment => 'From test code', - :translations_attributes => { - 'es' => { :locale => "es", - :name => "Mi Nuevo Quango", - :short_name => "", - :request_email => 'newquango@localhost' } - } - } - } - PublicBody.count.should == n + 1 + context 'on success for multiple locales' do + + before(:each) do + @params = { :public_body => { :name => 'New Quango', + :short_name => 'nq', + :request_email => 'newquango@localhost', + :tag_string => 'spec', + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :locale => 'es', + :name => 'Los Quango' } + } } } + end + + it 'saves the body' do + # FIXME: Can't call PublicBody.destroy_all because database + # database contstraints prevent them being deleted. + existing = PublicBody.count + expected = existing + 1 + expect { + post :create, @params + }.to change{ PublicBody.count }.from(existing).to(expected) + end + + it 'saves the default locale translation' do + post :create, @params + + body = PublicBody.find_by_name('New Quango') + + I18n.with_locale(:en) do + expect(body.name).to eq('New Quango') + end + end + + it 'saves the alternative locale translation' do + post :create, @params + + body = PublicBody.find_by_name('New Quango') + + I18n.with_locale(:es) do + expect(body.name).to eq('Los Quango') + end + end + + end + + context 'on failure' do + + it 'renders the form if creating the record was unsuccessful' do + post :create, :public_body => { :name => '', + :translations_attributes => {} } + expect(response).to render_template('new') + end + + it 'is rebuilt with the given params' do + post :create, :public_body => { :name => '', + :request_email => 'newquango@localhost', + :translations_attributes => {} } + expect(assigns(:public_body).request_email).to eq('newquango@localhost') + end + + end + + context 'on failure for multiple locales' do - body = PublicBody.find_by_name("New Quango") - body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] - I18n.with_locale(:en) do - body.name.should == "New Quango" - body.url_name.should == "new_quango" - body.first_letter.should == "N" + before(:each) do + @params = { :public_body => { :name => '', + :request_email => 'newquango@localhost', + :translations_attributes => { + 'es' => { :locale => 'es', + :name => 'Los Quango' } + } } } end - I18n.with_locale(:es) do - body.name.should == "Mi Nuevo Quango" - body.url_name.should == "mi_nuevo_quango" - body.first_letter.should == "M" + + it 'is rebuilt with the default locale translation' do + post :create, @params + expect(assigns(:public_body)).to_not be_persisted + expect(assigns(:public_body).request_email).to eq('newquango@localhost') + end + + it 'is rebuilt with the alternative locale translation' do + post :create, @params + + expect(assigns(:public_body)).to_not be_persisted + I18n.with_locale(:es) do + expect(assigns(:public_body).name).to eq('Los Quango') + end end - response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) end context 'when the body is being created as a result of a change request' do @@ -157,10 +243,34 @@ end describe AdminPublicBodyController, "when editing a public body" do render_views - it "edits a public body" do - get :edit, :id => 2 + before do + @body = FactoryGirl.create(:public_body) + I18n.with_locale('es') do + @body.name = 'Los Body' + @body.save! + end + end + + it 'responds successfully' do + get :edit, :id => @body.id + expect(response).to be_success end + it 'finds the requested body' do + get :edit, :id => @body.id + expect(assigns[:public_body]).to eq(@body) + end + + it 'builds new translations if the body does not already have a translation in the specified locale' do + get :edit, :id => @body.id + expect(assigns[:public_body].translations.map(&:locale)).to include(:fr) + end + + it 'renders the edit template' do + get :edit, :id => @body.id + expect(response).to render_template('edit') + end + it "edits a public body in another locale" do get :edit, {:id => 3, :locale => :en} @@ -170,12 +280,6 @@ describe AdminPublicBodyController, "when editing a public body" do response.should render_template('edit') end - it "builds new translations if the body does not already have a translation in the specified locale" do - public_body = FactoryGirl.create(:public_body) - get :edit, :id => public_body.id - expect(assigns[:public_body].translations.map(&:locale)).to include(:fr) - end - context 'when passed a change request id as a param' do render_views @@ -201,159 +305,200 @@ end describe AdminPublicBodyController, "when updating a public body" do render_views - it "saves edits to a public body" do - public_bodies(:humpadink_public_body).name.should == "Department for Humpadinking" - post :update, { :id => 3, :public_body => { :name => "Renamed", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code' } } - request.flash[:notice].should include('successful') - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) - pb.name.should == "Renamed" - end - - it 'adds a new translation' do - pb = public_bodies(:humpadink_public_body) - pb.translation_for(:es).destroy - pb.reload - - post :update, { - :id => pb.id, - :public_body => { - :name => "Department for Humpadinking", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code', - :translations_attributes => { - 'es' => { :locale => "es", - :name => "El Department for Humpadinking", - :short_name => "", - :request_email => 'edited@localhost' } - } - } - } - - request.flash[:notice].should include('successful') - - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) - - I18n.with_locale(:es) do - expect(pb.name).to eq('El Department for Humpadinking') - end - end - - it 'adds new translations' do - pb = public_bodies(:humpadink_public_body) - pb.translation_for(:es).destroy - pb.reload - - post :update, { - :id => pb.id, - :public_body => { - :name => "Department for Humpadinking", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code', - :translations_attributes => { - 'es' => { :locale => "es", - :name => "El Department for Humpadinking", - :short_name => "", - :request_email => 'edited@localhost' }, - 'fr' => { :locale => "fr", - :name => "Le Department for Humpadinking", - :short_name => "", - :request_email => 'edited@localhost' } - } - } - } - - request.flash[:notice].should include('successful') - - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) - - I18n.with_locale(:es) do - expect(pb.name).to eq('El Department for Humpadinking') - end - - I18n.with_locale(:fr) do - expect(pb.name).to eq('Le Department for Humpadinking') - end - end - - it 'updates an existing translation and adds a third translation' do - pb = public_bodies(:humpadink_public_body) - - put :update, { - :id => pb.id, - :public_body => { - :name => "Department for Humpadinking", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code', - :translations_attributes => { - # Update existing translation - 'es' => { :locale => "es", - :name => "Renamed Department for Humpadinking", - :short_name => "", - :request_email => 'edited@localhost' }, - # Add new translation - 'fr' => { :locale => "fr", - :name => "Le Department for Humpadinking", - :short_name => "", - :request_email => 'edited@localhost' } - } - } - } + before do + @body = FactoryGirl.create(:public_body) + I18n.with_locale('es') do + @body.name = 'Los Quango' + @body.save! + end + + @params = { :id => @body.id, + :public_body => { :name => 'Renamed', + :short_name => @body.short_name, + :request_email => @body.request_email, + :tag_string => @body.tag_string, + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :id => @body.translation_for(:es).id, + :locale => 'es', + :title => @body.name(:es) } + } } } + end + + it 'finds the heading to update' do + post :update, @params + expect(assigns(:heading)).to eq(@heading) + end - request.flash[:notice].should include('successful') + context 'on success' do - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + it 'saves edits to a public body heading' do + post :update, @params + body = PublicBody.find(@body.id) + expect(body.name).to eq('Renamed') + end - I18n.with_locale(:es) do - expect(pb.name).to eq('Renamed Department for Humpadinking') + it 'notifies the admin that the body was updated' do + post :update, @params + expect(flash[:notice]).to eq('PublicBody was successfully updated.') end - I18n.with_locale(:fr) do - expect(pb.name).to eq('Le Department for Humpadinking') + it 'redirects to the admin body page' do + post :update, @params + expect(response).to redirect_to(admin_body_path(@body)) end end - it "saves edits to a public body in another locale" do - I18n.with_locale(:es) do - pb = PublicBody.find(id=3) - pb.name.should == "El Department for Humpadinking" - post :update, { - :id => 3, - :public_body => { - :name => "Department for Humpadinking", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code', - :translations_attributes => { - 'es' => { :locale => "es", - :name => "Renamed", - :short_name => "", - :request_email => 'edited@localhost' } - } - } - } - request.flash[:notice].should include('successful') + context 'on success for multiple locales' do + + it 'saves edits to a public body heading in another locale' do + @body.name(:es).should == 'Los Quango' + post :update, :id => @body.id, + :public_body => { + :name => @body.name(:en), + :translations_attributes => { + 'es' => { :id => @body.translation_for(:es).id, + :locale => 'es', + :name => 'Renamed' } + } + } + + body = PublicBody.find(@body.id) + expect(body.name(:es)).to eq('Renamed') + expect(body.name(:en)).to eq(@body.name(:en)) end - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + it 'adds a new translation' do + @body.translation_for(:es).destroy + @body.reload + + put :update, { + :id => @body.id, + :public_body => { + :name => @body.name(:en), + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Example Public Body ES" } + } + } + } + + request.flash[:notice].should include('successful') + + body = PublicBody.find(@body.id) + + I18n.with_locale(:es) do + expect(body.name).to eq('Example Public Body ES') + end + end + + it 'adds new translations' do + @body.translation_for(:es).destroy + @body.reload + + post :update, { + :id => @body.id, + :public_body => { + :name => @body.name(:en), + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Example Public Body ES" }, + 'fr' => { :locale => "fr", + :name => "Example Public Body FR" } + } + } + } + + request.flash[:notice].should include('successful') + + body = PublicBody.find(@body.id) + + I18n.with_locale(:es) do + expect(body.name).to eq('Example Public Body ES') + end + I18n.with_locale(:fr) do + expect(body.name).to eq('Example Public Body FR') + end + end + + it 'updates an existing translation and adds a third translation' do + post :update, { + :id => @body.id, + :public_body => { + :name => @body.name(:en), + :translations_attributes => { + # Update existing translation + 'es' => { :id => @body.translation_for(:es).id, + :locale => "es", + :name => "Renamed Example Public Body ES" }, + # Add new translation + 'fr' => { :locale => "fr", + :name => "Example Public Body FR" } + } + } + } + + request.flash[:notice].should include('successful') + + body = PublicBody.find(@body.id) + + I18n.with_locale(:es) do + expect(body.name).to eq('Renamed Example Public Body ES') + end + I18n.with_locale(:fr) do + expect(body.name).to eq('Example Public Body FR') + end + end + + end + + context 'on failure' do + + it 'renders the form if creating the record was unsuccessful' do + post :update, :id => @body.id, + :public_body => { + :name => '', + :translations_attributes => {} + } + expect(response).to render_template('edit') + end + + it 'is rebuilt with the given params' do + post :update, :id => @body.id, + :public_body => { + :name => '', + :request_email => 'updated@localhost', + :translations_attributes => {} + } + expect(assigns(:public_body).request_email).to eq('updated@localhost') + end + + end - I18n.with_locale(:es) do - expect(pb.name).to eq('Renamed') + context 'on failure for multiple locales' do + + before(:each) do + @params = { :id => @body.id, + :public_body => { :name => '', + :translations_attributes => { + 'es' => { :id => @body.translation_for(:es).id, + :locale => 'es', + :name => 'Mi Nuevo Body' } + } } } + end + + it 'is rebuilt with the default locale translation' do + post :update, @params + expect(assigns(:public_body).name(:en)).to eq('') end - I18n.with_locale(:en) do - expect(pb.name).to eq('Department for Humpadinking') + it 'is rebuilt with the alternative locale translation' do + post :update, @params + + I18n.with_locale(:es) do + expect(assigns(:public_body).name).to eq('Mi Nuevo Body') + end end end @@ -402,7 +547,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 @@ -416,7 +561,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 afbe0fa30..ccdfdecfb 100644 --- a/spec/controllers/admin_public_body_headings_controller_spec.rb +++ b/spec/controllers/admin_public_body_headings_controller_spec.rb @@ -2,161 +2,466 @@ require 'spec_helper' describe AdminPublicBodyHeadingsController do - context 'when showing the form for a new public body category' do - it 'should assign a new public body heading to the view' do + describe :new do + + it 'responds successfully' do get :new - assigns[:heading].should be_a(PublicBodyHeading) + expect(response).to be_success end - it 'renders the new template' do + it 'builds a new PublicBodyHeading' do get :new - expect(response).to render_template('new') + expect(assigns(:heading)).to be_new_record end + + it 'builds new translations for all locales' do + get :new + + translations = assigns(:heading).translations.map{ |t| t.locale.to_s }.sort + available = I18n.available_locales.map{ |l| l.to_s }.sort + + expect(translations).to eq(available) + end + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + end - context 'when creating a public body heading' do - it "creates a new public body heading in one locale" do - n = PublicBodyHeading.count - post :create, { - :public_body_heading => { - :name => 'New Heading' - } - } - PublicBodyHeading.count.should == n + 1 - - heading = PublicBodyHeading.find_by_name("New Heading") - response.should redirect_to(admin_categories_path) - end + describe :create do - it 'creates a new public body heading with multiple locales' do - n = PublicBodyHeading.count - post :create, { - :public_body_heading => { - :name => 'New Heading', - :translated_versions => [{ :locale => "es", - :name => "Mi Nuevo Heading" }] - } - } - PublicBodyHeading.count.should == n + 1 - - heading = PublicBodyHeading.find_by_name("New Heading") - heading.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] - I18n.with_locale(:en) do - heading.name.should == "New Heading" + context 'on success' do + + before(:each) do + PublicBodyHeading.destroy_all + @params = { :translations_attributes => { + 'en' => { :locale => 'en', + :name => 'New Heading' } + } } end - I18n.with_locale(:es) do - heading.name.should == "Mi Nuevo Heading" + + it 'creates a new heading in the default locale' do + expect { + post :create, :public_body_heading => @params + }.to change{ PublicBodyHeading.count }.from(0).to(1) end - response.should redirect_to(admin_categories_path) - end + it 'notifies the admin that the heading was created' do + post :create, :public_body_heading => @params + expect(flash[:notice]).to eq('Heading was successfully created.') + end + + it 'redirects to the categories index' do + post :create, :public_body_heading => @params + expect(response).to redirect_to(admin_categories_path) + end - it "renders the form if creating the record was unsuccessful" do - post :create, :public_body_heading => { :name => '' } - expect(response).to render_template('new') end - end + context 'on success for multiple locales' do - context 'when editing a public body heading' do - before do - @heading = FactoryGirl.create(:public_body_heading) - end + before(:each) do + PublicBodyHeading.destroy_all + @params = { :translations_attributes => { + 'en' => { :locale => 'en', + :name => 'New Heading' }, + 'es' => { :locale => 'es', + :name => 'Mi Nuevo Heading' } + } } + end - render_views + it 'saves the heading' do + expect { + post :create, :public_body_heading => @params + }.to change{ PublicBodyHeading.count }.from(0).to(1) + end - it "finds the requested heading" do - get :edit, :id => @heading.id - expect(assigns[:heading]).to eq(@heading) - end + it 'saves the default locale translation' do + post :create, :public_body_heading => @params - it "renders the edit template" do - get :edit, :id => @heading.id - expect(assigns[:heading]).to render_template('edit') - end - end + heading = PublicBodyHeading.find_by_name('New Heading') + + I18n.with_locale(:en) do + expect(heading.name).to eq('New Heading') + end + end + + it 'saves the alternative locale translation' do + post :create, :public_body_heading => @params + + heading = PublicBodyHeading.find_by_name('New Heading') + + I18n.with_locale(:es) do + expect(heading.name).to eq('Mi Nuevo Heading') + end + end - context 'when updating a public body heading' do - before do - @heading = FactoryGirl.create(:public_body_heading) - @name = @heading.name end - it "saves edits to a public body heading" do - post :update, { :id => @heading.id, - :public_body_heading => { :name => "Renamed" } } - request.flash[:notice].should include('successful') - found_heading = PublicBodyHeading.find(@heading.id) - found_heading.name.should == "Renamed" + context 'on failure' do + + it 'renders the form if creating the record was unsuccessful' do + post :create, :public_body_heading => { :name => '' } + expect(response).to render_template('new') + end + + it 'is rebuilt with the given params' do + post :create, :public_body_heading => { :name => 'Need a description' } + expect(assigns(:heading).name).to eq('Need a description') + end + end - it "saves edits to a public body heading in another locale" do - I18n.with_locale(:es) do - post :update, { - :id => @heading.id, - :public_body_heading => { - :name => @name, - :translated_versions => { - @heading.id => {:locale => "es", - :name => "Renamed"} - } - } - } - request.flash[:notice].should include('successful') + context 'on failure for multiple locales' do + + before(:each) do + @params = { :translations_attributes => { + 'en' => { :locale => 'en', + :name => 'Need a description' }, + 'es' => { :locale => 'es', + :name => 'Mi Nuevo Heading' } + } } + end + + it 'is rebuilt with the default locale translation' do + post :create, :public_body_heading => @params + expect(assigns(:heading).name).to eq('Need a description') end - heading = PublicBodyHeading.find(@heading.id) - I18n.with_locale(:es) do - heading.name.should == "Renamed" + it 'is rebuilt with the alternative locale translation' do + post :create, :public_body_heading => @params + + I18n.with_locale(:es) do + expect(assigns(:heading).name).to eq('Mi Nuevo Heading') + end end - I18n.with_locale(:en) do - heading.name.should == @name + + end + + end + + describe :edit do + + before do + @heading = FactoryGirl.create(:public_body_heading) + I18n.with_locale('es') do + @heading.name = 'Los heading' + @heading.save! end end - it "redirects to the edit page after a successful update" do - post :update, { :id => @heading.id, - :public_body_heading => { :name => "Renamed" } } + it 'responds successfully' do + get :edit, :id => @heading.id + expect(response).to be_success + end - expect(response).to redirect_to(edit_admin_heading_path(@heading)) + it 'finds the requested heading' do + get :edit, :id => @heading.id + expect(assigns[:heading]).to eq(@heading) end - it "re-renders the edit form after an unsuccessful update" do - post :update, { :id => @heading.id, - :public_body_heading => { :name => '' } } + it 'builds new translations if the body does not already have a translation in the specified locale' do + get :edit, :id => @heading.id + expect(assigns[:heading].translations.map(&:locale)).to include(:fr) + end + it 'renders the edit template' do + get :edit, :id => @heading.id expect(response).to render_template('edit') end end - context 'when destroying a public body heading' do + describe :update do + + before do + @heading = FactoryGirl.create(:public_body_heading) + I18n.with_locale('es') do + @heading.name = 'Los heading' + @heading.save! + end + @params = { :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) }, + 'es' => { :id => @heading.translation_for(:es).id, + :locale => 'es', + :title => @heading.name(:es) } + } } + end + + it 'finds the heading to update' do + post :update, :id => @heading.id, + :public_body_category => @params + expect(assigns(:heading)).to eq(@heading) + end + + context 'on success' do + + before(:each) do + @params = { :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :name => 'Renamed' } + } + } + } + end + + it 'saves edits to a public body heading' do + post :update, @params + heading = PublicBodyHeading.find(@heading.id) + expect(heading.name).to eq('Renamed') + end + + it 'notifies the admin that the heading was updated' do + post :update, @params + expect(flash[:notice]).to eq('Heading was successfully updated.') + end + + it 'redirects to the heading edit page' do + post :update, @params + expect(response).to redirect_to(edit_admin_heading_path(@heading)) + end + + end + + context 'on success for multiple locales' do + + it 'saves edits to a public body heading in another locale' do + @heading.name(:es).should == 'Los heading' + post :update, :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) }, + 'es' => { :id => @heading.translation_for(:es).id, + :locale => 'es', + :name => 'Renamed' } + } + } + + heading = PublicBodyHeading.find(@heading.id) + expect(heading.name(:es)).to eq('Renamed') + expect(heading.name(:en)).to eq(@heading.name(:en)) + end + + it 'adds a new translation' do + @heading.translation_for(:es).destroy + @heading.reload + + put :update, { + :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) }, + 'es' => { :locale => "es", + :name => "Example Public Body Heading ES" } + } + } + } + + request.flash[:notice].should include('successful') + + heading = PublicBodyHeading.find(@heading.id) + + I18n.with_locale(:es) do + expect(heading.name).to eq('Example Public Body Heading ES') + end + end + + it 'adds new translations' do + @heading.translation_for(:es).destroy + @heading.reload + + post :update, { + :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) }, + 'es' => { :locale => "es", + :name => "Example Public Body Heading ES" }, + 'fr' => { :locale => "fr", + :name => "Example Public Body Heading FR" } + } + } + } + + request.flash[:notice].should include('successful') + + heading = PublicBodyHeading.find(@heading.id) + + I18n.with_locale(:es) do + expect(heading.name).to eq('Example Public Body Heading ES') + end + I18n.with_locale(:fr) do + expect(heading.name).to eq('Example Public Body Heading FR') + end + end + + it 'updates an existing translation and adds a third translation' do + post :update, { + :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) }, + # Update existing translation + 'es' => { :id => @heading.translation_for(:es).id, + :locale => "es", + :name => "Renamed Example Public Body Heading ES" }, + # Add new translation + 'fr' => { :locale => "fr", + :name => "Example Public Body Heading FR" } + } + } + } + + request.flash[:notice].should include('successful') + + heading = PublicBodyHeading.find(@heading.id) + + I18n.with_locale(:es) do + expect(heading.name).to eq('Renamed Example Public Body Heading ES') + end + I18n.with_locale(:fr) do + expect(heading.name).to eq('Example Public Body Heading FR') + end + end + + it "redirects to the edit page after a successful update" do + post :update, :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => @heading.name(:en) } + } } + + expect(response).to redirect_to(edit_admin_heading_path(@heading)) + end + + end + + context 'on failure' do + + it 'renders the form if creating the record was unsuccessful' do + post :update, :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => '' } + } } + expect(response).to render_template('edit') + end + + it 'is rebuilt with the given params' do + post :update, :id => @heading.id, + :public_body_heading => { + :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => 'Need a description' } + } } + expect(assigns(:heading).name).to eq('Need a description') + end + + end + + context 'on failure for multiple locales' do + + before(:each) do + @params = { :translations_attributes => { + 'en' => { :id => @heading.translation_for(:en).id, + :locale => 'en', + :name => '' }, + 'es' => { :id => @heading.translation_for(:es).id, + :locale => 'es', + :name => 'Mi Nuevo Heading' } + } } + end + + it 'is rebuilt with the default locale translation' do + post :update, :id => @heading.id, + :public_body_heading => @params + expect(assigns(:heading).name(:en)).to eq('') + end + + it 'is rebuilt with the alternative locale translation' do + post :update, :id => @heading.id, + :public_body_heading => @params + + I18n.with_locale(:es) do + expect(assigns(:heading).name).to eq('Mi Nuevo Heading') + end + end + + end + + end + + describe :destroy do + + it 'uses the current locale by default' do + heading = FactoryGirl.create(:public_body_heading) + post :destroy, :id => heading.id + expect(assigns(:locale)).to eq(I18n.locale.to_s) + end - before do - @heading = FactoryGirl.create(:public_body_heading) + it 'sets the locale if the show_locale param is passed' do + heading = FactoryGirl.create(:public_body_heading) + post :destroy, :id => heading.id, :show_locale => 'es' + expect(assigns(:locale)).to eq('es') end - it "destroys a public body heading that has associated categories" do + it 'destroys the public body heading' do + PublicBodyHeading.destroy_all + + heading = FactoryGirl.create(:public_body_heading) + + expect{ + post :destroy, :id => heading.id + }.to change{ PublicBodyHeading.count }.from(1).to(0) + end + + it 'destroys a heading that has associated categories' do + PublicBodyHeading.destroy_all + PublicBodyCategory.destroy_all + + heading = FactoryGirl.create(:public_body_heading) category = FactoryGirl.create(:public_body_category) link = FactoryGirl.create(:public_body_category_link, :public_body_category => category, - :public_body_heading => @heading, + :public_body_heading => heading, :category_display_order => 0) - n = PublicBodyHeading.count - n_links = PublicBodyCategoryLink.count - post :destroy, { :id => @heading.id } - response.should redirect_to(admin_categories_path) - PublicBodyHeading.count.should == n - 1 - PublicBodyCategoryLink.count.should == n_links - 1 + expect{ + post :destroy, :id => heading.id + }.to change{ PublicBodyHeading.count }.from(1).to(0) + end + + it 'notifies the admin that the heading was destroyed' do + heading = FactoryGirl.create(:public_body_heading) + post :destroy, :id => heading.id + expect(flash[:notice]).to eq('Heading was successfully destroyed.') end - it "destroys an empty public body heading" do - n = PublicBodyHeading.count - post :destroy, { :id => @heading.id } - response.should redirect_to(admin_categories_path) - PublicBodyHeading.count.should == n - 1 + it 'redirects to the categories index' do + heading = FactoryGirl.create(:public_body_heading) + post :destroy, :id => heading.id + expect(response).to redirect_to(admin_categories_path) end + end context 'when reordering public body headings' do diff --git a/spec/controllers/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 8652d9b17..ae8d4f256 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -102,6 +102,14 @@ describe GeneralController, "when showing the frontpage" do end end + it 'should generate a feed URL for successful requests' do + get :frontpage + assigns[:feed_autodetect].size.should == 1 + successful_request_feed = assigns[:feed_autodetect].first + successful_request_feed[:title].should == 'Successful requests' + end + + it "should render the front page with default language and ignore the browser setting" do config = MySociety::Config.load_default() config['USE_DEFAULT_BROWSER_LANGUAGE'] = false @@ -138,6 +146,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/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 840b4bb28..130631ef6 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -70,6 +70,18 @@ describe PublicBodyController, "when showing a body" do get :show, :url_name => "dFh", :view => 'all' response.should redirect_to(:controller => 'public_body', :action => 'show', :url_name => "dfh") end + + it 'keeps the search_params flash' do + # Make two get requests to simulate the flash getting swept after the + # first response. + search_params = { 'query' => 'Quango' } + get :show, { :url_name => 'dfh', :view => 'all' }, + nil, + { :search_params => search_params } + get :show, :url_name => 'dfh', :view => 'all' + expect(flash[:search_params]).to eq(search_params) + end + end describe PublicBodyController, "when listing bodies" do @@ -479,4 +491,15 @@ describe PublicBodyController, "when doing type ahead searches" do response.should render_template('public_body/_search_ahead') assigns[:xapian_requests].should be_nil end + + it 'remembers the search params' do + search_params = { + 'query' => 'Quango', + 'page' => '1', + 'bodies' => '1' + } + get :search_typeahead, search_params + expect(flash[:search_params]).to eq(search_params) + end + end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 26e46a966..02237b29d 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -956,6 +956,20 @@ describe RequestController, "when searching for an authority" do }.should_not raise_error(StandardError) end end + + it "remembers the search params" do + session[:user_id] = @user.id + search_params = { + 'query' => 'Quango', + 'page' => '1', + 'bodies' => '1' + } + + get :select_authority, search_params + + expect(flash[:search_params]).to eq(search_params) + end + end describe RequestController, "when creating a new request" do @@ -1073,6 +1087,16 @@ describe RequestController, "when creating a new request" do response.redirect_url.should =~ /request\/why_is_your_quango_called_gerald\/new$/ end + it "sets the request_sent flash to true if successful" do + session[:user_id] = @user.id + post :new, :info_request => { :public_body_id => @body.id, + :title => "Why is your quango called Geraldine?", :tag_string => "" }, + :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." }, + :submitted_new_request => 1, :preview => 0 + + expect(flash[:request_sent]).to be_true + end + it "should give an error if the same request is submitted twice" do session[:user_id] = @user.id @@ -2392,6 +2416,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 @@ -2442,7 +2483,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) @@ -2537,10 +2578,9 @@ describe RequestController, "#new_batch" do assigns[:existing_batch].should_not be_nil end - it 'should display a success notice' do + it 'sets the batch_sent flash to true' do make_request - notice_text = "<p>Your Freedom of Information requests will be <strong>sent</strong> shortly!" - flash[:notice].should match notice_text + expect(flash[:batch_sent]).to be_true end end @@ -2655,7 +2695,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..621dbaaac 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -12,6 +12,14 @@ describe ServicesController, "when returning a message for people in other count @old_locale = FastGettext.locale() end + it 'keeps the flash' do + # Make two get requests to simulate the flash getting swept after the + # first response. + get :other_country_message, nil, nil, :some_flash_key => 'abc' + get :other_country_message + expect(flash[:some_flash_key]).to eq('abc') + end + it "should show no alaveteli message when in the deployed country" do config = MySociety::Config.load_default() config['ISO_COUNTRY_CODE'] = "DE" @@ -60,21 +68,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 == '' diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 413d395c5..443856cf3 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -1,6 +1,63 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe UserController do + + describe :set_profile_photo do + + context 'user is banned' do + + before(:each) do + @user = FactoryGirl.create(:user, :ban_text => 'Causing trouble') + session[:user_id] = @user.id + @uploadedfile = fixture_file_upload("/files/parrot.png") + + post :set_profile_photo, :id => @user.id, + :file => @uploadedfile, + :submitted_draft_profile_photo => 1, + :automatically_crop => 1 + end + + it 'redirects to the profile page' do + expect(response).to redirect_to(set_profile_photo_path) + end + + it 'renders an error message' do + msg = 'Banned users cannot edit their profile' + expect(flash[:error]).to eq(msg) + end + + end + + end + + describe :set_profile_about_me do + + context 'user is banned' do + + before(:each) do + @user = FactoryGirl.create(:user, :ban_text => 'Causing trouble') + session[:user_id] = @user.id + + post :set_profile_about_me, :submitted_about_me => '1', + :about_me => 'Bad stuff' + end + + it 'redirects to the profile page' do + expect(response).to redirect_to(set_profile_about_me_path) + end + + it 'renders an error message' do + msg = 'Banned users cannot edit their profile' + expect(flash[:error]).to eq(msg) + end + + end + + end + +end + # TODO: Use route_for or params_from to check /c/ links better # http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Example/ControllerExampleGroup.html describe UserController, "when redirecting a show request to a canonical url" do |