<commit_before>require "spec_helper"

describe VmInfraController do
  let(:host_1x1)  { FactoryGirl.create(:host_vmware_esx, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB)) }
  let(:host_2x2)  { FactoryGirl.create(:host_vmware_esx, :hardware => FactoryGirl.create(:hardware, :cpu2x2, :ram1GB)) }
  let(:vm_vmware) { FactoryGirl.create(:vm_vmware) }
  before do
    set_user_privileges

    session[:settings] = {:quadicons => nil, :views => {:treesize => 20}}

    EvmSpecHelper.create_guid_miq_server_zone
  end

  render_views

  it 'can render the explorer' do
    session[:settings] = {:views => {}, :perpage => {:list => 10}}

    get :explorer
    expect(response.status).to eq(200)
    expect(response.body).to_not be_empty
  end

  # http://localhost:3000/vm_infra/show/10000000001403?display=vmtree_info
  it 'can render the genealogy tree' do
    seed_session_trees('vm_infra', 'vms_instances_filter_tree')
    xhr :post, :show, :id => vm_vmware.id, :display => 'vmtree_info'
    expect(response.status).to eq(200)
    response.should render_template('vm_common/_vmtree')
  end

  # http://localhost:3000/vm_infra/show/10000000000449
  it 'can open a VM and select it in the left tree' do
    get :show, :id => vm_vmware.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    node_id = "v-#{vm_vmware.compressed_id}"
    expect(response.body).to match(/miqDynatreeActivateNodeSilently\('vandt_tree', '#{node_id}'\);/)

    response.should render_template('shared/summary/_textual_tags')
    expect(response.body).to match(/VM and Instance &quot;#{vm_vmware.name}&quot;/)

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

  it 'can open the right size tab' do
    get :show, :id => vm_vmware.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_right_size', :id => vm_vmware.id
    expect(response.status).to eq(200)
  end

  it 'can open the reconfigure tab' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_1x1, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => '04'))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
  end

  it 'the reconfigure tab for a vm with max_cpu_cores_per_socket <= 1 should not display the cpu_cores_per_socket dropdown' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_1x1, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => '04'))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to_not include('Total Processors')
  end

  it 'the reconfigure tab for a vm with max_cpu_cores_per_socket > 1 should display the cpu_cores_per_socket dropdown' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_2x2, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => "07"))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to include('Total Processors')
  end

  it 'the reconfigure tab displays the submit and cancel buttons' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_2x2, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => "07"))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to include('button=submit')
    expect(response.body).to include('button=cancel')
  end

  context "skip or drop breadcrumb" do
    before do
      session[:settings] = {:views => {}, :perpage => {:list => 10}}
      get :explorer
      request.env['HTTP_REFERER'] = request.fullpath
    end

    it 'skips dropping a breadcrumb when a button action is executed' do
      post :x_button, :id => vm_vmware.id, :pressed => 'vm_ownership'
      breadcrumbs = controller.instance_variable_get(:@breadcrumbs)
      expect(breadcrumbs.size).to eq(1)
      expect(breadcrumbs).to include(:name => "VM or Templates", :url => "/vm_infra/explorer")
    end

    it 'drops a breadcrumb when an action allowing breadcrumbs is executed' do
      post :accordion_select, :id => "vms_filter"<commit_msg>Be consistent in using expect syntax<commit_after>require "spec_helper"

describe VmInfraController do
  let(:host_1x1)  { FactoryGirl.create(:host_vmware_esx, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB)) }
  let(:host_2x2)  { FactoryGirl.create(:host_vmware_esx, :hardware => FactoryGirl.create(:hardware, :cpu2x2, :ram1GB)) }
  let(:vm_vmware) { FactoryGirl.create(:vm_vmware) }
  before do
    set_user_privileges

    session[:settings] = {:quadicons => nil, :views => {:treesize => 20}}

    EvmSpecHelper.create_guid_miq_server_zone
  end

  render_views

  it 'can render the explorer' do
    session[:settings] = {:views => {}, :perpage => {:list => 10}}

    get :explorer
    expect(response.status).to eq(200)
    expect(response.body).to_not be_empty
  end

  # http://localhost:3000/vm_infra/show/10000000001403?display=vmtree_info
  it 'can render the genealogy tree' do
    seed_session_trees('vm_infra', 'vms_instances_filter_tree')
    xhr :post, :show, :id => vm_vmware.id, :display => 'vmtree_info'
    expect(response.status).to eq(200)
    expect(response).to render_template('vm_common/_vmtree')
  end

  # http://localhost:3000/vm_infra/show/10000000000449
  it 'can open a VM and select it in the left tree' do
    get :show, :id => vm_vmware.id
    response.should redirect_to(:action => 'explorer')

    post :explorer
    node_id = "v-#{vm_vmware.compressed_id}"
    expect(response.body).to match(/miqDynatreeActivateNodeSilently\('vandt_tree', '#{node_id}'\);/)

    expect(response).to render_template('shared/summary/_textual_tags')
    expect(response.body).to match(/VM and Instance &quot;#{vm_vmware.name}&quot;/)

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

  it 'can open the right size tab' do
    get :show, :id => vm_vmware.id
    expect(response).to redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_right_size', :id => vm_vmware.id
    expect(response.status).to eq(200)
  end

  it 'can open the reconfigure tab' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_1x1, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => '04'))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    expect(response).to redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
  end

  it 'the reconfigure tab for a vm with max_cpu_cores_per_socket <= 1 should not display the cpu_cores_per_socket dropdown' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_1x1, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => '04'))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    expect(response).to redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to_not include('Total Processors')
  end

  it 'the reconfigure tab for a vm with max_cpu_cores_per_socket > 1 should display the cpu_cores_per_socket dropdown' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_2x2, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => "07"))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    expect(response).to redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to include('Total Processors')
  end

  it 'the reconfigure tab displays the submit and cancel buttons' do
    vm = FactoryGirl.create(:vm_vmware, :host => host_2x2, :hardware => FactoryGirl.create(:hardware, :cpu1x1, :ram1GB, :virtual_hw_version => "07"))
    controller.stub(:x_node).and_return("v-#{vm.compressed_id}")

    get :show, :id => vm.id
    expect(response).to redirect_to(:action => 'explorer')

    post :explorer
    expect(response.status).to eq(200)

    post :x_button, :pressed => 'vm_reconfigure', :id => vm.id
    expect(response.status).to eq(200)
    expect(response.body).to include('button=submit')
    expect(response.body).to include('button=cancel')
  end

  context "skip or drop breadcrumb" do
    before do
      session[:settings] = {:views => {}, :perpage => {:list => 10}}
      get :explorer
      request.env['HTTP_REFERER'] = request.fullpath
    end

    it 'skips dropping a breadcrumb when a button action is executed' do
      post :x_button, :id => vm_vmware.id, :pressed => 'vm_ownership'
      breadcrumbs = controller.instance_variable_get(:@breadcrumbs)
      expect(breadcrumbs.size).to eq(1)
      expect(breadcrumbs).to include(:name => "VM or Templates", :url => "/vm_infra/explorer")
    end

    it 'drops a breadcrumb when an action allowing breadcrumbs is executed' do
      post :accordion_select, :id => "vms_filter"
{'fraction_non_alphanumeric': 0.14209453557216345, 'fraction_numerical': 0.023682422595360575, 'mean_word_length': 4.257142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '518536', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3513, 'n_words': 776}