<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PitchesController do
  describe "on GET to /pitchs/new" do
    before(:each) do
      Pitch.stub!(:createable_by?).and_return(true)
      @user = Factory(:user)
      controller.stub!(:current_user).and_return(@user)
      get :new
    end

    it "assigns the new pitch the current user" do
      assigns(:pitch).user_id.should == @user.id
    end
  end

  describe "when can't create on new" do
    before(:each) do
      Pitch.stub!(:createable_by?).and_return(false)
      get :new
    end
    it_denies_access
  end

  describe "when can't edit" do
    it "should deny access and add flash when not owner" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => Factory(:user))
      Pitch.stub!(:find).and_return(pitch)
      get :edit, :id => pitch.id
      flash[:error].should match(/You cannot edit this pitch, since you didn't create it./)
      response.should redirect_to(pitch_path(pitch))
    end

    it "should deny access and add flash when donations added" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => user)
      controller.stub!(:current_user).and_return(user)
      donation = Factory(:donation, :pitch => pitch, :amount => 2, :status => 'paid')
      Pitch.stub!(:find).and_return(pitch)
      get :edit, :id => pitch.id
      flash[:error].should match(/You cannot edit a pitch that has donations.  For minor changes, contact <EMAIL>/)
      response.should redirect_to(pitch_path(pitch))
    end
  end

  describe "can_edit?" do
    it "should allow the owner of a pitch to have access" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => user)
      pitch.should be_editable_by(user)
      get :edit, :id => pitch.id
    end

    it "should allow an admin to have access" do
      user = Factory(:admin)
      pitch = Factory(:pitch)
      controller.stub!(:current_user).and_return(user)
      donation = Factory(:donation, :pitch => pitch, :amount => 3, :status => 'paid')
      get :edit, :id => pitch.id
      flash[:error].should be_nil
    end
  end

  describe "on GET to /pitches/1/edit" do
    describe "without donations" do
      it "renders edit" do
        controller.stub!(:can_edit?).and_return(true)
        pitch = Factory(:pitch)
        get :edit, :id => pitch.to_param
        response.should render_template(:edit)
      end
    end
  end

  describe "on POST to /pitches/1/feature" do
    before do
      @pitch = Factory(:pitch, :feature => true)
      @pitch2 = Factory(:pitch)
    end

    it "should unset the orginal featured pitch and set new to featured" do
      post :feature, :id => @pitch2.to_param
      @pitch2.reload.feature.should be_true
      @pitch.reload.feature.should be_false
    end

    it "should redirect back to the pitch" do
      post :feature, :id => @pitch2.to_param
      response.should redirect_to(pitch_path(@pitch2))
    end

  end

  describe "on GET to show" do
    before do
      @pitch = Factory(:pitch)
    end

    it "should store the location in the session" do
      do_show
      session[:return_to].should == pitch_path(@pitch)
    end

    def do_show
      get :show, :id => @pitch.id
    end
  end

  describe "on GET to new with a headline" do
    before do
      login_as Factory(:user)
      controller.stub!(:can_create?).and_return(true)
      get :new, :headline => 'example'
    end

    it "should prefill the headline on the pitch" do
      assigns[:pitch].headline.should == 'example'
    end
  end

  describe "on GET to index" do
    it "should redirect to /news_items" do
      get :index
      response.should redirect_to news_items_path
    end
  end
end<commit_msg>Fix test suite warning<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PitchesController do
  describe "on GET to /pitchs/new" do
    before(:each) do
      Pitch.stub!(:createable_by?).and_return(true)
      @user = Factory(:user)
      controller.stub!(:current_user).and_return(@user)
      get :new
    end

    it "assigns the new pitch the current user" do
      assigns(:pitch).user_id.should == @user.id
    end
  end

  describe "when can't create on new" do
    before(:each) do
      Pitch.stub!(:createable_by?).and_return(false)
      get :new
    end
    it_denies_access
  end

  describe "when can't edit" do
    it "should deny access and add flash when not owner" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => Factory(:user))
      Pitch.stub!(:find).and_return(pitch)
      get :edit, :id => pitch.id
      flash[:error].should match(/You cannot edit this pitch, since you didn't create it./)
      response.should redirect_to(pitch_path(pitch))
    end

    it "should deny access and add flash when donations added" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => user)
      controller.stub!(:current_user).and_return(user)
      donation = Factory(:donation, :pitch => pitch, :amount => 2, :status => 'paid')
      Pitch.stub!(:find).and_return(pitch)
      get :edit, :id => pitch.id
      flash[:error].should match(/You cannot edit a pitch that has donations.  For minor changes, contact <EMAIL>/)
      response.should redirect_to(pitch_path(pitch))
    end
  end

  describe "can_edit?" do
    it "should allow the owner of a pitch to have access" do
      user = Factory(:user)
      pitch = Factory(:pitch, :user => user)
      pitch.should be_editable_by(user)
      get :edit, :id => pitch.id
    end

    it "should allow an admin to have access" do
      user = Factory(:admin)
      pitch = Factory(:pitch)
      controller.stub!(:current_user).and_return(user)
      donation = Factory(:donation, :pitch => pitch, :amount => 3, :status => 'paid')
      get :edit, :id => pitch.id
      flash[:error].should be_nil
    end
  end

  describe "on GET to /pitches/1/edit" do
    describe "without donations" do
      it "renders edit" do
        controller.stub!(:can_edit?).and_return(true)
        pitch = Factory(:pitch)
        get :edit, :id => pitch.to_param
        response.should render_template(:edit)
      end
    end
  end

  describe "on POST to /pitches/1/feature" do
    before do
      @pitch = Factory(:pitch, :feature => true)
      @pitch2 = Factory(:pitch)
    end

    it "should unset the orginal featured pitch and set new to featured" do
      post :feature, :id => @pitch2.to_param
      @pitch2.reload.feature.should be_true
      @pitch.reload.feature.should be_false
    end

    it "should redirect back to the pitch" do
      post :feature, :id => @pitch2.to_param
      response.should redirect_to(pitch_path(@pitch2))
    end

  end

  describe "on GET to show" do
    before do
      @pitch = Factory(:pitch)
    end

    it "should store the location in the session" do
      do_show
      session[:return_to].should == pitch_path(@pitch)
    end

    def do_show
      get :show, :id => @pitch.id
    end
  end

  describe "on GET to new with a headline" do
    before do
      login_as Factory(:user)
      controller.stub!(:can_create?).and_return(true)
      get :new, :headline => 'example'
    end

    it "should prefill the headline on the pitch" do
      assigns[:pitch].headline.should == 'example'
    end
  end

  describe "on GET to index" do
    it "should redirect to /news_items" do
      get :index
      response.should redirect_to(news_items_path)
    end
  end
end
{'fraction_non_alphanumeric': 0.1110813704496788, 'fraction_numerical': 0.0024089935760171306, 'mean_word_length': 2.7609461499748362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4544481', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2297, 'n_words': 725}