Passed
Pull Request — master (#70)
by Jan
03:58
created

test_arf_to_json   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 101
dl 0
loc 151
rs 10
c 0
b 0
f 0

11 Functions

Rating   Name   Duplication   Size   Complexity  
A try_expection_for_prepare_graph() 0 5 2
A test_prepare_json() 0 15 1
A get_client_arf_to_json_with_define_dest() 0 6 1
A test_if_file_has_content() 0 6 1
A get_client_arf_to_json() 0 3 1
A test_creation_json_with_two_more_rule() 0 19 2
A test_prepare_graph_with_non_existent_rule() 0 4 1
A test_prepare_graph_with_not_selected_rule() 0 4 1
A test_is_empty_file() 0 6 1
A test_prepare_json_and_save_in_defined_destination() 0 7 1
A test_creation_json_two_selected_rules() 0 17 2
1
import pytest
2
import tempfile
3
import os
4
import uuid
5
import json
6
from datetime import datetime
7
import time
8
9
from oval_graph.arf_to_json import ArfToJson
10
import tests.any_test_help
11
12
13
def get_client_arf_to_json(src, rule):
14
    return ArfToJson(
15
        [tests.any_test_help.get_src(src), rule])
16
17
18
def get_client_arf_to_json_with_define_dest(src, rule):
19
    return ArfToJson(["--output",
20
                      tests.any_test_help.get_src(os.path.join(tempfile.gettempdir(),
21
                                                               str(uuid.uuid4()) + ".json")),
22
                      tests.any_test_help.get_src(src),
23
                      rule])
24
25
26
def try_expection_for_prepare_graph(src, rule, err):
27
    client = get_client_arf_to_json(src, rule)
28
    rules = {'rules': [rule]}
29
    with pytest.raises(Exception, match=err):
30
        assert client.prepare_data(rules)
31
32
33
def test_prepare_graph_with_non_existent_rule():
34
    src = 'test_data/ssg-fedora-ds-arf.xml'
35
    rule = 'non-existent_rule'
36
    try_expection_for_prepare_graph(src, rule, '404')
37
38
39
def test_prepare_graph_with_not_selected_rule():
40
    src = 'test_data/ssg-fedora-ds-arf.xml'
41
    rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
42
    try_expection_for_prepare_graph(src, rule, 'not selected')
43
44
45
def test_prepare_json(capsys):
46
    src = 'test_data/ssg-fedora-ds-arf.xml'
47
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
48
    client = get_client_arf_to_json(src, rule)
49
    rules = {'rules': [rule]}
50
    date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S"))
51
    results_src = client.prepare_data(rules)
52
    assert not results_src
53
    captured = capsys.readouterr()
54
    print(repr(captured.out))
55
    assert captured.out == (
56
        '{\n'
57
        '    "graph-of-xccdf_org.ssgproject.content_rule_package_abrt_removed' +
58
        date +
59
        '": {\n'
60
        '        "node_id": "xccdf_org.ssgproject.content_rule_package_abrt_removed",\n'
61
        '        "type": "operator",\n'
62
        '        "value": "and",\n'
63
        '        "negation": false,\n'
64
        '        "comment": "Package abrt Removed",\n'
65
        '        "child": [\n'
66
        '            {\n'
67
        '                "node_id": "oval:ssg-package_abrt_removed:def:1",\n'
68
        '                "type": "operator",\n'
69
        '                "value": "and",\n'
70
        '                "negation": false,\n'
71
        '                "comment": null,\n'
72
        '                "child": [\n'
73
        '                    {\n'
74
        '                        "node_id": "oval:ssg-test_package_abrt_removed:tst:1",\n'
75
        '                        "type": "value",\n'
76
        '                        "value": "false",\n'
77
        '                        "negation": false,\n'
78
        '                        "comment": "package abrt is removed",\n'
79
        '                        "child": null\n'
80
        '                    }\n'
81
        '                ]\n'
82
        '            }\n'
83
        '        ]\n'
84
        '    }\n'
85
        '}\n')
86
87
88
def test_prepare_json_and_save_in_defined_destination():
89
    src = 'test_data/ssg-fedora-ds-arf.xml'
90
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
91
    client = get_client_arf_to_json_with_define_dest(src, rule)
92
    rules = {'rules': [rule]}
93
    results_src = client.prepare_data(rules)
94
    tests.any_test_help.compare_results_json(results_src[0])
95
96
97
def test_is_empty_file():
98
    src = 'test_data/ssg-fedora-ds-arf.xml'
99
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
100
    client = get_client_arf_to_json_with_define_dest(src, rule)
101
    empty_file_src = tests.any_test_help.get_src('test_data/empty_file.json')
102
    assert client.file_is_empty(empty_file_src)
103
104
105
def test_if_file_has_content():
106
    src = 'test_data/ssg-fedora-ds-arf.xml'
107
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
108
    client = get_client_arf_to_json_with_define_dest(src, rule)
109
    file_src = tests.any_test_help.get_src('test_data/JsTree_json0.json')
110
    assert not client.file_is_empty(file_src)
111
112
113
def test_creation_json_with_two_more_rule():
114
    src = 'test_data/ssg-fedora-ds-arf.xml'
115
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
116
    client = get_client_arf_to_json_with_define_dest(src, rule)
117
    rules = {'rules': [rule]}
118
    result_src = client.prepare_data(rules)
119
    time.sleep(1)  # wait for change time in saved rule name
120
    result_src_second_rule = client.prepare_data(rules)
121
    assert result_src == result_src_second_rule
122
    data = None
123
    with open(result_src[-1], 'r') as f:
124
        data = json.load(f)
125
    rules_id = list(data.keys())
126
    assert len(rules_id) == 2
127
    assert data[rules_id[0]] == data[rules_id[1]]
128
    referenc_result = tests.any_test_help.any_get_test_data_json(
129
        'test_data/referenc_result_data_json.json')
130
    assert referenc_result[
131
        "xccdf_org.ssgproject.content_rule_package_abrt_removed"] == data[rules_id[0]]
132
133
134
def test_creation_json_two_selected_rules():
135
    src = 'test_data/ssg-fedora-ds-arf.xml'
136
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
137
    rule1 = 'xccdf_org.ssgproject.content_rule_disable_host_auth'
138
    client = get_client_arf_to_json_with_define_dest(src, rule)
139
    rules = {'rules': [rule, rule1]}
140
    result_src = client.prepare_data(rules)
141
    data = None
142
    with open(result_src[-1], 'r') as f:
143
        data = json.load(f)
144
    rules_id = list(data.keys())
145
    assert len(rules_id) == 2
146
    referenc_result = tests.any_test_help.any_get_test_data_json(
147
        'test_data/referenc_result_data_json.json')
148
    assert referenc_result[
149
        "xccdf_org.ssgproject.content_rule_package_abrt_removed"] == data[rules_id[0]]
150
    assert rule1 in rules_id[1]
151