Passed
Push — master ( d3cca2...6c802e )
by Matěj
03:03 queued 12s
created

test_client.test_if_not_installed_inquirer()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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