Passed
Push — master ( f92a60...087b33 )
by Matěj
02:24 queued 11s
created

test_client.test_search_not_selected_rule()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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