<commit_before>require 'spec_helper'

describe UsersController do

  describe "GET 'index'" do
    before :each do
      sign_in_admin
    end

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

    it "should render the right template" do
      get :index
      response.should render_template :index
    end
  end

  shared_examples_for "user with permission to all actions" do

    before(:each) do
      @dummy_user = User.make(:company => @logged_user.company)
    end

    it "should be able to GET index" do
      get :index
      response.should be_success
    end

    it "should be able to GET new" do
      get :new
      response.should be_success
    end

    it "should be able to GET edit" do
      get :edit, :id => @dummy_user
      response.should be_success
    end

    it "should be able to GET destroy" do
      get :destroy, :id => @dummy_user.id
      response.should be_redirect
    end

    it "should be able to delete a user" do
      expect {
        get :destroy, :id => @dummy_user.id
      }.to change { User.count }.by(-1)
    end

    it "should be able to POST create" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      post :create, :user => attrs, :email => "<EMAIL>"
      response.should be_redirect
    end

    it "should be able to create a new user" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      expect {
        post :create, :user => attrs, :email => "<EMAIL>"
      }.to change { User.count }.by(1)
    end

    it "should be able to update any user" do
      new_attrs = @dummy_user.attributes.merge('name' => 'bananas')
      new_attrs = new_attrs.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      put :update, :id => @dummy_user, :user => new_attrs
      @dummy_user.reload
      @dummy_user.name.should match 'bananas'
    end
  end

  shared_examples_for "user without permission to admin protected actions" do
 
    before(:each) do
      @dummy_user = User.make(:company => @logged_user.company)
    end

   it "should not be able to GET index" do
      get :index
      response.should be_redirect
    end

    it "should not be able to GET new" do
      get :new
      response.should be_redirect
    end

    it "should not be able to GET edit" do
      get :edit, :id => @dummy_user
      response.should be_redirect
    end

    it "should not be able to destroy any user" do
      expect {
        get :destroy, :id => @dummy_user
      }.to_not change { User.count }
      response.should be_redirect
    end

    it "should not be able to create a new user" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      expect {
        post :create, :user => attrs, :email => "<EMAIL>"
      }.to_not change { User.count }
    end

    it "should not be able to update any user" do
      new_attrs = @dummy_user.attributes.merge('name' => 'bananas')
      new_attrs = new_attrs.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      put :update, :id => @dummy_user, :user => new_attrs
      @dummy_user.reload
      @dummy_user.name.should_not match 'bananas'
    end
  end


  context "when logged in user is admin," do
    before(:each) do
      sign_in_admin
    end

    it_should_behave_like "user with permission to all actions"
  end

  context "when logged in user is not admin," do
    before(:each) do
      sign_in_normal_user
    end

    it_should_behave_like "user without permission to admin protected actions"
  end

  context "when logged user is not admin but can edit clients," do
    before(:each) do
      sign_in_normal_user(:edit_clients => true)
    end

    it_should_behave_like 'user with permission to all actions'
  end
end<commit_msg>users_controller spec for disabled contact creation<commit_after>require 'spec_helper'

describe UsersController do

  describe "GET 'index'" do
    before :each do
      Setting.contact_creation_allowed = true
      sign_in_admin
    end

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

    it "should render the right template" do
      get :index
      response.should render_template :index
    end
  end

  shared_examples_for "user with permission to all actions" do

    before(:each) do
      @dummy_user = User.make(:company => @logged_user.company)
    end

    it "should be able to GET index" do
      get :index
      response.should be_success
    end

    it "should be able to GET new" do
      get :new
      response.should be_success
    end

    it "should be able to GET edit" do
      get :edit, :id => @dummy_user
      response.should be_success
    end

    it "should be able to GET destroy" do
      get :destroy, :id => @dummy_user.id
      response.should be_redirect
    end

    it "should be able to delete a user" do
      expect {
        get :destroy, :id => @dummy_user.id
      }.to change { User.count }.by(-1)
    end

    it "should be able to POST create" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      post :create, :user => attrs, :email => "<EMAIL>"
      response.should be_redirect
    end

    it "should be able to create a new user" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      expect {
        post :create, :user => attrs, :email => "<EMAIL>"
      }.to change { User.count }.by(1)
    end

    it "should be able to update any user" do
      new_attrs = @dummy_user.attributes.merge('name' => 'bananas')
      new_attrs = new_attrs.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      put :update, :id => @dummy_user, :user => new_attrs
      @dummy_user.reload
      @dummy_user.name.should match 'bananas'
    end
  end

  shared_examples_for "user without permission to admin protected actions" do

    before(:each) do
      @dummy_user = User.make(:company => @logged_user.company)
    end

   it "should not be able to GET index" do
      get :index
      response.should be_redirect
    end

    it "should not be able to GET new" do
      get :new
      response.should be_redirect
    end

    it "should not be able to GET edit" do
      get :edit, :id => @dummy_user
      response.should be_redirect
    end

    it "should not be able to destroy any user" do
      expect {
        get :destroy, :id => @dummy_user
      }.to_not change { User.count }
      response.should be_redirect
    end

    it "should not be able to create a new user" do
      new_user = User.make
      attrs = new_user.attributes.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      expect {
        post :create, :user => attrs, :email => "<EMAIL>"
      }.to_not change { User.count }
    end

    it "should not be able to update any user" do
      new_attrs = @dummy_user.attributes.merge('name' => 'bananas')
      new_attrs = new_attrs.with_indifferent_access.slice(:name, :password, :customer_id, :date_format, :time_zone, :time_format, :username)
      put :update, :id => @dummy_user, :user => new_attrs
      @dummy_user.reload
      @dummy_user.name.should_not match 'bananas'
    end
  end


  context "when logged in user is admin," do
    before(:each) do
      Setting.contact_creation_allowed = true
      sign_in_admin
    end

    it_should_behave_like "user with permission to all actions"
  end

  context "when logged in user is not admin," do
    before(:each) do
      Setting.contact_creation_allowed = true
      sign_in_normal_user
    end

    it_should_behave_like "user without permission to admin protected actions"
  end

  context "when logged in user is admin, but contact creation is disabled in app" do
    before(:each) do
      Setting.contact_creation_allowed = false
      sign_in_admin
    end

    it_should_behave_like "user without permission to admin protected actions"
  end


  context "when logged user is not admin but can edit clients," do
    before(:each) do
      Setting.contact_creation_allowed = true
      sign_in_normal_user(:edit_clients => true)
    end

    it_should_behave_like 'user with permission to all actions'
  end
end
{'fraction_non_alphanumeric': 0.08726042841037204, 'fraction_numerical': 0.00045095828635851183, 'mean_word_length': 2.8270060396893872, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, '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': '2877408', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2679, 'n_words': 904}