Passed
Pull Request — rhel8-branch (#148)
by Matěj
01:16
created

test_content_handling   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 66
dl 0
loc 102
rs 10
c 0
b 0
f 0

11 Functions

Rating   Name   Duplication   Size   Complexity  
A ds_handler() 0 3 1
A test_get_profiles() 0 8 1
A test_get_data_streams() 0 2 1
A test_get_checklists() 0 5 1
A test_get_data_streams_checklists() 0 5 1
A test_identify_files() 0 12 1
A test_get_checklists_invalid() 0 4 2
A test_init_not_scap_content() 0 4 2
A test_init_xccdf_content() 0 4 2
A test_get_profiles_with_default() 0 8 1
A test_init_invalid_file_path() 0 4 2
1
import os
2
import glob
3
4
import pytest
5
6
from org_fedora_oscap import content_handling as ch
7
8
9
TESTING_FILES_PATH = os.path.join(
10
    os.path.dirname(__file__), os.path.pardir, "testing_files")
11
DS_FILEPATH = os.path.join(
12
    TESTING_FILES_PATH, "testing_ds.xml")
13
14
DS_IDS = "scap_org.open-scap_datastream_tst"
15
CHK_FIRST_ID = "scap_org.open-scap_cref_first-xccdf.xml"
16
CHK_SECOND_ID = "scap_org.open-scap_cref_second-xccdf.xml"
17
18
PROFILE1_ID = "xccdf_com.example_profile_my_profile"
19
PROFILE2_ID = "xccdf_com.example_profile_my_profile2"
20
PROFILE3_ID = "xccdf_com.example_profile_my_profile3"
21
22
23
@pytest.fixture()
24
def ds_handler():
25
    return ch.DataStreamHandler(DS_FILEPATH)
26
27
28
def test_init_invalid_file_path():
29
    with pytest.raises(ch.DataStreamHandlingError) as excinfo:
30
        ch.DataStreamHandler("testing_ds.xmlll")
31
    assert "Invalid file path" in str(excinfo.value)
32
33
34
def test_init_not_scap_content():
35
    with pytest.raises(ch.DataStreamHandlingError) as excinfo:
36
        ch.DataStreamHandler(os.path.join(TESTING_FILES_PATH, "testing_ks.cfg"))
37
    assert "not a valid SCAP content file" in str(excinfo.value)
38
39
40
def test_init_xccdf_content():
41
    with pytest.raises(ch.DataStreamHandlingError) as excinfo:
42
        ch.DataStreamHandler(os.path.join(TESTING_FILES_PATH, "xccdf.xml"))
43
    assert "not a data stream collection" in str(excinfo.value)
44
45
46
def test_get_data_streams(ds_handler):
47
    assert DS_IDS in ds_handler.get_data_streams()
48
49
50
def test_get_data_streams_checklists(ds_handler):
51
    expected_ids = {DS_IDS: [CHK_FIRST_ID, CHK_SECOND_ID]}
52
53
    ds_ids = ds_handler.get_data_streams_checklists()
54
    assert expected_ids == ds_ids
55
56
57
def test_get_checklists(ds_handler):
58
    expected_checklists = [CHK_FIRST_ID, CHK_SECOND_ID]
59
60
    chk_ids = ds_handler.get_checklists(DS_IDS)
61
    assert expected_checklists == chk_ids
62
63
64
def test_get_checklists_invalid(ds_handler):
65
    with pytest.raises(ch.DataStreamHandlingError) as excinfo:
66
        ds_handler.get_checklists("invalid.id")
67
        assert "Invalid data stream id given" in str(excinfo.value)
68
69
70
def test_get_profiles(ds_handler):
71
    profile_ids = ds_handler.get_profiles(DS_IDS, CHK_FIRST_ID)
72
73
    # When Benchmark doesn't contain Rules selected by default
74
    # the default Profile should not be present
75
    assert 2 == len(profile_ids)
76
    assert PROFILE1_ID == profile_ids[0].id
77
    assert PROFILE2_ID == profile_ids[1].id
78
79
80
def test_get_profiles_with_default(ds_handler):
81
    profile_ids = ds_handler.get_profiles(DS_IDS, CHK_SECOND_ID)
82
83
    # When Benchmark contains Rules selected by default
84
    # the default Profile should be present
85
    assert 2 == len(profile_ids)
86
    assert "default" == profile_ids[0].id
87
    assert PROFILE3_ID == profile_ids[1].id
88
89
90
def test_identify_files():
91
    filenames = glob.glob(TESTING_FILES_PATH + "/*")
92
    identified = ch.identify_files(filenames)
93
    assert identified[DS_FILEPATH] == ch.CONTENT_TYPES["DATASTREAM"]
94
    assert identified[
95
        os.path.join(TESTING_FILES_PATH, "scap-mycheck-oval.xml")] == ch.CONTENT_TYPES["OVAL"]
96
    assert identified[
97
        os.path.join(TESTING_FILES_PATH, "tailoring.xml")] == ch.CONTENT_TYPES["TAILORING"]
98
    assert identified[
99
        os.path.join(TESTING_FILES_PATH, "testing_xccdf.xml")] == ch.CONTENT_TYPES["XCCDF_CHECKLIST"]
100
    assert identified[
101
        os.path.join(TESTING_FILES_PATH, "cpe-dict.xml")] == ch.CONTENT_TYPES["CPE_DICT"]
102