<commit_before>describe ApplicationHelper do
  describe '#ownership_user_options' do
    let(:child_tenant)                  { FactoryGirl.create(:tenant) }
    let(:grand_child_tenant)            { FactoryGirl.create(:tenant, :parent => child_tenant) }
    let(:great_grand_child_tenant)      { FactoryGirl.create(:tenant, :parent => grand_child_tenant) }
    let(:child_role)                    { FactoryGirl.create(:miq_user_role) }
    let(:grand_child_tenant_role)       { FactoryGirl.create(:miq_user_role) }
    let(:great_grand_child_tenant_role) { FactoryGirl.create(:miq_user_role) }
    let(:child_group)                   { FactoryGirl.create(:miq_group, :role => child_role, :tenant => child_tenant) }
    let(:grand_child_group) do
      FactoryGirl.create(:miq_group, :role   => grand_child_tenant_role,
                                     :tenant => grand_child_tenant)
    end
    let(:great_grand_child_group) do
      FactoryGirl.create(:miq_group, :role   => great_grand_child_tenant_role,
                                     :tenant => great_grand_child_tenant)
    end
    let!(:admin_user)             { FactoryGirl.create(:user_admin) }
    let!(:child_user)             { FactoryGirl.create(:user, :miq_groups => [child_group]) }
    let!(:grand_child_user)       { FactoryGirl.create(:user, :miq_groups => [grand_child_group]) }
    let!(:great_grand_child_user) { FactoryGirl.create(:user, :miq_groups => [great_grand_child_group]) }

    subject { helper.ownership_user_options }
    context 'admin user' do
      it 'lists all users' do
        allow(User).to receive(:server_timezone).and_return('UTC')
        allow(User).to receive(:current_user).and_return(admin_user)
        expect(subject.count).to eq(User.count)
      end
    end

    context 'a tenant user' do
      it 'lists users in that tenant' do
        allow(User).to receive(:server_timezone).and_return('UTC')
        allow(User).to receive(:current_user).and_return(grand_child_user)

        ids = [great_grand_child_tenant, grand_child_tenant].collect(&:user_ids).flatten
        expect(subject.values(&:id).map(&:to_i)).to match_array(ids)
      end
    end
  end
end<commit_msg>RBAC should display only users from his groups<commit_after>describe ApplicationHelper do
  describe '#ownership_user_options' do
    let(:child_tenant)                  { FactoryGirl.create(:tenant) }
    let(:grand_child_tenant)            { FactoryGirl.create(:tenant, :parent => child_tenant) }
    let(:great_grand_child_tenant)      { FactoryGirl.create(:tenant, :parent => grand_child_tenant) }
    let(:child_role)                    { FactoryGirl.create(:miq_user_role) }
    let(:grand_child_tenant_role)       { FactoryGirl.create(:miq_user_role) }
    let(:great_grand_child_tenant_role) { FactoryGirl.create(:miq_user_role) }
    let(:child_group)                   { FactoryGirl.create(:miq_group, :role => child_role, :tenant => child_tenant) }
    let(:grand_child_group) do
      FactoryGirl.create(:miq_group, :role   => grand_child_tenant_role,
                                     :tenant => grand_child_tenant)
    end
    let(:great_grand_child_group) do
      FactoryGirl.create(:miq_group, :role   => great_grand_child_tenant_role,
                                     :tenant => great_grand_child_tenant)
    end
    let!(:admin_user)             { FactoryGirl.create(:user_admin) }
    let!(:child_user)             { FactoryGirl.create(:user, :miq_groups => [child_group]) }
    let!(:grand_child_user)       { FactoryGirl.create(:user, :miq_groups => [grand_child_group, great_grand_child_group]) }
    let!(:great_grand_child_user) { FactoryGirl.create(:user, :miq_groups => [great_grand_child_group]) }

    subject { helper.ownership_user_options }
    context 'admin user' do
      it 'lists all users' do
        allow(User).to receive(:server_timezone).and_return('UTC')
        allow(User).to receive(:current_user).and_return(admin_user)
        expect(subject.count).to eq(User.count)
      end
    end

    context 'a tenant user' do
      it 'lists users in his group' do
        allow(User).to receive(:server_timezone).and_return('UTC')
        allow(User).to receive(:current_user).and_return(grand_child_user)
        ids = grand_child_user.miq_groups.collect(&:user_ids).flatten.uniq
        expect(subject.values(&:id).map(&:to_i)).to match_array(ids)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11617880644429317, 'fraction_numerical': 0.0, 'mean_word_length': 3.1234798877455567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3627194', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1373, 'n_words': 210}