Passed
Push — master ( 24a582...ab5396 )
by Jan
05:47 queued 03:29
created

test_client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 96
dl 0
loc 129
rs 10
c 0
b 0
f 0

13 Functions

Rating   Name   Duplication   Size   Complexity  
A get_client() 0 3 1
A get_client_on_web_browser() 0 3 1
A test_search_rules_with_regex() 0 5 1
A test_get_questions() 0 9 1
A test_search_rules_id_on_web_browser() 0 5 1
A test_client() 0 6 1
A test_find_not_selected_rule() 0 6 2
A test_search_rules_id() 0 5 1
A test_get_wanted_rules_from_array_of_IDs() 0 12 1
A test_get_wanted_not_selected_rules_from_array_of_IDs() 0 15 1
A test_search_non_existent_rule() 0 6 2
A test_search_not_selected_rule() 0 5 2
A test_if_not_installed_inquirer() 0 11 2
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_search_non_existent_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='404 rule "{}" not found!'.format(rule)):
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_from_array_of_IDs():
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
        'xccdf_org.ssgproject.content_rule_package_nis_removed',
87
        'xccdf_org.ssgproject.content_rule_package_ntpdate_removed',
88
        'xccdf_org.ssgproject.content_rule_package_telnetd_removed',
89
        'xccdf_org.ssgproject.content_rule_package_gdm_removed',
90
        'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed',
91
        'xccdf_org.ssgproject.content_rule_package_mcstrans_removed']
92
93
    assert out == client._get_wanted_rules_from_array_of_IDs(
94
        client.xml_parser.notselected_rules)
95
96
97
def test_get_wanted_rules_from_array_of_IDs():
98
    src = 'test_data/ssg-fedora-ds-arf.xml'
99
    regex = r'_package_\w+_removed'
100
    client = get_client(src, regex)
101
102
    out = [
103
        'xccdf_org.ssgproject.content_rule_package_abrt_removed',
104
        'xccdf_org.ssgproject.content_rule_package_sendmail_removed',
105
    ]
106
107
    assert out == client._get_wanted_rules_from_array_of_IDs(
108
        client.xml_parser.used_rules.keys())
109
110
111
def test_search_not_selected_rule():
112
    src = 'test_data/ssg-fedora-ds-arf.xml'
113
    non_existent_rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
114
    with pytest.raises(Exception, match=non_existent_rule):
115
        assert get_client(src, non_existent_rule).search_rules_id()
116
117
118
def test_if_not_installed_inquirer(capsys):
119
    with mock.patch.dict(sys.modules, {'inquirer': None}):
120
        src = 'test_data/ssg-fedora-ds-arf.xml'
121
        regex = r'_package_\w+_removed'
122
        client = get_client(src, regex)
123
        client.isatty = True
124
        out = client.run_gui_and_return_answers()
125
        assert out is None
126
        captured = capsys.readouterr()
127
        assert captured.out == (
128
            '== The Rule IDs ==\n'
129
            "'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b'\n"
130
            "'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b'\n"
131
            "You haven't got installed inquirer lib. Please copy id rule with you"
132
            " want use and put it in command\n")
133