Skip to content

Commit bfafa57

Browse files
committed
Moved relevant tests fro compliance-backend
1 parent 7696329 commit bfafa57

9 files changed

Lines changed: 100844 additions & 12 deletions

File tree

lib/openscap_parser.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,5 @@ def rule_results
4040
rule_result_oscap
4141
end
4242
end
43-
private
44-
45-
def test_result_node
46-
@test_result_node ||= @report_xml.at_css('TestResult')
47-
end
4843
end
4944
end

lib/openscap_parser/xml_report.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ module XMLReport
88
def self.included(base)
99
base.class_eval do
1010
def host
11-
@metadata&.dig('fqdn') || report_xml.search('target').text
11+
@report_xml.search('target').text
1212
end
1313

1414
def description
15-
report_xml.search('description').first.text
15+
@report_xml.search('description').first.text
1616
end
1717

1818
def report_xml(report_contents = '')

openscap_parser.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ Gem::Specification.new do |spec|
4040
spec.add_development_dependency "bundler", "~> 2.0"
4141
spec.add_development_dependency "rake", "~> 10.0"
4242
spec.add_development_dependency "minitest", "~> 5.0"
43+
spec.add_development_dependency "shoulda-context"
4344
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class ProfilesTest < Minitest::Test
6+
def test_result
7+
OpenStruct.new(id: ['xccdf_org.ssgproject.content_profile_standard'])
8+
end
9+
10+
def report_description
11+
'description'
12+
end
13+
14+
include OpenscapParser::Profiles
15+
include OpenscapParser::XMLReport
16+
17+
def setup
18+
report_xml(file_fixture('xccdf_report.xml').read)
19+
end
20+
21+
test 'profiles' do
22+
expected = {
23+
'xccdf_org.ssgproject.content_profile_standard' => \
24+
'Standard System Security Profile for Fedora'
25+
}
26+
assert_equal expected, profiles
27+
end
28+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class XMLReportTest < Minitest::Test
6+
include OpenscapParser::XMLReport
7+
8+
def setup
9+
report_xml(file_fixture('xccdf_report.xml').read)
10+
end
11+
12+
test 'report_xml parses the XML report' do
13+
assert_equal @report_xml.class, Nokogiri::XML::Document
14+
end
15+
16+
test 'report_description' do
17+
assert_match(/^This guide presents/, description)
18+
end
19+
20+
test 'report_host' do
21+
assert_match host, 'lenovolobato.lobatolan.home'
22+
end
23+
end

test/fixtures/files/rhel-xccdf-report.xml

Lines changed: 73298 additions & 0 deletions
Large diffs are not rendered by default.

test/fixtures/files/xccdf_report.xml

Lines changed: 27415 additions & 0 deletions
Large diffs are not rendered by default.

test/openscap_parser_test.rb

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
1-
require "test_helper"
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
24

35
class OpenscapParserTest < Minitest::Test
4-
def test_that_it_has_a_version_number
5-
refute_nil ::OpenscapParser::VERSION
6+
def setup
7+
fake_report = file_fixture('xccdf_report.xml').read
8+
@profile = {
9+
'xccdf_org.ssgproject.content_profile_standard' =>
10+
'Standard System Security Profile for Fedora'
11+
}
12+
@report_parser = ::OpenscapParser::Base.new(fake_report)
613
end
714

8-
def test_it_does_something_useful
9-
assert false
15+
context 'profile' do
16+
should 'be able to parse it' do
17+
assert_equal(@profile, @report_parser.profiles)
18+
end
19+
20+
should 'not save more than one profile when there are no test results' do
21+
fake_report = file_fixture('rhel-xccdf-report.xml').read
22+
@profile = {
23+
'xccdf_org.ssgproject.content_profile_rht-ccp' =>
24+
'Red Hat Corporate Profile for Certified Cloud Providers (RH CCP)'
25+
}
26+
@report_parser = ::OpenscapParser::Base.new(fake_report)
27+
assert_equal 1, @report_parser.profiles.count
28+
end
29+
end
30+
31+
context 'host' do
32+
should 'be able to parse host name' do
33+
assert_equal 'lenovolobato.lobatolan.home', @report_parser.host
34+
end
35+
end
36+
37+
test 'score can be parsed' do
38+
assert_equal(16.220238, @report_parser.score)
39+
end
40+
41+
context 'rules' do
42+
setup do
43+
@arbitrary_rules = [
44+
# rubocop:disable Metrics/LineLength
45+
'xccdf_org.ssgproject.content_rule_dir_perms_world_writable_system_owned',
46+
'xccdf_org.ssgproject.content_rule_bios_enable_execution_restrictions',
47+
'xccdf_org.ssgproject.content_rule_gconf_gnome_screensaver_lock_enabled',
48+
'xccdf_org.ssgproject.content_rule_selinux_all_devicefiles_labeled'
49+
# rubocop:enable Metrics/LineLength
50+
]
51+
end
52+
53+
should 'list all rules' do
54+
assert_empty(@arbitrary_rules - @report_parser.rule_ids)
55+
end
1056
end
1157
end

test/test_helper.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,29 @@
22
require "openscap_parser"
33

44
require "minitest/autorun"
5+
require 'shoulda-context'
6+
7+
def test(name, &block)
8+
test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
9+
defined = method_defined? test_name
10+
raise "#{test_name} is already defined in #{self}" if defined
11+
if block_given?
12+
define_method(test_name, &block)
13+
else
14+
define_method(test_name) do
15+
flunk "No implementation provided for #{name}"
16+
end
17+
end
18+
end
19+
20+
def file_fixture(fixture_name)
21+
file_fixture_path = './test/fixtures/files'
22+
path = Pathname.new(File.join(file_fixture_path, fixture_name))
23+
24+
if path.exist?
25+
path
26+
else
27+
msg = "the directory '%s' does not contain a file named '%s'"
28+
raise ArgumentError, msg % [file_fixture_path, fixture_name]
29+
end
30+
end

0 commit comments

Comments
 (0)