diff --git a/app/controllers/work_packages_controller.rb b/app/controllers/work_packages_controller.rb index 872ea2a965..a1bb1ff45d 100644 --- a/app/controllers/work_packages_controller.rb +++ b/app/controllers/work_packages_controller.rb @@ -407,6 +407,8 @@ class WorkPackagesController < ApplicationController def load_query @query ||= retrieve_query + rescue ActiveRecord::RecordNotFound + render_404 end def not_found_unless_work_package diff --git a/spec/controllers/work_packages_controller_spec.rb b/spec/controllers/work_packages_controller_spec.rb index 324560c610..b796a19a25 100644 --- a/spec/controllers/work_packages_controller_spec.rb +++ b/spec/controllers/work_packages_controller_spec.rb @@ -133,116 +133,134 @@ describe WorkPackagesController do project, :global => true) .and_return(true) - - controller.stub(:retrieve_query).and_return(query) - query.stub_chain(:results, :work_packages, :page, :per_page, :all).and_return(work_packages) end - describe 'html' do - let(:call_action) { get('index', :project_id => project.id) } - before { call_action } + describe 'with valid query' do + before do + controller.stub(:retrieve_query).and_return(query) + query.stub_chain(:results, :work_packages, :page, :per_page, :all).and_return(work_packages) + end - describe "w/o a project" do - let(:project) { nil } - let(:call_action) { get('index') } + describe 'html' do + let(:call_action) { get('index', :project_id => project.id) } + before { call_action } - it 'should render the index template' do - response.should render_template('work_packages/index', :formats => ["html"], - :layout => :base) - end - end + describe "w/o a project" do + let(:project) { nil } + let(:call_action) { get('index') } - context "w/ a project" do - it 'should render the index template' do - response.should render_template('work_packages/index', :formats => ["html"], - :layout => :base) + it 'should render the index template' do + response.should render_template('work_packages/index', :formats => ["html"], + :layout => :base) + end end - end - context 'when a query has been previously selected' do - let(:query) do - FactoryGirl.build_stubbed(:query).tap {|q| q.filters = [Queries::WorkPackages::Filter.new('done_ratio', operator: ">=", values: [10]) ]} + context "w/ a project" do + it 'should render the index template' do + response.should render_template('work_packages/index', :formats => ["html"], + :layout => :base) + end end - before { session.stub(:query).and_return query } + context 'when a query has been previously selected' do + let(:query) do + FactoryGirl.build_stubbed(:query).tap {|q| q.filters = [Queries::WorkPackages::Filter.new('done_ratio', operator: ">=", values: [10]) ]} + end + + before { session.stub(:query).and_return query } - it 'preserves the query' do - assigns['query'].filters.should == query.filters + it 'preserves the query' do + assigns['query'].filters.should == query.filters + end end end - end - describe 'csv' do - let(:params) { {} } - let(:call_action) { get('index', params.merge(:format => 'csv')) } + describe 'csv' do + let(:params) { {} } + let(:call_action) { get('index', params.merge(:format => 'csv')) } - requires_export_permission do + requires_export_permission do - before do - mock_csv = double('csv export') + before do + mock_csv = double('csv export') - WorkPackage::Exporter.should_receive(:csv).with(work_packages, project) - .and_return(mock_csv) + WorkPackage::Exporter.should_receive(:csv).with(work_packages, project) + .and_return(mock_csv) - controller.should_receive(:send_data).with(mock_csv, - :type => 'text/csv; header=present', - :filename => 'export.csv') do |*args| - # We need to render something because otherwise - # the controller will and he will not find a suitable template - controller.render :text => "success" + controller.should_receive(:send_data).with(mock_csv, + :type => 'text/csv; header=present', + :filename => 'export.csv') do |*args| + # We need to render something because otherwise + # the controller will and he will not find a suitable template + controller.render :text => "success" + end end - end - it 'should fulfill the defined should_receives' do - call_action + it 'should fulfill the defined should_receives' do + call_action + end end end - end - describe 'pdf' do - let(:params) { {} } - let(:call_action) { get('index', params.merge(:format => 'pdf')) } + describe 'pdf' do + let(:params) { {} } + let(:call_action) { get('index', params.merge(:format => 'pdf')) } - requires_export_permission do - before do - mock_pdf = double('pdf export') + requires_export_permission do + before do + mock_pdf = double('pdf export') + + WorkPackage::Exporter.should_receive(:pdf).and_return(mock_pdf) - WorkPackage::Exporter.should_receive(:pdf).and_return(mock_pdf) + controller.should_receive(:send_data).with(mock_pdf, + :type => 'application/pdf', + :filename => 'export.pdf') do |*args| + # We need to render something because otherwise + # the controller will and he will not find a suitable template + controller.render :text => "success" + end + end - controller.should_receive(:send_data).with(mock_pdf, - :type => 'application/pdf', - :filename => 'export.pdf') do |*args| - # We need to render something because otherwise - # the controller will and he will not find a suitable template - controller.render :text => "success" + it 'should fulfill the defined should_receives' do + call_action end end + end - it 'should fulfill the defined should_receives' do - call_action + describe 'atom' do + let(:params) { {} } + let(:call_action) { get('index', params.merge(:format => 'atom')) } + + requires_export_permission do + before do + controller.should_receive(:render_feed).with(work_packages, anything()) do |*args| + # We need to render something because otherwise + # the controller will and he will not find a suitable template + controller.render :text => "success" + end + end + + it 'should fulfill the defined should_receives' do + call_action + end end end end - describe 'atom' do - let(:params) { {} } - let(:call_action) { get('index', params.merge(:format => 'atom')) } + describe 'with invalid query' do + context 'when a non-existant query has been previously selected' do + let(:call_action) { get('index', :project_id => project.id, :query_id => "hokusbogus") } + before { call_action } - requires_export_permission do - before do - controller.should_receive(:render_feed).with(work_packages, anything()) do |*args| - # We need to render something because otherwise - # the controller will and he will not find a suitable template - controller.render :text => "success" - end + it 'renders a 404' do + response.response_code.should === 404 end - it 'should fulfill the defined should_receives' do - call_action + it 'preserves the project' do + assigns['project'].should === project end end end - end describe 'index with a broken project reference' do