<commit_before>describe Mixins::CheckedIdMixin do
  describe '#find_records_with_rbac' do
    # include tested mixin
    let(:mixin) { Object.new.tap { |s| s.singleton_class.send(:include, described_class) } }
    # create user, user role and group
    let(:user_role) { FactoryGirl.create(:miq_user_role) }
    let(:group) { FactoryGirl.create(:miq_group, :miq_user_role => user_role) }
    let(:current_user) { FactoryGirl.create(:user, :miq_groups => [group]) }
    before :each do
      allow(mixin).to receive(:current_user).and_return(current_user)
      allow(current_user).to receive(:get_timezone).and_return("Prague")
    end
    # create records
    let!(:vm1) { FactoryGirl.create(:vm_or_template) }
    let!(:vm2) { FactoryGirl.create(:vm_or_template) }
    let!(:vm3) { FactoryGirl.create(:vm_or_template) }

    subject { mixin.send(:find_records_with_rbac, model, id) }

    context 'when single record is checked in show list' do
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id] }
      it { is_expected.to eq([vm1]) }
    end

    context 'when multiple records are checked in show list' do
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id, vm2.id] }
      it { is_expected.to match_array([vm1, vm2]) }
    end

    context 'when user is not authorized to access the record' do
      before { allow(Rbac).to receive(:filtered).and_return([vm1, vm2]) }
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id, vm2.id, vm3.id] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access selected records")
      end
    end

    context 'when user tries to access non-existent record' do
      let(:model) { VmOrTemplate }
      let(:missing_vm_id) { VmOrTemplate.maximum(:id) + 1000 }
      let(:id) { [vm1.id, vm2.id, missing_vm_id] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access selected records")
      end
    end
  end
end<commit_msg>Add specs for empty ids or contains nil<commit_after>describe Mixins::CheckedIdMixin do
  describe '#find_records_with_rbac' do
    # include tested mixin
    let(:mixin) { Object.new.tap { |s| s.singleton_class.send(:include, described_class) } }
    # create user, user role and group
    let(:user_role) { FactoryGirl.create(:miq_user_role) }
    let(:group) { FactoryGirl.create(:miq_group, :miq_user_role => user_role) }
    let(:current_user) { FactoryGirl.create(:user, :miq_groups => [group]) }
    before :each do
      allow(mixin).to receive(:current_user).and_return(current_user)
      allow(current_user).to receive(:get_timezone).and_return("Prague")
    end
    # create records
    let!(:vm1) { FactoryGirl.create(:vm_or_template) }
    let!(:vm2) { FactoryGirl.create(:vm_or_template) }
    let!(:vm3) { FactoryGirl.create(:vm_or_template) }

    subject { mixin.send(:find_records_with_rbac, model, id) }

    context 'when single record is checked in show list' do
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id] }
      it { is_expected.to eq([vm1]) }
    end

    context 'when multiple records are checked in show list' do
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id, vm2.id] }
      it { is_expected.to match_array([vm1, vm2]) }
    end

    context 'when user is not authorized to access the record' do
      before { allow(Rbac).to receive(:filtered).and_return([vm1, vm2]) }
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id, vm2.id, vm3.id] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access selected records")
      end
    end

    context 'when user tries to access non-existent record' do
      let(:model) { VmOrTemplate }
      let(:missing_vm_id) { VmOrTemplate.maximum(:id) + 1000 }
      let(:id) { [vm1.id, vm2.id, missing_vm_id] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access selected records")
      end
    end

    context 'when there is no record id' do
      let(:model) { VmOrTemplate }
      let(:id) { [] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access records without id")
      end
    end

    context 'when user tries to access record with nil id ' do
      let(:model) { VmOrTemplate }
      let(:id) { [vm1.id, nil] }
      it 'is expected to raise exeption' do
        expect { subject }.to raise_error("Can't access records without id")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12405513561582926, 'fraction_numerical': 0.009115162294353045, 'mean_word_length': 3.1580406654343807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5993627', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1481, 'n_words': 389}