Passed
Pull Request — master (#25)
by Jan
04:15
created

test_client.test_prepare_tree()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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