Passed
Pull Request — master (#24)
by Jan
02:27
created

test_client.test_find_not_selected_rule()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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