diff options
Diffstat (limited to 'spec/controllers/request_controller_spec.rb')
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 269 |
1 files changed, 267 insertions, 2 deletions
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 23f19f389..1e7df4536 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -811,7 +811,7 @@ describe RequestController, "when handling prominence" do expect_hidden('request/hidden_correspondence') end - it 'should download attachments for an admin user', :focus => true do + it 'should download attachments for an admin user' do session[:user_id] = FactoryGirl.create(:admin_user).id get :get_attachment, :incoming_message_id => @incoming_message.id, :id => @info_request.id, @@ -873,7 +873,7 @@ describe RequestController, "when handling prominence" do response.should be_success end - it 'should download attachments for an admin user', :focus => true do + it 'should download attachments for an admin user' do session[:user_id] = FactoryGirl.create(:admin_user).id get :get_attachment, :incoming_message_id => @incoming_message.id, :id => @info_request.id, @@ -2423,3 +2423,268 @@ describe RequestController, "when caching fragments" do end +describe RequestController, "#new_batch" do + + context "when batch requests is enabled" do + + before do + AlaveteliConfiguration.stub!(:allow_batch_requests).and_return(true) + end + + context "when the current user can make batch requests" do + + before do + @user = FactoryGirl.create(:user, :can_make_batch_requests => true) + @public_body = FactoryGirl.create(:public_body) + @other_public_body = FactoryGirl.create(:public_body) + @public_body_ids = [@public_body.id, @other_public_body.id] + @default_post_params = { :info_request => { :title => "What does it all mean?", + :tag_string => "" }, + :public_body_ids => @public_body_ids, + :outgoing_message => { :body => "This is a silly letter." }, + :submitted_new_request => 1, + :preview => 1 } + end + + it 'should be successful' do + get :new_batch, {:public_body_ids => @public_body_ids}, {:user_id => @user.id} + response.should be_success + end + + it 'should render the "new" template' do + get :new_batch, {:public_body_ids => @public_body_ids}, {:user_id => @user.id} + response.should render_template('request/new') + end + + it 'should redirect to "select_authorities" if no public_body_ids param is passed' do + get :new_batch, {}, {:user_id => @user.id} + response.should redirect_to select_authorities_path + end + + it "should render 'preview' when given a good title and body" do + post :new_batch, @default_post_params, { :user_id => @user.id } + response.should render_template('preview') + end + + it "should give an error and render 'new' template when a summary isn't given" do + @default_post_params[:info_request].delete(:title) + post :new_batch, @default_post_params, { :user_id => @user.id } + assigns[:info_request].errors[:title].should == ['Please enter a summary of your request'] + response.should render_template('new') + end + + it "should allow re-editing of a request" do + params = @default_post_params.merge(:preview => 0, :reedit => 1) + post :new_batch, params, { :user_id => @user.id } + response.should render_template('new') + end + + context "on success" do + + def make_request + @params = @default_post_params.merge(:preview => 0) + post :new_batch, @params, { :user_id => @user.id } + end + + it 'should create an info request batch and redirect to the new batch on success' do + make_request + new_info_request_batch = assigns[:info_request_batch] + new_info_request_batch.should_not be_nil + response.should redirect_to(info_request_batch_path(new_info_request_batch)) + end + + it 'should prevent double submission of a batch request' do + make_request + post :new_batch, @params, { :user_id => @user.id } + response.should render_template('new') + assigns[:existing_batch].should_not be_nil + end + + it 'should display a success notice' do + make_request + notice_text = "<p>Your Freedom of Information requests will be <strong>sent</strong> shortly!" + flash[:notice].should match notice_text + end + + end + + context "when the user is banned" do + + before do + @user.ban_text = "bad behaviour" + @user.save! + end + + it 'should show the "banned" template' do + post :new_batch, @default_post_params, { :user_id => @user.id } + response.should render_template('user/banned') + assigns[:details].should == 'bad behaviour' + end + + end + + end + + context "when the current user can't make batch requests" do + + render_views + + before do + @user = FactoryGirl.create(:user) + end + + it 'should return a 403 with an appropriate message' do + get :new_batch, {}, {:user_id => @user.id} + response.code.should == '403' + response.body.should match("Users cannot usually make batch requests to multiple authorities at once") + end + + end + + context 'when there is no logged-in user' do + + it 'should return a redirect to the login page' do + get :new_batch + post_redirect = PostRedirect.get_last_post_redirect + response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token) + end + end + + + end + + context "when batch requests is not enabled" do + + it 'should return a 404' do + Rails.application.config.stub!(:consider_all_requests_local).and_return(false) + get :new_batch + response.code.should == '404' + end + + end + +end + +describe RequestController, "#select_authorities" do + + context "when batch requests is enabled" do + + before do + get_fixtures_xapian_index + load_raw_emails_data + AlaveteliConfiguration.stub!(:allow_batch_requests).and_return(true) + end + + context "when the current user can make batch requests" do + + before do + @user = FactoryGirl.create(:user, :can_make_batch_requests => true) + end + + context 'when asked for HTML' do + + it 'should be successful' do + get :select_authorities, {}, {:user_id => @user.id} + response.should be_success + end + + it 'should render the "select_authorities" template' do + get :select_authorities, {}, {:user_id => @user.id} + response.should render_template('request/select_authorities') + end + + it 'should assign a list of search results to the view if passed a query' do + get :select_authorities, {:public_body_query => "Quango"}, {:user_id => @user.id} + assigns[:search_bodies].results.size.should == 1 + assigns[:search_bodies].results[0][:model].name.should == public_bodies(:geraldine_public_body).name + end + + it 'should assign a list of public bodies to the view if passed a list of ids' do + get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id]}, + {:user_id => @user.id} + assigns[:public_bodies].size.should == 1 + assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name + end + + it 'should subtract a list of public bodies to remove from the list of bodies assigned to + the view' do + get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id, + public_bodies(:geraldine_public_body).id], + :remove_public_body_ids => [public_bodies(:geraldine_public_body).id]}, + {:user_id => @user.id} + assigns[:public_bodies].size.should == 1 + assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name + end + + end + + context 'when asked for JSON', :focus => true do + + it 'should be successful' do + get :select_authorities, {:public_body_query => "Quan", :format => 'json'}, {:user_id => @user.id} + response.should be_success + end + + it 'should return a list of public body names and ids' do + get :select_authorities, {:public_body_query => "Quan", :format => 'json'}, + {:user_id => @user.id} + + JSON(response.body).should == [{ 'id' => public_bodies(:geraldine_public_body).id, + 'name' => public_bodies(:geraldine_public_body).name }] + end + + it 'should return an empty list if no search is passed' do + get :select_authorities, {:format => 'json' },{:user_id => @user.id} + JSON(response.body).should == [] + end + + it 'should return an empty list if there are no bodies' do + get :select_authorities, {:public_body_query => 'fknkskalnr', :format => 'json' }, + {:user_id => @user.id} + JSON(response.body).should == [] + end + + end + + end + + context "when the current user can't make batch requests" do + + render_views + + before do + @user = FactoryGirl.create(:user) + end + + it 'should return a 403 with an appropriate message' do + get :select_authorities, {}, {:user_id => @user.id} + response.code.should == '403' + response.body.should match("Users cannot usually make batch requests to multiple authorities at once") + end + + end + + context 'when there is no logged-in user' do + + it 'should return a redirect to the login page' do + get :select_authorities + post_redirect = PostRedirect.get_last_post_redirect + response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token) + end + end + + + end + + context "when batch requests is not enabled" do + + it 'should return a 404' do + Rails.application.config.stub!(:consider_all_requests_local).and_return(false) + get :select_authorities + response.code.should == '404' + end + + end + +end + |