-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_validation_filter.py
91 lines (76 loc) · 2.84 KB
/
test_validation_filter.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
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 ValidationFilterController(RestController):
@json
@authorize
@validate_form(
filter_=['filteredParamForAll'],
client=dict(filter_=['filteredParamForClient']),
admin=dict(filter_=['filteredParamForAdmin'])
)
def post(self):
result = copy.deepcopy(context.form)
result.update(context.query)
return result
class Root(RootController):
validation = ValidationFilterController()
class ValidationFilterTestCase(WebAppTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.application.__root__ = Root()
def test_validation_filter(self):
# Test `filter`
# role -> All
resp = self.wsgi_app.post(
'/validation',
params={
'customParam': 'param',
'filteredParamForAll': 'param',
'filteredParamForClient': 'param',
'filteredParamForAdmin': 'param',
}
)
self.assertNotIn('customParam', resp.text)
self.assertNotIn('filteredParamForClient', resp.text)
self.assertNotIn('filteredParamForAdmin', resp.text)
self.assertIn('filteredParamForAll', resp.text)
# -----------------------------
# role -> Client
self.wsgi_app.extra_environ['fake_roles'] = 'client'
resp = self.wsgi_app.post(
'/validation',
params={
'customParam': 'param',
'filteredParamForAll': 'param',
'filteredParamForClient': 'param',
'filteredParamForAdmin': 'param',
}
)
self.assertNotIn('customParam', resp.text)
self.assertIn('filteredParamForClient', resp.text)
self.assertNotIn('filteredParamForAdmin', resp.text)
self.assertIn('filteredParamForAll', resp.text)
# -----------------------------
# role -> Admin
self.wsgi_app.extra_environ['fake_roles'] = 'admin'
resp = self.wsgi_app.post(
'/validation',
params={
'customParam': 'param',
'filteredParamForAll': 'param',
'filteredParamForClient': 'param',
'filteredParamForAdmin': 'param',
}
)
self.assertNotIn('customParam', resp.text)
self.assertNotIn('filteredParamForClient', resp.text)
self.assertIn('filteredParamForAdmin', resp.text)
self.assertIn('filteredParamForAll', resp.text)
if __name__ == '__main__': # pragma: no cover
unittest.main()