<commit_before>require 'spec_helper'
describe UsersController do
  describe 'PUT update user-organization status' do
    before(:each) do
      admin_user = double("User")
      admin_user.stub(:admin?).and_return(true)
      request.env['warden'].stub :authenticate! => admin_user
      controller.stub(:current_user).and_return(admin_user)
      @nonadmin_user = double("User")
      User.stub(:find_by_id).with("4").and_return(@nonadmin_user)
      @nonadmin_user.stub(:pending_organization_id=).with('5')
      @nonadmin_user.stub(:save!)
      @org = double("Organization")
      @org.stub(:name).and_return('<NAME>')
      Organization.stub(:find).and_return(@org)
    end
    context 'user requesting pending status to be admin of charity' do
      it 'should update the pending organization id to nested org id' do
        User.should_receive(:find_by_id).with("4").and_return(@nonadmin_user)
        @nonadmin_user.should_receive(:pending_organization_id=).with('5')
        @nonadmin_user.should_receive(:save!)
        put :update, id: 4, organization_id: 5
      end
      it 'should redirect to the show page for nested org' do
        put :update, id: 4, organization_id: 5
        expect(response).to redirect_to(organization_path(5))
      end
      it 'should display that a user has requested admin status for nested org' do
        put :update, id: 4, organization_id: 5
        expect(flash[:notice]).to have_content("You have requested admin status for #{@org.name}")
      end
    end
    context 'admin promoting user to charity admin' do
      before(:each) do
        @nonadmin_user.stub(:promote_to_org_admin)
        @nonadmin_user.stub(:email).and_return('<EMAIL>')
      end
      it 'non-admins get refused' do
        @nonadmin_user.stub(:admin?).and_return(false)
        controller.stub(:current_user).and_return(@nonadmin_user)
        put :update, {:id => '4'}
        response.response_code.should == 404
      end

      it "calls a model method to add user's organziation and remove user's pending organization" do
        @nonadmin_user.should_receive(:promote_to_org_admin)
        put :update, {:id => '4'}
      end
      it 'redirect to index page after update succeeds' do
        put :update, {:id => '4'}
        response.should redirect_to users_path
      end
      it 'shows a flash telling which user got approved' do
        put :update, {:id => '4'}
        expect(flash[:notice]).to have_content("You have approved #{@nonadmin_user.email}.")
      end
    end
  end
  describe 'GET index to view pending users' do
    before(:each) do
      @user = double("User")
      request.env['warden'].stub :authenticate! => @user
    end

    it 'non-admins get redirected away' do
      @user.stub(:admin?).and_return(false)
      controller.stub(:current_user).and_return(@user)
      get :index
      response.should redirect_to root_path
    end
    it 'shows a message in the flash when non-admin is redirected to root path' do
      @user.stub(:admin?).and_return(false)
      controller.stub(:current_user).and_return(@user)
      get :index
      expect(flash[:notice]).to have_content("You must be signed in as an admin to perform this action!")
    end
    it 'assigns @users with all users' do
      @user.stub(:admin?).and_return(true)
      controller.stub(:current_user).and_return(@user)
      regular_user = double("User")
      regular_user.stub(:pending_organization_id=).with('5')

      User.should_receive(:all).and_return([regular_user])
      get :index
      assigns(:users).should == [regular_user]
    end
  end
end<commit_msg>Clean up UsersController#index tests in the users_controller_spec<commit_after>require 'spec_helper'
describe UsersController do
  describe 'PUT update user-organization status' do
    before(:each) do
      admin_user = double("User")
      admin_user.stub(:admin?).and_return(true)
      request.env['warden'].stub :authenticate! => admin_user
      controller.stub(:current_user).and_return(admin_user)
      @nonadmin_user = double("User")
      User.stub(:find_by_id).with("4").and_return(@nonadmin_user)
      @nonadmin_user.stub(:pending_organization_id=).with('5')
      @nonadmin_user.stub(:save!)
      @org = double("Organization")
      @org.stub(:name).and_return('Red Cross')
      Organization.stub(:find).and_return(@org)
    end
    context 'user requesting pending status to be admin of charity' do
      it 'should update the pending organization id to nested org id' do
        User.should_receive(:find_by_id).with("4").and_return(@nonadmin_user)
        @nonadmin_user.should_receive(:pending_organization_id=).with('5')
        @nonadmin_user.should_receive(:save!)
        put :update, id: 4, organization_id: 5
      end
      it 'should redirect to the show page for nested org' do
        put :update, id: 4, organization_id: 5
        expect(response).to redirect_to(organization_path(5))
      end
      it 'should display that a user has requested admin status for nested org' do
        put :update, id: 4, organization_id: 5
        expect(flash[:notice]).to have_content("You have requested admin status for #{@org.name}")
      end
    end
    context 'admin promoting user to charity admin' do
      before(:each) do
        @nonadmin_user.stub(:promote_to_org_admin)
        @nonadmin_user.stub(:email).and_return('<EMAIL>')
      end
      it 'non-admins get refused' do
        @nonadmin_user.stub(:admin?).and_return(false)
        controller.stub(:current_user).and_return(@nonadmin_user)
        put :update, {:id => '4'}
        response.response_code.should == 404
      end

      it "calls a model method to add user's organziation and remove user's pending organization" do
        @nonadmin_user.should_receive(:promote_to_org_admin)
        put :update, {:id => '4'}
      end
      it 'redirect to index page after update succeeds' do
        put :update, {:id => '4'}
        response.should redirect_to users_path
      end
      it 'shows a flash telling which user got approved' do
        put :update, {:id => '4'}
        expect(flash[:notice]).to have_content("You have approved #{@nonadmin_user.email}.")
      end
    end
  end

  describe 'GET index to view pending users' do
    before(:each) do
      @user = double("User")
      request.env['warden'].stub :authenticate! => @user
    end

    context "user signed in" do
      before(:each) do
        controller.stub(:current_user).and_return(@user)
      end

      context "as admin" do
        before(:each) do
          @user.stub(:admin?).and_return(true)
        end

        it "assigns all users to @users" do
          user_double = double("User")
          User.stub(:all).and_return([user_double])
          get :index
          expect(assigns(:users)).to eql([user_double])
        end

        it "renders the index template" do
          get :index
          expect(response).to render_template("index")
        end
      end

      context "as non-admin" do
        before(:each) do
          @user.stub(:admin?).and_return(false)
        end

        it "redirects user to root and flashes a notice" do
          get :index
          response.should redirect_to root_path
        end

        it "flashes a relevant notice" do
          get :index
          expect(flash[:notice]).to have_content("You must be signed in as an admin to perform this action!")
        end
      end
    end

    context "user not signed in" do
      it "redirects user to root and flashes a notice" do
        get :index
        response.should redirect_to root_path
      end

      it "flashes the relevant notice" do
        get :index
        expect(flash[:notice]).to have_content("You must be signed in as an admin to perform this action!")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1089834211540933, 'fraction_numerical': 0.00475517285695926, 'mean_word_length': 2.892946473236618, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 18, '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': '964612', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2281, 'n_words': 644}