<commit_before>RSpec.describe ExtManagementSystem do
  describe ".with_tenant" do
    # tenant_root
    #   \___ tenant_eye_bee_em (service_template_eye_bee_em)
    #     \__ subtenant_tenant_eye_bee_em_1 (ems_1)
    #       \__ subtenant_tenant_eye_bee_em_1_1 (ems_1_1, ems_1_1_a)
    #     \__ subtenant_tenant_eye_bee_em_3  (ems_3, ems_3_a)

    let!(:tenant_root) { Tenant.seed }

    let!(:tenant_eye_bee_em) { FactoryBot.create(:tenant, :parent => tenant_root) }
    let!(:subtenant_tenant_eye_bee_em_1) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) }
    let!(:subtenant_tenant_eye_bee_em_3) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) }

    let!(:subtenant_tenant_eye_bee_em_1_1) { FactoryBot.create(:tenant, :parent => subtenant_tenant_eye_bee_em_1) }

    let!(:ems_eye_bee_em) { FactoryBot.create(:ext_management_system, :tenant => tenant_eye_bee_em) }
    let!(:ems_1)          { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1) }
    let!(:ems_3)          { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_3) }
    let!(:ems_3_a)        { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_3) }
    let!(:ems_1_1)        { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1_1) }
    let!(:ems_1_1_a)      { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1_1) }

    it "lists ancestor service templates" do
      expect(ExtManagementSystem.with_tenant(subtenant_tenant_eye_bee_em_1_1.id).ids).to match_array([ems_1_1.id, ems_1_1_a.id, ems_1.id, ems_eye_bee_em.id])
      expect(ExtManagementSystem.with_tenant(subtenant_tenant_eye_bee_em_3.id).ids).to match_array([ems_3.id, ems_3_a.id, ems_eye_bee_em.id])
    end
  end

  it ".model_name_from_emstype" do
    described_class.leaf_subclasses.each do |klass|
      expect(described_class.model_name_from_emstype(klass.ems_type)).to eq(klass.name)
    end
    expect(described_class.model_name_from_emstype('foo')).to be_nil
  end

  it ".create_discovered_ems" do
    ost = OpenStruct.new(:ipaddr => "0.0.0.0", :hypervisor => [:virtualcenter])

    allow(MiqServer).to receive(:my_server).and_return(
      double("MiqServer", :zone => double("Zone", :name => "foo", :id => 1)))
    expect(AuditEvent).to receive(:success)

    described_class.create_discovered_ems(ost)
  end

  let(:all_types_and_descriptions) do
    {
      "ansible_tower_automation"    => "Ansible Tower Automation",
      "azure"                       => "Azure",
      "azure_network"               => "Azure Network",
      "azure_stack"                 => "Azure Stack",
      "azure_stack_network"         => "Azure Stack Network",
      "ec2"                         => "Amazon EC2",
      "ec2_network"                 => "Amazon EC2 Network",
      "ec2_ebs_storage"             => "Amazon EBS",
      "embedded_ansible_automation" => "Embedded Ansible Automation",
      "s3"                          => "Amazon S3",
      "foreman_configuration"       => "Foreman Configuration",
      "foreman_provisioning"        => "Foreman Provisioning",
      "gce"                         => "Google Compute Engine",
      "gce_network"                 => "Google Network",
      "kubernetes"                  => "Kubernetes",
      "kubernetes_monitor"          => "Kubernetes Monitor",
      "kubevirt"                    => "KubeVirt",
      "openshift"                   => "OpenShift",
      "openshift_monitor"           => "Openshift Monitor",
      "openstack"                   => "OpenStack",
      "openstack_infra"             => "OpenStack Platform Director",
      "openstack_network"           => "OpenStack Network",
      "lenovo_ph_infra"             => "Lenovo XClarity",
      "nuage_network"               => "Nuage Network Manager",
      "redfish_ph_infra"            => "Redfish",
      "redhat_network"              => "Redhat Network",
      "rhevm"                       => "Red Hat Virtualization",
      "scvmm"                       => "Microsoft System Center VMM",
      "vmwarews"                    => "VMware vCenter",
      "vmware_cloud"                => "VMware vCloud",
      "vmware_cloud_network"        => "VMware Cloud Network",
      "cinder"                      => "Cinder ",
      "swift"                       => "Swift ",
    }
  end

  it ".types" do
    expect(described_class.types).to match_array(all_types_and_descriptions.keys)
  end

  describe ".supported_types" do
    it "with default permissions" do
      expect(described_class.supported_types).to match_array(all_types_and_descriptions.keys)
    end

    it "with removed permissions" do
      stub_vmdb_permission_store_with_types(["ems-type:vmwarews"]) do
        expect(described_class.supported_types).not_to include("vmwarews")
      end
    end
  end

  describe ".supported_types_and_descriptions_hash" do
    it "with default permissions" do
      expect(described_class.supported_types_and_descriptions_hash).to eq(all_types_and_descriptions)
    end

    it "with removed permissions" do
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).and_return(true)
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).with("vmwarews").and_return(false)
      expect(described_class.supported_types_and_descriptions_hash).to_not include("vmwarews")
    end
  end

  it ".ems_infra_discovery_types" do
    expected_types = %w(scvmm rhevm virtualcenter openstack_infra)

    expect(described_class.ems_infra_discovery_types).to match_array(expected_types)
  end

  it ".with_eligible_manager_types" do<commit_msg>Fix a missing permission stub in a spec<commit_after>RSpec.describe ExtManagementSystem do
  describe ".with_tenant" do
    # tenant_root
    #   \___ tenant_eye_bee_em (service_template_eye_bee_em)
    #     \__ subtenant_tenant_eye_bee_em_1 (ems_1)
    #       \__ subtenant_tenant_eye_bee_em_1_1 (ems_1_1, ems_1_1_a)
    #     \__ subtenant_tenant_eye_bee_em_3  (ems_3, ems_3_a)

    let!(:tenant_root) { Tenant.seed }

    let!(:tenant_eye_bee_em) { FactoryBot.create(:tenant, :parent => tenant_root) }
    let!(:subtenant_tenant_eye_bee_em_1) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) }
    let!(:subtenant_tenant_eye_bee_em_3) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) }

    let!(:subtenant_tenant_eye_bee_em_1_1) { FactoryBot.create(:tenant, :parent => subtenant_tenant_eye_bee_em_1) }

    let!(:ems_eye_bee_em) { FactoryBot.create(:ext_management_system, :tenant => tenant_eye_bee_em) }
    let!(:ems_1)          { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1) }
    let!(:ems_3)          { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_3) }
    let!(:ems_3_a)        { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_3) }
    let!(:ems_1_1)        { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1_1) }
    let!(:ems_1_1_a)      { FactoryBot.create(:ext_management_system, :tenant => subtenant_tenant_eye_bee_em_1_1) }

    it "lists ancestor service templates" do
      expect(ExtManagementSystem.with_tenant(subtenant_tenant_eye_bee_em_1_1.id).ids).to match_array([ems_1_1.id, ems_1_1_a.id, ems_1.id, ems_eye_bee_em.id])
      expect(ExtManagementSystem.with_tenant(subtenant_tenant_eye_bee_em_3.id).ids).to match_array([ems_3.id, ems_3_a.id, ems_eye_bee_em.id])
    end
  end

  it ".model_name_from_emstype" do
    described_class.leaf_subclasses.each do |klass|
      expect(described_class.model_name_from_emstype(klass.ems_type)).to eq(klass.name)
    end
    expect(described_class.model_name_from_emstype('foo')).to be_nil
  end

  it ".create_discovered_ems" do
    ost = OpenStruct.new(:ipaddr => "0.0.0.0", :hypervisor => [:virtualcenter])

    allow(MiqServer).to receive(:my_server).and_return(
      double("MiqServer", :zone => double("Zone", :name => "foo", :id => 1)))
    expect(AuditEvent).to receive(:success)

    described_class.create_discovered_ems(ost)
  end

  let(:all_types_and_descriptions) do
    {
      "ansible_tower_automation"    => "Ansible Tower Automation",
      "azure"                       => "Azure",
      "azure_network"               => "Azure Network",
      "azure_stack"                 => "Azure Stack",
      "azure_stack_network"         => "Azure Stack Network",
      "ec2"                         => "Amazon EC2",
      "ec2_network"                 => "Amazon EC2 Network",
      "ec2_ebs_storage"             => "Amazon EBS",
      "embedded_ansible_automation" => "Embedded Ansible Automation",
      "s3"                          => "Amazon S3",
      "foreman_configuration"       => "Foreman Configuration",
      "foreman_provisioning"        => "Foreman Provisioning",
      "gce"                         => "Google Compute Engine",
      "gce_network"                 => "Google Network",
      "kubernetes"                  => "Kubernetes",
      "kubernetes_monitor"          => "Kubernetes Monitor",
      "kubevirt"                    => "KubeVirt",
      "openshift"                   => "OpenShift",
      "openshift_monitor"           => "Openshift Monitor",
      "openstack"                   => "OpenStack",
      "openstack_infra"             => "OpenStack Platform Director",
      "openstack_network"           => "OpenStack Network",
      "lenovo_ph_infra"             => "Lenovo XClarity",
      "nuage_network"               => "Nuage Network Manager",
      "redfish_ph_infra"            => "Redfish",
      "redhat_network"              => "Redhat Network",
      "rhevm"                       => "Red Hat Virtualization",
      "scvmm"                       => "Microsoft System Center VMM",
      "vmwarews"                    => "VMware vCenter",
      "vmware_cloud"                => "VMware vCloud",
      "vmware_cloud_network"        => "VMware Cloud Network",
      "cinder"                      => "Cinder ",
      "swift"                       => "Swift ",
    }
  end

  it ".types" do
    expect(described_class.types).to match_array(all_types_and_descriptions.keys)
  end

  describe ".supported_types" do
    it "with default permissions" do
      expect(described_class.supported_types).to match_array(all_types_and_descriptions.keys)
    end

    it "with removed permissions" do
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).and_return(true)
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).with("vmwarews").and_return(false)
      expect(described_class.supported_types).not_to include("vmwarews")
    end
  end

  describe ".supported_types_and_descriptions_hash" do
    it "with default permissions" do
      expect(described_class.supported_types_and_descriptions_hash).to eq(all_types_and_descriptions)
    end

    it "with removed permissions" do
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).and_return(true)
      allow(Vmdb::PermissionStores.instance).to receive(:supported_ems_type?).with("vmwarews").and_return(false)
      expect(described_class.supported_types_and_descriptions_hash).to_not include("vmwarews")
    end
  end

  it ".ems_infra_discovery_types" do
    expected_types = %w(scvmm rhevm virtualcenter openstack_infra)

    expect(described_class.ems_infra_discovery_types).to match_array(expected_types)
  end

  it ".with_eligible_manager_types" do
{'fraction_non_alphanumeric': 0.11306228373702422, 'fraction_numerical': 0.008996539792387544, 'mean_word_length': 2.9524786324786323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4217087', 'n_tokens_mistral': 3883, 'n_tokens_neox': 3780, 'n_words': 569}