Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Dec 23, 2024
1 parent 3b30531 commit 783ba1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ module InsightsAdvisor
class InsightsAdvisorController < ::Api::V2::BaseController
include ::Api::Version2

before_action :find_organization

api :GET, "insights_advisor/host_details", N_('Fetch Insights-related host details')
param :host_uuids, Array, required: true, desc: N_('List of host UUIDs')
def host_details
@hosts = ::Host::Managed.search_for(params[:search] || "", :order => params[:order]).where(:organization_id => @organization.id).includes(:insights)
respond_to do |format|
format.json { render 'api/v2/insights_advisor/host_details' }
uuids = params.require(:host_uuids)
@hosts = ::Host.joins(:insights).where(:insights => { :uuid => uuids })
if @hosts.empty?
render json: { error: 'No hosts found for the given UUIDs' }, status: :not_found
else
respond_to do |format|
format.json { render 'api/v2/insights_advisor/host_details' }
end
end
end

private

def find_organization
@organization ||= Organization.find_by(label: params[:organization_label]) if params[:organization_label]
@organization ||= Organization.find_by(label: params[:organization]) if params[:organization]
@organization ||= Organization.find(params[:organization_id]) if params[:organization_id]
raise ::Foreman::Exception.new(N_("Organization not found")) unless @organization
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,19 @@ class InsightsAdvisorControllerTest < ActionController::TestCase
@host3 = FactoryBot.create(:host, organization: @test_org)
end

test 'shows all hosts with no search param' do
get :host_details, params: { organization_id: @test_org.id }

assert_response :success
assert_template 'api/v2/insights_advisor/host_details'
assert_equal @test_org.hosts.count, assigns(:hosts).count
end

test 'shows hosts with search param' do
search = @host1.name[0..4]
get :host_details, params: { organization_id: @test_org.id, search: search }
test 'shows hosts with uuids' do
uuids = [@host1.insights.uuid, @host2.insights.uuid]
get :host_details, params: { organization_id: @test_org.id, host_uuids: uuids }
assert_response :success
assert_template 'api/v2/insights_advisor/host_details'
assert_equal @test_org.hosts.where('name LIKE ?', "%#{search}%").count, assigns(:hosts).count
assert_equal @test_org.hosts.joins(:insights).where(:insights => { :uuid => uuids }).count, assigns(:hosts).count
refute_equal @test_org.hosts.count, assigns(:hosts).count
end

test 'fails without org id' do
response = get :host_details

assert_includes response.body, 'Organization not found'
test 'shows error when no hosts found' do
get :host_details, params: { organization_id: @test_org.id, host_uuids: ['nonexistentuuid'] }
assert_response :not_found
assert_equal 'No hosts found for the given UUIDs', JSON.parse(response.body)['error']
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/factories/insights_factories.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :insights_facet do
# sequence(:uuid) { |n| "uuid-#{n}" }
sequence(:uuid) { |n| "uuid-#{n}" }

trait :with_hits do
hits do
Expand Down

0 comments on commit 783ba1e

Please sign in to comment.