Project

General

Profile

Download (31.1 KB) Statistics
| Branch: | Tag: | Revision:
2f077f63 Ohad Levy
require 'test_helper'

class HostsControllerTest < ActionController::TestCase
d91160ea Lucas Tolchinsky
setup :initialize_host

b09b4515 Ohad Levy
def test_show
86744a6b Ohad Levy
get :show, {:id => Host.first.name}, set_session_user
b09b4515 Ohad Levy
assert_template 'show'
2f077f63 Ohad Levy
end
8f10d0f8 Juan Manuel
b09b4515 Ohad Levy
def test_create_invalid
Host.any_instance.stubs(:valid?).returns(false)
9c0e127b Paul Kelly
post :create, {}, set_session_user
b09b4515 Ohad Levy
assert_template 'new'
8f10d0f8 Juan Manuel
end

b09b4515 Ohad Levy
def test_create_valid
Host.any_instance.stubs(:valid?).returns(true)
9c0e127b Paul Kelly
post :create, {:host => {:name => "test"}}, set_session_user
assert_redirected_to host_url(assigns('host'))
8f10d0f8 Juan Manuel
end

9c0e127b Paul Kelly
test "should get index" do
get :index, {}, set_session_user
assert_response :success
assert_template 'index'
end

b6e0cb16 Ohad Levy
test "should render 404 when host is not found" do
4e7b2b98 Paul Kelly
get :show, {:id => "no.such.host"}, set_session_user
b6e0cb16 Ohad Levy
assert_response :missing
assert_template 'common/404'
end

9c0e127b Paul Kelly
test "should get new" do
get :new, {}, set_session_user
assert_response :success
assert_template 'new'
end

test "should create new host" do
assert_difference 'Host.count' do
post :create, { :commit => "Create",
: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,
77f70152 Stephen Benjamin
:realm_id => realms(:myrealm).id,
316a4ccd Ohad Levy
:disk => "empty partition",
c4bfd47f Stephen Benjamin
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
:root_pass => "xybxa6JUkz63w"
6874bbd9 Paul Kelly
}
9c0e127b Paul Kelly
}, set_session_user
end
6874bbd9 Paul Kelly
assert_redirected_to host_url(assigns['host'])
9c0e127b Paul Kelly
end

test "should get edit" do
86744a6b Ohad Levy
get :edit, {:id => @host.name}, 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
86744a6b Ohad Levy
put :update, { :commit => "Update", :id => @host.name, :host => {:disk => "ntfs"} }, set_session_user
@host = Host.find(@host)
9c0e127b Paul Kelly
assert_equal @host.disk, "ntfs"
end

b09b4515 Ohad Levy
def test_update_invalid
Host.any_instance.stubs(:valid?).returns(false)
3595a70c Joseph Mitchell Magen
put :update, {:id => Host.first.name, :host => {}}, 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)
3595a70c Joseph Mitchell Magen
put :update, {:id => Host.first.name, :host => {}}, 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
assert_difference('Host.count', -1) do
86744a6b Ohad Levy
delete :destroy, {:id => @host.name}, 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'])
358ec5a3 Dominic Cleal
get :externalNodes, {:name => @host.name}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
be96f201 Ohad Levy
assert_template :text => @host.info.to_yaml.gsub("\n","<br/>")
cd060345 Lucas Tolchinsky
end

test "externalNodes should render yml request correctly" do
0323590f Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
137f08b9 Ohad Levy
get :externalNodes, {:name => @host.name, :format => "yml"}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
d91160ea Lucas Tolchinsky
assert_template :text => @host.info.to_yaml
cd060345 Lucas Tolchinsky
end

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

86744a6b Ohad Levy
get :setBuild, {:id => @host.name}, set_session_user
05880730 Lucas Tolchinsky
assert_response :found
assert_redirected_to hosts_path
754b1a01 Ohad Levy
assert_not_nil flash[:notice]
90b83222 Ohad Levy
assert flash[:notice] == "Enabled #{@host} for rebuild on next boot"
05880730 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

86744a6b Ohad Levy
get :setBuild, {:id => @host.name}, 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

0203bf27 Ohad Levy
def test_clone
86744a6b Ohad Levy
get :clone, {:id => Host.first.name}, set_session_user
4638a878 Martin Matuska
assert assigns(:clone_host)
0203bf27 Ohad Levy
assert_template 'new'
end

4638a878 Martin Matuska
def test_clone_empties_fields
get :clone, {:id => Host.first.name}, set_session_user
refute assigns(:host).name
refute assigns(:host).ip
refute assigns(:host).mac
end

acfbc458 Marek Hulan
def setup_user operation, type = 'hosts', filter = nil
super
end

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

9fd7478e Paul Kelly
as_admin do
@host1 = hosts(:one)
@host1.owner = users(:admin)
@host1.save!
@host2 = hosts(:two)
@host2.owner = users(:admin)
@host2.save!
end
Host.per_page == 1000
@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
6f0fc8f1 Ohad Levy
@host1.update_attribute(:domain, domains(:mydomain))
@host2.update_attribute(:domain, domains(:yourdomain))
dde34153 Paul Kelly
end
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
0fe147c7 Joseph Mitchell Magen
assert_match /#{@host1.shortname}/, @response.body
acfbc458 Marek Hulan
refute_match /#{@host2.name}/, @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)
@host1.save!
@host2.save!
end
d7bd2f22 Ohad Levy
get :index, {}, 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
d7bd2f22 Ohad Levy
get :index, {}, 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
5d264a2d Ohad Levy
fn_id = FactName.find_or_create_by_name("architecture").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
d7bd2f22 Ohad Levy
get :index, {}, 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"
d7bd2f22 Ohad Levy
get :edit, {:id => @host1.id}, set_session_user.merge(:user => @one.id)
assert_equal @response.status, 403
9fd7478e Paul Kelly
end

2b0af5e7 Ohad Levy
test 'multiple without hosts' do
2d9308eb Paul Kelly
post :update_multiple_hostgroup, {}, 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
2d9308eb Paul Kelly
post :update_multiple_hostgroup, {:host_ids => [-1], :host_names => ["no.such.host"]}, set_session_user
2b0af5e7 Ohad Levy
assert_redirected_to hosts_url
assert_equal "No hosts were found with that id or name", flash[:error]
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
hosts = [hosts(:one), hosts(:two)]
hosts.each { |host| assert_nil host.hostgroup }

hostgroup = hostgroups(:unusual)
2d9308eb Paul Kelly
post :update_multiple_hostgroup, { :host_ids => hosts.map(&:id), :hostgroup => { :id => hostgroup.id } }, set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
# reloads hosts
hosts.map! {|h| Host.find(h.id)}
hosts.each { |host| assert_equal hostgroup, host.hostgroup }
end


test 'multiple hostgroup change by host names' do
bf228dd6 Paul Kelly
@request.env['HTTP_REFERER'] = hosts_path
316a4ccd Ohad Levy
host_names = %w{temp.yourdomain.net my5name.mydomain.net }
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)
2d9308eb Paul Kelly
post :update_multiple_hostgroup, { :host_names => host_names, :hostgroup => { :id => hostgroup.id} }, set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
host_names.each do |name|
host = Host.find_by_name name
assert_not_nil host
assert_equal host.hostgroup, hostgroup
end
end

9fd7478e Paul Kelly
def setup_multiple_environments
setup_user_and_host "edit"
as_admin do
@host1 = hosts(:otherfullhost)
@host2 = hosts(:anotherfullhost)
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)
1a51088d Ohad Levy
post :update_multiple_environment, { :host_ids => [@host1.id, @host2.id],
:environment => { :id => environments(:global_puppetmaster).id}},
d7bd2f22 Ohad Levy
set_session_user.merge(:user => User.first.id)
9fd7478e Paul Kelly
assert Host.find(@host1.id).environment == environments(:global_puppetmaster)
assert Host.find(@host2.id).environment == environments(:global_puppetmaster)
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)
hostgroup.environment = environments(:global_puppetmaster)
hostgroup.save(:validate => false)

@host1.hostgroup = hostgroup
@host1.save(:validate => false)
@host2.hostgroup = hostgroup
@host2.save(:validate => false)

params = { :host_ids => [@host1.id, @host2.id],
:environment => { :id => 'inherit' } }

post :update_multiple_environment, params,
set_session_user.merge(:user => User.first.id)

assert Host.find(@host1.id).environment == hostgroup.environment
assert Host.find(@host2.id).environment == hostgroup.environment
end

9fd7478e Paul Kelly
test "user with edit host rights with update parameters should change parameters" do
setup_multiple_environments
@host1.host_parameters = [HostParameter.create(:name => "p1", :value => "yo")]
@host2.host_parameters = [HostParameter.create(:name => "p1", :value => "hi")]
post :update_multiple_parameters,
1a51088d Ohad Levy
{:name => { "p1" => "hello"},:host_ids => [@host1.id, @host2.id]},
d7bd2f22 Ohad Levy
set_session_user.merge(:user => User.first.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
6e8defed Ohad Levy
test "should get errors" do
get :errors, {}, set_session_user
assert_response :success
assert_template 'index'
end

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

test "should get out of sync" do
get :out_of_sync, {}, set_session_user
assert_response :success
assert_template 'index'
end

9b41cf08 Ohad Levy
test "should get pending" do
get :pending, {}, set_session_user
assert_response :success
assert_template 'index'
end

6e8defed Ohad Levy
test "should get disabled hosts" do
get :disabled, {}, set_session_user
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)]
5d264a2d Ohad Levy
fn = FactName.create :name =>"architecture"
02acc2ec Ohad Levy
ufact = UserFact.create :user => one, :fact_name => fn, :criteria => "="
assert !(ufact.new_record?)
76e5dd41 Joseph Mitchell Magen
get :disabled, {:user => one.id}, set_session_user
7adf0ee3 Nacho Barrientos
assert_response :success
end

test "if only authorize_login_delegation is set, REMOTE_USER should be
ignored for API requests" do
4e7ea9b8 Marek Hulan
Setting[:signo_sso] = false
7adf0ee3 Nacho Barrientos
Setting[:authorize_login_delegation] = true
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
852ff714 Daniel Lobato
User.current = nil # User.current is admin at this point (from initialize_host)
7adf0ee3 Nacho Barrientos
host = Host.first
get :show, {:id => host.to_param, :format => 'json'}
assert_response 401
get :show, {:id => host.to_param}
assert_response :success
end

test "if both authorize_login_delegation{,_api} are unset,
REMOTE_USER should ignored in all cases" do
4e7ea9b8 Marek Hulan
Setting[:signo_sso] = false
7adf0ee3 Nacho Barrientos
Setting[:authorize_login_delegation] = false
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
852ff714 Daniel Lobato
User.current = nil # User.current is admin at this point (from initialize_host)
7adf0ee3 Nacho Barrientos
host = Host.first
get :show, {:id => host.to_param, :format => 'json'}
assert_response 401
get :show, {:id => host.to_param}
assert_redirected_to "/users/login"
end

def set_remote_user_to user
@request.env['REMOTE_USER'] = user.login
end

80e0157c Paul Kelly
def test_submit_multiple_build
assert !hosts(:one).build
assert !hosts(:two).build
1a51088d Ohad Levy
post :submit_multiple_build, {:host_ids => [hosts(:one).id, hosts(:two).id]}, set_session_user
6285a614 Ohad Levy
assert_response :found
80e0157c Paul Kelly
assert_redirected_to hosts_path
assert flash[:notice] == "The selected hosts will execute a build operation on next reboot"
assert Host.find(hosts(:one)).build
assert Host.find(hosts(:two)).build
end

a67e7179 Paul Kelly
def test_set_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
assert @host.update_attribute :managed, false
acfbc458 Marek Hulan
assert_empty @host.errors
a67e7179 Paul Kelly
put :toggle_manage, {:id => @host.name}, set_session_user
assert_redirected_to :controller => :hosts, :action => :edit
assert flash[:notice] == "Foreman now manages the build cycle for #{@host.name}"
end

def test_unset_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
assert @host.update_attribute :managed, true
acfbc458 Marek Hulan
assert_empty @host.errors
a67e7179 Paul Kelly
put :toggle_manage, {:id => @host.name}, set_session_user
assert_redirected_to :controller => :hosts, :action => :edit
assert flash[:notice] == "Foreman now no longer manages the build cycle for #{@host.name}"
end

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

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

test 'hosts with a registered smart proxy on should get externalNodes successfully' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = false

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

test 'hosts without a registered smart proxy on should not be able to get externalNodes' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = false

Resolv.any_instance.stubs(:getnames).returns(['another.host'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

test 'hosts with a registered smart proxy and SSL cert should get externalNodes successfully' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true

@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'])
get :externalNodes, {: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
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
Setting[:trusted_puppetmaster_hosts] = ['else.where']

@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'])
get :externalNodes, {:name => @host.name, :format => "yml"}
358ec5a3 Dominic Cleal
assert_response :success
end

test 'hosts without a registered smart proxy but with an SSL cert should not be able to get externalNodes' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true

@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'
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

test 'hosts with an unverified SSL cert should not be able to get externalNodes' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true

@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'
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

test 'when "require_ssl_puppetmasters" and "require_ssl" are true, HTTP requests should not be able to get externalNodes' do
User.current = nil
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
SETTINGS[:require_ssl] = true

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

test 'when "require_ssl_puppetmasters" is true and "require_ssl" is false, HTTP requests should be able to get externalNodes' do
User.current = nil
# since require_ssl_puppetmasters is only applicable to HTTPS connections, both should be set
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
SETTINGS[:require_ssl] = false

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

test 'authenticated users over HTTP should be able to get externalNodes' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
SETTINGS[:require_ssl] = false

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

test 'authenticated users over HTTPS should be able to get externalNodes' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
SETTINGS[:require_ssl] = false

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

671b45e9 Joseph Mitchell Magen
#Pessimistic - Location
test "update multiple location fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
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
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
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
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
end
end

#Optimistic - Location
test "update multiple location succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
assert_redirected_to :controller => :hosts, :action => :index
fc94df42 Dominic Cleal
assert_equal "Updated hosts: Changed Location", flash[:notice]
671b45e9 Joseph Mitchell Magen
end
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
assert_difference "location.hosts.count", (Host.count - cnt_hosts_location) do
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
end
end
test "update multiple location imports taxable_taxonomies rows if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
111cde57 Joseph Magen
assert_difference "location.taxable_taxonomies.count", 8 do
671b45e9 Joseph Mitchell Magen
post :update_multiple_location, {
:location => {:id => location.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
end
end

#Pessimistic - organization
test "update multiple organization fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
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
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
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
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "no"},
:host_ids => Host.all.map(&:id)
}, set_session_user
end
end

#Optimistic - Organization
test "update multiple organization succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
assert_redirected_to :controller => :hosts, :action => :index
fc94df42 Dominic Cleal
assert_equal "Updated hosts: Changed Organization", flash[:notice]
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
assert_difference "organization.hosts.count", (Host.count - cnt_hosts_organization) do
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
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)
111cde57 Joseph Magen
assert_difference "organization.taxable_taxonomies.count", 10 do
671b45e9 Joseph Mitchell Magen
post :update_multiple_organization, {
:organization => {:id => organization.id, :optimistic_import => "yes"},
:host_ids => Host.all.map(&:id)
}, set_session_user
end
end

796352ed Greg Sutcliffe
test "can change sti type to valid subtype" do
class Host::Valid < Host::Base ; end
put :update, { :commit => "Update", :id => @host.name, :host => {:type => "Host::Valid"} }, set_session_user
02c054b2 Greg Sutcliffe
@host = Host::Base.find(@host.id)
796352ed Greg Sutcliffe
assert_equal "Host::Valid", @host.type
end

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

3595a70c Joseph Mitchell Magen
test "blank root password submitted does not erase existing password" do
old_root_pass = @host.root_pass
put :update, { :commit => "Update", :id => @host.name, :host => {:root_pass => ''} }, set_session_user
@host = Host.find(@host.id)
assert_equal old_root_pass, @host.root_pass
end

test "blank BMC password submitted does not 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
old_password = bmc1.password
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => ''} } } }, set_session_user
@host = Host.find(@host.id)
assert_equal old_password, @host.interfaces.bmc.first.password
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"
ecd9c9c6 Joseph Mitchell Magen
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => new_password, :mac => bmc1.mac} } } }, set_session_user
eaa6aec0 Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal new_password, @host.interfaces.bmc.first.password
end

86e9a056 Andy Bohne
test "index returns YAML output for rundeck" do
get :index, {:format => 'yaml', :rundeck => true}, set_session_user
hosts = YAML.load(@response.body)
acfbc458 Marek Hulan
assert_not_empty hosts
86e9a056 Andy Bohne
host = Host.first
assert_equal host.os.name, hosts[host.name]["osName"] # rundeck-specific field
end

test "show returns YAML output for rundeck" do
host = Host.first
get :show, {:id => host.to_param, :format => 'yaml', :rundeck => true}, set_session_user
yaml = YAML.load(@response.body)
assert_kind_of Hash, yaml[host.name]
assert_equal host.name, yaml[host.name]["hostname"]
assert_equal host.os.name, yaml[host.name]["osName"] # rundeck-specific field
end

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

test "#disassociate removes UUID and CR association from host" do
host = FactoryGirl.create(:host, :on_compute_resource)
@request.env["HTTP_REFERER"] = hosts_path
put :disassociate, {:id => host.to_param}, set_session_user
assert_response :redirect, hosts_path
host.reload
refute host.uuid
refute host.compute_resource_id
end

test '#update_multiple_disassociate' do
host = FactoryGirl.create(:host, :on_compute_resource)
post :update_multiple_disassociate, {:host_ids => [host.id], :host_names => [host.name]}, set_session_user
assert_response :redirect, hosts_path
assert_not_nil flash[:notice]
host.reload
refute host.uuid
refute host.compute_resource_id
end

d91160ea Lucas Tolchinsky
private
def initialize_host
9fd7478e Paul Kelly
User.current = users(:admin)
a6f4f5f7 Ohad Levy
disable_orchestration
017e1049 Ohad Levy
@host = Host.create(:name => "myfullhost",
: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,
017e1049 Ohad Levy
:disk => "empty partition",
3595a70c Joseph Mitchell Magen
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
:root_pass => "123456789"
017e1049 Ohad Levy
)
cd060345 Lucas Tolchinsky
end
2f077f63 Ohad Levy
end