Skip to content

Commit

Permalink
wip adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Nov 6, 2023
1 parent 2999007 commit 7f4544e
Showing 1 changed file with 188 additions and 0 deletions.
188 changes: 188 additions & 0 deletions test/controllers/concerns/bulk_hosts_extension_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
require 'test_helper'

class BulkHostsExtensionTestController < ::Api::BaseController
include Api::Version2
include ::Api::V2::BulkHostsExtension

def initialize(params = {})
@params = params
end

attr_reader :params
end

class BulkHostsExtensionTest < ActiveSupport::TestCase
def models
# @organization = get_organization
# @host1 = hosts(:one)
# @host2 = hosts(:two)
# @host3 = hosts(:without_content_facet)
# @host4 = hosts(:without_subscription_facet)
# @host5 = hosts(:without_errata)
# @host6 = hosts(:without_organization)

as_admin do
@organization = FactoryBot.create(:organization)
@host1 = FactoryBot.create(:host, :organization => @organization)
@host2 = FactoryBot.create(:host, :organization => @organization)
@host3 = FactoryBot.create(:host, :organization => @organization)
@host4 = FactoryBot.create(:host, :organization => @organization)
@host5 = FactoryBot.create(:host, :organization => @organization)
@host6 = FactoryBot.create(:host, :organization => @organization)
end
end

def permissions
@edit = :edit_hosts
end

def setup
# set_user
models
permissions
@controller = BulkHostsExtensionTestController.new(organization_id: @organization.id)
end

def test_search
bulk_params = {
:included => {
:search => "name = #{@host1.name}"
}
}
result = @controller.find_bulk_hosts(@edit, bulk_params)

assert_equal result, [@host1]
end

def test_search_restrict
bulk_params = {
:included => {
:search => "name ~ host"
}
}
restrict = lambda { |hosts| hosts.where("id != #{@host2.id}") }
result = @controller.find_bulk_hosts(@edit, bulk_params, restrict)

assert_includes result, @host1
refute_includes result, @host2
assert_includes result, @host3
end

def test_search_exclude
bulk_params = {
:included => {
:search => "name ~ host"
},
:excluded => {
:ids => [@host1.id]
}
}
result = @controller.find_bulk_hosts(@edit, bulk_params)

refute_includes result, @host1
assert_includes result, @host2
assert_includes result, @host3
end

def test_select_all_hosts_for_errata_apply
@controller.instance_variable_set(
:@params,
{
:install_all => true
})
result = @controller.find_bulk_hosts(@edit, {})

assert_equal_arrays [@host1, @host2, @host3, @host4, @host5, @host6], result
end

def test_no_hosts_specified
bulk_params = {
:included => {}
}

@controller.find_bulk_hosts(@edit, bulk_params)
assert_response :bad_request
end

def test_ids
bulk_params = {
:included => {
:ids => [@host1.id, @host2.id]
}
}
result = @controller.find_bulk_hosts(@edit, bulk_params)

assert_equal [@host1, @host2].sort, result.sort
end

def test_ids_excluded
bulk_params = {
:included => {
:ids => [@host1.id, @host2.id]
},
:excluded => {
:ids => [@host2.id]
}
}
result = @controller.find_bulk_hosts(@edit, bulk_params)

assert_equal result, [@host1]
end

def test_ids_restricted
bulk_params = {
:included => {
:ids => [@host1.id, @host2.id]
}
}
restrict = lambda { |hosts| hosts.where("id != #{@host2.id}") }
result = @controller.find_bulk_hosts(@edit, bulk_params, restrict)

assert_equal result, [@host1]
end

def test_included_ids_with_nil_scoped_search
bulk_params = {
:included => {
:ids => [@host1.id, @host2.id],
:search => nil
}
}

result = @controller.find_bulk_hosts(@edit, bulk_params)

assert_equal [@host1, @host2].sort, result.sort
end

def test_ids_with_scoped_search
bulk_params = {
:included => {
:ids => [@host1.id, @host2.id],
:search => "name != #{@host2.name}"
}
}

result = @controller.find_bulk_hosts(@edit, bulk_params)

assert_equal result, [@host1]
end

def test_forbidden
bulk_params = {
:included => {
:ids => [@host1.id]
},
:excluded => {
:ids => [@host1.id]
}
}

@controller.find_bulk_hosts(@edit, bulk_params)
assert_response :forbidden
end

def test_empty_params
@controller.find_bulk_hosts(@edit, {})
assert_response :bad_request
end
end

0 comments on commit 7f4544e

Please sign in to comment.