diff options
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/admin_public_body_change_requests_controller_spec.rb | 35 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 459 | ||||
-rw-r--r-- | spec/controllers/admin_user_controller_spec.rb | 23 | ||||
-rw-r--r-- | spec/controllers/api_controller_spec.rb | 12 | ||||
-rw-r--r-- | spec/controllers/info_request_batch_controller_spec.rb | 53 | ||||
-rw-r--r-- | spec/controllers/public_body_change_requests_controller_spec.rb | 99 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 355 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 95 |
9 files changed, 834 insertions, 299 deletions
diff --git a/spec/controllers/admin_public_body_change_requests_controller_spec.rb b/spec/controllers/admin_public_body_change_requests_controller_spec.rb new file mode 100644 index 000000000..b478e851d --- /dev/null +++ b/spec/controllers/admin_public_body_change_requests_controller_spec.rb @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminPublicBodyChangeRequestsController, "editing a change request" do + + it "should render the edit template" do + change_request = FactoryGirl.create(:add_body_request) + get :edit, :id => change_request.id + response.should render_template("edit") + end + +end + +describe AdminPublicBodyChangeRequestsController, 'updating a change request' do + + before do + @change_request = FactoryGirl.create(:add_body_request) + post :update, { :id => @change_request.id, + :response => 'Thanks but no', + :subject => 'Your request' } + end + + it 'should close the change request' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should == false + end + + it 'should send a response email to the user who requested the change' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Your request' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /Thanks but no/ + end +end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index fe5087d7c..095d23245 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,255 @@ 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 showing the form for a new public body' do + + it 'should assign a new public body to the view' do + get :new + assigns[:public_body].should be_a(PublicBody) + end + + context 'when passed a change request id as a param' do + render_views + + it 'should populate the name, email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:public_body].name.should == change_request.public_body_name + assigns[:public_body].request_email.should == change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:change_request_user_response].should match("Thanks for your suggestion to add A New Body") + end + 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' } } + 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' } } + 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 + + context 'when the body is being created as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:add_body_request) + post :create, { :public_body => { :name => "New Quango", + :short_name => "", + :tag_string => "blah", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Adding a new body', + :response => 'The URL will be [Authority URL will be inserted here]'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Adding a new body' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /The URL will be http:\/\/test.host\/body\/new_quango/ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + end + 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 + + context 'when passed a change request id as a param' do + render_views + + before do + @change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => @change_request.public_body_id, :change_request_id => @change_request.id + end + + it 'should populate the email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => change_request.public_body_id, :change_request_id => change_request.id + assigns[:public_body].request_email.should == @change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + assigns[:change_request_user_response].should match("Thanks for your suggestion to update the email address") + end + 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 + + context 'when the body is being updated as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:update_body_request) + post :update, { :id => @change_request.public_body_id, + :public_body => { :name => "New Quango", + :short_name => "", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Body update', + :response => 'Done.'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Body update' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /Done./ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + 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 +276,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 +289,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 - before do - PublicBody.stub!(:import_csv).and_return([[],[]]) - @file_object = fixture_file_upload('/files/fake-authority-type.csv') + describe 'if there is a csv file param' do + + 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 +493,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 diff --git a/spec/controllers/admin_user_controller_spec.rb b/spec/controllers/admin_user_controller_spec.rb index a6e5a0d7e..99894a414 100644 --- a/spec/controllers/admin_user_controller_spec.rb +++ b/spec/controllers/admin_user_controller_spec.rb @@ -15,13 +15,32 @@ describe AdminUserController, "when administering users" do it "shows a user" do get :show, :id => users(:bob_smith_user) end - + it "logs in as another user" do get :login_as, :id => users(:bob_smith_user).id post_redirect = PostRedirect.get_last_post_redirect response.should redirect_to(:controller => 'user', :action => 'confirm', :email_token => post_redirect.email_token) end - + # See also "allows an admin to log in as another user" in spec/integration/admin_spec.rb end +describe AdminUserController, "when updating a user" do + + it "saves a change to 'can_make_batch_requests'" do + user = FactoryGirl.create(:user) + user.can_make_batch_requests?.should be_false + post :update, {:id => user.id, :admin_user => {:can_make_batch_requests => '1', + :name => user.name, + :email => user.email, + :admin_level => user.admin_level, + :ban_text => user.ban_text, + :about_me => user.about_me, + :no_limit => user.no_limit}} + flash[:notice].should == 'User successfully updated.' + response.should be_redirect + user = User.find(user.id) + user.can_make_batch_requests?.should be_true + end + +end diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 2b1c515f7..6b02bd5b4 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -1,18 +1,6 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -def normalise_whitespace(s) - s = s.gsub(/\A\s+|\s+\Z/, "") - s = s.gsub(/\s+/, " ") - return s -end - -RSpec::Matchers.define :be_equal_modulo_whitespace_to do |expected| - match do |actual| - normalise_whitespace(actual) == normalise_whitespace(expected) - end -end - describe ApiController, "when using the API" do describe 'checking API keys' do diff --git a/spec/controllers/info_request_batch_controller_spec.rb b/spec/controllers/info_request_batch_controller_spec.rb new file mode 100644 index 000000000..d08f02e10 --- /dev/null +++ b/spec/controllers/info_request_batch_controller_spec.rb @@ -0,0 +1,53 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe InfoRequestBatchController, "when showing a request" do + + before do + @first_public_body = FactoryGirl.create(:public_body) + @second_public_body = FactoryGirl.create(:public_body) + @info_request_batch = FactoryGirl.create(:info_request_batch, :title => 'Matched title', + :body => 'Matched body', + :public_bodies => [@first_public_body, + @second_public_body]) + @first_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch, + :public_body => @first_public_body) + @second_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch, + :public_body => @second_public_body) + @default_params = {:id => @info_request_batch.id} + end + + def make_request(params=@default_params) + get :show, params + end + + it 'should be successful' do + make_request + response.should be_success + end + + it 'should assign an info_request_batch to the view' do + make_request + assigns[:info_request_batch].should == @info_request_batch + end + + context 'when the batch has not been sent' do + + it 'should assign public_bodies to the view' do + make_request + assigns[:public_bodies].should == [@first_public_body, @second_public_body] + end + end + + context 'when the batch has been sent' do + + it 'should assign info_requests to the view' do + @info_request_batch.sent_at = Time.now + @info_request_batch.save! + make_request + assigns[:info_requests].sort.should == [@first_request, @second_request] + 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 new file mode 100644 index 000000000..7b878b893 --- /dev/null +++ b/spec/controllers/public_body_change_requests_controller_spec.rb @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe PublicBodyChangeRequestsController, "making a new change request" do + + it "should show the form" do + get :new + response.should render_template("new") + end + +end + +describe PublicBodyChangeRequestsController, "creating a change request" do + + context 'when handling a request for a new authority' do + + before do + @email = "test@example.com" + name = "Test User" + @change_request_params = {:user_email => @email, + :user_name => name, + :public_body_name => 'New Body', + :public_body_email => 'new_body@example.com', + :notes => 'Please', + :source => 'http://www.example.com'} + end + + it "should send an email to the site contact address" do + post :create, {:public_body_change_request => @change_request_params} + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should =~ /Add authority - New Body/ + mail.from.should include(@email) + mail.to.should include('postmaster@localhost') + 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/') + end + + it 'should show a notice' do + post :create, {:public_body_change_request => @change_request_params} + expected_text = "Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon." + flash[:notice].should == expected_text + end + + it 'should redirect to the frontpage' do + post :create, {:public_body_change_request => @change_request_params} + response.should redirect_to frontpage_url + end + + end + + context 'when handling a request for an update to an existing authority' do + + before do + @email = "test@example.com" + name = "Test User" + @public_body = FactoryGirl.create(:public_body) + @change_request_params = {:user_email => @email, + :user_name => name, + :public_body_id => @public_body.id, + :public_body_email => 'new_body@example.com', + :notes => 'Please', + :source => 'http://www.example.com'} + end + + it 'should send an email to the site contact address' do + post :create, {:public_body_change_request => @change_request_params} + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should =~ /Update email address - #{@public_body.name}/ + mail.from.should include(@email) + mail.to.should include('postmaster@localhost') + 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/') + end + + it 'should show a notice' do + post :create, {:public_body_change_request => @change_request_params} + expected_text = "Your request to update the address for #{@public_body.name} has been sent. Thank you for getting in touch! We'll get back to you soon." + flash[:notice].should == expected_text + end + + it 'should redirect to the frontpage' do + post :create, {:public_body_change_request => @change_request_params} + response.should redirect_to frontpage_url + end + + end + + +end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index def9dfc7e..1e7df4536 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -17,92 +17,6 @@ describe RequestController, "when listing recent requests" do response.should render_template('list') end - it "should filter requests" do - get :list, :view => 'all' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all - - # default sort order is the request with the most recently created event first - assigns[:list_results].map(&:info_request).should == InfoRequest.all( - :order => "(select max(info_request_events.created_at) from info_request_events where info_request_events.info_request_id = info_requests.id) DESC") - - get :list, :view => 'successful' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in ( - select info_request_id - from info_request_events - where not exists ( - select * - from info_request_events later_events - where later_events.created_at > info_request_events.created_at - and later_events.info_request_id = info_request_events.info_request_id - and later_events.described_state is not null - ) - and info_request_events.described_state in ('successful', 'partially_successful') - )") - end - - it "should filter requests by date" do - # The semantics of the search are that it finds any InfoRequest - # that has any InfoRequestEvent created in the specified range - - get :list, :view => 'all', :request_date_before => '13/10/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at < '2007-10-13'::date)") - - get :list, :view => 'all', :request_date_after => '13/10/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at > '2007-10-13'::date)") - - get :list, :view => 'all', :request_date_after => '13/10/2007', :request_date_before => '01/11/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at between '2007-10-13'::date and '2007-11-01'::date)") - end - - it "should list internal_review requests as unresolved ones" do - get :list, :view => 'awaiting' - - # This doesn’t precisely duplicate the logic of the actual - # query, but it is close enough to give the same result with - # the current set of test data. - assigns[:list_results].should =~ InfoRequestEvent.all( - :conditions => "described_state in ( - 'waiting_response', 'waiting_clarification', - 'internal_review', 'gone_postal', 'error_message', 'requires_admin' - ) and not exists ( - select * - from info_request_events later_events - where later_events.created_at > info_request_events.created_at - and later_events.info_request_id = info_request_events.info_request_id - )") - - - get :list, :view => 'awaiting' - assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == false - - event = info_request_events(:useless_incoming_message_event) - event.described_state = event.calculated_state = "internal_review" - event.save! - rebuild_xapian_index - - get :list, :view => 'awaiting' - assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == true - end - - it "should assign the first page of results" do - xap_results = mock(ActsAsXapian::Search, - :results => (1..25).to_a.map { |m| { :model => m } }, - :matches_estimated => 1000000) - - ActsAsXapian::Search.should_receive(:new). - with([InfoRequestEvent]," (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)", - :sort_by_prefix => "created_at", :offset => 0, :limit => 25, :sort_by_ascending => true, - :collapse_by_prefix => "request_collapse"). - and_return(xap_results) - get :list, :view => 'all' - assigns[:list_results].size.should == 25 - assigns[:show_no_more_than].should == RequestController::MAX_RESULTS - end - it "should return 404 for pages we don't want to serve up" do xap_results = mock(ActsAsXapian::Search, :results => (1..25).to_a.map { |m| { :model => m } }, @@ -897,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, @@ -959,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, @@ -2509,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 + diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 57d084f6b..40865d2b9 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -10,7 +10,7 @@ describe TrackController, "when making a new track on a request" do :tracking_user_id= => nil) TrackThing.stub!(:create_track_for_request).and_return(@track_thing) TrackThing.stub!(:create_track_for_search_query).and_return(@track_thing) - TrackThing.stub!(:find_by_existing_track).and_return(nil) + TrackThing.stub!(:find_existing).and_return(nil) InfoRequest.stub!(:find_by_url_title!) do |url_title| if url_title == "myrequest" @ir diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 0033309a5..cf361d898 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -30,55 +30,90 @@ describe UserController, "when redirecting a show request to a canonical url" do end describe UserController, "when showing a user" do - render_views + before(:each) do - load_raw_emails_data - get_fixtures_xapian_index + @user = FactoryGirl.create(:user) end it "should be successful" do - get :show, :url_name => "bob_smith" + get :show, :url_name => @user.url_name response.should be_success end it "should render with 'show' template" do - get :show, :url_name => "bob_smith" + get :show, :url_name => @user.url_name response.should render_template('show') end - it "should distinguish between 'my profile' and 'my requests' for logged in users" do - session[:user_id] = users(:bob_smith_user).id - get :show, :url_name => "bob_smith", :view => 'requests' - response.body.should_not include("Change your password") - response.body.should match(/Your [0-9]+ Freedom of Information requests/) - get :show, :url_name => "bob_smith", :view => 'profile' - response.body.should include("Change your password") - response.body.should_not match(/Your [0-9]+ Freedom of Information requests/) + it "should assign the user" do + get :show, :url_name => @user.url_name + assigns[:display_user].should == @user end - it "should assign the user" do - get :show, :url_name => "bob_smith" - assigns[:display_user].should == users(:bob_smith_user) + context "when viewing the user's own profile" do + + render_views + + def make_request + get :show, {:url_name => @user.url_name, :view => 'profile'}, {:user_id => @user.id} + end + + it 'should not show requests, or batch requests, but should show account options' do + make_request + response.body.should_not match(/Freedom of Information requests made by you/) + assigns[:show_batches].should be_false + response.body.should include("Change your password") + end + end - it "should search the user's contributions" do - get :show, :url_name => "bob_smith" - assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( - :conditions => "user_id = #{users(:bob_smith_user).id}") + context "when viewing a user's own requests" do + + render_views + + def make_request + get :show, {:url_name => @user.url_name, :view => 'requests'}, {:user_id => @user.id} + end + + it 'should show requests, batch requests, but no account options' do + make_request + response.body.should match(/Freedom of Information requests made by you/) + assigns[:show_batches].should be_true + response.body.should_not include("Change your password") + end - get :show, :url_name => "bob_smith", :user_query => "money" - assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ [ - info_requests(:naughty_chicken_request), - info_requests(:another_boring_request), - ] end - it "should not show unconfirmed users" do - begin - get :show, :url_name => "unconfirmed_user" - rescue => e +end + +describe UserController, "when showing a user" do + + context 'when using fixture data' do + + before do + load_raw_emails_data + get_fixtures_xapian_index end - e.should be_an_instance_of(ActiveRecord::RecordNotFound) + + it "should search the user's contributions" do + get :show, :url_name => "bob_smith" + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( + :conditions => "user_id = #{users(:bob_smith_user).id}") + + get :show, :url_name => "bob_smith", :user_query => "money" + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ [ + info_requests(:naughty_chicken_request), + info_requests(:another_boring_request), + ] + end + + it "should not show unconfirmed users" do + begin + get :show, :url_name => "unconfirmed_user" + rescue => e + end + e.should be_an_instance_of(ActiveRecord::RecordNotFound) + end end end |