Project

General

Profile

Download (4.95 KB) Statistics
| Branch: | Tag: | Revision:
46ac4aa8 Ohad Levy
require 'test_helper'

class FactValuesControllerTest < ActionController::TestCase
b729fb04 Paul Kelly
def fact_fixture
017e1049 Ohad Levy
Pathname.new("#{Rails.root}/test/fixtures/brslc022.facts.yaml").read
b729fb04 Paul Kelly
end

358ec5a3 Dominic Cleal
def setup
User.current = nil
end

00de4f4b Dmitri Dolguikh
fixtures

b729fb04 Paul Kelly
def test_index
get :index, {}, set_session_user
98791bd6 Lucas Tolchinsky
assert_response :success
c4b56e43 Ohad Levy
assert_template FactValue.unconfigured? ? 'welcome' : 'index'
b729fb04 Paul Kelly
assert_not_nil :fact_values
98791bd6 Lucas Tolchinsky
end

b729fb04 Paul Kelly
def test_create_invalid
4634fd09 Ohad Levy
User.current = nil
358ec5a3 Dominic Cleal
post :create, {:facts => fact_fixture[1..-1], :format => "yml"}, set_session_user
895a7680 Ohad Levy
assert_response :bad_request
b729fb04 Paul Kelly
end

399dca9e Ohad Levy
def test_create_valid_puppet_node_facts_object
4634fd09 Ohad Levy
User.current = nil
358ec5a3 Dominic Cleal
post :create, {:facts => fact_fixture, :format => "yml"}, set_session_user
b729fb04 Paul Kelly
assert_response :success
46ac4aa8 Ohad Levy
end
895a7680 Ohad Levy
399dca9e Ohad Levy
def test_create_valid_facter_yaml_output
User.current = nil
a424ba79 Ohad Levy
facts = Facter.to_hash
assert_instance_of Hash, facts
358ec5a3 Dominic Cleal
post :create, {:facts => facts.to_yaml, :format => "yml"}, set_session_user
399dca9e Ohad Levy
assert_response :success
end

9fd7478e Paul Kelly
test 'user with viewer rights should succeed in viewing facts' do
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => users(:one).id)
9fd7478e Paul Kelly
assert_response :success
end
b96931f2 Ohad Levy
test 'show nested fact json' do
d076d573 Joseph Mitchell Magen
as_user :admin do
get :index, {:format => "json", :fact_id => "kernelversion"}, set_session_user
end
b96931f2 Ohad Levy
factvalues = ActiveSupport::JSON.decode(@response.body)
assert_equal "fact = kernelversion", @request.params[:search]
assert factvalues.is_a?(Hash)
00de4f4b Dmitri Dolguikh
assert_equal [["kernelversion"]], factvalues.values.map(&:keys).uniq
b96931f2 Ohad Levy
assert_response :success
end

358ec5a3 Dominic Cleal
test 'when ":restrict_registered_puppetmasters" is false, HTTP requests should be able to import facts' do
Setting[:restrict_registered_puppetmasters] = false
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
post :create, {:facts => fact_fixture, :format => "yml"}
assert_response :success
end

test 'hosts with a registered smart proxy on should import facts successfully' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
post :create, {:facts => fact_fixture, :format => "yml"}
assert_response :success
end

test 'hosts without a registered smart proxy on should not be able to import facts' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = false

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

test 'hosts with a registered smart proxy and SSL cert should import facts successfully' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true

@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN_CN'] = 'else.where'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
post :create, {:facts => fact_fixture, :format => "yml"}
assert_response :success
end

test 'hosts without a registered smart proxy but with an SSL cert should not be able to import facts' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true

@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN_CN'] = 'another.host'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
post :create, {:facts => fact_fixture, :format => "yml"}
assert_equal 403, @response.status
end

test 'hosts with an unverified SSL cert should not be able to import facts' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
b96931f2 Ohad Levy
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN_CN'] = 'secure.host'
@request.env['SSL_CLIENT_VERIFY'] = 'FAILED'
post :create, {:facts => fact_fixture, :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 import facts' do
Setting[:restrict_registered_puppetmasters] = true
Setting[:require_ssl_puppetmasters] = true
SETTINGS[:require_ssl] = true

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
post :create, {:facts => fact_fixture, :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 import facts' do
# 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'])
post :create, {:facts => fact_fixture, :format => "yml"}
assert_response :success
end
46ac4aa8 Ohad Levy
end