-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
190 lines (152 loc) · 6.5 KB
/
test.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
from unittest import TestCase
from flask import Flask
from flask.testing import FlaskClient
from flask_validation import common_regex as cr
from flask_validation import *
class BaseTestCase(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def _get_test_client_of_decorated_view_function_registered_flask_app(self, decorator_func) -> FlaskClient:
def view_func():
return 'hello'
decorated_view_func = decorator_func(view_func)
app = Flask(__name__)
Validator(app)
app.add_url_rule('/', view_func=decorated_view_func, methods=['POST'])
return app.test_client()
def _json_post_request(self, client, *args, **kwargs):
return client.post('/', headers={'Content-Type': 'application/json'}, *args, **kwargs)
def _plain_post_request(self, client, *args, **kwargs):
return client.post('/', headers={'Content-Type': 'text/plain'}, *args, **kwargs)
class TestJsonRequired(BaseTestCase):
def setUp(self):
self.target_func = json_required
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func)
def test_200(self):
resp = self._json_post_request(self.client)
self.assertEqual(resp.status_code, 200)
def test_invalid_content_type(self):
resp = self._plain_post_request(self.client)
self.assertEqual(resp.status_code, 406)
class TestValidateKeys(BaseTestCase):
def setUp(self):
self.target_func = validate_keys
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func([
'a',
'b',
{
'c': [
'd', 'e'
]
}
]))
def test_200(self):
resp = self._json_post_request(self.client, json={'a': 1, 'b': 1, 'c': {'d': 1, 'e': 1}})
self.assertEqual(resp.status_code, 200)
def test_key_missing(self):
resp = self._json_post_request(self.client, json={'a': 1, 'b': 1, 'c': {'d': 1}})
self.assertEqual(resp.status_code, 400)
class TestValidateCommon(BaseTestCase):
def setUp(self):
self.target_func = validate_common
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func({
'a': str,
'b': int,
'c': {
'd': int
}
}))
def test_200(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': {'d': 1}})
self.assertEqual(resp.status_code, 200)
def test_key_missing(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1})
self.assertEqual(resp.status_code, 400)
def test_invalid_type(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': {'d': 'a'}})
self.assertEqual(resp.status_code, 400)
class TestValidateWithFields(BaseTestCase):
def setUp(self):
self.target_func = validate_with_fields
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func({
'a': StringField(max_length=10),
'b': IntField(min_value=0),
'c': FloatField(min_value=3.8),
'd': ListField(max_length=10),
'e': {
'f': BooleanField(allow_null=True)
}
}))
def test_200(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': 6.5, 'd': [1, 2, 3], 'e': {'f': None}})
self.assertEqual(resp.status_code, 200)
def test_key_missing(self):
resp = self._json_post_request(self.client, json={'a': 'a'})
self.assertEqual(resp.status_code, 400)
def test_validation_failure(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': 1.5, 'd': [1, 2, 3], 'e': {'f': None}})
self.assertEqual(resp.status_code, 400)
class TestValidateWithJsonSchema(BaseTestCase):
def setUp(self):
self.target_func = validate_with_jsonschema
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func({
'type': 'object',
'properties': {
'a': {'type': 'string'},
'b': {'type': 'number'},
'c': {
'type': 'object',
'properties': {
'd': {'type': 'number'}
}
}
}
}))
def test_200(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': {'d': 1}})
self.assertEqual(resp.status_code, 200)
def test_validation_error(self):
resp = self._json_post_request(self.client, json={'a': 'a', 'b': 1, 'c': {'d': 'a'}})
self.assertEqual(resp.status_code, 400)
class TestValidateWithCommonRegex(BaseTestCase):
def setUp(self):
self.target_func = validate_with_fields
self.client = self._get_test_client_of_decorated_view_function_registered_flask_app(self.target_func({
'email': StringField(regex=cr.email),
'url': StringField(regex=cr.url),
'visa_card': StringField(regex=cr.visa_card),
'master_card': StringField(regex=cr.master_card),
'isbn': StringField(regex=cr.isbn),
'hex': StringField(regex=cr.hex),
'ipv4': StringField(regex=cr.ipv4),
'digit': StringField(regex=cr.digit),
'date': StringField(regex=cr.date)
}))
def test_200(self):
resp = self._json_post_request(self.client, json={
'email': 'viper@istruly.sexy',
'url': 'https://www.google.com/',
'visa_card': '4000 1234 5678 9010',
'master_card': '5412 5412 5412 5412',
'isbn': '9791160501308',
'hex': '#ffb2d9',
'ipv4': '127.0.0.1',
'digit': '20010420',
'date': '2018/04/20'
})
self.assertEqual(resp.status_code, 200)
def test_validation_error(self):
resp = self._json_post_request(self.client, json={
'email': 'https://www.google.com/',
'url': 'viper@istruly.sexy',
'visa_card': '5412 5412 5412 5412',
'master_card': '4000 1234 5678 9010',
'isbn': '#ffb2d9',
'hex': '9791160501308',
'ipv4': '2018/04/20',
'digit': '127.0.0.1',
'date': '20010420'
})
self.assertEqual(resp.status_code, 400)