diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 101 | ||||
-rw-r--r-- | spec/integration/request_controller_spec.rb | 41 | ||||
-rw-r--r-- | spec/lib/ability_spec.rb | 51 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/request_mailer_spec.rb | 14 | ||||
-rw-r--r-- | spec/spec_helper.rb | 5 |
6 files changed, 186 insertions, 30 deletions
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 21dd0853a..f35523532 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1250,8 +1250,7 @@ describe RequestController, "when classifying an information request" do end it 'should redirect to the request page' do - post :describe_state, :id => @external_request.id, - :submitted_describe_state => 1 + post :describe_state, :id => @external_request.id response.should redirect_to(:action => 'show', :controller => 'request', :url_title => @external_request.url_title) @@ -1271,8 +1270,7 @@ describe RequestController, "when classifying an information request" do def post_status(status) post :describe_state, :incoming_message => { :described_state => status }, :id => @dog_request.id, - :last_info_request_event_id => @dog_request.last_event_id_needing_description, - :submitted_describe_state => 1 + :last_info_request_event_id => @dog_request.last_event_id_needing_description end it "should require login" do @@ -1282,6 +1280,7 @@ describe RequestController, "when classifying an information request" do end it 'should ask whether the request is old and unclassified' do + session[:user_id] = users(:silly_name_user).id @dog_request.should_receive(:is_old_unclassified?) post_status('rejected') end @@ -1319,7 +1318,7 @@ describe RequestController, "when classifying an information request" do it 'should classify the request' do @dog_request.stub!(:calculate_status).and_return('rejected') - @dog_request.should_receive(:set_described_state).with('rejected') + @dog_request.should_receive(:set_described_state).with('rejected', users(:silly_name_user), nil) post_status('rejected') end @@ -1347,6 +1346,26 @@ describe RequestController, "when classifying an information request" do flash[:notice].should == 'Thank you for updating this request!' end + context "playing the classification game" do + before :each do + session[:request_game] = true + end + + it "should continue the game after classifying a request" do + post_status("rejected") + flash[:notice].should =~ /There are some more requests below for you to classify/ + response.should redirect_to play_url + end + end + + it "should send a mail from the user who changed the state to requires_admin" do + post :describe_state, :incoming_message => { :described_state => "requires_admin", :message => "a message" }, :id => @dog_request.id, :incoming_message_id => incoming_messages(:useless_incoming_message), :last_info_request_event_id => @dog_request.last_event_id_needing_description + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.from_addrs.first.to_s.should == users(:silly_name_user).name_and_email + end end end @@ -1362,7 +1381,7 @@ describe RequestController, "when classifying an information request" do it 'should update the status of the request' do @dog_request.stub!(:calculate_status).and_return('rejected') - @dog_request.should_receive(:set_described_state).with('rejected') + @dog_request.should_receive(:set_described_state).with('rejected', @admin_user, nil) post_status('rejected') end @@ -1413,7 +1432,7 @@ describe RequestController, "when classifying an information request" do it 'should update the status of the request' do @dog_request.stub!(:calculate_status).and_return('rejected') - @dog_request.should_receive(:set_described_state).with('rejected') + @dog_request.should_receive(:set_described_state).with('rejected', @admin_user, nil) post_status('rejected') end @@ -1448,6 +1467,22 @@ describe RequestController, "when classifying an information request" do @dog_request.stub!(:each).and_return([@dog_request]) end + it "should let you know when you forget to select a status" do + post :describe_state, :id => @dog_request.id, + :last_info_request_event_id => @dog_request.last_event_id_needing_description + response.should redirect_to request_url(@dog_request) + flash[:error].should == _("Please choose whether or not you got some of the information that you wanted.") + end + + it "should not change the status if the request has changed while viewing it" do + @dog_request.stub!(:last_event_id_needing_description).and_return(2) + + post :describe_state, :incoming_message => { :described_state => "rejected" }, + :id => @dog_request.id, :last_info_request_event_id => 1 + response.should redirect_to request_url(@dog_request) + flash[:error].should =~ /The request has been updated since you originally loaded this page/ + end + it "should successfully classify response if logged in as user controlling request" do post_status('rejected') response.should redirect_to(:controller => 'help', :action => 'unhappy', :url_title => @dog_request.url_title) @@ -1468,22 +1503,34 @@ describe RequestController, "when classifying an information request" do post_status('rejected') end - it "should send email when classified as requires_admin" do - post :describe_state, :incoming_message => { :described_state => "requires_admin" }, :id => @dog_request.id, :incoming_message_id => incoming_messages(:useless_incoming_message), :last_info_request_event_id => @dog_request.last_event_id_needing_description, :submitted_describe_state => 1 - response.should redirect_to(:controller => 'help', :action => 'contact') + it "should go to the page asking for more information when classified as requires_admin" do + post :describe_state, :incoming_message => { :described_state => "requires_admin" }, :id => @dog_request.id, :incoming_message_id => incoming_messages(:useless_incoming_message), :last_info_request_event_id => @dog_request.last_event_id_needing_description + response.should redirect_to describe_state_message_url(:url_title => @dog_request.url_title, :described_state => "requires_admin") @dog_request.reload - @dog_request.awaiting_description.should == false - @dog_request.described_state.should == 'requires_admin' - @dog_request.get_last_response_event.calculated_state.should == 'requires_admin' + @dog_request.described_state.should_not == 'requires_admin' + + ActionMailer::Base.deliveries.should be_empty + end + + context "message is included when classifying as requires_admin" do + it "should send an email including the message" do + post :describe_state, + :incoming_message => { + :described_state => "requires_admin", + :message => "Something weird happened" }, + :id => @dog_request.id, + :last_info_request_event_id => @dog_request.last_event_id_needing_description - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.body.should =~ /as needing admin/ - mail.from_addrs.first.to_s.should == @request_owner.name_and_email + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.body.should =~ /as needing admin/ + mail.body.should =~ /Something weird happened/ + end end + it 'should say it is showing advice as to what to do next' do post_status('rejected') flash[:notice].should match(/Here is what to do now/) @@ -1594,12 +1641,22 @@ describe RequestController, "when classifying an information request" do expect_redirect('internal_review', request_url) end - it 'should redirect to the "help general url" when status is updated to "requires admin"' do - expect_redirect('requires_admin', "help/contact") + it 'should redirect to the "request url" when status is updated to "requires admin"' do + post :describe_state, :incoming_message => { + :described_state => 'requires_admin', + :message => "A message" }, + :id => @dog_request.id, + :last_info_request_event_id => @dog_request.last_event_id_needing_description + response.should redirect_to show_request_url(:url_title => @dog_request.url_title) end - it 'should redirect to the "help general url" when status is updated to "error message"' do - expect_redirect('error_message', "help/contact") + it 'should redirect to the "request url" when status is updated to "error message"' do + post :describe_state, :incoming_message => { + :described_state => 'error_message', + :message => "A message" }, + :id => @dog_request.id, + :last_info_request_event_id => @dog_request.last_event_id_needing_description + response.should redirect_to show_request_url(:url_title => @dog_request.url_title) end it 'should redirect to the "respond to last url url" when status is updated to "user_withdrawn"' do diff --git a/spec/integration/request_controller_spec.rb b/spec/integration/request_controller_spec.rb new file mode 100644 index 000000000..24667bdf1 --- /dev/null +++ b/spec/integration/request_controller_spec.rb @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe RequestController, "when classifying an information request" do + + describe 'when the request is internal' do + + before(:each) do + @dog_request = info_requests(:fancy_dog_request) + # This should happen automatically before each test but doesn't with these integration + # tests for some reason. + ActionMailer::Base.deliveries = [] + end + + describe 'when logged in as the requestor' do + + before :each do + @request_owner = @dog_request.user + visit signin_path + fill_in "Your e-mail:", :with => @request_owner.email + fill_in "Password:", :with => "jonespassword" + click_button "Sign in" + end + + it "should send an email including the message" do + visit describe_state_message_path(:url_title => @dog_request.url_title, + :described_state => "requires_admin") + fill_in "Please tell us more:", :with => "Okay. I don't quite understand." + click_button "Submit status and send message" + + response.should contain "Thank you! We'll look into what happened and try and fix it up." + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.body.should =~ /as needing admin/ + mail.body.should =~ /Okay. I don't quite understand./ + end + end + end +end diff --git a/spec/lib/ability_spec.rb b/spec/lib/ability_spec.rb new file mode 100644 index 000000000..f075d0f32 --- /dev/null +++ b/spec/lib/ability_spec.rb @@ -0,0 +1,51 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Ability do + describe ".can_update_request_state?" do + context "old and unclassified request" do + let(:request) { mock_model(InfoRequest, :is_old_unclassified? => true) } + + context "logged out" do + let(:user) { nil } + before(:each) { request.stub!(:is_owning_user?).and_return(false) } + it { Ability::can_update_request_state?(user, request).should be_false } + end + + context "logged in but not owner of request" do + let(:user) { mock_model(User) } + before(:each) { request.stub!(:is_owning_user?).and_return(false) } + + it { Ability::can_update_request_state?(user, request).should be_true } + end + end + + context "new request" do + let(:request) { mock_model(InfoRequest, :is_old_unclassified? => false) } + + context "logged out" do + let(:user) { nil } + before(:each) { request.stub!(:is_owning_user?).and_return(false) } + + it { Ability::can_update_request_state?(user, request).should be_false } + end + + context "logged in" do + let(:user) { mock_model(User) } + + # An owner of a request can also be someone with admin powers + context "as owner of request" do + before(:each) { request.stub!(:is_owning_user?).and_return(true) } + + it { Ability::can_update_request_state?(user, request).should be_true } + end + + context "but not owner of request" do + before(:each) { request.stub!(:is_owning_user?).and_return(false) } + + it { Ability::can_update_request_state?(user, request).should be_false } + end + end + end + end +end + diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 544852f91..728a538f9 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -426,8 +426,8 @@ describe InfoRequest do before do Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) - @mock_comment_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment') - @mock_response_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response') + @mock_comment_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment', :response? => false) + @mock_response_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response', :response? => true) @info_request = InfoRequest.new(:prominence => 'normal', :awaiting_description => true, :info_request_events => [@mock_response_event, @mock_comment_event]) diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index dd5a322fb..40133eedc 100644 --- a/spec/models/request_mailer_spec.rb +++ b/spec/models/request_mailer_spec.rb @@ -28,7 +28,7 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', 'dummy@localhost') ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last last_event.params[:rejected_reason].should == "Could not identify the request from the email address" deliveries = ActionMailer::Base.deliveries @@ -48,7 +48,7 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last last_event.params[:rejected_reason].should =~ /there is no "From" address/ deliveries = ActionMailer::Base.deliveries @@ -68,7 +68,7 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last last_event.params[:rejected_reason].should =~ /Only the authority can reply/ deliveries = ActionMailer::Base.deliveries @@ -153,7 +153,7 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 # arrives in holding pen - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last last_event.params[:rejected_reason].should =~ /allow new responses from nobody/ # should be a message to admin regarding holding pen @@ -366,4 +366,10 @@ describe RequestMailer, 'requires_admin' do mail.body.should include('http://test.host/en/admin/request/show/123') end + + it "body should contain the message from the user" do + mail = RequestMailer.deliver_requires_admin(@info_request, nil, "Something has gone wrong") + mail.body.should include 'Something has gone wrong' + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e05ef75dd..33e39e6e4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,8 +30,9 @@ config['REPLY_LATE_AFTER_DAYS'] = 20 require 'fakeweb' FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK") -# Uncomment the next line to use webrat's matchers -#require 'webrat/integrations/rspec-rails' +Webrat.configure do |config| + config.mode = :rails +end # Use test-specific translations FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po |