Passed
Push — master ( 073977...6a9e01 )
by Matěj
03:16 queued 12s
created

test_client.test_prepare_tree()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 0
dl 0
loc 11
rs 9.85
c 0
b 0
f 0
1
import oval_graph.client
2
import pytest
3
import tests.any_test_help
4
import re
5
import json
6
import mock
7
import sys
8
import os
9
import tempfile
10
11
12
def get_client(src, rule):
13
    return oval_graph.client.client(
14
        ["--off-web-browser", tests.any_test_help.get_src(src), rule])
15
16
17
def get_client_on_web_browser(src, rule):
18
    return oval_graph.client.client(
19
        [tests.any_test_help.get_src(src), rule])
20
21
22
def get_client_tree(src, rule):
23
    return oval_graph.client.client(
24
        ["--tree", "--off-web-browser", tests.any_test_help.get_src(src), rule])
25
26
27
def get_client_with_option_show_fail_rules(src, rule):
28
    return oval_graph.client.client(
29
        ["--show-fail-rules", "--off-web-browser", tests.any_test_help.get_src(src), rule])
30
31
32
def get_client_with_option_show_not_selected_rules(src, rule):
33
    return oval_graph.client.client(
34
        ["--show-not-selected-rules", "--off-web-browser", tests.any_test_help.get_src(src), rule])
35
36
37
def get_client_with_option_show_not_selected_rules_and_show_fail_rules(
38
        src,
39
        rule):
40
    return oval_graph.client.client(["--show-not-selected-rules",
41
                                     "--show-fail-rules",
42
                                     "--off-web-browser",
43
                                     tests.any_test_help.get_src(src),
44
                                     rule])
45
46
47
def test_client():
48
    src = 'test_data/ssg-fedora-ds-arf.xml'
49
    rule = 'rule'
50
    client = get_client(src, rule)
51
    assert client.source_filename == tests.any_test_help.get_src(src)
52
    assert client.rule_name == rule
53
54
55
def test_search_rules_id():
56
    src = 'test_data/ssg-fedora-ds-arf.xml'
57
    part_of_id_rule = 'xccdf_org.ssgproject.'
58
    client = get_client(src, part_of_id_rule)
59
    assert len(client.search_rules_id()) == 184
60
61
62
def test_search_rules_id_on_web_browser():
63
    src = 'test_data/ssg-fedora-ds-arf.xml'
64
    part_of_id_rule = 'xccdf_org.ssgproject.'
65
    client = get_client_on_web_browser(src, part_of_id_rule)
66
    assert len(client.search_rules_id()) == 184
67
68
69
def test_find_does_not_exist_rule():
70
    rule = 'random_rule_which_doest_exist'
71
    src = 'test_data/ssg-fedora-ds-arf.xml'
72
    client = get_client(src, rule)
73
    with pytest.raises(Exception, match="err- 404 rule not found!"):
74
        assert client.search_rules_id()
75
76
77
def test_find_not_selected_rule():
78
    rule = 'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server'
79
    src = 'test_data/ssg-fedora-ds-arf.xml'
80
    client = get_client(src, rule)
81
    with pytest.raises(Exception, match=rule):
82
        assert client.search_rules_id()
83
84
85
def test_search_rules_with_regex():
86
    src = 'test_data/ssg-fedora-ds-arf.xml'
87
    regex = r'_package_\w+_removed'
88
    client = get_client(src, regex)
89
    assert len(client.search_rules_id()) == 2
90
91
92
def test_get_questions():
93
    src = 'test_data/ssg-fedora-ds-arf.xml'
94
    regex = r'_package_\w+_removed'
95
    client = get_client(src, regex)
96
    out = client.get_questions()[0].choices
97
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
98
    rule2 = 'xccdf_org.ssgproject.content_rule_package_sendmail_removed'
99
    assert out[0] == rule1
100
    assert out[1] == rule2
101
102
103
def test_get_questions_not_selected(capsys):
104
    src = 'test_data/ssg-fedora-ds-arf.xml'
105
    regex = r'_package_\w+_removed'
106
    client = get_client_with_option_show_not_selected_rules(src, regex)
107
    out = client.get_questions()[0].choices
108
    outResult = [
109
        'xccdf_org.ssgproject.content_rule_package_abrt_removed',
110
        'xccdf_org.ssgproject.content_rule_package_sendmail_removed']
111
    assert out == outResult
112
    captured = capsys.readouterr()
113
    assert captured.out == (
114
        '== The not selected rule IDs ==\n'
115
        'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
116
        'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
117
        'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
118
        'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
119
        'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
120
        'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
121
122
123
def test_get_questions_not_selected_and_show_fail_rules(capsys):
124
    src = 'test_data/ssg-fedora-ds-arf.xml'
125
    regex = r'_package_\w+_removed'
126
    client = get_client_with_option_show_not_selected_rules_and_show_fail_rules(
127
        src, regex)
128
    out = client.get_questions()[0].choices
129
    outResult = ['xccdf_org.ssgproject.content_rule_package_abrt_removed']
130
    assert out == outResult
131
    assert len(out) == 1
132
    captured = capsys.readouterr()
133
    print(captured.out)
134
    assert captured.out == (
135
        '== The not selected rule IDs ==\n'
136
        'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
137
        'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
138
        'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
139
        'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
140
        'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
141
        'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
142
143
144
def test_get_questions_with_option_show_fail_rules():
145
    src = 'test_data/ssg-fedora-ds-arf.xml'
146
    regex = r'_package_\w+_removed'
147
    client = get_client_with_option_show_fail_rules(src, regex)
148
    out = client.get_questions()[0].choices
149
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
150
    assert out[0] == rule1
151
    with pytest.raises(Exception, match="list index out of range"):
152
        assert out[2] is None
153
154
155
def test_get_wanted_not_selected_rules():
156
    src = 'test_data/ssg-fedora-ds-arf.xml'
157
    regex = r'_package_\w+_removed'
158
    client = get_client(src, regex)
159
160
    out = [
161
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_nis_removed'},
162
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_ntpdate_removed'},
163
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_telnetd_removed'},
164
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_gdm_removed'},
165
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed'},
166
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_mcstrans_removed'}]
167
168
    assert out == client._get_wanted_not_selected_rules()
169
170
171
def test_get_wanted_rules():
172
    src = 'test_data/ssg-fedora-ds-arf.xml'
173
    regex = r'_package_\w+_removed'
174
    client = get_client(src, regex)
175
176
    out = [
177
        {'href': '#oval0',
178
         'id_def': 'oval:ssg-package_abrt_removed:def:1',
179
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_abrt_removed',
180
         'result': 'fail'},
181
        {'href': '#oval0',
182
         'id_def': 'oval:ssg-package_sendmail_removed:def:1',
183
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_sendmail_removed',
184
         'result': 'pass'}]
185
186
    assert out == client._get_wanted_rules()
187
188
189
def test_search_non_existent_rule():
190
    src = 'test_data/ssg-fedora-ds-arf.xml'
191
    non_existent_rule = 'non-existent_rule'
192
    with pytest.raises(Exception, match="err- 404 rule not found!"):
193
        assert get_client(src, non_existent_rule).search_rules_id()
194
195
196
def test_search_not_selected_rule():
197
    src = 'test_data/ssg-fedora-ds-arf.xml'
198
    non_existent_rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
199
    with pytest.raises(Exception, match=non_existent_rule):
200
        assert get_client(src, non_existent_rule).search_rules_id()
201
202
203
def test_prepare_graph():
204
    src = 'test_data/ssg-fedora-ds-arf.xml'
205
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
206
    client = get_client(src, rule)
207
    rules = {'rules': [rule]}
208
    results_src = client.prepare_data(rules)
209
    result = load_tested_file(
210
        os.path.join(results_src[0], 'data.js'))
211
    referenc_result = load_tested_file(
212
        'test_data/referenc_result_data_graph.js')
213
    assert result == referenc_result
214
215
216
def test_prepare_tree():
217
    src = 'test_data/ssg-fedora-ds-arf.xml'
218
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
219
    client = get_client_tree(src, rule)
220
    rules = {'rules': [rule]}
221
    results_src = client.prepare_data(rules)
222
    result = load_tested_file(
223
        os.path.join(results_src[0], 'data.js'))
224
    referenc_result = load_tested_file(
225
        'test_data/referenc_result_data_tree.js')
226
    assert result == referenc_result
227
228
229
def load_tested_file(src):
230
    if '/tmp' in src:
231
        with open(src, 'r') as f:
232
            data = f.readlines()
233
    else:
234
        with open(tests.any_test_help.get_src(src), 'r') as f:
235
            data = f.readlines()
236
    out = []
237
    edge = False
238
    for row in data:
239
        if row == '    "edges": [\n':
240
            edge = True
241
        if not edge:
242
            out.append(row)
243
    return out
244
245
246
def try_expection_for_prepare_graph(src, rule, err):
247
    client = get_client(src, rule)
248
    rules = {'rules': [rule]}
249
    with pytest.raises(Exception, match=err):
250
        assert client.prepare_data(rules)
251
252
253
def test_prepare_graph_with_non_existent_rule():
254
    src = 'test_data/ssg-fedora-ds-arf.xml'
255
    rule = 'non-existent_rule'
256
    try_expection_for_prepare_graph(src, rule, '404')
257
258
259
def test_prepare_graph_with_not_selected_rule():
260
    src = 'test_data/ssg-fedora-ds-arf.xml'
261
    rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
262
    try_expection_for_prepare_graph(src, rule, 'not selected')
263
264
265
def test_if_not_installed_inquirer(capsys):
266
    with mock.patch.dict(sys.modules, {'inquirer': None}):
267
        src = 'test_data/ssg-fedora-ds-arf.xml'
268
        regex = r'_package_\w+_removed'
269
        client = get_client(src, regex)
270
        out = client.run_gui_and_return_answers()
271
        assert out is None
272
        captured = capsys.readouterr()
273
        assert captured.out == (
274
            '== The Rule IDs ==\n'
275
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
276
            'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b\n')
277
278
279
def test_if_not_installed_inquirer_with_option_show_fail_rules(capsys):
280
    with mock.patch.dict(sys.modules, {'inquirer': None}):
281
        src = 'test_data/ssg-fedora-ds-arf.xml'
282
        regex = r'_package_\w+_removed'
283
        client = get_client_with_option_show_fail_rules(src, regex)
284
        out = client.run_gui_and_return_answers()
285
        assert out is None
286
        captured = capsys.readouterr()
287
        assert captured.out == (
288
            '== The Rule IDs ==\n'
289
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n')
290
291
292
def test_if_not_installed_inquirer_with_option_show_not_selected_rules(
293
        capsys):
294
    with mock.patch.dict(sys.modules, {'inquirer': None}):
295
        src = 'test_data/ssg-fedora-ds-arf.xml'
296
        regex = r'_package_\w+_removed'
297
        client = get_client_with_option_show_not_selected_rules(src, regex)
298
        out = client.run_gui_and_return_answers()
299
        assert out is None
300
        captured = capsys.readouterr()
301
        assert captured.out == (
302
            '== The Rule IDs ==\n'
303
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
304
            'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b\n'
305
            '== The not selected rule IDs ==\n'
306
            'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
307
            'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
308
            'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
309
            'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
310
            'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
311
            'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
312
313
314
def test_if_not_installed_inquirer_with_option_show_not_selected_rules_and_show_fail_rules(
315
        capsys):
316
    with mock.patch.dict(sys.modules, {'inquirer': None}):
317
        src = 'test_data/ssg-fedora-ds-arf.xml'
318
        regex = r'_package_\w+_removed'
319
        client = get_client_with_option_show_not_selected_rules_and_show_fail_rules(
320
            src, regex)
321
        out = client.run_gui_and_return_answers()
322
        assert out is None
323
        captured = capsys.readouterr()
324
        assert captured.out == (
325
            '== The Rule IDs ==\n'
326
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
327
            '== The not selected rule IDs ==\n'
328
            'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
329
            'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
330
            'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
331
            'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
332
            'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
333
            'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
334