Skip to content

Commit

Permalink
Fix rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Jan 15, 2024
1 parent 99a0c2a commit c493dd7
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 98 deletions.
23 changes: 6 additions & 17 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Lint/BooleanSymbol:
Exclude:
- 'app/models/concerns/foreman_openscap/host_extensions.rb'
- 'app/models/foreman_openscap/arf_report.rb'
- 'test/unit/policy_test.rb'

# Offense count: 2
# Configuration parameters: AllowedMethods.
Expand Down Expand Up @@ -113,23 +114,6 @@ Rails/Delegate:
Exclude:
- 'app/models/foreman_openscap/asset.rb'

# Offense count: 18
# Configuration parameters: Include.
# Include: app/models/**/*.rb
Rails/HasManyOrHasOneDependent:
Exclude:
- 'app/models/concerns/foreman_openscap/oval_facet_host_extensions.rb'
- 'app/models/concerns/foreman_openscap/smart_proxy_extensions.rb'
- 'app/models/foreman_openscap/arf_report.rb'
- 'app/models/foreman_openscap/asset.rb'
- 'app/models/foreman_openscap/cve.rb'
- 'app/models/foreman_openscap/oval_content.rb'
- 'app/models/foreman_openscap/oval_policy.rb'
- 'app/models/foreman_openscap/policy.rb'
- 'app/models/foreman_openscap/scap_content.rb'
- 'app/models/foreman_openscap/scap_content_profile.rb'
- 'app/models/foreman_openscap/tailoring_file.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -178,6 +162,11 @@ Rails/ReadWriteAttribute:
Rails/TimeZone:
Exclude:
- 'app/models/foreman_openscap/arf_report.rb'
- 'test/functional/api/v2/compliance/arf_reports_controller_test.rb'
- 'test/factories/arf_report_factory.rb'

Rails/HasManyOrHasOneDependent:
Enabled: false

Rails/RefuteMethods:
Enabled: false
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ Rake::TestTask.new(:test) do |t|
end

task :default => :test

task :rubocop do
require 'rubocop/rake_task'
RuboCop::RakeTask.new do |task|
task.options = ['--config', '.rubocop.yml']
task.patterns = ["app/**/*.rb",
"lib/**/*.rb",
"test/**/*.rb"]
end
rescue StandardError
puts 'Rubocop not loaded.'
end
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase
test "should find passed reports by compliance status" do
reports_cleanup
policy = FactoryBot.create(:policy)
passing_1 = create_arf_report_for_search({ "passed" => 4, "othered" => 0, "failed" => 0 }, policy)
passing_2 = create_arf_report_for_search({ "passed" => 1, "othered" => 0, "failed" => 0 }, policy)
create_arf_report_for_search({ "passed" => 4, "othered" => 0, "failed" => 0 }, 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)

Expand Down Expand Up @@ -419,7 +419,7 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase

test "should find reports with rule name" do
reports_cleanup
host = FactoryBot.create(:compliance_host)
FactoryBot.create(:compliance_host)
rule_name = 'xccdf_org.something_installed'
rule_names_1 = ['xccdf_org.something_tested', rule_name]
rule_names_2 = ['xccdf_org.nothing', 'xccdf_org.whatever']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Api::V2::Compliance::OvalPoliciesControllerTest < ActionController::TestCa
test "should get index of OVAL policies" do
get :index, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert !response['results'].empty?
assert_not response['results'].empty?
assert_response :success
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Api::V2::Compliance::OvalReportsControllerTest < ActionController::TestCas

response = ActiveSupport::JSON.decode(@response.body)
assert_equal 'fail', response['result']
refute response['errors'].empty?
refute_empty response['errors']
assert_response :unprocessable_entity
end
end
22 changes: 11 additions & 11 deletions test/functional/api/v2/compliance/policies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ class Api::V2::Compliance::PoliciesControllerTest < ActionController::TestCase
FactoryBot.create(:policy)
get :index, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert !response['results'].empty?
assert_not response['results'].empty?
assert_response :success
end

test "should get index without hosts and hostgroups" do
FactoryBot.create(:policy)
get :index, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert response['results'][0]['hosts'].empty?
assert response['results'][0]['hostgroups'].empty?
assert_empty response['results'][0]['hosts']
assert_empty response['results'][0]['hostgroups']
assert_response :success
end

test "should get index and show hosts" do
host = FactoryBot.create(:host)
asset = FactoryBot.create(:asset, :assetable_id => host.id, :assetable_type => 'Host::Base')
policy = FactoryBot.create(:policy, :assets => [asset])
FactoryBot.create(:policy, :assets => [asset])
get :index, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert !response['results'].empty?
assert !response['results'][0]['hosts'].empty?
assert_not response['results'].empty?
assert_not response['results'][0]['hosts'].empty?
assert_response :success
end

Expand All @@ -45,11 +45,11 @@ class Api::V2::Compliance::PoliciesControllerTest < ActionController::TestCase
ForemanOpenscap::Policy.any_instance.stubs(:populate_overrides)
hostgroup = FactoryBot.create(:hostgroup)
asset = FactoryBot.create(:asset, :assetable_id => hostgroup.id, :assetable_type => 'Hostgroup')
policy = FactoryBot.create(:policy, :assets => [asset])
FactoryBot.create(:policy, :assets => [asset])
get :index, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert !response['results'].empty?
assert !response['results'][0]['hostgroups'].empty?
assert_not response['results'].empty?
assert_not response['results'][0]['hostgroups'].empty?
assert_response :success
end

Expand All @@ -72,8 +72,8 @@ class Api::V2::Compliance::PoliciesControllerTest < ActionController::TestCase
policy = FactoryBot.create(:policy, :assets => [hostgroup_asset, host_asset])
get :show, :params => { :id => policy.to_param }, :session => set_session_user
response = ActiveSupport::JSON.decode(@response.body)
assert !response['hosts'].empty?
assert !response['hostgroups'].empty?
assert_not response['hosts'].empty?
assert_not response['hostgroups'].empty?
assert_response :success
end

Expand Down
2 changes: 1 addition & 1 deletion test/functional/openscap_proxies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class OpenscapProxiesControllerTest < ActionController::TestCase
proxy = FactoryBot.create(:openscap_proxy)
get :openscap_spool, :params => { :id => proxy.id }, :session => set_session_user
assert_template :partial => 'smart_proxies/_openscap_spool'
assert @response.body.match('4 spool errors detected, inspect the appropriate file directly on proxy')
assert_match '4 spool errors detected, inspect the appropriate file directly on proxy', @response.body
end
end
6 changes: 3 additions & 3 deletions test/test_plugin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def create_report_with_rules(host, rule_names, rule_results)
metrics = {
'passed' => rule_results.count { |result| result == 'pass' },
'failed' => rule_results.count { |result| result == 'fail' },
'othered' => rule_results.reject { |result| result == 'fail' || result == 'pass' }.count
'othered' => rule_results.count { |result| result != 'fail' && result != 'pass' }
}
report = FactoryBot.create(:arf_report, :host_id => host.id, :metrics => metrics, :status => metrics)
rule_names.each_with_index do |item, index|
rule_names.each_with_index do |_item, index|
source = FactoryBot.create(:compliance_source, :value => rule_names[index])
log = FactoryBot.create(:compliance_log, :source => source, :report => report, :result => rule_results[index])
FactoryBot.create(:compliance_log, :source => source, :report => report, :result => rule_results[index])
end
report
end
Expand Down
24 changes: 12 additions & 12 deletions test/unit/arf_report_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ArfReportTest < ActiveSupport::TestCase
end

test 'equal? should return false when there is change in report results' do
new_source = FactoryBot.create(:source)
FactoryBot.create(:source)

report_1 = FactoryBot.create(:arf_report, :policy => @policy, :host_id => @host.id)
report_2 = FactoryBot.create(:arf_report, :policy => @policy, :host_id => @host.id)
Expand Down Expand Up @@ -93,8 +93,8 @@ class ArfReportTest < ActiveSupport::TestCase
reports << FactoryBot.create(:arf_report, :host_id => host.id, :status => @status)
FactoryBot.create(:policy_arf_report, :arf_report_id => reports.last.id)
end
assert ForemanOpenscap::ArfReport.latest.to_a.include? reports[-2]
assert ForemanOpenscap::ArfReport.latest.to_a.include? reports[-1]
assert_includes ForemanOpenscap::ArfReport.latest.to_a, reports[-2]
assert_includes ForemanOpenscap::ArfReport.latest.to_a, reports[-1]
end

test 'should return latest report of policy for each of the hosts' do
Expand All @@ -115,10 +115,10 @@ class ArfReportTest < ActiveSupport::TestCase
FactoryBot.create(:policy_arf_report, :arf_report_id => reports.last.id, :policy_id => policy.id)
end

assert ForemanOpenscap::ArfReport.latest_of_policy(policy).include? reports[-1]
assert ForemanOpenscap::ArfReport.latest_of_policy(policy).include? reports[-2]
assert ForemanOpenscap::ArfReport.latest_of_policy(@policy).include? reports[-3]
assert ForemanOpenscap::ArfReport.latest_of_policy(@policy).include? reports[-4]
assert_includes ForemanOpenscap::ArfReport.latest_of_policy(policy), reports[-1]
assert_includes ForemanOpenscap::ArfReport.latest_of_policy(policy), reports[-2]
assert_includes ForemanOpenscap::ArfReport.latest_of_policy(@policy), reports[-3]
assert_includes ForemanOpenscap::ArfReport.latest_of_policy(@policy), reports[-4]
assert_equal 2, ForemanOpenscap::ArfReport.latest_of_policy(@policy).count
assert_equal 2, ForemanOpenscap::ArfReport.latest_of_policy(policy).count
end
Expand All @@ -139,17 +139,17 @@ class ArfReportTest < ActiveSupport::TestCase

test 'should return failed reports' do
assert_equal 3, ForemanOpenscap::ArfReport.failed.count
@failed_reports.each { |failure| assert ForemanOpenscap::ArfReport.failed.include?(failure) }
@failed_reports.each { |failure| assert_includes ForemanOpenscap::ArfReport.failed, failure }
end

test 'should return othered reports' do
assert_equal 3, ForemanOpenscap::ArfReport.othered.count
@othered_reports.each { |other| assert ForemanOpenscap::ArfReport.othered.include?(other) }
@othered_reports.each { |other| assert_includes ForemanOpenscap::ArfReport.othered, other }
end

test 'should return passed reports' do
assert_equal 3, ForemanOpenscap::ArfReport.passed.count
@passed_reports.each { |pass| assert ForemanOpenscap::ArfReport.passed.include?(pass) }
@passed_reports.each { |pass| assert_includes ForemanOpenscap::ArfReport.passed, pass }
end
end

Expand All @@ -162,7 +162,7 @@ class ArfReportTest < ActiveSupport::TestCase
create_logs_for_report(report, [@passed_log_params, @failed_log_params])

report.destroy
refute ForemanOpenscap::ArfReport.all.include? report
refute_includes ForemanOpenscap::ArfReport.all, report
end

test 'should get reports by rule result' do
Expand All @@ -173,7 +173,7 @@ class ArfReportTest < ActiveSupport::TestCase
rule_results_2 = ['fail', 'fail']
host = FactoryBot.create(:compliance_host)
report_1 = create_report_with_rules(host, rule_names_1, rule_results_1)
report_2 = create_report_with_rules(host, rule_names_2, rule_results_2)
create_report_with_rules(host, rule_names_2, rule_results_2)
res = ForemanOpenscap::ArfReport.by_rule_result(rule_name, 'pass').first
assert_equal res, report_1
end
Expand Down
8 changes: 4 additions & 4 deletions test/unit/concerns/host_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ class HostExtensionsTest < ActiveSupport::TestCase
context "find_with_any_policy" do
setup do
@host.destroy
policy, host, host_2 = setup_hosts_with_inherited_policy.values_at(:policy, :host, :host_2)
policy, host_3, host_4 = setup_hosts_with_policy.values_at(:policy, :host, :host_2)
_policy, host, host_2 = setup_hosts_with_inherited_policy.values_at(:policy, :host, :host_2)
_policy, host_3, host_4 = setup_hosts_with_policy.values_at(:policy, :host, :host_2)
@compliance_hosts = [host, host_2, host_3, host_4]
@host_without_policy = FactoryBot.create(:host)
end
Expand Down Expand Up @@ -139,7 +139,7 @@ class HostExtensionsTest < ActiveSupport::TestCase
test "should find hosts removed from policy" do
rule_names = ['xccdf_org.something', 'xccdf_org.nothing']
rule_results = ['pass', 'pass']
host_1 = setup_host_with_reports_and_rules([rule_names], [rule_results], @policy)
setup_host_with_reports_and_rules([rule_names], [rule_results], @policy)
host_2 = setup_host_with_reports_and_rules([rule_names], [rule_results], @policy)

scap_content = FactoryBot.create(:scap_content)
Expand Down Expand Up @@ -200,7 +200,7 @@ def setup_host_with_reports_and_rules(rule_names_ary, rule_results_ary, policy)
host = FactoryBot.create(:host)
asset = FactoryBot.create(:asset, :assetable_id => host.id)
FactoryBot.create(:asset_policy, :asset_id => asset.id, :policy_id => policy.id)
rule_names_ary.each_with_index do |item, index|
rule_names_ary.each_with_index do |_item, index|
report = create_report_with_rules host, rule_names_ary[index], rule_results_ary[index]
FactoryBot.create(:policy_arf_report, :policy_id => policy.id, :arf_report_id => report.id)
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/openscap_host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class OpenscapHostTest < ActiveSupport::TestCase
@report_2.save!
reports = @host.reports_for_policy(@policy)
assert_equal 2, reports.count
assert reports.include?(@report_1)
assert reports.include?(@report_2)
assert_includes reports, @report_1
assert_includes reports, @report_2
# Ensure the last report list first
assert_equal @report_2, reports.first
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/oval_host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OvalHostTest < ActiveSupport::TestCase
policy = FactoryBot.create(:oval_policy, :oval_content => content)
proxy = FactoryBot.create(:openscap_proxy)
host = FactoryBot.create(:oval_host, :ansible_roles => [@ansible_role], :openscap_proxy => proxy)
facet = FactoryBot.create(:oval_facet, :host => host, :oval_policies => [policy])
FactoryBot.create(:oval_facet, :host => host, :oval_policies => [policy])

host_params = host.info["parameters"]
policies = JSON.parse(host_params[@config.policies_param])
Expand Down
22 changes: 11 additions & 11 deletions test/unit/oval_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OvalPolicyTest < ActiveSupport::TestCase
:cron_line => 'aaa',
:oval_content => @oval_content)
refute policy.save
assert policy.errors[:cron_line].include?("does not consist of 5 parts separated by space")
assert_includes policy.errors[:cron_line], "does not consist of 5 parts separated by space"
end

test "should create OVAL policy with weekly period" do
Expand All @@ -28,7 +28,7 @@ class OvalPolicyTest < ActiveSupport::TestCase
:weekday => 'someday',
:oval_content => @oval_content)
refute policy.save
assert policy.errors[:weekday].include?("is not a valid value")
assert_includes policy.errors[:weekday], "is not a valid value"
end

test "should create OVAL policy with monthly period" do
Expand All @@ -45,7 +45,7 @@ class OvalPolicyTest < ActiveSupport::TestCase
:day_of_month => '0',
:oval_content => @oval_content)
refute policy.save
assert policy.errors[:day_of_month].include?("must be between 1 and 31")
assert_includes policy.errors[:day_of_month], "must be between 1 and 31"
end

test "should not create OVAL policy when attributes do not correspond to selected period in new record" do
Expand Down Expand Up @@ -84,11 +84,11 @@ class OvalPolicyTest < ActiveSupport::TestCase
:oval_content => @oval_content)

assert policy.save
assert policy.reload.hosts.include?(host)
assert_includes policy.reload.hosts, host

policy.host_ids = []
assert policy.save
refute policy.reload.hosts.include?(host)
refute_includes policy.reload.hosts, host
end

test "should add and remove hostgroups for OVAL policy" do
Expand All @@ -99,11 +99,11 @@ class OvalPolicyTest < ActiveSupport::TestCase
:hostgroup_ids => [hostgroup.id],
:oval_content => @oval_content)
assert policy.save
assert policy.reload.hostgroups.include?(hostgroup)
assert_includes policy.reload.hostgroups, hostgroup

policy.hostgroup_ids = []
assert policy.save
refute policy.reload.hostgroups.include?(hostgroup)
refute_includes policy.reload.hostgroups, hostgroup
end

test "should add and remove inherited OVAL policy" do
Expand All @@ -122,12 +122,12 @@ class OvalPolicyTest < ActiveSupport::TestCase
assert policy_1.save
assert policy_2.save

assert host.reload.combined_oval_policies.include?(policy_1)
assert host.combined_oval_policies.include?(policy_2)
assert_includes host.reload.combined_oval_policies, policy_1
assert_includes host.combined_oval_policies, policy_2

policy_1.hostgroup_ids = []
assert policy_1.save
refute host.reload.combined_oval_policies.include?(policy_1)
assert host.combined_oval_policies.include?(policy_2)
refute_includes host.reload.combined_oval_policies, policy_1
assert_includes host.combined_oval_policies, policy_2
end
end
Loading

0 comments on commit c493dd7

Please sign in to comment.