-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_validation_types.py
124 lines (106 loc) · 3.46 KB
/
test_validation_types.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
import copy
import unittest
from nanohttp import json, context
from microhttp_restful.validation import validate_form
from microhttp_restful.controllers import RestController, RootController
from microhttp_restful.tests.helpers import WebAppTestCase
from microhttp_restful.tests.helpers import authorize
class ValidationTypesController(RestController):
@json
@authorize
@validate_form(
types={
'typedParam1': float,
'typedParam2': float,
'typedParam3': float,
},
client={
'types': {
'typedParam1': int,
'typedParam2': int
}
},
admin={
'types': {
'typedParam1': complex,
'typedParam4': complex
}
}
)
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query)
return result
class Root(RootController):
validation = ValidationTypesController()
class ValidationTypesTestCase(WebAppTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.application.__root__ = Root()
def test_validation_types(self):
# Test `type`
# role -> All
resp = self.wsgi_app.post(
'/validation',
params={
'typedParam1': '1',
'typedParam2': '2',
'typedParam3': '3',
'typedParam4': '4'
}
)
self.assertEqual(type(resp.json['typedParam1']), float)
self.assertEqual(type(resp.json['typedParam2']), float)
self.assertEqual(type(resp.json['typedParam3']), float)
self.assertEqual(type(resp.json['typedParam4']), str)
self.wsgi_app.post(
'/validation',
params={'typedParam1': 'not_convertible'},
status=400
)
# -----------------------------
# role -> Client
self.wsgi_app.extra_environ['fake_roles'] = 'client'
resp = self.wsgi_app.post(
'/validation',
params={
'typedParam1': '1',
'typedParam2': '2',
'typedParam3': '3',
'typedParam4': '4'
}
)
self.assertEqual(type(resp.json['typedParam1']), int)
self.assertEqual(type(resp.json['typedParam2']), int)
self.assertEqual(type(resp.json['typedParam3']), float)
self.assertEqual(type(resp.json['typedParam4']), str)
self.wsgi_app.post(
'/validation',
params={'typedParam1': 'not_convertible'},
status=400
)
# -----------------------------
# role -> Admin
self.wsgi_app.extra_environ['fake_roles'] = 'admin'
resp = self.wsgi_app.post(
'/validation',
params={
'typedParam1': '1',
'typedParam2': '2',
'typedParam3': '3',
'typedParam4': '4'
}
)
# type complex is dict
self.assertEqual(type(resp.json['typedParam1']), dict)
self.assertEqual(type(resp.json['typedParam2']), float)
self.assertEqual(type(resp.json['typedParam3']), float)
self.assertEqual(type(resp.json['typedParam4']), dict)
self.wsgi_app.post(
'/validation',
params={'typedParam1': 'not_convertible'},
status=400
)
if __name__ == '__main__': # pragma: no cover
unittest.main()