Passed
Push — master ( c4e6f9...dffb5c )
by Matěj
01:34 queued 11s
created

test_client.test_get_wanted_rules()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nop 0
dl 0
loc 16
rs 9.7
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 test_client():
25
    src = 'test_data/ssg-fedora-ds-arf.xml'
26
    rule = 'rule'
27
    client = get_client(src, rule)
28
    assert client.source_filename == tests.any_test_help.get_src(src)
29
    assert client.rule_name == rule
30
31
32
def test_search_rules_id():
33
    src = 'test_data/ssg-fedora-ds-arf.xml'
34
    part_of_id_rule = 'xccdf_org.ssgproject.'
35
    client = get_client(src, part_of_id_rule)
36
    assert len(client.search_rules_id()) == 184
37
38
39
def test_search_rules_id_on_web_browser():
40
    src = 'test_data/ssg-fedora-ds-arf.xml'
41
    part_of_id_rule = 'xccdf_org.ssgproject.'
42
    client = get_client_on_web_browser(src, part_of_id_rule)
43
    assert len(client.search_rules_id()) == 184
44
45
46
def test_find_does_not_exist_rule():
47
    rule = 'random_rule_which_doest_exist'
48
    src = 'test_data/ssg-fedora-ds-arf.xml'
49
    client = get_client(src, rule)
50
    with pytest.raises(Exception, match="err- 404 rule not found!"):
51
        assert client.search_rules_id()
52
53
54
def test_find_not_selected_rule():
55
    rule = 'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server'
56
    src = 'test_data/ssg-fedora-ds-arf.xml'
57
    client = get_client(src, rule)
58
    with pytest.raises(Exception, match=rule):
59
        assert client.search_rules_id()
60
61
62
def test_search_rules_with_regex():
63
    src = 'test_data/ssg-fedora-ds-arf.xml'
64
    regex = r'_package_\w+_removed'
65
    client = get_client(src, regex)
66
    assert len(client.search_rules_id()) == 2
67
68
69
def test_get_questions():
70
    src = 'test_data/ssg-fedora-ds-arf.xml'
71
    regex = r'_package_\w+_removed'
72
    client = get_client(src, regex)
73
    out = client.get_questions()[0].choices
74
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
75
    rule2 = 'xccdf_org.ssgproject.content_rule_package_sendmail_removed'
76
    assert out[0] == rule1
77
    assert out[1] == rule2
78
79
80
def test_get_wanted_not_selected_rules():
81
    src = 'test_data/ssg-fedora-ds-arf.xml'
82
    regex = r'_package_\w+_removed'
83
    client = get_client(src, regex)
84
85
    out = [
86
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_nis_removed'},
87
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_ntpdate_removed'},
88
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_telnetd_removed'},
89
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_gdm_removed'},
90
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed'},
91
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_mcstrans_removed'}]
92
93
    assert out == client._get_wanted_not_selected_rules()
94
95
96
def test_get_wanted_rules():
97
    src = 'test_data/ssg-fedora-ds-arf.xml'
98
    regex = r'_package_\w+_removed'
99
    client = get_client(src, regex)
100
101
    out = [
102
        {'href': '#oval0',
103
         'id_def': 'oval:ssg-package_abrt_removed:def:1',
104
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_abrt_removed',
105
         'result': 'fail'},
106
        {'href': '#oval0',
107
         'id_def': 'oval:ssg-package_sendmail_removed:def:1',
108
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_sendmail_removed',
109
         'result': 'pass'}]
110
111
    assert out == client._get_wanted_rules()
112
113
114
def test_search_non_existent_rule():
115
    src = 'test_data/ssg-fedora-ds-arf.xml'
116
    non_existent_rule = 'non-existent_rule'
117
    with pytest.raises(Exception, match="err- 404 rule not found!"):
118
        assert get_client(src, non_existent_rule).search_rules_id()
119
120
121
def test_search_not_selected_rule():
122
    src = 'test_data/ssg-fedora-ds-arf.xml'
123
    non_existent_rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
124
    with pytest.raises(Exception, match=non_existent_rule):
125
        assert get_client(src, non_existent_rule).search_rules_id()
126
127
128
def test_if_not_installed_inquirer(capsys):
129
    with mock.patch.dict(sys.modules, {'inquirer': None}):
130
        src = 'test_data/ssg-fedora-ds-arf.xml'
131
        regex = r'_package_\w+_removed'
132
        client = get_client(src, regex)
133
        client.isatty = True
134
        out = client.run_gui_and_return_answers()
135
        assert out is None
136
        captured = capsys.readouterr()
137
        assert captured.out == (
138
            '== The Rule IDs ==\n'
139
            "'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b'\n"
140
            "'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b'\n"
141
            "You haven't got installed inquirer lib. Please copy id rule with you"
142
            " want use and put it in command\n")
143