<commit_before>require "spec_helper"

describe Rbac do
  context "tenant scoping" do
    before do
      default_tenant = EvmSpecHelper.create_root_tenant
      User.stub(:server_timezone => "UTC")

      @owner_tenant  = FactoryGirl.create(:tenant, :divisible => false, :parent => default_tenant)
      @owner_group   = FactoryGirl.create(:miq_group, :tenant => @owner_tenant)
      @owner_user    = FactoryGirl.create(:user, :userid => 'foo', :miq_groups => [@owner_group])

      @other_tenant  = FactoryGirl.create(:tenant, :divisible => false, :parent => default_tenant)
      @other_group   = FactoryGirl.create(:miq_group, :tenant => @other_tenant)
      @other_user    = FactoryGirl.create(:user, :userid => 'bar', :miq_groups => [@other_group])
    end

    klass_factory_names = [
      "ExtManagementSystem", :ems_vmware,
      "MiqAeNamespace", :miq_ae_namespace,
      "MiqAeDomain", :miq_ae_domain,
      #"MiqRequest", :miq_request,  # MiqRequest is an abstract class that can't be instantiated currently
      "MiqRequestTask", :miq_request_task,
      "Provider", :provider,
      "Service", :service,
      "ServiceTemplate", :service_template,
      "ServiceTemplateCatalog", :service_template_catalog,
      "Vm", :vm_vmware
    ]

    klass_factory_names.each_slice(2) do |klass, factory_name|
      context "#{klass} basic filtering" do
        before do
          @owned_object = FactoryGirl.create(factory_name, :tenant => @owner_tenant)
        end

        it ".search with :userid, finds user's tenant #{klass}" do
          results, = Rbac.search(:class => klass, :results_format => :objects, :userid => @owner_user.userid)
          expect(results).to eq [@owned_object]
        end

        it ".search with :userid filters out other tenants" do
          results, = Rbac.search(:class => klass, :results_format => :objects, :userid => @other_user.userid)
          expect(results).to eq []
        end
      end
    end

    context "Advanced filtering" do
      before do
        @owned_object = FactoryGirl.create(:vm_vmware, :tenant => @owner_tenant)
      end

      it ".search with User.with_userid finds user's tenant object" do
        User.with_userid(@owner_user.userid) do
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq [@owned_object]
        end
      end

      it ".search with User.with_userid filters out other tenants" do
        User.with_userid(@other_user.userid) do
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq []
        end
      end

      it ".search with :miq_group_id, finds user's tenant object" do
        results, = Rbac.search(:class => "Vm", :results_format => :objects, :miq_group_id => @owner_group.id)
        expect(results).to eq [@owned_object]
      end

      it ".search with :miq_group_id filters out other tenants" do
        results, = Rbac.search(:class => "Vm", :results_format => :objects, :miq_group_id => @other_group.id)
        expect(results).to eq []
      end

      it ".search with User.with_userid leaving tenant" do
        User.with_userid(@owner_user.userid) do
          @owner_user.miq_groups = [@other_group]
          @owner_user.save
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq []
        end
      end

      it ".search with User.with_userid joining tenant" do
        User.with_userid(@other_user.userid) do
          @other_user.miq_groups = [@owner_group]
          @other_user.save
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq [@owned_object]
        end
      end
    end
  end

  context "common setup" do
    before(:each) do
      User.stub(:server_timezone => "UTC")

      MiqRegion.seed
      @tags = {
        2  => "/managed/environment/prod",
        3  => "/managed/environment/dev",
        4  => "/managed/service_level/gold",
        5  => "/managed/service_level/silver"
      }

      User.any_instance.stub(:validate => true)
      @group = FactoryGirl.create(:miq_group)
      @user  = FactoryGirl.create(:user, :miq_groups => [@group])
    end

    context "with Hosts" do
      before(:each) do
        @host1 = FactoryGirl.create(:host, :name => "Host1", :hostname => "host1.local")
        @host2 = FactoryGirl.create(:host, :name => "Host2", :hostname => "host2.local")
        @hosts = [@host1, @host2]
      end

      context "having Metric data" do
        before(:each) do
          @timestamps = [
            ["2010-04-14T20:52:30Z", 100.0],
            ["2010-04-14T21:51:10Z", 1.0],
            ["2010-04-14T21:51:30Z", 2.0],
            ["2010-04-14T21:51:50Z", 4.0],
            ["2010-04-14T21:52:10Z", 8.0],
            ["2010-04-14T21:52:30Z", 15.0],
            ["2010-04-14T22:52:30Z", 100.0],<commit_msg>Tests pass without stubbing User#validate.<commit_after>require "spec_helper"

describe Rbac do
  context "tenant scoping" do
    before do
      default_tenant = EvmSpecHelper.create_root_tenant
      User.stub(:server_timezone => "UTC")

      @owner_tenant  = FactoryGirl.create(:tenant, :divisible => false, :parent => default_tenant)
      @owner_group   = FactoryGirl.create(:miq_group, :tenant => @owner_tenant)
      @owner_user    = FactoryGirl.create(:user, :userid => 'foo', :miq_groups => [@owner_group])

      @other_tenant  = FactoryGirl.create(:tenant, :divisible => false, :parent => default_tenant)
      @other_group   = FactoryGirl.create(:miq_group, :tenant => @other_tenant)
      @other_user    = FactoryGirl.create(:user, :userid => 'bar', :miq_groups => [@other_group])
    end

    klass_factory_names = [
      "ExtManagementSystem", :ems_vmware,
      "MiqAeNamespace", :miq_ae_namespace,
      "MiqAeDomain", :miq_ae_domain,
      #"MiqRequest", :miq_request,  # MiqRequest is an abstract class that can't be instantiated currently
      "MiqRequestTask", :miq_request_task,
      "Provider", :provider,
      "Service", :service,
      "ServiceTemplate", :service_template,
      "ServiceTemplateCatalog", :service_template_catalog,
      "Vm", :vm_vmware
    ]

    klass_factory_names.each_slice(2) do |klass, factory_name|
      context "#{klass} basic filtering" do
        before do
          @owned_object = FactoryGirl.create(factory_name, :tenant => @owner_tenant)
        end

        it ".search with :userid, finds user's tenant #{klass}" do
          results, = Rbac.search(:class => klass, :results_format => :objects, :userid => @owner_user.userid)
          expect(results).to eq [@owned_object]
        end

        it ".search with :userid filters out other tenants" do
          results, = Rbac.search(:class => klass, :results_format => :objects, :userid => @other_user.userid)
          expect(results).to eq []
        end
      end
    end

    context "Advanced filtering" do
      before do
        @owned_object = FactoryGirl.create(:vm_vmware, :tenant => @owner_tenant)
      end

      it ".search with User.with_userid finds user's tenant object" do
        User.with_userid(@owner_user.userid) do
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq [@owned_object]
        end
      end

      it ".search with User.with_userid filters out other tenants" do
        User.with_userid(@other_user.userid) do
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq []
        end
      end

      it ".search with :miq_group_id, finds user's tenant object" do
        results, = Rbac.search(:class => "Vm", :results_format => :objects, :miq_group_id => @owner_group.id)
        expect(results).to eq [@owned_object]
      end

      it ".search with :miq_group_id filters out other tenants" do
        results, = Rbac.search(:class => "Vm", :results_format => :objects, :miq_group_id => @other_group.id)
        expect(results).to eq []
      end

      it ".search with User.with_userid leaving tenant" do
        User.with_userid(@owner_user.userid) do
          @owner_user.miq_groups = [@other_group]
          @owner_user.save
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq []
        end
      end

      it ".search with User.with_userid joining tenant" do
        User.with_userid(@other_user.userid) do
          @other_user.miq_groups = [@owner_group]
          @other_user.save
          results, = Rbac.search(:class => "Vm", :results_format => :objects)
          expect(results).to eq [@owned_object]
        end
      end
    end
  end

  context "common setup" do
    before(:each) do
      User.stub(:server_timezone => "UTC")

      MiqRegion.seed
      @tags = {
        2  => "/managed/environment/prod",
        3  => "/managed/environment/dev",
        4  => "/managed/service_level/gold",
        5  => "/managed/service_level/silver"
      }

      @group = FactoryGirl.create(:miq_group)
      @user  = FactoryGirl.create(:user, :miq_groups => [@group])
    end

    context "with Hosts" do
      before(:each) do
        @host1 = FactoryGirl.create(:host, :name => "Host1", :hostname => "host1.local")
        @host2 = FactoryGirl.create(:host, :name => "Host2", :hostname => "host2.local")
        @hosts = [@host1, @host2]
      end

      context "having Metric data" do
        before(:each) do
          @timestamps = [
            ["2010-04-14T20:52:30Z", 100.0],
            ["2010-04-14T21:51:10Z", 1.0],
            ["2010-04-14T21:51:30Z", 2.0],
            ["2010-04-14T21:51:50Z", 4.0],
            ["2010-04-14T21:52:10Z", 8.0],
            ["2010-04-14T21:52:30Z", 15.0],
            ["2010-04-14T22:52:30Z", 100.0],
{'fraction_non_alphanumeric': 0.135096497498213, 'fraction_numerical': 0.0265495762279179, 'mean_word_length': 2.6287513894034826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 90, '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': '7024814', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3162, 'n_words': 687}