<commit_before>require 'spec_helper'

describe UsersController do
  render_views

  let!(:user) { FactoryGirl.create(:user) }
  let(:success_attr) {{
    "user_name" => "fuzbar",
    "password" => "<PASSWORD>",
    "password_confirmation" => "<PASSWORD>",
  }}
  let(:fail_attr) {{
    "user_name" => "",
    "password" => "",
    "password_confirmation" => "",
  }}

  describe "GET 'new'" do

    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should have the right title" do
      get :new
      response.body.should have_selector("title", :text => "Register")
    end

    it "gets the workstations" do
      Workstation.should_receive(:of_type).with("td")
      Workstation.should_receive(:of_type).with("ops")
      get :new
    end
  end

  describe "POST 'create'" do

    describe "failure" do

      let(:params) {{ user: fail_attr }}

      it "should not create a user" do
        lambda do
          post :create, params
        end.should_not change(User, :count)
      end

      it "should have the right title" do
        post :create, params
        response.body.should have_selector("title", :text => "Register")
      end

      it "should render the 'new' page" do
        post :create, params
        response.should render_template :new
      end

      it "renders a checkbox for each workstation" do
        workstation = FactoryGirl.create(:workstation, name: "CUS North", abrev: "CUSN", user_id: 0)
        post :create, params
        response.body.should have_selector("input##{workstation.abrev}")
      end
    end

    describe "success" do
      let!(:cusn) { FactoryGirl.create(:workstation, name: "CUS North", abrev: "CUSN") }
      let!(:aml) { FactoryGirl.create(:workstation, name: "AML / NOL", abrev: "AML") }
      let(:params) {{ 
        "id" => user.user_name,
        "user" => success_attr,
        "CUSN" => "1",
        "AML" => "1",
        "controller" => "users",
        "action" => "create" 
      }}
      let(:normal_workstations) { %w( CUSN AML) }
      
      it "should create a user" do
        lambda do
          post :create, params
        end.should change(User, :count).by(1)
      end

      it "should not sign the user in" do
        post :create, params
        controller.should_not be_signed_in
      end

      it "should redirect to the sign in page" do
        post :create, params
        response.should redirect_to(signin_path)
      end

      it "should display a flash message" do
        post :create, params
        flash[:success].should eql("Registration was successful! Sign in now to access Messenger.")
      end

      it "merges the workstation params into the params[:users] hash" do
        controller.should_receive(:merge_workstation_params).with(params)
        put :create, params
      end

      it "sets the user's normal_workstations attribute, given valid parameters" do
        post :create, params
        assigns(:user).normal_workstations.should == normal_workstations
      end
    end
  end

  describe "GET 'show'" do

    before(:each) do
      test_sign_in(user)
    end

    context "format html" do

      it "should be success" do
        get :show, :id => user
        response.should be_success
      end

      it "should find the right user" do
        get :show, :id => user
        assigns(:user).should == user
      end

      it "should have the right title" do
        get :show, :id => user
        response.body.should have_selector("title", content: user.user_name)
      end

      it "should include the users name" do
        get :show, :id => user
        response.should have_selector("p", content: user.user_name)
      end
      
      it "should create a new attachment (for a subsequent create)" do
        get :show, :id => user
        assigns(:attachment).should be_kind_of(Attachment) 
      end
    end

    context "format json" do
      
      it "should be success" do
        get :show, id: user, format: :json
        response.should be_success
      end

      it "should return the correct user id" do
        get :show, id: user, format: :json
        response.body.should == user.as_json  
      end
    end
  end

  describe "GET index" do
    
    before(:each) do
      test_sign_in(user)
    end

    it "assigns the current user to a variable" do
      get :index
      assigns(:user).should == user
    end

    it "find all of the users" do
      User.should_receive(:all)
      get :index
    end
  end
  
  describe "GET 'edit'" do

    before(:each) do
      test_sign_in(user)
    end

    it "should be successful" do
      get :edit, :id => user
      response.should be_success
    end

    it "should have the right title" do
      get :edit, :id => user
      response.body.should have_selector("title", :text => "Edit user")
    end
<commit_msg>get the user index 'finds all users' spec passing<commit_after>require 'spec_helper'

describe UsersController do
  render_views

  let!(:user) { FactoryGirl.create(:user) }
  let(:success_attr) {{
    "user_name" => "fuzbar",
    "password" => "<PASSWORD>",
    "password_confirmation" => "<PASSWORD>",
  }}
  let(:fail_attr) {{
    "user_name" => "",
    "password" => "",
    "password_confirmation" => "",
  }}

  describe "GET 'new'" do

    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should have the right title" do
      get :new
      response.body.should have_selector("title", :text => "Register")
    end

    it "gets the workstations" do
      Workstation.should_receive(:of_type).with("td")
      Workstation.should_receive(:of_type).with("ops")
      get :new
    end
  end

  describe "POST 'create'" do

    describe "failure" do

      let(:params) {{ user: fail_attr }}

      it "should not create a user" do
        lambda do
          post :create, params
        end.should_not change(User, :count)
      end

      it "should have the right title" do
        post :create, params
        response.body.should have_selector("title", :text => "Register")
      end

      it "should render the 'new' page" do
        post :create, params
        response.should render_template :new
      end

      it "renders a checkbox for each workstation" do
        workstation = FactoryGirl.create(:workstation, name: "CUS North", abrev: "CUSN", user_id: 0)
        post :create, params
        response.body.should have_selector("input##{workstation.abrev}")
      end
    end

    describe "success" do
      let!(:cusn) { FactoryGirl.create(:workstation, name: "CUS North", abrev: "CUSN") }
      let!(:aml) { FactoryGirl.create(:workstation, name: "AML / NOL", abrev: "AML") }
      let(:params) {{ 
        "id" => user.user_name,
        "user" => success_attr,
        "CUSN" => "1",
        "AML" => "1",
        "controller" => "users",
        "action" => "create" 
      }}
      let(:normal_workstations) { %w( CUSN AML) }
      
      it "should create a user" do
        lambda do
          post :create, params
        end.should change(User, :count).by(1)
      end

      it "should not sign the user in" do
        post :create, params
        controller.should_not be_signed_in
      end

      it "should redirect to the sign in page" do
        post :create, params
        response.should redirect_to(signin_path)
      end

      it "should display a flash message" do
        post :create, params
        flash[:success].should eql("Registration was successful! Sign in now to access Messenger.")
      end

      it "merges the workstation params into the params[:users] hash" do
        controller.should_receive(:merge_workstation_params).with(params)
        put :create, params
      end

      it "sets the user's normal_workstations attribute, given valid parameters" do
        post :create, params
        assigns(:user).normal_workstations.should == normal_workstations
      end
    end
  end

  describe "GET 'show'" do

    before(:each) do
      test_sign_in(user)
    end

    context "format html" do

      it "should be success" do
        get :show, :id => user
        response.should be_success
      end

      it "should find the right user" do
        get :show, :id => user
        assigns(:user).should == user
      end

      it "should have the right title" do
        get :show, :id => user
        response.body.should have_selector("title", content: user.user_name)
      end

      it "should include the users name" do
        get :show, :id => user
        response.should have_selector("p", content: user.user_name)
      end
      
      it "should create a new attachment (for a subsequent create)" do
        get :show, :id => user
        assigns(:attachment).should be_kind_of(Attachment) 
      end
    end

    context "format json" do
      
      it "should be success" do
        get :show, id: user, format: :json
        response.should be_success
      end

      it "should return the correct user id" do
        get :show, id: user, format: :json
        response.body.should == user.as_json  
      end
    end
  end

  describe "GET index" do
    
    before(:each) do
      test_sign_in(user)
    end

    it "assigns the current user to a variable" do
      get :index
      assigns(:user).should == user
    end

    it "finds all of the users" do
      users = mock_model(User)
      User.should_receive(:all).and_return(users)
      get :index
    end
  end
  
  describe "GET 'edit'" do

    before(:each) do
      test_sign_in(user)
    end

    it "should be successful" do
      get :edit, :id => user
      response.should be_success
    end

    it "should have the right title" do
      get :edit, :id => user
      response.body.should have_selector("title", :text => "Edit user")
    end

{'fraction_non_alphanumeric': 0.10571428571428572, 'fraction_numerical': 0.0008163265306122449, 'mean_word_length': 2.26808936312104, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, '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': '5313033', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2890, 'n_words': 942}