foreman/test/functional/api/v2/hosts_controller_test.rb @ 4269abbd
0fda4cf6 | Joseph Mitchell Magen | require 'test_helper'
|
|
class Api::V2::HostsControllerTest < ActionController::TestCase
|
|||
e14b5758 | Greg Sutcliffe | def setup
|
|
@host = FactoryGirl.create(:host)
|
|||
510d53cd | Marek Hulan | @ptable = FactoryGirl.create(:ptable)
|
|
@ptable.operatingsystems = [ Operatingsystem.find_by_name('Redhat') ]
|
|||
e14b5758 | Greg Sutcliffe | end
|
|
cfd1c413 | Tomas Strachota | def basic_attrs
|
|
dc38aad3 | Joseph Magen | { :name => 'testhost11',
|
|
:environment_id => environments(:production).id,
|
|||
:domain_id => domains(:mydomain).id,
|
|||
510d53cd | Marek Hulan | :ptable_id => @ptable.id,
|
|
6568415c | Joseph Magen | :medium_id => media(:one).id,
|
|
dc38aad3 | Joseph Magen | :architecture_id => Architecture.find_by_name('x86_64').id,
|
|
:operatingsystem_id => Operatingsystem.find_by_name('Redhat').id,
|
|||
:puppet_proxy_id => smart_proxies(:one).id,
|
|||
c4bfd47f | Stephen Benjamin | :compute_resource_id => compute_resources(:one).id,
|
|
160e24ea | Joseph Magen | :root_pass => "xybxa6JUkz63w",
|
|
:location_id => taxonomies(:location1).id,
|
|||
:organization_id => taxonomies(:organization1).id
|
|||
dc38aad3 | Joseph Magen | }
|
|
end
|
|||
cfd1c413 | Tomas Strachota | def valid_attrs
|
|
net_attrs = {
|
|||
:ip => '10.0.0.20',
|
|||
:mac => '52:53:00:1e:85:93'
|
|||
}
|
|||
basic_attrs.merge(net_attrs)
|
|||
end
|
|||
def basic_attrs_with_profile(compute_attrs)
|
|||
basic_attrs.merge(
|
|||
:compute_resource_id => compute_attrs.compute_resource_id,
|
|||
:compute_profile_id => compute_attrs.compute_profile_id
|
|||
)
|
|||
end
|
|||
def nics_attrs
|
|||
[{
|
|||
:primary => true,
|
|||
:ip => '10.0.0.20',
|
|||
:mac => '00:11:22:33:44:00'
|
|||
},{
|
|||
:type => 'bmc',
|
|||
:provider => 'IPMI',
|
|||
:mac => '00:11:22:33:44:01'
|
|||
},{
|
|||
:mac => '00:11:22:33:44:02',
|
|||
:_destroy => 1
|
|||
}]
|
|||
end
|
|||
def expected_compute_attributes(compute_attrs, index)
|
|||
compute_attrs.vm_interfaces[index].update("from_profile" => compute_attrs.compute_profile.name)
|
|||
end
|
|||
def last_host
|
|||
Host.order('id asc').last
|
|||
end
|
|||
dc38aad3 | Joseph Magen | test "should get index" do
|
|
get :index, { }
|
|||
assert_response :success
|
|||
assert_not_nil assigns(:hosts)
|
|||
hosts = ActiveSupport::JSON.decode(@response.body)
|
|||
assert !hosts.empty?
|
|||
end
|
|||
test "should show individual record" do
|
|||
e14b5758 | Greg Sutcliffe | get :show, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
show_response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert !show_response.empty?
|
|||
end
|
|||
test "should create host" do
|
|||
disable_orchestration
|
|||
assert_difference('Host.count') do
|
|||
post :create, { :host => valid_attrs }
|
|||
end
|
|||
d575926a | alongoldboim | assert_response :created
|
|
dc38aad3 | Joseph Magen | end
|
|
cfd1c413 | Tomas Strachota | test "should create interfaces" do
|
|
disable_orchestration
|
|||
post :create, { :host => basic_attrs.merge!(:interfaces_attributes => nics_attrs) }
|
|||
d575926a | alongoldboim | assert_response :created
|
|
cfd1c413 | Tomas Strachota | assert_equal 2, last_host.interfaces.count
|
|
assert last_host.interfaces.find_by_mac('00:11:22:33:44:00').primary?
|
|||
assert_equal Nic::Managed, last_host.interfaces.find_by_mac('00:11:22:33:44:00').class
|
|||
assert_equal Nic::BMC, last_host.interfaces.find_by_mac('00:11:22:33:44:01').class
|
|||
end
|
|||
test "should create interfaces sent in a hash" do
|
|||
disable_orchestration
|
|||
hash_nics_attrs = nics_attrs.inject({}) do |hash, item|
|
|||
hash.update(item.to_s => item)
|
|||
end
|
|||
post :create, { :host => basic_attrs.merge!(:interfaces_attributes => hash_nics_attrs) }
|
|||
d575926a | alongoldboim | assert_response :created
|
|
cfd1c413 | Tomas Strachota | assert_equal 2, last_host.interfaces.count
|
|
assert last_host.interfaces.find_by_mac('00:11:22:33:44:00').primary?
|
|||
assert_equal Nic::Managed, last_host.interfaces.find_by_mac('00:11:22:33:44:00').class
|
|||
assert_equal Nic::BMC, last_host.interfaces.find_by_mac('00:11:22:33:44:01').class
|
|||
end
|
|||
test "should fail with unknown interface type" do
|
|||
disable_orchestration
|
|||
attrs = basic_attrs.merge!(:interfaces_attributes => nics_attrs)
|
|||
attrs[:interfaces_attributes][0][:type] = "unknown"
|
|||
post :create, { :host => attrs }
|
|||
assert_response :unprocessable_entity
|
|||
assert_match /Unknown interface type/, JSON.parse(response.body)['error']['message']
|
|||
end
|
|||
test "should create interfaces from compute profile" do
|
|||
disable_orchestration
|
|||
compute_attrs = compute_attributes(:with_interfaces)
|
|||
post :create, { :host => basic_attrs_with_profile(compute_attrs).merge(:interfaces_attributes => nics_attrs) }
|
|||
d575926a | alongoldboim | assert_response :created
|
|
cfd1c413 | Tomas Strachota | ||
assert_equal compute_attrs.vm_interfaces.count, last_host.interfaces.count
|
|||
assert_equal expected_compute_attributes(compute_attrs, 0), last_host.interfaces.find_by_mac('00:11:22:33:44:00').compute_attributes
|
|||
assert_equal expected_compute_attributes(compute_attrs, 1), last_host.interfaces.find_by_mac('00:11:22:33:44:01').compute_attributes
|
|||
end
|
|||
dc38aad3 | Joseph Magen | test "should create host with managed is false if parameter is passed" do
|
|
disable_orchestration
|
|||
post :create, { :host => valid_attrs.merge!(:managed => false) }
|
|||
d575926a | alongoldboim | assert_response :created
|
|
dc38aad3 | Joseph Magen | assert_equal false, last_host.managed?
|
|
end
|
|||
test "should update host" do
|
|||
445dd7ab | Daniel Lobato | put :update, { :id => @host.to_param, :host => valid_attrs }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
end
|
|||
cfd1c413 | Tomas Strachota | test "should update interfaces from compute profile" do
|
|
disable_orchestration
|
|||
compute_attrs = compute_attributes(:with_interfaces)
|
|||
put :update, { :id => @host.to_param, :host => basic_attrs_with_profile(compute_attrs) }
|
|||
assert_response :success
|
|||
@host.interfaces.reload
|
|||
assert_equal compute_attrs.vm_interfaces.count, @host.interfaces.count
|
|||
assert_equal expected_compute_attributes(compute_attrs, 0), @host.interfaces.find_by_primary(true).compute_attributes
|
|||
assert_equal expected_compute_attributes(compute_attrs, 1), @host.interfaces.find_by_primary(false).compute_attributes
|
|||
end
|
|||
a2610e98 | Joseph Magen | test "should update host without :host root node and rails wraps it correctly" do
|
|
e14b5758 | Greg Sutcliffe | put :update, { :id => @host.to_param, :name => 'newhostname' }
|
|
a2610e98 | Joseph Magen | request_parameters = @request.env['action_dispatch.request.request_parameters']
|
|
assert request_parameters[:host]
|
|||
assert_equal 'newhostname', request_parameters[:host][:name]
|
|||
assert_response :success
|
|||
end
|
|||
dc38aad3 | Joseph Magen | test "should destroy hosts" do
|
|
assert_difference('Host.count', -1) do
|
|||
e14b5758 | Greg Sutcliffe | delete :destroy, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | end
|
|
assert_response :success
|
|||
end
|
|||
test "should show status hosts" do
|
|||
e14b5758 | Greg Sutcliffe | get :status, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
end
|
|||
test "should be able to create hosts even when restricted" do
|
|||
disable_orchestration
|
|||
assert_difference('Host.count') do
|
|||
post :create, { :host => valid_attrs }
|
|||
end
|
|||
assert_response :success
|
|||
end
|
|||
test "should allow access to restricted user who owns the host" do
|
|||
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host, :owner => users(:restricted))
|
|
acfbc458 | Marek Hulan | setup_user 'view', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | get :show, { :id => host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
end
|
|||
test "should allow to update for restricted user who owns the host" do
|
|||
disable_orchestration
|
|||
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host, :owner => users(:restricted))
|
|
acfbc458 | Marek Hulan | setup_user 'edit', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
445dd7ab | Daniel Lobato | put :update, { :id => host.to_param, :host => valid_attrs }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
end
|
|||
test "should allow destroy for restricted user who owns the hosts" do
|
|||
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host, :owner => users(:restricted))
|
|
dc38aad3 | Joseph Magen | assert_difference('Host.count', -1) do
|
|
acfbc458 | Marek Hulan | setup_user 'destroy', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | delete :destroy, { :id => host.to_param }
|
|
dc38aad3 | Joseph Magen | end
|
|
assert_response :success
|
|||
end
|
|||
test "should allow show status for restricted user who owns the hosts" do
|
|||
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host, :owner => users(:restricted))
|
|
acfbc458 | Marek Hulan | setup_user 'view', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | get :status, { :id => host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :success
|
|
end
|
|||
test "should not allow access to a host out of users hosts scope" do
|
|||
acfbc458 | Marek Hulan | setup_user 'view', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | get :show, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :not_found
|
|
end
|
|||
test "should not list a host out of users hosts scope" do
|
|||
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host, :owner => users(:restricted))
|
|
acfbc458 | Marek Hulan | setup_user 'view', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
get :index, {}
|
|||
dc38aad3 | Joseph Magen | assert_response :success
|
|
hosts = ActiveSupport::JSON.decode(@response.body)
|
|||
cc739ab4 | Joseph Mitchell Magen | ids = hosts['results'].map { |hash| hash['id'] }
|
|
e14b5758 | Greg Sutcliffe | refute_includes ids, @host.id
|
|
assert_includes ids, host.id
|
|||
dc38aad3 | Joseph Magen | end
|
|
test "should not update host out of users hosts scope" do
|
|||
acfbc458 | Marek Hulan | setup_user 'edit', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | put :update, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :not_found
|
|
end
|
|||
test "should not delete hosts out of users hosts scope" do
|
|||
acfbc458 | Marek Hulan | setup_user 'destroy', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | delete :destroy, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :not_found
|
|
end
|
|||
test "should not show status of hosts out of users hosts scope" do
|
|||
acfbc458 | Marek Hulan | setup_user 'view', 'hosts', "owner_type = User and owner_id = #{users(:restricted).id}", :restricted
|
|
e14b5758 | Greg Sutcliffe | get :status, { :id => @host.to_param }
|
|
dc38aad3 | Joseph Magen | assert_response :not_found
|
|
end
|
|||
7e8bfe82 | Walter Huf | test "should show hosts vm attributes" do
|
|
host = FactoryGirl.create(:host, :compute_resource => compute_resources(:one))
|
|||
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns( :cpus => 4 )
|
|||
get :vm_compute_attributes, { :id => host.to_param }
|
|||
assert_response :success
|
|||
data = JSON.parse(@response.body)
|
|||
4269abbd | Tomas Strachota | assert_equal data, "cpus" => 4, "memory" => nil
|
|
7e8bfe82 | Walter Huf | ComputeResource.any_instance.unstub(:vm_compute_attributes_for)
|
|
end
|
|||
5f029ed6 | Daniel Lobato | def set_remote_user_to(user)
|
|
dc38aad3 | Joseph Magen | @request.env['REMOTE_USER'] = user.login
|
|
end
|
|||
test "when REMOTE_USER is provided and both authorize_login_delegation{,_api}
|
|||
are set, authentication should succeed w/o valid session cookies" do
|
|||
Setting[:authorize_login_delegation] = true
|
|||
Setting[:authorize_login_delegation_api] = true
|
|||
set_remote_user_to users(:admin)
|
|||
User.current = nil # User.current is admin at this point (from initialize_host)
|
|||
host = Host.first
|
|||
get :show, {:id => host.to_param, :format => 'json'}
|
|||
assert_response :success
|
|||
get :show, {:id => host.to_param}
|
|||
assert_response :success
|
|||
end
|
|||
3ccd0ef6 | Jason Montleon | test "should disassociate host" do
|
|
host = FactoryGirl.create(:host, :on_compute_resource)
|
|||
assert host.compute?
|
|||
put :disassociate, { :id => host.to_param }
|
|||
assert_response :success
|
|||
refute host.reload.compute?
|
|||
end
|
|||
01055e77 | Greg Sutcliffe | def fact_json
|
|
@json ||= JSON.parse(Pathname.new("#{Rails.root}/test/fixtures/brslc022.facts.json").read)
|
|||
end
|
|||
0fda4cf6 | Joseph Mitchell Magen | test "should run puppet for specific host" do
|
|
e14b5758 | Greg Sutcliffe | as_admin { @phost = FactoryGirl.create(:host, :with_puppet) }
|
|
01055e77 | Greg Sutcliffe | User.current=nil
|
|
390e2281 | David Davis | ProxyAPI::Puppet.any_instance.stubs(:run).returns(true)
|
|
e14b5758 | Greg Sutcliffe | put :puppetrun, { :id => @phost.to_param }
|
|
0fda4cf6 | Joseph Mitchell Magen | assert_response :success
|
|
end
|
|||
01055e77 | Greg Sutcliffe | def test_create_valid_node_from_json_facts_object_without_certname
|
|
User.current=nil
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}, set_session_user
|
|||
assert_response :success
|
|||
end
|
|||
def test_create_valid_node_from_json_facts_object_with_certname
|
|||
User.current=nil
|
|||
hostname = fact_json['name']
|
|||
certname = fact_json['certname']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :certname => certname, :facts => facts}, set_session_user
|
|||
assert_response :success
|
|||
end
|
|||
def test_create_invalid
|
|||
User.current=nil
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts'].except('operatingsystem')
|
|||
post :facts, {:name => hostname, :facts => facts}, set_session_user
|
|||
assert_response :unprocessable_entity
|
|||
end
|
|||
c3b33536 | Stephen Benjamin | test 'when ":restrict_registered_smart_proxies" is false, HTTP requests should be able to import facts' do
|
|
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = false
|
|
01055e77 | Greg Sutcliffe | SETTINGS[:require_ssl] = false
|
|
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
5ab79857 | Ohad Levy | assert_nil @controller.detected_proxy
|
|
01055e77 | Greg Sutcliffe | assert_response :success
|
|
end
|
|||
test 'hosts with a registered smart proxy on should import facts successfully' do
|
|||
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = false
|
|||
01055e77 | Greg Sutcliffe | ||
5ab79857 | Ohad Levy | proxy = smart_proxies(:puppetmaster)
|
|
host = URI.parse(proxy.url).host
|
|||
Resolv.any_instance.stubs(:getnames).returns([host])
|
|||
01055e77 | Greg Sutcliffe | hostname = fact_json['name']
|
|
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
5ab79857 | Ohad Levy | assert_equal proxy, @controller.detected_proxy
|
|
01055e77 | Greg Sutcliffe | assert_response :success
|
|
end
|
|||
test 'hosts without a registered smart proxy on should not be able to import facts' do
|
|||
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = false
|
|||
01055e77 | Greg Sutcliffe | ||
Resolv.any_instance.stubs(:getnames).returns(['another.host'])
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :forbidden
|
|||
end
|
|||
test 'hosts with a registered smart proxy and SSL cert should import facts successfully' do
|
|||
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = true
|
|||
01055e77 | Greg Sutcliffe | ||
@request.env['HTTPS'] = 'on'
|
|||
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
|
|||
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :success
|
|||
end
|
|||
test 'hosts without a registered smart proxy but with an SSL cert should not be able to import facts' do
|
|||
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = true
|
|||
01055e77 | Greg Sutcliffe | ||
@request.env['HTTPS'] = 'on'
|
|||
@request.env['SSL_CLIENT_S_DN'] = 'CN=another.host'
|
|||
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :forbidden
|
|||
end
|
|||
test 'hosts with an unverified SSL cert should not be able to import facts' do
|
|||
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = true
|
|||
01055e77 | Greg Sutcliffe | ||
@request.env['HTTPS'] = 'on'
|
|||
@request.env['SSL_CLIENT_S_DN'] = 'CN=secure.host'
|
|||
@request.env['SSL_CLIENT_VERIFY'] = 'FAILED'
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :forbidden
|
|||
end
|
|||
c3b33536 | Stephen Benjamin | test 'when "require_ssl_smart_proxies" and "require_ssl" are true, HTTP requests should not be able to import facts' do
|
|
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
c3b33536 | Stephen Benjamin | Setting[:restrict_registered_smart_proxies] = true
|
|
Setting[:require_ssl_smart_proxies] = true
|
|||
01055e77 | Greg Sutcliffe | SETTINGS[:require_ssl] = true
|
|
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :forbidden
|
|||
end
|
|||
c3b33536 | Stephen Benjamin | test 'when "require_ssl_smart_proxies" is true and "require_ssl" is false, HTTP requests should be able to import facts' do
|
|
8588f9ac | Daniel Lobato | User.current = users(:one) #use an unprivileged user, not apiadmin
|
|
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
|
|||
01055e77 | Greg Sutcliffe | SETTINGS[:require_ssl] = false
|
|
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}
|
|||
assert_response :success
|
|||
end
|
|||
test "when a bad :type is requested, :unprocessable_entity is returned" do
|
|||
User.current=nil
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts, :type => "Host::Invalid"}, set_session_user
|
|||
assert_response :unprocessable_entity
|
|||
2656873b | Martin Bačovský | assert JSON.parse(response.body)['message'] =~ /ERF42-3624/
|
|
01055e77 | Greg Sutcliffe | end
|
|
test "when the imported host failed to save, :unprocessable_entity is returned" do
|
|||
Host::Managed.any_instance.stubs(:save).returns(false)
|
|||
43c4bd72 | Marek Hulan | Nic::Managed.any_instance.stubs(:save).returns(false)
|
|
01055e77 | Greg Sutcliffe | errors = ActiveModel::Errors.new(Host::Managed.new)
|
|
errors.add :foo, 'A stub failure'
|
|||
Host::Managed.any_instance.stubs(:errors).returns(errors)
|
|||
User.current=nil
|
|||
hostname = fact_json['name']
|
|||
facts = fact_json['facts']
|
|||
post :facts, {:name => hostname, :facts => facts}, set_session_user
|
|||
assert_response :unprocessable_entity
|
|||
0f7d219a | Daniel Lobato | assert_equal 'A stub failure', JSON.parse(response.body)['error']['errors']['foo'].first
|
|
01055e77 | Greg Sutcliffe | end
|
|
e02a2ff2 | Julien Pivotto | test 'non-admin user with power_host permission can boot a vm' do
|
|
@bmchost = FactoryGirl.create(:host, :managed)
|
|||
FactoryGirl.create(:nic_bmc, :host => @bmchost)
|
|||
ProxyAPI::BMC.any_instance.stubs(:power).with(:action => 'status').returns("on")
|
|||
role = FactoryGirl.create(:role, :name => 'power_hosts')
|
|||
role.add_permissions!(['power_hosts'])
|
|||
api_user = FactoryGirl.create(:user)
|
|||
api_user.update_attribute :roles, [role]
|
|||
as_user(api_user) do
|
|||
put :power, { :id => @bmchost.to_param, :power_action => 'status' }
|
|||
end
|
|||
assert_response :success
|
|||
assert @response.body =~ /on/
|
|||
end
|
|||
8588f9ac | Daniel Lobato | context 'BMC proxy operations' do
|
|
setup :initialize_proxy_ops
|
|||
def initialize_proxy_ops
|
|||
User.current = users(:apiadmin)
|
|||
e14b5758 | Greg Sutcliffe | @bmchost = FactoryGirl.create(:host, :managed)
|
|
43c4bd72 | Marek Hulan | FactoryGirl.create(:nic_bmc, :host => @bmchost)
|
|
8588f9ac | Daniel Lobato | end
|
|
test "power call to interface" do
|
|||
ProxyAPI::BMC.any_instance.stubs(:power).with(:action => 'status').returns("on")
|
|||
e14b5758 | Greg Sutcliffe | put :power, { :id => @bmchost.to_param, :power_action => 'status' }
|
|
8588f9ac | Daniel Lobato | assert_response :success
|
|
assert @response.body =~ /on/
|
|||
end
|
|||
test "wrong power call fails gracefully" do
|
|||
e14b5758 | Greg Sutcliffe | put :power, { :id => @bmchost.to_param, :power_action => 'wrongmethod' }
|
|
8588f9ac | Daniel Lobato | assert_response 422
|
|
2656873b | Martin Bačovský | assert @response.body =~ /available methods are/
|
|
8588f9ac | Daniel Lobato | end
|
|
test "boot call to interface" do
|
|||
ProxyAPI::BMC.any_instance.stubs(:boot).with(:function => 'bootdevice', :device => 'bios').
|
|||
returns( { "action" => "bios", "result" => true } .to_json)
|
|||
e14b5758 | Greg Sutcliffe | put :boot, { :id => @bmchost.to_param, :device => 'bios' }
|
|
8588f9ac | Daniel Lobato | assert_response :success
|
|
assert @response.body =~ /true/
|
|||
end
|
|||
test "wrong boot call to interface fails gracefully" do
|
|||
e14b5758 | Greg Sutcliffe | put :boot, { :id => @bmchost.to_param, :device => 'wrongbootdevice' }
|
|
8588f9ac | Daniel Lobato | assert_response 422
|
|
2656873b | Martin Bačovský | assert @response.body =~ /available devices are/
|
|
8588f9ac | Daniel Lobato | end
|
|
6f943886 | Joseph Magen | test "should return correct total and subtotal metadata if search param is passed" do
|
|
e14b5758 | Greg Sutcliffe | FactoryGirl.create_list(:host, 8)
|
|
get :index, {:search => @bmchost.name }
|
|||
6f943886 | Joseph Magen | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
e14b5758 | Greg Sutcliffe | assert_equal 10, response['total'] # one from setup, one from bmc setup, 8 here
|
|
6f943886 | Joseph Magen | assert_equal 1, response['subtotal']
|
|
e14b5758 | Greg Sutcliffe | assert_equal @bmchost.name, response['search']
|
|
6f943886 | Joseph Magen | end
|
|
8588f9ac | Daniel Lobato | end
|
|
0fda4cf6 | Joseph Mitchell Magen | end
|