This repository has been archived by the owner on May 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_validator.py
222 lines (183 loc) · 7.76 KB
/
test_validator.py
1
2
3
4
5
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import unittest
import yota
from yota.validators import *
from yota.nodes import *
from yota.exceptions import *
class TestValidators(unittest.TestCase):
""" Testing for all our built-in validators """
def run_check(self, values, validator):
""" a utility method to run the check by hand. Pass it key value data
like what a post object would have and it builds the args for your
check accordingly """
args = []
for key, val in values.items():
args.append(EntryNode(_attr_name=key, data=val))
c = Check(validator, *args)
c.resolved = True
c()
ret = []
for a in args:
if len(a.errors) > 0:
ret.append(a)
return ret
def test_unicode_validator(self):
""" make sure unicode strings don't break validators """
for val in [MinLength(5),
MaxLength(5),
Regex(regex='^[a-z]*$'),
Required(),
Email()]:
errors = self.run_check({'t': u"\u041f\u0440\u0438\u0432\u0435\u0442"}, val)
def test_min_required(self):
""" min validator testing, pos and neg """
meth = MinLength(5, message="Darn")
errors = self.run_check({'t':'short'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'shor'}, meth)
assert(len(errors) > 0)
def test_max_required(self):
""" max validator testing, pos and neg """
meth = MaxLength(5, message="Darn")
errors = self.run_check({'t':'shor'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'toolong'}, meth)
assert(len(errors) > 0)
def test_regex_valid(self):
""" regex validator testing, pos and neg """
meth = Regex(regex='^[a-z]*$', message='darn')
errors = self.run_check({'t':'asdlkfdfsljgdlkfj'}, meth)
assert(len(errors) == 0)
# negative
errors = self.run_check({'t':'123sdflkns'}, meth)
assert(len(errors) > 0)
def test_url_valid(self):
""" regex validator testing, pos and neg """
meth = URL(message='darn')
errors = self.run_check({'t':'http://google.com'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'http://localhost'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'http://localhost:8080/login'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'http://192.168.1.1:8080/login'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'https://192.168.1.1:8080/'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t':'ftp://192.168.1.1:8080/'}, meth)
assert(len(errors) == 0)
# negative
errors = self.run_check({'t':'http;//google.com'}, meth)
assert(len(errors) > 0)
def test_email(self):
""" email validator testing, all branches """
meth = Email(message="Darn")
errors = self.run_check({'t': 'm@testing.com'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t': 'toolong'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'm@t%.com'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': u'm@t\x80%.com'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': u'@$%^%&^*m@t\x80%.com'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': u'm@\xc3.com'}, meth)
assert(len(errors) == 0)
def test_minmax(self):
""" minmax validator testing, pos and neg """
meth = MinMax(2, 5, minmsg="Darn", maxmsg="Darn")
errors = self.run_check({'t': 'ai'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t': 'a'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'asdlkjdsfgljksdfg'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'asdfg'}, meth)
assert(len(errors) == 0)
meth = MinMax(2, 5)
errors = self.run_check({'t': 'aiadsflgkj'}, meth)
assert(len(errors) > 0)
assert("fewer" in errors[0].errors[0]['message'])
def test_password(self):
""" password validator testing, pos and neg """
meth = Password()
errors = self.run_check({'t': 'a'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'a&'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aA&'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm%klsdfg'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm% '}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm%\n'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'A7Sdfkls$gdf'}, meth)
assert(len(errors) == 0)
def test_username(self):
""" username validator testing, pos and neg """
meth = Username()
errors = self.run_check({'t': 'a'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'adfgl&'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'adfgsdfg '}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm%klsdfg'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm% '}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'aklm%\n'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'some_thing-new'}, meth)
assert(len(errors) == 0)
def test_integer(self):
""" integer validator testing, pos and neg """
meth = Integer(message="Darn")
errors = self.run_check({'t': '12'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t': '12a'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'asdfsd'}, meth)
assert(len(errors) > 0)
def test_matching(self):
""" matching validator """
meth = Matching(message="Darn")
errors = self.run_check({'t': 'toolong', 'b': 'notmatching'}, meth)
assert(len(errors) > 0)
errors = self.run_check({'t': 'something', 'b': 'something'}, meth)
assert(len(errors) == 0)
def test_strength(self):
""" password strength validator """
meth = PasswordStrength(message="Darn")
errors = self.run_check({'t': 'AA'}, meth)
assert('1' in errors[0].errors[0]['message'])
errors = self.run_check({'t': 'SOmething'}, meth)
assert('2' in errors[0].errors[0]['message'])
errors = self.run_check({'t': 'SOme33ing'}, meth)
assert('3' in errors[0].errors[0]['message'])
errors = self.run_check({'t': 'SOme33ing%'}, meth)
assert('4' in errors[0].errors[0]['message'])
meth = PasswordStrength(message="Darn",
regex=["(?=.*[A-Z].*[A-Z])"])
errors = self.run_check({'t': 'SOme33ing%'}, meth)
assert('1' in errors[0].errors[0]['message'])
def test_required(self):
""" required validator """
meth = Required(message="Darn")
errors = self.run_check({'t': 'toolong'}, meth)
assert(len(errors) == 0)
errors = self.run_check({'t': ''}, meth)
assert(len(errors) > 0)
class TestCheck(unittest.TestCase):
def test_key_access_exception(self):
""" Proper raising of access exception when missing a required piece of
data """
class TForm(yota.Form):
t = EntryNode()
_t_valid = yota.Check(Required(message="Darn"), 't')
test = TForm()
test.t._null_val = ['test']
test._gen_validate({})
assert len(test.t.data) == 1