diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-12-19 14:04:55 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2014-01-13 12:36:42 +0000 |
commit | 18181d149130490cacd2bd4e558b94e9647137f1 (patch) | |
tree | b5c6a1ae81b2ad987b787a4911f462f083df5429 /spec/controllers/admin_public_body_controller_spec.rb | |
parent | 98189eedf4c80d730eb89f860e474c20759cfcec (diff) |
Reorganise specs
In order to make it clearer where to add new specs, group specs by
controller and action, merging specs relating to internationalisation
with other specs as there seem to be no specific prerequisites in terms
of set up for them, and removing some duplicate specs.
Diffstat (limited to 'spec/controllers/admin_public_body_controller_spec.rb')
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 353 |
1 files changed, 187 insertions, 166 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index fe5087d7c..aecbbf42b 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe AdminPublicBodyController, "when administering public bodies" do +describe AdminPublicBodyController, "when showing the index of public bodies" do render_views it "shows the index page" do @@ -12,28 +12,149 @@ describe AdminPublicBodyController, "when administering public bodies" do assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] end + it "searches for 'humpa' in another locale" do + get :index, {:query => "humpa", :locale => "es"} + assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] + end + +end + +describe AdminPublicBodyController, "when showing a public body" do + render_views + it "shows a public body" do get :show, :id => 2 end - it "creates a new public body" do + it "sets a using_admin flag" do + get :show, :id => 2 + session[:using_admin].should == 1 + end + + it "shows a public body in another locale" do + get :show, {:id => 2, :locale => "es" } + end + +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' } } + 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 + + 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', + :translated_versions => [{ :locale => "es", + :name => "Mi Nuevo Quango", + :short_name => "", + :request_email => 'newquango@localhost' }] + } + } + PublicBody.count.should == n + 1 + + 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" + end + I18n.with_locale(:es) do + body.name.should == "Mi Nuevo Quango" + body.url_name.should == "mi_nuevo_quango" + body.first_letter.should == "M" + end + + response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) + end + +end + +describe AdminPublicBodyController, "when editing a public body" do + render_views + it "edits a public body" do get :edit, :id => 2 end + it "edits a public body in another locale" do + get :edit, {:id => 3, :locale => :en} + + # When editing a body, the controller returns all available translations + assigns[:public_body].find_translation_by_locale("es").name.should == 'El Department for Humpadinking' + assigns[:public_body].name.should == 'Department for Humpadinking' + response.should render_template('edit') + end +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' } } + 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 "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', + :translated_versions => { + 3 => {:locale => "es", + :name => "Renamed", + :short_name => "", + :request_email => 'edited@localhost'} + } + } + } + request.flash[:notice].should include('successful') + end + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + I18n.with_locale(:es) do + pb.name.should == "Renamed" + end + I18n.with_locale(:en) do + pb.name.should == "Department for Humpadinking" + end + end +end + +describe AdminPublicBodyController, "when destroying a public body" do + render_views + it "does not destroy a public body that has associated requests" do id = public_bodies(:humpadink_public_body).id n = PublicBody.count @@ -49,10 +170,10 @@ describe AdminPublicBodyController, "when administering public bodies" do PublicBody.count.should == n - 1 end - it "sets a using_admin flag" do - get :show, :id => 2 - session[:using_admin].should == 1 - end +end + +describe AdminPublicBodyController, "when assigning public body tags" do + render_views it "mass assigns tags" do condition = "public_body_translations.locale = ?" @@ -62,81 +183,81 @@ describe AdminPublicBodyController, "when administering public bodies" do response.should redirect_to(:action=>'list') PublicBody.find_by_tag("department").count.should == n end +end - describe 'import_csv' do +describe AdminPublicBodyController, "when importing a csv" do + render_views - describe 'when handling a GET request' do + describe 'when handling a GET request' do - it 'should get the page successfully' do - get :import_csv - response.should be_success - end + it 'should get the page successfully' do + get :import_csv + response.should be_success + end + + end + + describe 'when handling a POST request' do + before do + PublicBody.stub!(:import_csv).and_return([[],[]]) + @file_object = fixture_file_upload('/files/fake-authority-type.csv') end - describe 'when handling a POST request' do + it 'should handle a nil csv file param' do + post :import_csv, { :commit => 'Dry run' } + response.should be_success + end + + describe 'if there is a csv file param' do - before do - PublicBody.stub!(:import_csv).and_return([[],[]]) - @file_object = fixture_file_upload('/files/fake-authority-type.csv') + it 'should try to get the contents and original name of a csv file param' do + @file_object.should_receive(:read).and_return('some contents') + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} end - it 'should handle a nil csv file param' do - post :import_csv, { :commit => 'Dry run' } - response.should be_success + it 'should assign the original filename to the view' do + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} + assigns[:original_csv_file].should == 'fake-authority-type.csv' end - describe 'if there is a csv file param' do + end - it 'should try to get the contents and original name of a csv file param' do - @file_object.should_receive(:read).and_return('some contents') - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - end + describe 'if there is no csv file param, but there are temporary_csv_file and + original_csv_file params' do - it 'should assign the original filename to the view' do - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - assigns[:original_csv_file].should == 'fake-authority-type.csv' - end + it 'should try and get the file contents from a temporary file whose name + is passed as a param' do + @controller.should_receive(:retrieve_csv_data).with('csv_upload-2046-12-31-394') + post :import_csv, { :temporary_csv_file => 'csv_upload-2046-12-31-394', + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + end + it 'should raise an error on an invalid temp file name' do + params = { :temporary_csv_file => 'bad_name', + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + expected_error = "Invalid filename in upload_csv: bad_name" + lambda{ post :import_csv, params }.should raise_error(expected_error) end - describe 'if there is no csv file param, but there are temporary_csv_file and - original_csv_file params' do - - it 'should try and get the file contents from a temporary file whose name - is passed as a param' do - @controller.should_receive(:retrieve_csv_data).with('csv_upload-2046-12-31-394') - post :import_csv, { :temporary_csv_file => 'csv_upload-2046-12-31-394', - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - end - - it 'should raise an error on an invalid temp file name' do - params = { :temporary_csv_file => 'bad_name', - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - expected_error = "Invalid filename in upload_csv: bad_name" - lambda{ post :import_csv, params }.should raise_error(expected_error) - end - - it 'should raise an error if the temp file does not exist' do - temp_name = "csv_upload-20461231-394" - params = { :temporary_csv_file => temp_name, - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - expected_error = "Missing file in upload_csv: csv_upload-20461231-394" - lambda{ post :import_csv, params }.should raise_error(expected_error) - end - - it 'should assign the temporary filename to the view' do - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - temporary_filename = assigns[:temporary_csv_file] - temporary_filename.should match(/csv_upload-#{Time.now.strftime("%Y%m%d")}-\d{1,5}/) - end + it 'should raise an error if the temp file does not exist' do + temp_name = "csv_upload-20461231-394" + params = { :temporary_csv_file => temp_name, + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + expected_error = "Missing file in upload_csv: csv_upload-20461231-394" + lambda{ post :import_csv, params }.should raise_error(expected_error) + end + it 'should assign the temporary filename to the view' do + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} + temporary_filename = assigns[:temporary_csv_file] + temporary_filename.should match(/csv_upload-#{Time.now.strftime("%Y%m%d")}-\d{1,5}/) end end @@ -266,103 +387,3 @@ describe AdminPublicBodyController, "when administering public bodies and paying end end -describe AdminPublicBodyController, "when administering public bodies with i18n" do - render_views - - it "shows the index page" do - get :index - end - - it "searches for 'humpa'" do - get :index, {:query => "humpa", :locale => "es"} - assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] - end - - it "shows a public body" do - get :show, {:id => 2, :locale => "es" } - end - - it "edits a public body" do - get :edit, {:id => 3, :locale => :en} - - # When editing a body, the controller returns all available translations - assigns[:public_body].find_translation_by_locale("es").name.should == 'El Department for Humpadinking' - assigns[:public_body].name.should == 'Department for Humpadinking' - response.should render_template('edit') - end - - it "saves edits to a public body" 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', - :translated_versions => { - 3 => {:locale => "es", :name => "Renamed",:short_name => "", :request_email => 'edited@localhost'} - } - } - } - request.flash[:notice].should include('successful') - end - - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) - I18n.with_locale(:es) do - pb.name.should == "Renamed" - end - I18n.with_locale(:en) do - pb.name.should == "Department for Humpadinking" - end - end - - it "destroy a public body" do - n = PublicBody.count - post :destroy, { :id => public_bodies(:forlorn_public_body).id } - PublicBody.count.should == n - 1 - end - -end - -describe AdminPublicBodyController, "when creating public bodies with i18n" 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 - - 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', - :translated_versions => [{ :locale => "es", :name => "Mi Nuevo Quango", :short_name => "", :request_email => 'newquango@localhost' }] - } - } - PublicBody.count.should == n + 1 - - 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" - end - I18n.with_locale(:es) do - body.name.should == "Mi Nuevo Quango" - body.url_name.should == "mi_nuevo_quango" - body.first_letter.should == "M" - end - - response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) - end -end |