<commit_before>require "spec_helper"

describe EmsCloudController do
  describe "#create" do
    before do
      controller.stub(:check_privileges).and_return(true)
      controller.stub(:assert_privileges).and_return(true)
      EvmSpecHelper.local_miq_server(:zone => Zone.seed)
      login_as FactoryGirl.create(:user, :features => "ems_cloud_new")
    end

    it "adds a new provider" do
      controller.instance_variable_set(:@breadcrumbs, [])
      get :new
      expect(response.status).to eq(200)
      expect(controller.stub(:edit)).to_not be_nil
    end

    render_views

    it 'shows the edit page' do
      get :edit, :id => FactoryGirl.create(:ems_amazon).id
      expect(response.status).to eq(200)
    end

    it 'creates on post' do
      expect do
        post :create,           "button"               => "add",
                                "name"                 => "foo",
                                "emstype"              => "ec2",
                                "provider_region"      => "ap-southeast-1",
                                "port"                 => "",
                                "zone"                 => "default",
                                "default_userid"       => "foo",
                                "default_password"     => "[FILTERED]",
                                "default_verify"       => "[FILTERED]",
                                "metrics_userid"       => "",
                                "metrics_password"     => "[FILTERED]",
                                "metrics_verify"       => "[FILTERED]",
                                "amqp_userid"          => "",
                                "amqp_password"        => "[FILTERED]",
                                "amqp_verify"          => "[FILTERED]",
                                "ssh_keypair_userid"   => "",
                                "ssh_keypair_password" => "[FILTERED]"
      end.to change { ManageIQ::Providers::Amazon::CloudManager.count }.by(1)
    end

    it 'creates an authentication record on post' do
      expect do
        post :create,
             "button"           => "add",
             "hostname"         => "host_openstack",
             "name"             => "foo_openstack",
             "emstype"          => "openstack",
             "provider_region"  => "",
             "port"             => "5000",
             "zone"             => "default",
             "default_userid"   => "foo",
             "default_password" => "[FILTERED]",
             "default_verify"   => "[FILTERED]"

        expect(response.status).to eq(200)
        openstack = ManageIQ::Providers::Openstack::CloudManager.where(:name => "foo_openstack")
        authentication = Authentication.where(:resource_id => openstack.to_a[0].id).first
        expect(authentication).not_to be_nil
      end.to change { Authentication.count }.by(1)
    end

    it 'updates an authentication record on post' do
      post :create,
           "button"           => "add",
           "hostname"         => "host_openstack",
           "name"             => "foo_openstack",
           "emstype"          => "openstack",
           "provider_region"  => "",
           "port"             => "5000",
           "zone"             => "default",
           "default_userid"   => "foo",
           "default_password" => "[FILTERED]",
           "default_verify"   => "[FILTERED]"

      expect(response.status).to eq(200)
      openstack = ManageIQ::Providers::Openstack::CloudManager.where(:name => "foo_openstack")
      authentication = Authentication.where(:resource_id => openstack.to_a[0].id).first
      expect(authentication).not_to be_nil

      post :update,
           "id"               => openstack.to_a[0].id,
           "button"           => "save",
           "hostname"         => "host_openstack_updated",
           "name"             => "foo_openstack",
           "emstype"          => "openstack",
           "provider_region"  => "",
           "port"             => "5000",
           "default_userid"   => "foo",
           "default_password" => "[<PASSWORD>]",
           "default_verify"   => "[FILTERED]"

      expect(response.status).to eq(200)
      openstack = ManageIQ::Providers::Openstack::CloudManager.where(:name => "foo_openstack")
      authentication = Authentication.where(:resource_id => openstack.to_a[0].id).first
      expect(authentication.userid).to eq("foo")
      expect(authentication.password).to eq("[FILTERED]")
    end

    it "validates credentials for a new record" do
      post :create,
           "button"           => "validate",
           "cred_type"        => "default",
           "name"             => "foo_ec2",
           "emstype"          => "ec2",
           "provider_region"  => "ap-southeast-1",
           "zone"             => "default",
           "default_userid"   => "foo",
           "default_password" => "[FILTERED]",
           "default_verify"   => "[FILTERED]"

      expect(response.status).to eq(200)
    end

    it "cancels a new record" do
      post :create,
           "button"           => "cancel",
           "cred_type"        => "default",
           "name"             => "foo_ec2",
           "emstype"          => "ec2",
           "provider_region"  => "ap-southeast-1",
           "zone"             => "default",
           "default_userid"   => "foo",<commit_msg>Consolidate and simplify redundant tests<commit_after>require "spec_helper"

describe EmsCloudController do
  describe "#create" do
    before do
      controller.stub(:check_privileges).and_return(true)
      controller.stub(:assert_privileges).and_return(true)
      EvmSpecHelper.local_miq_server(:zone => Zone.seed)
      login_as FactoryGirl.create(:user, :features => "ems_cloud_new")
    end

    it "adds a new provider" do
      controller.instance_variable_set(:@breadcrumbs, [])
      get :new
      expect(response.status).to eq(200)
      expect(controller.stub(:edit)).to_not be_nil
    end

    render_views

    it 'shows the edit page' do
      get :edit, :id => FactoryGirl.create(:ems_amazon).id
      expect(response.status).to eq(200)
    end

    it 'creates on post' do
      expect do
        post :create,           "button"               => "add",
                                "name"                 => "foo",
                                "emstype"              => "ec2",
                                "provider_region"      => "ap-southeast-1",
                                "port"                 => "",
                                "zone"                 => "default",
                                "default_userid"       => "foo",
                                "default_password"     => "[FILTERED]",
                                "default_verify"       => "[FILTERED]",
                                "metrics_userid"       => "",
                                "metrics_password"     => "[FILTERED]",
                                "metrics_verify"       => "[FILTERED]",
                                "amqp_userid"          => "",
                                "amqp_password"        => "[FILTERED]",
                                "amqp_verify"          => "[FILTERED]",
                                "ssh_keypair_userid"   => "",
                                "ssh_keypair_password" => "[FILTERED]"
      end.to change { ManageIQ::Providers::Amazon::CloudManager.count }.by(1)
    end

    it 'creates and updates an authentication record on post' do
      expect do
        post :create,
             "button"           => "add",
             "hostname"         => "host_openstack",
             "name"             => "foo_openstack",
             "emstype"          => "openstack",
             "provider_region"  => "",
             "port"             => "5000",
             "zone"             => "default",
             "default_userid"   => "foo",
             "default_password" => "[FILTERED]",
             "default_verify"   => "[FILTERED]"
      end.to change { Authentication.count }.by(1)

      expect(response.status).to eq(200)
      openstack = ManageIQ::Providers::Openstack::CloudManager.where(:name => "foo_openstack").first
      expect(openstack.authentications.size).to eq(1)

      expect do
        post :update,
             "id"               => openstack.id,
             "button"           => "save",
             "hostname"         => "host_openstack_updated",
             "name"             => "foo_openstack",
             "emstype"          => "openstack",
             "provider_region"  => "",
             "port"             => "5000",
             "default_userid"   => "foo",
             "default_password" => "[FILTERED]",
             "default_verify"   => "[FILTERED]"
      end.not_to change { Authentication.count }

      expect(response.status).to eq(200)
      expect(openstack.authentications.first).to have_attributes(:userid => "foo", :password => "[<PASSWORD>]")
    end

    it "validates credentials for a new record" do
      post :create,
           "button"           => "validate",
           "cred_type"        => "default",
           "name"             => "foo_ec2",
           "emstype"          => "ec2",
           "provider_region"  => "ap-southeast-1",
           "zone"             => "default",
           "default_userid"   => "foo",
           "default_password" => "[<PASSWORD>]",
           "default_verify"   => "[FILTERED]"

      expect(response.status).to eq(200)
    end

    it "cancels a new record" do
      post :create,
           "button"           => "cancel",
           "cred_type"        => "default",
           "name"             => "foo_ec2",
           "emstype"          => "ec2",
           "provider_region"  => "ap-southeast-1",
           "zone"             => "default",
           "default_userid"   => "foo",
{'fraction_non_alphanumeric': 0.13303634706894807, 'fraction_numerical': 0.007897134757517466, 'mean_word_length': 1.3923468152094938, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 137, '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': '1878579', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2647, 'n_words': 488}