test_config.py 7.2 KB
Newer Older
1
2
3
4
import sbws.util.config as con
from configparser import ConfigParser


Matt Traudt's avatar
Matt Traudt committed
5
class PseudoSection:
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    def __init__(self, key, value, mini=None, maxi=None):
        self.key = key
        self.value = value
        self.mini = mini
        self.maxi = maxi

    def getfloat(self, key):
        assert key == self.key, 'But in tests; key should exist'
        return float(self.value)

    def getint(self, key):
        assert key == self.key, 'But in tests; key should exist'
        return int(self.value)


def test_validate_fingerprint():
    fp_len = 40
    bads = [
        'A' * (fp_len - 1), 'A' * (fp_len + 1),
        '', 'A' * (1000000),
        'a' * fp_len, 'O' * fp_len
    ]
    goods = [
        'A' * fp_len,
        ''.join(list('0123456789ABCDEF' * 3)[0:fp_len])
    ]
    for fp in bads:
        d = {'': fp}
        valid, reason = con._validate_fingerprint(d, '')
        assert not valid, 'Fingerprint {} should not have passed '\
            'validation'.format(fp)
    for fp in goods:
        d = {'': fp}
        valid, reason = con._validate_fingerprint(d, '')
        assert valid, 'Fingerprint {} should have passed, but didn\'t '\
            'because {}'.format(fp, reason)


def test_validate_int_simple():
    bads = [
Matt Traudt's avatar
Matt Traudt committed
46
47
        PseudoSection('', 'NotAInt'),
        PseudoSection('', '-0.1'), PseudoSection('', '0.1'),
48
49
    ]
    goods = [
Matt Traudt's avatar
Matt Traudt committed
50
51
52
53
        PseudoSection('', '0'),
        PseudoSection('', '1'), PseudoSection('', '-1'),
        PseudoSection('', '100000000'), PseudoSection('', '-1000000000'),
        PseudoSection('', '+0'), PseudoSection('', '-0'),
54
55
56
57
58
59
60
61
62
63
64
65
66
    ]
    for sec in bads:
        valid, reason = con._validate_int(sec, sec.key)
        assert not valid, '{} should not have been a valid '\
            'int'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_int(sec, sec.key)
        assert valid, '{} should have been a valid int, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_float_simple():
    bads = [
Matt Traudt's avatar
Matt Traudt committed
67
        PseudoSection('', 'NotAFloat'),
68
69
    ]
    goods = [
Matt Traudt's avatar
Matt Traudt committed
70
71
72
73
74
        PseudoSection('', '0'),
        PseudoSection('', '1'), PseudoSection('', '-1'),
        PseudoSection('', '-0.1'), PseudoSection('', '0.1'),
        PseudoSection('', '100000000'), PseudoSection('', '-1000000000'),
        PseudoSection('', '+0'), PseudoSection('', '-0'),
75
76
77
78
79
80
81
82
83
84
85
86
87
    ]
    for sec in bads:
        valid, reason = con._validate_float(sec, sec.key)
        assert not valid, '{} should not have been a valid '\
            'float'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_float(sec, sec.key)
        assert valid, '{} should have been a valid float, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_int_min():
    goods = [
Matt Traudt's avatar
Matt Traudt committed
88
89
        PseudoSection('', '0', mini=0),
        PseudoSection('', '1', mini=1), PseudoSection('', '-1', mini=-1),
90
91
    ]
    bads = [
Matt Traudt's avatar
Matt Traudt committed
92
93
        PseudoSection('', '1', mini=2),
        PseudoSection('', '0', mini=1),
94
95
96
97
98
99
100
101
102
103
104
105
106
    ]
    for sec in bads:
        valid, reason = con._validate_int(sec, sec.key, minimum=sec.mini)
        assert not valid, '{} should not have been a valid '\
            'int'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_int(sec, sec.key, minimum=sec.mini)
        assert valid, '{} should have been a valid int, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_float_min():
    goods = [
Matt Traudt's avatar
Matt Traudt committed
107
108
109
110
        PseudoSection('', '0', mini=0.0),
        PseudoSection('', '0.1', mini=0.1),
        PseudoSection('', '-0.1', mini=-0.1),
        PseudoSection('', '0.1', mini=-0.1),
111
112
    ]
    bads = [
Matt Traudt's avatar
Matt Traudt committed
113
        PseudoSection('', '0.0999999999', mini=0.1),
114
115
116
117
118
119
120
121
122
123
124
125
126
    ]
    for sec in bads:
        valid, reason = con._validate_float(sec, sec.key, minimum=sec.mini)
        assert not valid, '{} should not have been a valid '\
            'float'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_float(sec, sec.key, minimum=sec.mini)
        assert valid, '{} should have been a valid float, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_int_max():
    goods = [
Matt Traudt's avatar
Matt Traudt committed
127
128
129
        PseudoSection('', '0', maxi=0),
        PseudoSection('', '1', maxi=1), PseudoSection('', '-1', maxi=-1),
        PseudoSection('', '-1', maxi=1),
130
131
    ]
    bads = [
Matt Traudt's avatar
Matt Traudt committed
132
133
        PseudoSection('', '2', maxi=1),
        PseudoSection('', '1', maxi=0),
134
135
136
137
138
139
140
141
142
143
144
145
146
    ]
    for sec in bads:
        valid, reason = con._validate_int(sec, sec.key, maximum=sec.maxi)
        assert not valid, '{} should not have been a valid '\
            'int'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_int(sec, sec.key, maximum=sec.maxi)
        assert valid, '{} should have been a valid int, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_float_max():
    goods = [
Matt Traudt's avatar
Matt Traudt committed
147
148
149
150
        PseudoSection('', '0', maxi=0.0),
        PseudoSection('', '0.1', maxi=0.1),
        PseudoSection('', '-0.1', maxi=-0.1),
        PseudoSection('', '-0.1', maxi=0.1),
151
152
    ]
    bads = [
Matt Traudt's avatar
Matt Traudt committed
153
        PseudoSection('', '0.10000000001', maxi=0.1),
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    ]
    for sec in bads:
        valid, reason = con._validate_float(sec, sec.key, maximum=sec.maxi)
        assert not valid, '{} should not have been a valid '\
            'float'.format(sec.value)
    for sec in goods:
        valid, reason = con._validate_float(sec, sec.key, maximum=sec.maxi)
        assert valid, '{} should have been a valid float, but '\
            'got: {}'.format(sec.value, reason)


def test_validate_bool():
    goods = [
        'on', 'True', 'true', 'yes',
        'off', 'False', 'false', 'no',
        '0', '1',
    ]
    bads = [
        'onn', 'offf',
        '2', '',
    ]
    for val in goods:
        conf = ConfigParser()
        conf.read_dict({'sec': {}})
        conf['sec']['key'] = val
        valid, reason = con._validate_boolean(conf['sec'], 'key')
        assert valid, '{} should have been a valid bool, but '\
            'got: {}'.format(val, reason)
    for val in bads:
        conf = ConfigParser()
        conf.read_dict({'sec': {}})
        conf['sec']['key'] = val
        valid, reason = con._validate_boolean(conf['sec'], 'key')
        assert not valid, '{} should not have been a valid '\
            'bool'.format(val)
Matt Traudt's avatar
Matt Traudt committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208


def test_validate_url():
    goods = [
        'http://example.com',
        'http://example.com/foo.bar',
    ]
    bads = [
        'ftp://example.com/foo.bar',
        'http://', 'http:///',
    ]
    for val in goods:
        d = {'': val}
        valid, reason = con._validate_url(d, '')
        assert valid, '{} should have been a valid URL, but '\
            'got: {}'.format(val, reason)
    for val in bads:
        d = {'': val}
        valid, reason = con._validate_url(d, '')
        assert not valid, '{} should not have been a valid URL'.format(val)
Matt Traudt's avatar
Matt Traudt committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230


def test_nickname():
    max_len = 32
    goods = [
        'aaa', 'AAA', 'aAa', 'A1a', '1aA', 'aA1',
        '!!!', '!@#',
        'a!A', '!Aa', 'Aa!',
        'a' * max_len,
    ]
    bads = [
        '', 'a' * (max_len + 1),
        '"', '\'',
    ]
    for nick in goods:
        d = {'n': nick}
        valid, reason = con._validate_nickname(d, 'n')
        assert valid, reason
    for nick in bads:
        d = {'n': nick}
        valid, reason = con._validate_nickname(d, 'n')
        assert not valid, reason