Project

General

Profile

Download (2.13 KB) Statistics
| Branch: | Tag: | Revision:
dc38aad3 Joseph Magen
require 'test_helper'

class Api::V2::FactValuesControllerTest < ActionController::TestCase
e14b5758 Greg Sutcliffe
def setup
8c6bc83e Marek Hulan
@host = FactoryBot.create(:host)
3cd8c84b Michael Moll
FactoryBot.create(:fact_value, :value => '2.6.9', :host => @host,
8c6bc83e Marek Hulan
:fact_name => FactoryBot.create(:fact_name, :name => 'kernelversion'))
e14b5758 Greg Sutcliffe
end

dc38aad3 Joseph Magen
test "should get index" do
d041d4bb Dominic Cleal
get :index
dc38aad3 Joseph Magen
assert_response :success
8ebbbecf Shira Maximov
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
d7c67746 Michael Moll
expected_hash = {@host.name => {"kernelversion" => "2.6.9"}}
8ebbbecf Shira Maximov
assert_equal expected_hash, fact_values
dc38aad3 Joseph Magen
end

test "should get facts for given host only" do
8ebbbecf Shira Maximov
get :index, params: {:host_id => @host.name}
dc38aad3 Joseph Magen
assert_response :success
e44f5c1c Daniel Lobato
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
d7c67746 Michael Moll
expected_hash = {@host.name => {"kernelversion" => "2.6.9"}, "kernelversion" => "2.6.9"}
e44f5c1c Daniel Lobato
assert_equal expected_hash, fact_values
end

test "should get facts for given host id" do
8ebbbecf Shira Maximov
get :index, params: {:host_id => @host.id}
e44f5c1c Daniel Lobato
assert_response :success
8ebbbecf Shira Maximov
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
d7c67746 Michael Moll
expected_hash = {@host.name => {"kernelversion" => "2.6.9"}, "kernelversion" => "2.6.9"}
e44f5c1c Daniel Lobato
assert_equal expected_hash, fact_values
dc38aad3 Joseph Magen
end
d6436499 Dominic Cleal
test "should get facts as non-admin user with joined search" do
768aad03 Tom Caspy
setup_user
8c6bc83e Marek Hulan
@host.update_attribute(:hostgroup, FactoryBot.create(:hostgroup))
768aad03 Tom Caspy
as_user(users(:one)) do
8ebbbecf Shira Maximov
get :index, params: {:search => "host.hostgroup = #{@host.hostgroup.name}"}
d6436499 Dominic Cleal
end
assert_response :success
8ebbbecf Shira Maximov
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
d7c67746 Michael Moll
expected_hash = {@host.name => {"kernelversion" => "2.6.9"}}
8ebbbecf Shira Maximov
assert_equal expected_hash, fact_values
end

test "should return empty result in case host doesn't exists" do
get :index, params: {:host_id => 9000}
assert_response :success
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
expected_hash = {}
d6436499 Dominic Cleal
assert_equal expected_hash, fact_values
end
768aad03 Tom Caspy
private

def setup_user
@request.session[:user] = users(:one).id
8ebbbecf Shira Maximov
users(:one).roles = [Role.default, Role.find_by_name('Viewer')]
768aad03 Tom Caspy
end
a2b3be55 Tomer Brisker
end