Project

General

Profile

Download (69.1 KB) Statistics
| Branch: | Tag: | Revision:
2f077f63 Ohad Levy
require 'test_helper'
510dbaae Timo Goebel
require 'pagelets_test_helper'
2f077f63 Ohad Levy
class HostsControllerTest < ActionController::TestCase
510dbaae Timo Goebel
include PageletsIsolation

d91160ea Lucas Tolchinsky
setup :initialize_host

a154c816 lizagilman
basic_pagination_rendered_test
basic_pagination_per_page_test

d30a7ff3 Ori Rabin
def host_attributes(host)
12612809 Dominic Cleal
known_attrs = HostsController.host_params_filter.accessible_attributes(HostsController.parameter_filter_context)
host.attributes.except('id', 'created_at', 'updated_at').slice(*known_attrs)
d30a7ff3 Ori Rabin
end

510d53cd Marek Hulan
test 'show' do
d041d4bb Dominic Cleal
get :show, params: { :id => Host.first.name }, session: set_session_user
b09b4515 Ohad Levy
assert_template 'show'
2f077f63 Ohad Levy
end
8f10d0f8 Juan Manuel
510d53cd Marek Hulan
test 'create_invalid' do
b09b4515 Ohad Levy
Host.any_instance.stubs(:valid?).returns(false)
d041d4bb Dominic Cleal
post :create, params: { :host => {:name => nil} }, session: set_session_user
b09b4515 Ohad Levy
assert_template 'new'
8f10d0f8 Juan Manuel
end

510d53cd Marek Hulan
test 'create_valid' do
b09b4515 Ohad Levy
Host.any_instance.stubs(:valid?).returns(true)
d041d4bb Dominic Cleal
post :create, params: { :host => {:name => "test"} }, session: set_session_user
9c0e127b Paul Kelly
assert_redirected_to host_url(assigns('host'))
8f10d0f8 Juan Manuel
end

9c0e127b Paul Kelly
test "should get index" do
d041d4bb Dominic Cleal
get :index, session: set_session_user
9c0e127b Paul Kelly
assert_response :success
assert_template 'index'
end

c8f5cd53 Tomer Brisker
test "should get csv index with data" do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :with_hostgroup, :with_environment, :on_compute_resource, :with_reports)
d041d4bb Dominic Cleal
get :index, params: { :format => 'csv', :search => "name = #{host.name}" }, session: set_session_user
c8f5cd53 Tomer Brisker
assert_response :success
buf = response.stream.instance_variable_get(:@buf)
assert_equal "Name,Operatingsystem,Environment,Compute Resource Or Model,Hostgroup,Last Report\n", buf.next
assert_equal "#{host.name},#{host.operatingsystem},#{host.environment},#{host.compute_resource.name},#{host.hostgroup},#{host.last_report}\n", buf.next
assert_raises StopIteration do
buf.next
end
end

87f8f03e Shimon Shtein
test "should include registered scope on index" do
# remember the previous state
old_scopes = HostsController.scopes_for(:index).dup

scope_accessed = false
HostsController.add_scope_for(:index) do |base_scope|
scope_accessed = true
base_scope
end
d041d4bb Dominic Cleal
get :index, session: set_session_user
87f8f03e Shimon Shtein
assert_response :success
assert_template 'index'
assert scope_accessed

68388bc2 Michael Moll
# restore the previous state
87f8f03e Shimon Shtein
new_scopes = HostsController.scopes_for(:index)
new_scopes.keep_if { |s| old_scopes.include?(s) }
end

b6e0cb16 Ohad Levy
test "should render 404 when host is not found" do
d041d4bb Dominic Cleal
get :show, params: { :id => "no.such.host" }, session: set_session_user
b6e0cb16 Ohad Levy
assert_response :missing
assert_template 'common/404'
end

9c0e127b Paul Kelly
test "should get new" do
d041d4bb Dominic Cleal
get :new, session: set_session_user
9c0e127b Paul Kelly
assert_response :success
assert_template 'new'
end

test "should create new host" do
5f606e11 Daniel Lobato Garcia
assert_difference 'Host.unscoped.count' do
d041d4bb Dominic Cleal
post :create, params: { :commit => "Create",
9c0e127b Paul Kelly
:host => {:name => "myotherfullhost",
6874bbd9 Paul Kelly
:mac => "aabbecddee06",
90b83222 Ohad Levy
:ip => "2.3.4.125",
feacea35 Amos Benari
:domain_id => domains(:mydomain).id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:environment_id => environments(:production).id,
:subnet_id => subnets(:one).id,
6568415c Joseph Magen
:medium_id => media(:one).id,
4cbf879e Lukas Zapletal
:pxe_loader => "Grub2 UEFI",
77f70152 Stephen Benjamin
:realm_id => realms(:myrealm).id,
316a4ccd Ohad Levy
:disk => "empty partition",
c4bfd47f Stephen Benjamin
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
9d43fc71 Michael Moll
:root_pass => "xybxa6JUkz63w",
160e24ea Joseph Magen
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
6874bbd9 Paul Kelly
}
d041d4bb Dominic Cleal
}, session: set_session_user
9c0e127b Paul Kelly
end
6874bbd9 Paul Kelly
assert_redirected_to host_url(assigns['host'])
04cb74c0 Shimon Shtein
end

test "should create new host with hostgroup inherited fields" do
leftovers = Host.search_for('myotherfullhost').first
refute leftovers
hostgroup = hostgroups(:common)
5f606e11 Daniel Lobato Garcia
assert_difference 'Host.unscoped.count' do
d041d4bb Dominic Cleal
post :create, params: { :commit => "Create",
04cb74c0 Shimon Shtein
:host => {:name => "myotherfullhost",
:mac => "aabbecddee06",
:ip => "2.3.4.125",
:domain_id => domains(:mydomain).id,
:hostgroup_id => hostgroup.id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:subnet_id => subnets(:one).id,
:medium_id => media(:one).id,
:realm_id => realms(:myrealm).id,
:disk => "empty partition",
4cbf879e Lukas Zapletal
:root_pass => "xybxa6JUkz63w",
04cb74c0 Shimon Shtein
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
}
d041d4bb Dominic Cleal
}, session: set_session_user
04cb74c0 Shimon Shtein
end
f54cb6a2 Dominic Cleal
as_admin do
new_host = Host.search_for('myotherfullhost').first
assert new_host.environment.present?
assert_equal hostgroup.environment, new_host.environment
assert new_host.puppet_proxy.present?
assert_equal hostgroup.puppet_proxy, new_host.puppet_proxy
end
04cb74c0 Shimon Shtein
assert_redirected_to host_url(assigns['host'])
9c0e127b Paul Kelly
end

test "should get edit" do
d041d4bb Dominic Cleal
get :edit, params: { :id => @host.name }, session: set_session_user
9c0e127b Paul Kelly
assert_response :success
b09b4515 Ohad Levy
assert_template 'edit'
end
fa9836c1 Lucas Tolchinsky
9c0e127b Paul Kelly
test "should update host" do
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:disk => "ntfs"} }, session: set_session_user
331fcdca Dominic Cleal
@host = Host.find(@host.id)
9c0e127b Paul Kelly
assert_equal @host.disk, "ntfs"
end

b09b4515 Ohad Levy
def test_update_invalid
Host.any_instance.stubs(:valid?).returns(false)
d041d4bb Dominic Cleal
put :update, params: { :id => Host.first.name, :host => {:disk => 'ntfs'} }, session: set_session_user
b09b4515 Ohad Levy
assert_template 'edit'
8f10d0f8 Juan Manuel
end

b09b4515 Ohad Levy
def test_update_valid
Host.any_instance.stubs(:valid?).returns(true)
d041d4bb Dominic Cleal
put :update, params: { :id => Host.first.name, :host => {:name => "Updated_#{Host.first.name}"} }, session: set_session_user
b09b4515 Ohad Levy
assert_redirected_to host_url(assigns(:host))
8f10d0f8 Juan Manuel
end
cd060345 Lucas Tolchinsky
6874bbd9 Paul Kelly
test "should destroy host" do
5f606e11 Daniel Lobato Garcia
assert_difference('Host.unscoped.count', -1) do
d041d4bb Dominic Cleal
delete :destroy, params: { :id => @host.name }, session: set_session_user
6874bbd9 Paul Kelly
end
b09b4515 Ohad Levy
assert_redirected_to hosts_url
end
9c0e127b Paul Kelly
86744a6b Ohad Levy
test "externalNodes should render correctly when format text/html is given" do
0323590f Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name }, session: set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
c7f55be1 Ori Rabin
as_admin { @enc = @host.info.to_yaml}
assert_equal "<pre>#{ERB::Util.html_escape(@enc)}</pre>", response.body
cd060345 Lucas Tolchinsky
end

test "externalNodes should render yml request correctly" do
0323590f Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }, session: set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
c7f55be1 Ori Rabin
as_admin { @enc = @host.info.to_yaml }
assert_equal @enc, response.body
cd060345 Lucas Tolchinsky
end

6874bbd9 Paul Kelly
test "when host is not saved after setBuild, the flash should inform it" do
d7611b24 Greg Sutcliffe
Host.any_instance.stubs(:setBuild).returns(false)
cd060345 Lucas Tolchinsky
@request.env['HTTP_REFERER'] = hosts_path

d041d4bb Dominic Cleal
put :setBuild, params: { :id => @host.name }, session: set_session_user
cd060345 Lucas Tolchinsky
assert_response :found
assert_redirected_to hosts_path
754b1a01 Ohad Levy
assert_not_nil flash[:error]
90b83222 Ohad Levy
assert flash[:error] =~ /Failed to enable #{@host} for installation/
cd060345 Lucas Tolchinsky
end

6e916e52 Shlomi Zadok
context "when host is saved after setBuild" do
setup do
@request.env['HTTP_REFERER'] = hosts_path
end

teardown do
afe02d30 Daniel Lobato
Host::Managed.any_instance.unstub(:setBuild)
6e916e52 Shlomi Zadok
@request.env['HTTP_REFERER'] = ''
end

test "the flash should inform it" do
Host::Managed.any_instance.stubs(:setBuild).returns(true)
d041d4bb Dominic Cleal
put :setBuild, params: { :id => @host.name }, session: set_session_user
6e916e52 Shlomi Zadok
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
assert flash[:success] == "Enabled #{@host} for rebuild on next boot"
6e916e52 Shlomi Zadok
end

test 'and reboot was requested, the flash should inform it' do
Host::Managed.any_instance.stubs(:setBuild).returns(true)
# Setup a power mockup
class PowerShmocker
def reset
true
end
end
Host::Managed.any_instance.stubs(:power).returns(PowerShmocker.new())

d041d4bb Dominic Cleal
put :setBuild, params: { :id => @host.name, :host => {:build => '1'} }, session: set_session_user
6e916e52 Shlomi Zadok
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
assert_equal(flash[:success], "Enabled #{@host} for reboot and rebuild")
6e916e52 Shlomi Zadok
end

test 'and reboot requested and reboot failed, the flash should inform it' do
6695a556 Timo Goebel
Host::Managed.any_instance.stubs(:setBuild).returns(true)
# Setup a power mockup
class PowerShmocker
def reset
false
end
end
Host::Managed.any_instance.stubs(:power).returns(PowerShmocker.new)
d041d4bb Dominic Cleal
put :setBuild, params: { :id => @host.name, :host => {:build => '1'} }, session: set_session_user
6695a556 Timo Goebel
@host.power.reset
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
assert_equal(flash[:success], "Enabled #{@host} for rebuild on next boot, but failed to power cycle the host")
6695a556 Timo Goebel
end

f694d292 Amir Fefer
test 'should render ajax_error when finding a vm has been faild' do
ComputeResource.any_instance.stubs(:find_vm_by_uuid).raises(ActiveRecord::RecordNotFound)
host = FactoryBot.create(:host, :with_hostgroup, :with_environment, :on_compute_resource)
get :vm, params: { :id => host.id }, session: set_session_user
expected_body = "<div class=\"alert alert-danger \"><span class=\"pficon pficon-error-circle-o \"></span> <span class=\"text\"><span>Failure: ActiveRecord::RecordNotFound</span></span></div>\n"
assert_equal response.body, expected_body
assert_response :internal_server_error
end

6695a556 Timo Goebel
test 'and reboot requested and reboot raised exception, the flash should inform it' do
6e916e52 Shlomi Zadok
Host::Managed.any_instance.stubs(:setBuild).returns(true)
d041d4bb Dominic Cleal
put :setBuild, params: { :id => @host.name, :host => {:build => '1'} }, session: set_session_user
6e916e52 Shlomi Zadok
assert_raise Foreman::Exception do
@host.power.reset
end
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
assert_equal(flash[:success], "Enabled #{@host} for rebuild on next boot")
6e916e52 Shlomi Zadok
end
end

0203bf27 Ohad Levy
def test_clone
f5ab56ae Dmitri Dolguikh
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
d041d4bb Dominic Cleal
get :clone, params: { :id => Host.first.name }, session: set_session_user
4638a878 Martin Matuska
assert assigns(:clone_host)
43c4bd72 Marek Hulan
assert_template 'clone'
0203bf27 Ohad Levy
end

4638a878 Martin Matuska
def test_clone_empties_fields
f5ab56ae Dmitri Dolguikh
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
d041d4bb Dominic Cleal
get :clone, params: { :id => Host.first.name }, session: set_session_user
4638a878 Martin Matuska
refute assigns(:host).name
refute assigns(:host).ip
refute assigns(:host).mac
end

5081efd9 Shimon Shtein
def test_clone_with_hostgroup
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :with_hostgroup)
d041d4bb Dominic Cleal
get :clone, params: { :id => host.id }, session: set_session_user
5081efd9 Shimon Shtein
assert assigns(:clone_host)
assert_template 'clone'
assert_response :success
end

935bc248 alongoldboim
def setup_user(operation, type = 'hosts', filter = nil, user = :one)
acfbc458 Marek Hulan
super
end

def setup_user_and_host(operation, filter = nil, &block)
setup_user operation, 'hosts', filter, &block

9fd7478e Paul Kelly
as_admin do
34f84c8d Michael Moll
@host1 = FactoryBot.build(:host)
9fd7478e Paul Kelly
@host1.owner = users(:admin)
@host1.save!
34f84c8d Michael Moll
@host2 = FactoryBot.build(:host)
9fd7478e Paul Kelly
@host2.owner = users(:admin)
@host2.save!
end
96144a47 Daniel Lobato
Host.per_page = 1000
9fd7478e Paul Kelly
@request.session[:user] = @one.id
end

acfbc458 Marek Hulan
test 'user with view host rights and domain is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "domain_id = #{domains(:mydomain).id}"

9fd7478e Paul Kelly
as_admin do
a61ecf63 Dominic Cleal
@host1.primary_interface.update_attribute(:domain, domains(:mydomain))
@host2.primary_interface.update_attribute(:domain, domains(:yourdomain))
dde34153 Paul Kelly
end
d041d4bb Dominic Cleal
get :index, session: set_session_user.merge(:user => @one.id)
43c4bd72 Marek Hulan
9fd7478e Paul Kelly
assert_response :success
0fe147c7 Joseph Mitchell Magen
assert_match /#{@host1.shortname}/, @response.body
43c4bd72 Marek Hulan
refute_match /#{@host2.shortname}/, @response.body
9fd7478e Paul Kelly
end
0203bf27 Ohad Levy
acfbc458 Marek Hulan
test 'user with view host rights and ownership is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "owner_id = #{users(:one).id} and owner_type = User"
9fd7478e Paul Kelly
as_admin do
@host1.owner = @one
@host2.owner = users(:two)
5f606e11 Daniel Lobato Garcia
@host2.organization = users(:two).organizations.first
@host2.location = users(:two).locations.first
9fd7478e Paul Kelly
@host1.save!
@host2.save!
end
d041d4bb Dominic Cleal
get :index, session: set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

acfbc458 Marek Hulan
test 'user with view host rights and hostgroup is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "hostgroup_id = #{hostgroups(:common).id}"
9fd7478e Paul Kelly
as_admin do
@host1.hostgroup = hostgroups(:common)
@host2.hostgroup = hostgroups(:unusual)
@host1.save!
@host2.save!
end
d041d4bb Dominic Cleal
get :index, session: set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

acfbc458 Marek Hulan
test 'user with edit host rights and facts are set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "facts.architecture = \"x86_64\""
9fd7478e Paul Kelly
as_admin do
0f436556 Daniel Lobato
fn_id = FactName.where(:name => "architecture").first_or_create.id
9fd7478e Paul Kelly
FactValue.create! :host => @host1, :fact_name_id => fn_id, :value => "x86_64"
FactValue.create! :host => @host2, :fact_name_id => fn_id, :value => "i386"
end
d041d4bb Dominic Cleal
get :index, session: set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

test 'user with view host rights should fail to edit host' do
acfbc458 Marek Hulan
setup_user_and_host "view"
d041d4bb Dominic Cleal
get :edit, params: { :id => @host1.id }, session: set_session_user.merge(:user => @one.id)
d7bd2f22 Ohad Levy
assert_equal @response.status, 403
9fd7478e Paul Kelly
end

c7f55be1 Ori Rabin
test 'user with view_params rights should see parameters in a host' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :with_parameter)
c7f55be1 Ori Rabin
setup_user "edit"
setup_user "view", "params"
d041d4bb Dominic Cleal
get :edit, params: { :id => host.id }, session: set_session_user.merge(:user => users(:one).id)
1991ee8e June Zhang
assert_not_nil response.body['Global Parameters']
c7f55be1 Ori Rabin
end

test 'user without view_params rights should not see parameters in a host' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :with_parameter)
8dcc90be Marek Hulán
setup_user "edit"
d041d4bb Dominic Cleal
get :edit, params: { :id => host.id }, session: set_session_user.merge(:user => users(:one).id)
1991ee8e June Zhang
assert_nil response.body['Global Parameters']
c7f55be1 Ori Rabin
end

2b0af5e7 Ohad Levy
test 'multiple without hosts' do
d041d4bb Dominic Cleal
post :update_multiple_hostgroup, session: set_session_user
2b0af5e7 Ohad Levy
assert_redirected_to hosts_url
9a280163 Dominic Cleal
assert_equal "No hosts selected", flash[:error]
2b0af5e7 Ohad Levy
# now try to pass an invalid id
d041d4bb Dominic Cleal
post :update_multiple_hostgroup, params: { :host_ids => [-1], :host_names => ["no.such.host"] }, session: set_session_user
2b0af5e7 Ohad Levy
assert_redirected_to hosts_url
13a7bf0d amirfefer
assert_equal "No hosts were found with that id, name or query filter", flash[:error]
2b0af5e7 Ohad Levy
end

test 'multiple hostgroup change by host ids' do
bf228dd6 Paul Kelly
@request.env['HTTP_REFERER'] = hosts_path
2b0af5e7 Ohad Levy
# check that we have hosts and their hostgroup is empty
8c6bc83e Marek Hulan
hosts = FactoryBot.create_list(:host, 2)
2b0af5e7 Ohad Levy
hosts.each { |host| assert_nil host.hostgroup }

hostgroup = hostgroups(:unusual)
d041d4bb Dominic Cleal
post :update_multiple_hostgroup, params: { :host_ids => hosts.map(&:id), :hostgroup => { :id => hostgroup.id } }, session: set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
# reloads hosts
5f606e11 Daniel Lobato Garcia
as_admin do
hosts.each { |host| assert_equal hostgroup, host.reload.hostgroup }
end
2b0af5e7 Ohad Levy
end

test 'multiple hostgroup change by host names' do
bf228dd6 Paul Kelly
@request.env['HTTP_REFERER'] = hosts_path
8c6bc83e Marek Hulan
hosts = FactoryBot.create_list(:host, 2)
e14b5758 Greg Sutcliffe
host_names = hosts.map(&:name)
2b0af5e7 Ohad Levy
# check that we have hosts and their hostgroup is empty
host_names.each do |name|
host = Host.find_by_name name
assert_not_nil host
assert_nil host.hostgroup
end

hostgroup = hostgroups(:common)
630061d2 Michael Moll
post :update_multiple_hostgroup, params: { :host_names => host_names, :hostgroup => { :id => hostgroup.id} }, session: set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
host_names.each do |name|
5f606e11 Daniel Lobato Garcia
as_admin do
host = Host.unscoped.find_by_name(name)
assert_not_nil host
assert_equal host.hostgroup, hostgroup
end
2b0af5e7 Ohad Levy
end
end

9fd7478e Paul Kelly
def setup_multiple_environments
setup_user_and_host "edit"
as_admin do
8c6bc83e Marek Hulan
@host1, @host2 = FactoryBot.create_list(:host, 2, :environment => environments(:production),
8dcc90be Marek Hulán
:organization => users(:one).organizations.first,
:location => users(:one).locations.first)
9fd7478e Paul Kelly
end
end

test "user with edit host rights with update environments should change environments" do
017e1049 Ohad Levy
@request.env['HTTP_REFERER'] = hosts_path
9fd7478e Paul Kelly
setup_multiple_environments
assert @host1.environment == environments(:production)
assert @host2.environment == environments(:production)
d041d4bb Dominic Cleal
post :update_multiple_environment, params: { :host_ids => [@host1.id, @host2.id],
:environment => { :id => environments(:global_puppetmaster).id} },
session: set_session_user.merge(:user => users(:admin).id)
5f606e11 Daniel Lobato Garcia
as_admin do
assert_equal environments(:global_puppetmaster), @host1.reload.environment
assert_equal environments(:global_puppetmaster), @host2.reload.environment
end
cf94f62e Avi Sharvit
assert_equal "Updated hosts: changed environment", flash[:success]
9fd7478e Paul Kelly
end
dde34153 Paul Kelly
dd9afdbf Daniel Lobato
test "should inherit the hostgroup environment if *inherit from hostgroup* selected" do
@request.env['HTTP_REFERER'] = hosts_path
setup_multiple_environments
assert @host1.environment == environments(:production)
assert @host2.environment == environments(:production)

hostgroup = hostgroups(:common)
8dcc90be Marek Hulán
as_admin do
hostgroup.environment = environments(:global_puppetmaster)
hostgroup.save(:validate => false)
dd9afdbf Daniel Lobato
8dcc90be Marek Hulán
@host1.hostgroup = hostgroup
@host1.save(:validate => false)
@host2.hostgroup = hostgroup
@host2.save(:validate => false)
end
dd9afdbf Daniel Lobato
params = { :host_ids => [@host1.id, @host2.id],
:environment => { :id => 'inherit' } }

d041d4bb Dominic Cleal
post :update_multiple_environment, params: params,
session: set_session_user.merge(:user => users(:admin).id)
dd9afdbf Daniel Lobato
5f606e11 Daniel Lobato Garcia
assert_equal hostgroup.environment_id, Host.unscoped.find(@host1.id).environment_id
assert_equal hostgroup.environment_id, Host.unscoped.find(@host2.id).environment_id
dd9afdbf Daniel Lobato
end

a92ee5bc Timo Goebel
test "user with edit host rights with update owner should change owner" do
@request.env['HTTP_REFERER'] = hosts_path
setup_user_and_host "edit"
assert_equal users(:admin).id_and_type, @host1.is_owned_by
assert_equal users(:admin).id_and_type, @host2.is_owned_by
d041d4bb Dominic Cleal
post :update_multiple_owner, params: { :host_ids => [@host1.id, @host2.id],
:owner => { :id => users(:one).id_and_type} },
session: set_session_user.merge(:user => users(:admin).id)
5f606e11 Daniel Lobato Garcia
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
a92ee5bc Timo Goebel
end

8ca6c6bb Timo Goebel
def setup_multiple_compute_resource
setup_user_and_host "edit"
as_admin do
8c6bc83e Marek Hulan
@host1, @host2 = FactoryBot.create_list(:host, 2, :on_compute_resource)
8ca6c6bb Timo Goebel
end
end

test "should change the power of multiple hosts" do
@request.env['HTTP_REFERER'] = hosts_path
setup_multiple_compute_resource

params = { :host_ids => [@host1.id, @host2.id],
:power => { :action => 'poweroff' } }

power_mock = mock("power")
power_mock.expects(:poweroff).twice
Host::Managed.any_instance.stubs(:power).returns(power_mock)

d041d4bb Dominic Cleal
post :update_multiple_power_state, params: params,
session: set_session_user.merge(:user => users(:admin).id)
8ca6c6bb Timo Goebel
end

13a7bf0d amirfefer
test "find multiple hosts by filter query" do
setup_user_and_host "edit"
d041d4bb Dominic Cleal
post :update_multiple_owner, params: { :search => "",
13a7bf0d amirfefer
:owner => { :id => users(:one).id_and_type}},
d041d4bb Dominic Cleal
session: set_session_user.merge(:user => users(:admin).id)
13a7bf0d amirfefer
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
end

test "use filter query which generate a collection" do
setup_user_and_host "edit"
d041d4bb Dominic Cleal
post :update_multiple_owner, params: { :search => "owner = #{users(:admin).login}",
13a7bf0d amirfefer
:owner => { :id => users(:one).id_and_type}},
d041d4bb Dominic Cleal
session: set_session_user.merge(:user => users(:admin).id)
13a7bf0d amirfefer
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
end

test "use a filter query which generates empty collection" do
setup_user_and_host "edit"
d041d4bb Dominic Cleal
post :update_multiple_owner, params: { :search => "owner = #{users(:one).login}",
13a7bf0d amirfefer
:owner => { :id => users(:one).id_and_type}},
d041d4bb Dominic Cleal
session: set_session_user.merge(:user => users(:admin).id)
13a7bf0d amirfefer
as_admin do
assert_equal users(:admin).id_and_type, @host1.reload.is_owned_by
assert_equal users(:admin).id_and_type, @host2.reload.is_owned_by
end
end

test "use empty filter query when it exists in params" do
setup_user_and_host "edit"
d041d4bb Dominic Cleal
post :update_multiple_owner, params: {:host_ids => [@host1.id], :search => "",
13a7bf0d amirfefer
:owner => { :id => users(:one).id_and_type}},
d041d4bb Dominic Cleal
session: set_session_user.merge(:user => users(:admin).id)
13a7bf0d amirfefer
as_admin do
assert_equal users(:one).id_and_type, @host1.reload.is_owned_by
assert_equal users(:one).id_and_type, @host2.reload.is_owned_by
end
end

fbb47ea5 Timo Goebel
describe "setting puppet proxy on multiple hosts" do
before do
setup_user_and_host "edit"
as_admin do
8c6bc83e Marek Hulan
@hosts = FactoryBot.create_list(:host, 2, :with_puppet)
fbb47ea5 Timo Goebel
end
5131edbf Timo Goebel
end

fbb47ea5 Timo Goebel
test "should change the puppet proxy" do
@request.env['HTTP_REFERER'] = hosts_path
5131edbf Timo Goebel
34f84c8d Michael Moll
proxy = as_admin { FactoryBot.build(:puppet_smart_proxy) }
5131edbf Timo Goebel
fbb47ea5 Timo Goebel
params = { :host_ids => @hosts.map(&:id),
:proxy => { :proxy_id => proxy.id } }
5131edbf Timo Goebel
d041d4bb Dominic Cleal
post :update_multiple_puppet_proxy, params: params,
session: set_session_user.merge(:user => users(:admin).id)
fbb47ea5 Timo Goebel
assert_empty flash[:error]
5131edbf Timo Goebel
fbb47ea5 Timo Goebel
@hosts.each do |host|
edd5310f Dominic Cleal
assert_nil host.reload.puppet_ca_proxy
fbb47ea5 Timo Goebel
end
end

test "should clear the puppet proxy of multiple hosts" do
@request.env['HTTP_REFERER'] = hosts_path

params = { :host_ids => @hosts.map(&:id),
:proxy => { :proxy_id => "" } }
5131edbf Timo Goebel
d041d4bb Dominic Cleal
post :update_multiple_puppet_proxy, params: params,
session: set_session_user.merge(:user => users(:admin).id)
fbb47ea5 Timo Goebel
assert_empty flash[:error]

@hosts.each do |host|
edd5310f Dominic Cleal
assert_nil host.reload.puppet_ca_proxy
fbb47ea5 Timo Goebel
end
end
5131edbf Timo Goebel
end

fbb47ea5 Timo Goebel
describe "setting puppet ca proxy on multiple hosts" do
before do
setup_user_and_host "edit"
as_admin do
8c6bc83e Marek Hulan
@hosts = FactoryBot.create_list(:host, 2, :with_puppet_ca)
fbb47ea5 Timo Goebel
end
end
5131edbf Timo Goebel
fbb47ea5 Timo Goebel
test "should change the puppet ca proxy" do
@request.env['HTTP_REFERER'] = hosts_path
5131edbf Timo Goebel
8c6bc83e Marek Hulan
proxy = as_admin { FactoryBot.create(:smart_proxy, :features => [FactoryBot.create(:feature, :puppetca)]) }
fbb47ea5 Timo Goebel
params = { :host_ids => @hosts.map(&:id),
:proxy => { :proxy_id => proxy.id } }

d041d4bb Dominic Cleal
post :update_multiple_puppet_ca_proxy, params: params,
session: set_session_user.merge(:user => users(:admin).id)
fbb47ea5 Timo Goebel
assert_empty flash[:error]
5131edbf Timo Goebel
fbb47ea5 Timo Goebel
@hosts.each do |host|
5f606e11 Daniel Lobato Garcia
as_admin do
assert_equal proxy, host.reload.puppet_ca_proxy
end
fbb47ea5 Timo Goebel
end
end

test "should clear the puppet ca proxy" do
@request.env['HTTP_REFERER'] = hosts_path

params = { :host_ids => @hosts.map(&:id),
:proxy => { :proxy_id => "" } }
5131edbf Timo Goebel
d041d4bb Dominic Cleal
post :update_multiple_puppet_ca_proxy, params: params,
session: set_session_user.merge(:user => users(:admin).id)
fbb47ea5 Timo Goebel
assert_empty flash[:error]

@hosts.each do |host|
8dcc90be Marek Hulán
as_admin do
assert_nil host.reload.puppet_ca_proxy
end
fbb47ea5 Timo Goebel
end
end
5131edbf Timo Goebel
end

9fd7478e Paul Kelly
test "user with edit host rights with update parameters should change parameters" do
setup_multiple_environments
8dcc90be Marek Hulán
param1 = HostParameter.create(:name => "p1", :value => "yo")
param2 = HostParameter.create(:name => "p1", :value => "hi")

as_admin do
@host1.host_parameters = [param1]
@host2.host_parameters = [param2]
end

d041d4bb Dominic Cleal
post :update_multiple_parameters, params: {
:name => { "p1" => "hello"}, :host_ids => [@host1.id, @host2.id] },
session: set_session_user.merge(:user => users(:admin).id)
9fd7478e Paul Kelly
assert Host.find(@host1.id).host_parameters[0][:value] == "hello"
assert Host.find(@host2.id).host_parameters[0][:value] == "hello"
dde34153 Paul Kelly
end
32468bce Tomer Brisker
test "parameter details should be html escaped" do
34f84c8d Michael Moll
hg = FactoryBot.build(:hostgroup, :name => "<script>alert('hacked')</script>")
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :with_puppetclass, :hostgroup => hg)
FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param,
32468bce Tomer Brisker
:override => true, :key_type => 'string',
:default_value => "<script>alert('hacked!');</script>",
:description => "<script>alert('hacked!');</script>",
:puppetclass => host.puppetclasses.first)
8c6bc83e Marek Hulan
FactoryBot.create(:hostgroup_parameter, :hostgroup => hg)
d041d4bb Dominic Cleal
get :edit, params: { :id => host.name }, session: set_session_user
32468bce Tomer Brisker
refute response.body.include?("<script>alert(")
assert response.body.include?("&lt;script&gt;alert(")
assert_equal 3, response.body.scan("&lt;script&gt;alert(").size
end

6e8defed Ohad Levy
test "should get errors" do
d041d4bb Dominic Cleal
get :errors, session: set_session_user
6e8defed Ohad Levy
assert_response :success
assert_template 'index'
end

test "should get active" do
d041d4bb Dominic Cleal
get :active, session: set_session_user
6e8defed Ohad Levy
assert_response :success
d7bd2f22 Ohad Levy
assert_template :partial => "_list"
6e8defed Ohad Levy
assert_template 'index'
end

test "should get out of sync" do
d041d4bb Dominic Cleal
get :out_of_sync, session: set_session_user
6e8defed Ohad Levy
assert_response :success
assert_template 'index'
end

9b41cf08 Ohad Levy
test "should get pending" do
d041d4bb Dominic Cleal
get :pending, session: set_session_user
9b41cf08 Ohad Levy
assert_response :success
assert_template 'index'
end

6e8defed Ohad Levy
test "should get disabled hosts" do
d041d4bb Dominic Cleal
get :disabled, session: set_session_user
6e8defed Ohad Levy
assert_response :success
assert_template 'index'
end

76e5dd41 Joseph Mitchell Magen
test "should get disabled hosts for a user with a fact_filter" do
02acc2ec Ohad Levy
one = users(:one)
one.roles << [roles(:manager)]
9d43fc71 Michael Moll
FactName.create :name => "architecture"
5cbba9ec Tomer Brisker
get :disabled, session: set_session_user(one)
7adf0ee3 Nacho Barrientos
assert_response :success
end

0bb4a75c Dominic Cleal
context 'authorize_login_delegation = true' do
setup do
@host = Host.first
Setting[:authorize_login_delegation] = true
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
User.current = nil # User.current is admin at this point (from initialize_host)
end

test "REMOTE_USER should be ignored for API requests" do
d041d4bb Dominic Cleal
get :show, params: {:id => @host.to_param, :format => 'json'}
0bb4a75c Dominic Cleal
assert_response 401
end

test "REMOTE_USER should be trusted for UI requests" do
d041d4bb Dominic Cleal
get :show, params: {:id => @host.to_param}
0bb4a75c Dominic Cleal
assert_response :success
end
7adf0ee3 Nacho Barrientos
end

0bb4a75c Dominic Cleal
context 'authorize_login_delegation = false' do
setup do
@host = Host.first
Setting[:authorize_login_delegation] = false
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
User.current = nil # User.current is admin at this point (from initialize_host)
end

test "REMOTE_USER should ignored for API requests" do
d041d4bb Dominic Cleal
get :show, params: {:id => @host.to_param, :format => 'json'}
0bb4a75c Dominic Cleal
assert_response 401
end

test "REMOTE_USER should trusted for UI requests" do
d041d4bb Dominic Cleal
get :show, params: {:id => @host.to_param}
0bb4a75c Dominic Cleal
assert_redirected_to "/users/login"
end
7adf0ee3 Nacho Barrientos
end

5f029ed6 Daniel Lobato
def set_remote_user_to(user)
7adf0ee3 Nacho Barrientos
@request.env['REMOTE_USER'] = user.login
end

996b3db5 Daniel Lobato
context 'submit actions with multiple hosts' do
setup do
8c6bc83e Marek Hulan
@host1, @host2 = FactoryBot.create_list(:host, 2, :managed)
996b3db5 Daniel Lobato
end

d6814ff0 Timo Goebel
test 'build without reboot' do
996b3db5 Daniel Lobato
assert !@host1.build
assert !@host2.build
multiple_hosts_submit_request('build', [@host1.id, @host2.id],
d6814ff0 Timo Goebel
'The selected hosts will execute a build operation on next reboot',
{:host => { :build => 0 }})
331fcdca Dominic Cleal
assert Host.find(@host1.id).build
assert Host.find(@host2.id).build
d6814ff0 Timo Goebel
end

test 'build with reboot' do
power_mock = mock('power')
power_mock.expects(:reset).twice.returns(nil)

Host::Managed.any_instance.expects(:supports_power_and_running?).twice.returns(true)
Host::Managed.any_instance.expects(:power).twice.returns(power_mock)
assert !@host1.build
assert !@host2.build
multiple_hosts_submit_request('build', [@host1.id, @host2.id],
'The selected hosts were enabled for reboot and rebuild',
{:host => { :build => 1 }})
331fcdca Dominic Cleal
assert Host.find(@host1.id).build
assert Host.find(@host2.id).build
996b3db5 Daniel Lobato
end

test 'destroy' do
multiple_hosts_submit_request('destroy', [@host1.id, @host2.id], 'Destroyed selected hosts')
assert Host.where(:id => [@host1.id, @host2.id]).empty?
end

test 'disable notifications' do
multiple_hosts_submit_request('disable', [@host1.id, @host2.id], 'Disabled selected hosts')
331fcdca Dominic Cleal
refute Host.find(@host1.id).enabled
refute Host.find(@host2.id).enabled
996b3db5 Daniel Lobato
end

test 'enable notifications' do
multiple_hosts_submit_request('enable', [@host1.id, @host2.id], 'Enabled selected hosts')
331fcdca Dominic Cleal
assert Host.find(@host1.id).enabled
assert Host.find(@host2.id).enabled
996b3db5 Daniel Lobato
end

cf94f62e Avi Sharvit
def multiple_hosts_submit_request(method, ids, success, params = {})
d041d4bb Dominic Cleal
post :"submit_multiple_#{method}", params: params.merge({:host_ids => ids}), session: set_session_user
996b3db5 Daniel Lobato
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_equal success, flash[:success]
996b3db5 Daniel Lobato
end
80e0157c Paul Kelly
end

a67e7179 Paul Kelly
def test_set_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
8dcc90be Marek Hulán
as_admin { assert @host.update_attribute :managed, false }
acfbc458 Marek Hulan
assert_empty @host.errors
d041d4bb Dominic Cleal
put :toggle_manage, params: { :id => @host.name }, session: set_session_user
a67e7179 Paul Kelly
assert_redirected_to :controller => :hosts, :action => :edit
9d43fc71 Michael Moll
assert flash[:success] == _("Foreman now manages the build cycle for %s") % @host.name
a67e7179 Paul Kelly
end

def test_unset_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
8dcc90be Marek Hulán
as_admin { assert @host.update_attribute :managed, true }
acfbc458 Marek Hulan
assert_empty @host.errors
d041d4bb Dominic Cleal
put :toggle_manage, params: { :id => @host.name }, session: set_session_user
a67e7179 Paul Kelly
assert_redirected_to :controller => :hosts, :action => :edit
9d43fc71 Michael Moll
assert flash[:success] == _("Foreman now no longer manages the build cycle for %s") % @host.name
a67e7179 Paul Kelly
end

c3b33536 Stephen Benjamin
test 'when ":restrict_registered_smart_proxies" is false, HTTP requests should be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = false
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_response :success
end

test 'hosts with a registered smart proxy on should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = false
358ec5a3 Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_response :success
end

test 'hosts without a registered smart proxy on should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = false
358ec5a3 Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['another.host'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_equal 403, @response.status
end

test 'hosts with a registered smart proxy and SSL cert should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
0323590f Dominic Cleal
assert_response :success
end

test 'hosts in trusted hosts list and SSL cert should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
a4956e35 Daniel Lobato Garcia
Setting[:trusted_hosts] = ['else.where']
0323590f Dominic Cleal
@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_response :success
end

2821b5e2 red-tux
test 'hosts with comma-separated SSL DN should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
a4956e35 Daniel Lobato Garcia
Setting[:trusted_hosts] = ['foreman.example']
2821b5e2 red-tux
@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = 'CN=foreman.example,OU=PUPPET,O=FOREMAN,ST=North Carolina,C=US'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
2821b5e2 red-tux
assert_response :success
end

test 'hosts with slash-separated SSL DN should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
a4956e35 Daniel Lobato Garcia
Setting[:trusted_hosts] = ['foreman.linux.lab.local']
2821b5e2 red-tux
@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = '/C=US/ST=NC/L=City/O=Example/OU=IT/CN=foreman.linux.lab.local/emailAddress=user@example.com'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
2821b5e2 red-tux
assert_response :success
end

358ec5a3 Dominic Cleal
test 'hosts without a registered smart proxy but with an SSL cert should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=another.host'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_equal 403, @response.status
end

test 'hosts with an unverified SSL cert should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'FAILURE'
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_equal 403, @response.status
end

c3b33536 Stephen Benjamin
test 'when "require_ssl_smart_proxies" and "require_ssl" are true, HTTP requests should not be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = true

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_equal 403, @response.status
end

c3b33536 Stephen Benjamin
test 'when "require_ssl_smart_proxies" is true and "require_ssl" is false, HTTP requests should be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
# since require_ssl_smart_proxies is only applicable to HTTPS connections, both should be set
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }
358ec5a3 Dominic Cleal
assert_response :success
end

test 'authenticated users over HTTP should be able to get externalNodes' do
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['users.host'])
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }, session: set_session_user
358ec5a3 Dominic Cleal
assert_response :success
end

test 'authenticated users over HTTPS should be able to get externalNodes' do
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['users.host'])
@request.env['HTTPS'] = 'on'
d041d4bb Dominic Cleal
get :externalNodes, params: { :name => @host.name, :format => "yml" }, session: set_session_user
358ec5a3 Dominic Cleal
assert_response :success
end

68388bc2 Michael Moll
# Pessimistic - Location
671b45e9 Joseph Mitchell Magen
test "update multiple location fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
d041d4bb Dominic Cleal
post :update_multiple_location, params: {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
assert flash[:error] == "Cannot update Location to Location 1 because of mismatch in settings"
end
test "update multiple location does not update location of hosts if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
assert_difference "location.hosts.count", 0 do
d041d4bb Dominic Cleal
post :update_multiple_location, params: {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple location does not import taxable_taxonomies rows if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
assert_difference "location.taxable_taxonomies.count", 0 do
d041d4bb Dominic Cleal
post :update_multiple_location, params: {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end

68388bc2 Michael Moll
# Optimistic - Location
671b45e9 Joseph Mitchell Magen
test "update multiple location updates location of hosts if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
cnt_hosts_location = location.hosts.count
5f606e11 Daniel Lobato Garcia
assert_difference "location.hosts.count", (Host.unscoped.count - cnt_hosts_location) do
d041d4bb Dominic Cleal
post :update_multiple_location, params: {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
510d53cd Marek Hulan
assert_redirected_to :controller => :hosts, :action => :index
cf94f62e Avi Sharvit
assert_equal "Updated hosts: Changed Location", flash[:success]
671b45e9 Joseph Mitchell Magen
end
test "update multiple location imports taxable_taxonomies rows if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain, :locations => [taxonomies(:location2)])
hosts = FactoryBot.create_list(:host, 2, :domain => domain,
e14b5758 Greg Sutcliffe
:environment => environments(:production),
:location => taxonomies(:location2))
assert_difference "location.taxable_taxonomies.count", 1 do
d041d4bb Dominic Cleal
post :update_multiple_location, params: {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "yes"},
e14b5758 Greg Sutcliffe
:host_ids => hosts.map(&:id)
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end

68388bc2 Michael Moll
# Pessimistic - organization
671b45e9 Joseph Mitchell Magen
test "update multiple organization fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
fc94df42 Dominic Cleal
assert_equal "Cannot update Organization to Organization 1 because of mismatch in settings", flash[:error]
671b45e9 Joseph Mitchell Magen
end
test "update multiple organization does not update organization of hosts if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
assert_difference "organization.hosts.count", 0 do
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple organization does not import taxable_taxonomies rows if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
assert_difference "organization.taxable_taxonomies.count", 0 do
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end

68388bc2 Michael Moll
# Optimistic - Organization
671b45e9 Joseph Mitchell Magen
test "update multiple organization succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
cf94f62e Avi Sharvit
assert_equal "Updated hosts: Changed Organization", flash[:success]
671b45e9 Joseph Mitchell Magen
end
test "update multiple organization updates organization of hosts if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
cnt_hosts_organization = organization.hosts.count
5f606e11 Daniel Lobato Garcia
assert_difference "organization.hosts.count", (Host.unscoped.count - cnt_hosts_organization) do
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple organization imports taxable_taxonomies rows if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain, :organizations => [taxonomies(:organization2)])
hosts = FactoryBot.create_list(:host, 2, :domain => domain,
e14b5758 Greg Sutcliffe
:environment => environments(:production),
:organization => taxonomies(:organization2))
assert_difference "organization.taxable_taxonomies.count", 1 do
d041d4bb Dominic Cleal
post :update_multiple_organization, params: {
96144a47 Daniel Lobato
:organization => { :id => organization.id, :optimistic_import => "yes"},
e14b5758 Greg Sutcliffe
:host_ids => hosts.map(&:id)
d041d4bb Dominic Cleal
}, session: set_session_user
671b45e9 Joseph Mitchell Magen
end
end

796352ed Greg Sutcliffe
test "can change sti type to valid subtype" do
43c4bd72 Marek Hulan
class Host::Valid < Host::Managed; end
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:type => "Host::Valid"} }, session: set_session_user
445dd7ab Daniel Lobato
@host = Host::Base.find(@host.id)
assert_equal "Host::Valid", @host.type
796352ed Greg Sutcliffe
end

test "cannot change sti type to invalid subtype" do
old_type = @host.type
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:type => "Host::Notvalid"} }, session: set_session_user
feacea35 Amos Benari
@host = Host.find(@host.id)
796352ed Greg Sutcliffe
assert_equal old_type, @host.type
end

2348fc8e amirfefer
test "host update without root password in the params does not erase existing password" do
3595a70c Joseph Mitchell Magen
old_root_pass = @host.root_pass
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:name => @host.name} }, session: set_session_user
3595a70c Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal old_root_pass, @host.root_pass
end

2348fc8e amirfefer
test 'blank root password submitted in host does erase existing password' do
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:root_pass => '' } }, session: set_session_user
2348fc8e amirfefer
@host = Host.find(@host.id)
assert @host.root_pass.empty?
end

test "host update without BMC paasword in the params does not erase existing password" do
3595a70c Joseph Mitchell Magen
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
old_password = bmc1.password
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id} } } }, session: set_session_user
3595a70c Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal old_password, @host.interfaces.bmc.first.password
end

2348fc8e amirfefer
test 'blank BMC password submitted in host does erase existing password' do
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => ''} } } }, session: set_session_user
2348fc8e amirfefer
@host = Host.find(@host.id)
assert @host.interfaces.bmc.first.password.empty?
end

ecd9c9c6 Joseph Mitchell Magen
# To test that work-around for Rails bug - https://github.com/rails/rails/issues/11031
test "BMC password updates successful even if attrs serialized field is the only dirty field" do
eaa6aec0 Joseph Mitchell Magen
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
new_password = "topsecret"
d041d4bb Dominic Cleal
put :update, params: { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => new_password, :mac => bmc1.mac} } } }, session: set_session_user
eaa6aec0 Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal new_password, @host.interfaces.bmc.first.password
end

935bc248 alongoldboim
test "test non admin multiple action" do
8dcc90be Marek Hulán
users(:restricted).organizations << taxonomies(:organization1)
users(:restricted).locations << taxonomies(:location1)
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :organization => taxonomies(:organization1), :location => taxonomies(:location1), :owner => users(:restricted))
935bc248 alongoldboim
setup_user 'edit', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
host_ids = [host.id]
68388bc2 Michael Moll
# the ajax can be any of the multiple actions, toke multiple_parameters for example
8decba41 Dominic Cleal
post :multiple_parameters, params: {:host_ids => host_ids}, session: set_session_user(:restricted), xhr: true
935bc248 alongoldboim
assert_response :success
end

f82a666d Amir Fefer
test "select multiple action with valid host_ids param should return a selection page" do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host)
host2 = FactoryBot.create(:host)
f82a666d Amir Fefer
host_ids = [host.id, host2.id]
8decba41 Dominic Cleal
post :multiple_parameters, params: {:host_ids => host_ids}, session: set_session_user, xhr: true
f82a666d Amir Fefer
assert_response :success
bb52a41d Dominic Cleal
assert_includes response.body, host.name
assert_includes response.body, host2.name
f82a666d Amir Fefer
end

test "select multiple action with empty host_ids should redirect to hosts page" do
8decba41 Dominic Cleal
post :multiple_parameters, params: {:host_ids => []}, session: set_session_user, xhr: true
f82a666d Amir Fefer
assert_response :redirect, hosts_path
assert_not_nil flash[:error]
end

test "select multiple action with not exists host_ids should redirect to hosts page" do
3cd8c84b Michael Moll
post :multiple_parameters, params: {:host_ids => [-1, 2]}, session: set_session_user, xhr: true
f82a666d Amir Fefer
assert_response :redirect, hosts_path
assert_not_nil flash[:error]
end

084f7554 Dominic Cleal
test "#disassociate shows error when used on non-CR host" do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host)
084f7554 Dominic Cleal
@request.env["HTTP_REFERER"] = hosts_path
d041d4bb Dominic Cleal
put :disassociate, params: { :id => host.to_param }, session: set_session_user
084f7554 Dominic Cleal
assert_response :redirect, hosts_path
assert_not_nil flash[:error]
end

test "#disassociate removes UUID and CR association from host" do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :on_compute_resource)
084f7554 Dominic Cleal
@request.env["HTTP_REFERER"] = hosts_path
d041d4bb Dominic Cleal
put :disassociate, params: { :id => host.to_param }, session: set_session_user
084f7554 Dominic Cleal
assert_response :redirect, hosts_path
host.reload
refute host.uuid
refute host.compute_resource_id
end

test '#update_multiple_disassociate' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :on_compute_resource)
d041d4bb Dominic Cleal
post :update_multiple_disassociate, params: { :host_ids => [host.id], :host_names => [host.name] }, session: set_session_user
084f7554 Dominic Cleal
assert_response :redirect, hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
084f7554 Dominic Cleal
host.reload
refute host.uuid
refute host.compute_resource_id
end

5a9ad12f Ori Rabin
test '#multiple_disassociate with vm' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :on_compute_resource)
d041d4bb Dominic Cleal
post :multiple_disassociate, params: { :host_ids => [host.id], :host_names => [host.name] }, session: set_session_user
5a9ad12f Ori Rabin
assert_equal 1, assigns(:non_physical_hosts).count
assert_equal 0, assigns(:physical_hosts).count
end

test '#multiple_disassociate with physical host' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host)
d041d4bb Dominic Cleal
post :multiple_disassociate, params: { :host_ids => [host.id], :host_names => [host.name] }, session: set_session_user
5a9ad12f Ori Rabin
assert_equal 0, assigns(:non_physical_hosts).count
assert_equal 1, assigns(:physical_hosts).count
end

6e916e52 Shlomi Zadok
test '#review_before_build' do
HostBuildStatus.any_instance.stubs(:host_status).returns(true)
df1215f9 Lukas Zapletal
HostBuildStatus.any_instance.stubs(:check_all_statuses).returns(true)
8decba41 Dominic Cleal
get :review_before_build, params: {:id => @host.name}, session: set_session_user, xhr: true
6e916e52 Shlomi Zadok
assert_response :success
assert_template 'review_before_build'
end

b3fb472c Daniel Lobato Garcia
describe '#template_used' do
setup do
@host.setBuild
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
@attrs = host_attributes(@host)
end
be4602e8 Greg Sutcliffe
b3fb472c Daniel Lobato Garcia
test 'returns templates with interfaces' do
9d43fc71 Michael Moll
nic = FactoryBot.build(:nic_managed, :host => @host)
b3fb472c Daniel Lobato Garcia
@attrs[:interfaces_attributes] = nic.attributes.except 'updated_at', 'created_at', 'attrs'
8decba41 Dominic Cleal
put :template_used, params: {:provisioning => 'build', :host => @attrs, :id => @host.id }, session: set_session_user, xhr: true
b3fb472c Daniel Lobato Garcia
assert_response :success
assert_template :partial => '_provisioning'
end
c24d6cf8 Dominic Cleal
b3fb472c Daniel Lobato Garcia
test 'returns templates with host parameters' do
@attrs[:host_parameters_attributes] = {'0' => {:name => 'foo', :value => 'bar', :id => '34'}}
8decba41 Dominic Cleal
put :template_used, params: {:provisioning => 'build', :host => @attrs }, session: set_session_user
b3fb472c Daniel Lobato Garcia
assert_response :success
assert_template :partial => '_provisioning'
end

test 'does not save has_many relations on existing hosts' do
@attrs[:config_group_ids] = [config_groups(:one).id]
8decba41 Dominic Cleal
put :template_used, params: {:provisioning => 'build', :host => @attrs, :id => @host.id }, session: set_session_user, xhr: true
b3fb472c Daniel Lobato Garcia
assert_response :success
assert_template :partial => '_provisioning'
end

test 'shows templates for image provisioning' do
image = compute_resources(:one).images.first
@attrs[:compute_resource_id] = compute_resources(:one).id
@attrs[:operatingsystem_id] = image.operatingsystem.id
@attrs[:compute_attributes] ||= {}
@attrs[:compute_attributes][compute_resources(:one).image_param_name] = image.uuid
8decba41 Dominic Cleal
put :template_used, params: {:provisioning => 'image', :host => @attrs }, session: set_session_user, xhr: true
b3fb472c Daniel Lobato Garcia
assert_response :success
assert_template :partial => '_provisioning'
assert_includes response.body, 'MyFinish'
end
d4e53f27 Avi Tal
end

be4602e8 Greg Sutcliffe
test 'process_taxonomy renders a host from the params correctly' do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_managed, :host => @host)
d30a7ff3 Ori Rabin
attrs = host_attributes(@host)
585329f5 Marek Hulan
attrs[:interfaces_attributes] = nic.attributes.except 'updated_at', 'created_at', 'attrs'
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
8decba41 Dominic Cleal
put :process_taxonomy, params: { :host => attrs }, session: set_session_user, xhr: true
be4602e8 Greg Sutcliffe
assert_response :success
56309d5d Tom Caspy
assert response.body.include?(nic.attributes["mac"])
be4602e8 Greg Sutcliffe
assert_template :partial => '_form'
end

38964973 Dominic Cleal
context 'test submit multiple rebuild config' do
def test_submit_multiple_rebuild_config_optimistic
@request.env['HTTP_REFERER'] = hosts_path
Host.any_instance.expects(:recreate_config).returns({"TFTP" => true, "DHCP" => true, "DNS" => true})
8c6bc83e Marek Hulan
h = as_admin {FactoryBot.create(:host)}
8dcc90be Marek Hulán
d041d4bb Dominic Cleal
post :submit_rebuild_config, params: { :host_ids => [h.id] }, session: set_session_user
2fba6ad7 Ondrej Prazak
38964973 Dominic Cleal
assert_response :found
assert_redirected_to hosts_path
cf94f62e Avi Sharvit
assert_not_nil flash[:success]
38964973 Dominic Cleal
end
2fba6ad7 Ondrej Prazak
38964973 Dominic Cleal
def test_submit_multiple_rebuild_config_pessimistic
@request.env['HTTP_REFERER'] = hosts_path
Host.any_instance.expects(:recreate_config).returns({"TFTP" => false, "DHCP" => false, "DNS" => false})
8c6bc83e Marek Hulan
h = as_admin {FactoryBot.create(:host)}
2fba6ad7 Ondrej Prazak
d041d4bb Dominic Cleal
post :submit_rebuild_config, params: { :host_ids => [h.id] }, session: set_session_user
38964973 Dominic Cleal
assert_response :found
assert_redirected_to hosts_path
assert_not_nil flash[:error]
end
2fba6ad7 Ondrej Prazak
end

5e813fae elad
context 'openstack-fog.mock!' do
setup do
Fog.mock!
end

teardown { Fog.unmock! }

test "#schedulerHintFilterSelected applies #scheduler_hint form for raw" do
8decba41 Dominic Cleal
post :scheduler_hint_selected, params: { :host => {:compute_attributes => { :scheduler_hint_filter => "Raw"}, :compute_resource_id => compute_resources(:openstack).id }}, session: set_session_user, xhr: true
5e813fae elad
assert_response :success
assert_template :partial => 'compute_resources_vms/form/openstack/scheduler_filters/_raw'
end
end

05d9c49e Dominic Cleal
context 'Fog.mock!' do
setup do
Fog.mock!
Foreman::Model::Libvirt.any_instance.stubs(:hypervisor).returns(stub(:hypervisor))
Foreman::Model::Libvirt.any_instance.expects(:max_cpu_count).returns(10)
Foreman::Model::Libvirt.any_instance.expects(:max_memory).returns(10000000000)
end
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
teardown { Fog.unmock! }
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
test '#process_hostgroup changes compute attributes' do
8c6bc83e Marek Hulan
group1 = FactoryBot.create(:hostgroup, :compute_profile => compute_profiles(:one))
34f84c8d Michael Moll
host = FactoryBot.build_stubbed(:host, :managed, :on_compute_resource)
68388bc2 Michael Moll
# remove unneeded expectation to :queue_compute
05d9c49e Dominic Cleal
host.unstub(:queue_compute)
host.hostgroup = group1
host.compute_resource = compute_resources(:one)
host.compute_profile = compute_profiles(:one)
host.set_compute_attributes
19d9e229 Shimon Shtein
d5d2d63f Ondřej Pražák
group2 = FactoryBot.create(:hostgroup, :compute_profile => compute_profiles(:two), :compute_resource => compute_resources(:one))
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
attrs = host_attributes(host)
attrs['hostgroup_id'] = group2.id
attrs.delete 'compute_profile_id'
19d9e229 Shimon Shtein
8decba41 Dominic Cleal
put :process_hostgroup, params: { :host => attrs }, session: set_session_user, xhr: true
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_form'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
test '#process_hostgroup does not change compute attributes if compute profile selected manually' do
8c6bc83e Marek Hulan
group1 = FactoryBot.create(:hostgroup, :compute_profile => compute_profiles(:one))
34f84c8d Michael Moll
host = FactoryBot.build_stubbed(:host, :managed, :on_compute_resource)
68388bc2 Michael Moll
# remove unneeded expectation to :queue_compute
05d9c49e Dominic Cleal
host.unstub(:queue_compute)
host.hostgroup = group1
host.compute_resource = compute_resources(:one)
host.compute_profile = compute_profiles(:one)
host.set_compute_attributes
19d9e229 Shimon Shtein
d5d2d63f Ondřej Pražák
group2 = FactoryBot.create(:hostgroup, :compute_profile => compute_profiles(:two), :compute_resource => compute_resources(:one))
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
attrs = host_attributes(host)
attrs['hostgroup_id'] = group2.id
attrs['compute_attributes'] = { 'cpus' => 3 }
attrs.delete 'uuid'
19d9e229 Shimon Shtein
8decba41 Dominic Cleal
put :process_hostgroup, params: { :host => attrs }, session: set_session_user, xhr: true
19d9e229 Shimon Shtein
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_form'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end

test '#compute_resource_selected renders compute tab without compute profile' do
8decba41 Dominic Cleal
get :compute_resource_selected, params: { :host => {:compute_resource_id => compute_resources(:one).id}}, session: set_session_user, xhr: true
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_compute'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end

test '#compute_resource_selected renders compute tab with explicit compute profile' do
8decba41 Dominic Cleal
get :compute_resource_selected, params: { :host => {:compute_resource_id => compute_resources(:one).id, :compute_profile_id => compute_profiles(:two).id}}, session: set_session_user, xhr: true
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_compute'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end

test '#compute_resource_selected renders compute tab with hostgroup\'s compute profile' do
34f84c8d Michael Moll
group = FactoryBot.build(:hostgroup, :compute_profile => compute_profiles(:two))
8decba41 Dominic Cleal
get :compute_resource_selected, params: { :host => {:compute_resource_id => compute_resources(:one).id, :hostgroup_id => group.id}}, session: set_session_user, xhr: true
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_compute'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end

test '#compute_resource_selected renders compute tab with hostgroup parent\'s compute profile' do
8c6bc83e Marek Hulan
parent = FactoryBot.create(:hostgroup, :compute_profile => compute_profiles(:two))
34f84c8d Michael Moll
group = FactoryBot.build(:hostgroup, :parent => parent)
8decba41 Dominic Cleal
get :compute_resource_selected, params: { :host => {:compute_resource_id => compute_resources(:one).id, :hostgroup_id => group.id}}, session: set_session_user, xhr: true
05d9c49e Dominic Cleal
assert_response :success
assert_template :partial => '_compute'
80963357 Marek Hulan
assert_select '#host_compute_attributes_cpus'
05d9c49e Dominic Cleal
end
end

7892585d Sean O\'Keeffe
test '#process_hostgroup works on Host subclasses' do
class Host::Test < Host::Base; end
8c6bc83e Marek Hulan
user = FactoryBot.create(:user, :with_mail, :admin => false)
FactoryBot.create(:filter, :role => roles(:create_hosts), :permissions => Permission.where(:name => [ 'edit_hosts', 'view_hosts' ]))
72a28408 Ondrej Prazak
user.roles << roles(:create_hosts)
user.save!
8c6bc83e Marek Hulan
hostgroup = FactoryBot.create(:hostgroup)
host = FactoryBot.create(:host, :type => "Host::Test", :hostgroup => hostgroup)
7892585d Sean O\'Keeffe
host.stubs(:set_hostgroup_defaults)
host.stubs(:set_compute_attributes)
host.stubs(:architecture)
host.stubs(:operatingsystem)
host.stubs(:environment)
host.stubs(:domain)
host.stubs(:subnet)
host.stubs(:compute_profile)
host.stubs(:realm)
attrs = host_attributes(host)
attrs[:id] = host.id
attrs[:hostgroup_id] = hostgroup.id
8decba41 Dominic Cleal
put :process_hostgroup, params: { :host => attrs }, session: set_session_user(user), xhr: true
7892585d Sean O\'Keeffe
assert_response :success
end

05d9c49e Dominic Cleal
test '#compute_resource_selected returns 404 without compute_resource_id' do
8decba41 Dominic Cleal
get :compute_resource_selected, params: { :host => {} }, session: set_session_user, xhr: true
05d9c49e Dominic Cleal
assert_response :not_found
19d9e229 Shimon Shtein
end

b299f9c9 Dominic Cleal
test '#interfaces applies compute profile and returns interfaces partial' do
modifier = mock('InterfaceMerge')
InterfaceMerge.expects(:new).with().returns(modifier)
Host::Managed.any_instance.expects(:apply_compute_profile).with(modifier)
8decba41 Dominic Cleal
get :interfaces, params: { :host => {:compute_resource_id => compute_resources(:one).id, :compute_profile_id => compute_profiles(:one).id}}, session: set_session_user, xhr: true
b299f9c9 Dominic Cleal
assert_response :success
assert_template :partial => '_interfaces'
end

9b1708f7 Shimon Shtein
test 'failed cancelBuild shows errors' do
@request.env['HTTP_REFERER'] = hosts_path
HostsController.any_instance.stubs(:resource_finder).returns(@host)
@host.errors[:test] << 'my error'
@host.interfaces = [] # force save failure
d041d4bb Dominic Cleal
get :cancelBuild, params: { id: @host.name }, session: set_session_user
9b1708f7 Shimon Shtein
assert_response :redirect
assert_match(/Failed to cancel/, flash[:error])
assert_match(/following errors/, flash[:error])
assert_match(/host must have/, flash[:error])
end

11617115 Ori Rabin
test "should create matcher for host turning into managed" do
8c6bc83e Marek Hulan
original_host = Host::Base.create(:name => 'test', :domain => FactoryBot.create(:domain))
lookup_key = FactoryBot.create(:lookup_key)
11617115 Ori Rabin
host = original_host.becomes(::Host::Managed)
host.type = 'Host::Managed'
host.managed = true
host.primary_interface.managed = true
9d43fc71 Michael Moll
host.lookup_values.build({"match" => "fqdn=#{host.fqdn}", "value" => '4', "lookup_key_id" => lookup_key.id, "host_or_hostgroup" => host})
11617115 Ori Rabin
assert_valid host.lookup_values.first
end

4090ccb5 Daniel Lobato Garcia
describe '#ipmi_boot' do
setup do
@request.env['HTTP_REFERER'] = host_path(@host.id)
setup_user 'ipmi_boot', 'hosts'
end

test 'returns error for non-admin user if BMC is not available' do
d041d4bb Dominic Cleal
put :ipmi_boot, params: { :id => @host.id, :ipmi_device => 'bios' },
session: set_session_user.merge(:user => @one.id)
4090ccb5 Daniel Lobato Garcia
assert_match(/No BMC NIC available for host/, flash[:error])
assert_redirected_to host_path(@host.id)
end

test 'responds correctly for non-admin user if BMC is available' do
Host::Managed.any_instance.expects(:ipmi_boot).with('bios').returns(true)
d041d4bb Dominic Cleal
put :ipmi_boot, params: { :id => @host.id, :ipmi_device => 'bios' },
session: set_session_user.merge(:user => @one.id)
cf94f62e Avi Sharvit
assert_match(/#{@host.name} now boots from BIOS/, flash[:success])
4090ccb5 Daniel Lobato Garcia
assert_redirected_to host_path(@host.id)
end
end

901fe896 Ondrej Prazak
test 'show power status for a host' do
Host.any_instance.stubs(:supports_power?).returns(true)
Host.any_instance.stubs(:supports_power_and_running?).returns(true)
8decba41 Dominic Cleal
get :get_power_state, params: { :id => @host.id }, session: set_session_user, xhr: true
901fe896 Ondrej Prazak
assert_response :success
response = JSON.parse @response.body
assert_equal({"id" => @host.id, "state" => "on", "title" => "On"}, response)
end

test 'show power status for a powered off host' do
Host.any_instance.stubs(:supports_power?).returns(true)
Host.any_instance.stubs(:supports_power_and_running?).returns(false)
8decba41 Dominic Cleal
get :get_power_state, params: { :id => @host.id }, session: set_session_user, xhr: true
901fe896 Ondrej Prazak
assert_response :success
response = JSON.parse @response.body
assert_equal({"id" => @host.id, "state" => "off", "title" => "Off"}, response)
end

test 'show power status for a host that has no power' do
Host.any_instance.stubs(:supports_power?).returns(false)
8decba41 Dominic Cleal
get :get_power_state, params: { :id => @host.id }, session: set_session_user, xhr: true
901fe896 Ondrej Prazak
assert_response :success
response = JSON.parse @response.body
assert_equal({"id" => @host.id, "state" => "na", "title" => 'N/A',
fc053564 Dominic Cleal
"statusText" => "Power operations are not enabled on this host."}, response)
901fe896 Ondrej Prazak
end

test 'show power status for a host that has an exception' do
Host.any_instance.stubs(:supports_power?).returns(true)
Host.any_instance.stubs(:power).raises(::Foreman::Exception.new(N_("Unknown power management support - can't continue")))
8decba41 Dominic Cleal
get :get_power_state, params: { :id => @host.id }, session: set_session_user, xhr: true
901fe896 Ondrej Prazak
assert_response :success
response = JSON.parse @response.body
assert_equal({"id" => @host.id, "state" => "na", "title" => "N/A",
fc053564 Dominic Cleal
"statusText" => "Failed to fetch power status: ERF42-9958 [Foreman::Exception]: Unknown power management support - can't continue"}, response)
901fe896 Ondrej Prazak
end

test 'do not provide power state on an unknown host' do
8decba41 Dominic Cleal
get :get_power_state, params: { :id => 'no-such-host' }, session: set_session_user, xhr: true
901fe896 Ondrej Prazak
assert_response :not_found
end

test 'do not provide power state for non ajax requests' do
d041d4bb Dominic Cleal
get :get_power_state, params: { :id => @host.id }, session: set_session_user
901fe896 Ondrej Prazak
assert_response :method_not_allowed
end

22a5bf03 Daniel Lobato Garcia
describe '#hostgroup_or_environment_selected' do
test 'choosing only one of hostgroup or environment renders classes' do
8decba41 Dominic Cleal
post :hostgroup_or_environment_selected, params: {
22a5bf03 Daniel Lobato Garcia
:host_id => nil,
:host => {
:environment_id => Environment.unscoped.first.id
}
8decba41 Dominic Cleal
}, session: set_session_user, xhr: true
22a5bf03 Daniel Lobato Garcia
assert_response :success
assert_template :partial => 'puppetclasses/_class_selection'
end

test 'choosing both hostgroup and environment renders classes' do
8decba41 Dominic Cleal
post :hostgroup_or_environment_selected, params: {
22a5bf03 Daniel Lobato Garcia
:host_id => @host.id,
:host => {
:environment_id => Environment.unscoped.first.id,
:hostgroup_id => Hostgroup.unscoped.first.id
}
8decba41 Dominic Cleal
}, session: set_session_user, xhr: true
22a5bf03 Daniel Lobato Garcia
assert_response :success
assert_template :partial => 'puppetclasses/_class_selection'
end
265e1d98 orrabin
test 'should not escape lookup values on environment change' do
host = FactoryBot.create(:host, :with_environment, :with_puppetclass)

host.environment.locations = [host.location]
host.environment.organizations = [host.organization]

lookup_key = FactoryBot.create(:puppetclass_lookup_key, :as_smart_class_param, :key_type => 'array',
3cd8c84b Michael Moll
:default_value => ['a', 'b'], :override => true, :puppetclass => host.puppetclasses.first)
265e1d98 orrabin
lookup_value = FactoryBot.create(:lookup_value, :lookup_key => lookup_key, :match => "fqdn=#{host.fqdn}", :value => ["c", "d"])

# sending exactly what the host form would send which is lookup_value.value_before_type_cast
9d43fc71 Michael Moll
lk = {"lookup_values_attributes" => {lookup_key.id.to_s => {"value" => lookup_value.value_before_type_cast, "id" => lookup_value.id, "lookup_key_id" => lookup_key.id, "_destroy" => false}}}
265e1d98 orrabin
params = {
host_id: host.id,
host: host.attributes.merge(lk)
}

# environment change calls puppetclass_parameters which caused the extra escaping
post :puppetclass_parameters, params: params, session: set_session_user, xhr: true

# if this was escaped during refresh_host the value in response.body after unescapeHTML would include "[\\\"c\\\",\\\"d\\\"]"
assert_includes CGI.unescapeHTML(response.body), "[\"c\",\"d\"]"
end
22a5bf03 Daniel Lobato Garcia
end

9ca77d08 Timo Goebel
context '#preview_host_collection' do
test 'should list hosts' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :managed)
8decba41 Dominic Cleal
get :preview_host_collection, params: { :q => '' }, session: set_session_user, xhr: true
9ca77d08 Timo Goebel
assert_response :success
response = JSON.parse(@response.body)
assert_kind_of Array, response
assert_kind_of Hash, response.first
expected = {'id' => host.id, 'name' => host.name}
assert_includes response, expected
end

test 'should find a host by name' do
8c6bc83e Marek Hulan
host1 = FactoryBot.create(:host, :managed, :hostname => 'aaaaaaa')
host2 = FactoryBot.create(:host, :managed, :hostname => 'zzzzzzz')
8decba41 Dominic Cleal
get :preview_host_collection, params: { :q => 'aaaaaaa' }, session: set_session_user, xhr: true
9ca77d08 Timo Goebel
assert_response :success
response = JSON.parse(@response.body)
expected = {'id' => host1.id, 'name' => host1.name}
not_expected = {'id' => host2.id, 'name' => host2.name}
assert_includes response, expected
assert_not_includes response, not_expected
end
end

510dbaae Timo Goebel
context 'with pagelets' do
setup do
@controller.prepend_view_path File.expand_path('../../static_fixtures', __FILE__)
Pagelets::Manager.add_pagelet('hosts/show', :main_tabs,
:name => 'TestTab',
:id => 'my-special-id',
:partial => 'views/test')
end

test '#show renders a pagelet tab' do
d041d4bb Dominic Cleal
get :show, params: {:id => Host.first.name}, session: set_session_user
510dbaae Timo Goebel
assert @response.body.match /id='my-special-id'/
end
end

d91160ea Lucas Tolchinsky
private
abd8f1d1 Daniel Lobato
d91160ea Lucas Tolchinsky
def initialize_host
9fd7478e Paul Kelly
User.current = users(:admin)
a6f4f5f7 Ohad Levy
disable_orchestration
96144a47 Daniel Lobato
@host = Host.create(:name => "myfullhost",
160e24ea Joseph Magen
:mac => "aabbecddeeff",
:ip => "2.3.4.99",
feacea35 Amos Benari
:domain_id => domains(:mydomain).id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:environment_id => environments(:production).id,
:subnet_id => subnets(:one).id,
160e24ea Joseph Magen
:disk => "empty partition",
3595a70c Joseph Mitchell Magen
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
160e24ea Joseph Magen
:root_pass => "123456789",
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
017e1049 Ohad Levy
)
cd060345 Lucas Tolchinsky
end
2f077f63 Ohad Levy
end