Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #38039 - Fix ordering by compliance_[failed|passed|othered] #587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ruby_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
uses: theforeman/actions/.github/workflows/foreman_plugin.yml@v0
with:
plugin: foreman_openscap
foreman_version: refs/pull/10381/head
3 changes: 1 addition & 2 deletions app/controllers/arf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def index
# Avoid using includes() with nested associations and "order by" together. Otherwise,
# includes() will use join tables instead and Rails somehow create many objects and
# high memory consumption.
@arf_reports_pg = resource_base.search_for(params[:search], :order => params[:order])
.paginate(:page => params[:page], :per_page => params[:per_page])
@arf_reports_pg = resource_base_search_and_page.load
arf_report_ids = @arf_reports_pg.pluck(:id)
@arf_reports = resource_base.includes(:policy, :openscap_proxy, :host => %i[policies last_report_object host_statuses])
.where(id: arf_report_ids)
Expand Down
3 changes: 3 additions & 0 deletions app/models/foreman_openscap/arf_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ class ArfReport < ::Report

scope :failed, lambda { where("(#{report_status_column} >> #{bit_mask 'failed'}) > 0") }
scope :not_failed, lambda { where("(#{report_status_column} >> #{bit_mask 'failed'}) = 0") }
scope :select_compliance_failed, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'failed'}) as compliance_failed") }

scope :othered, lambda { where("(#{report_status_column} >> #{bit_mask 'othered'}) > 0").merge(not_failed) }
scope :not_othered, lambda { where("(#{report_status_column} >> #{bit_mask 'othered'}) = 0") }
scope :select_compliance_othered, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'othered'}) as compliance_othered") }

scope :passed, lambda { where("(#{report_status_column} >> #{bit_mask 'passed'}) > 0").merge(not_failed).merge(not_othered) }
scope :select_compliance_passed, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'passed'}) as compliance_passed") }

scope :by_rule_result, lambda { |rule_name, rule_result| joins(:sources).where(:sources => { :value => rule_name }, :logs => { :result => rule_result }) }

Expand Down
24 changes: 24 additions & 0 deletions test/functional/api/v2/compliance/arf_reports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,30 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase
assert_equal report.id, response['results'].first["id"].to_i
end

test "should order by compliance_[failed|passed|othered]" do
reports_cleanup
policy = FactoryBot.create(:policy)
create_arf_report_for_search({ "passed" => 4, "othered" => 0, "failed" => 1 }, policy)
create_arf_report_for_search({ "passed" => 1, "othered" => 0, "failed" => 0 }, policy)
create_arf_report_for_search({ "passed" => 15, "othered" => 9, "failed" => 0 }, policy)
create_arf_report_for_search({ "passed" => 2, "othered" => 3, "failed" => 7 }, policy)

get :index, :params => { :order => "compliance_failed DESC" }, :session => set_session_user
assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_equal 7, response['results'].first['failed']

get :index, :params => { :order => "compliance_passed DESC" }, :session => set_session_user
assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_equal 2, response['results'].first['passed']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd expect 15, but due to default scope https://github.com/theforeman/foreman_openscap/blob/master/app/models/foreman_openscap/arf_report.rb#L30, it will always order by latest time creation first.


get :index, :params => { :order => "compliance_othered DESC" }, :session => set_session_user
assert_response :success
response = ActiveSupport::JSON.decode(@response.body)
assert_equal 3, response['results'].first['othered']
end

private

def reports_cleanup
Expand Down
9 changes: 9 additions & 0 deletions test/functional/arf_reports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ class ArfReportsControllerTest < ActionController::TestCase
get :download_html, :params => { :id => arf_report.id }, :session => set_session_user
assert_equal report_html, @response.body
end

test "should order arf reports by compliance_[failed|passed|othered]" do
get :index, params: { order: "compliance_failed DESC" }, :session => set_session_user
assert_response :success
get :index, params: { order: "compliance_passed DESC" }, :session => set_session_user
assert_response :success
get :index, params: { order: "compliance_othered DESC" }, :session => set_session_user
assert_response :success
end
end
Loading