aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers')
-rw-r--r--spec/controllers/admin_user_controller_spec.rb23
-rw-r--r--spec/controllers/info_request_batch_controller_spec.rb53
-rw-r--r--spec/controllers/request_controller_spec.rb269
-rw-r--r--spec/controllers/track_controller_spec.rb2
-rw-r--r--spec/controllers/user_controller_spec.rb95
5 files changed, 407 insertions, 35 deletions
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/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/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
+
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