/
test_decorator_validate.py
128 lines (107 loc) · 4.47 KB
/
test_decorator_validate.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
# -*- coding: utf-8 -*-
from paste.fixture import TestApp
from paste.registry import RegistryManager
from pylons.decorators import validate
from pylons.controllers import WSGIController
from __init__ import ControllerWrap, SetupCacheGlobal, TestWSGIController
import formencode
from formencode.htmlfill import html_quote
def custom_error_formatter(error):
return '<p><span class="pylons-error">%s</span></p>\n' % html_quote(error)
class NetworkForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
new_network = formencode.validators.URL(not_empty=True)
class HelloForm(formencode.Schema):
hello = formencode.ForEach(formencode.validators.Int())
class ValidatingController(WSGIController):
def new_network(self):
return """
<html>
<form action="/dhcp/new_form" method="POST">
<table>
<tr>
<th>Network</th>
<td>
<input id="new_network" name="new_network" type="text" value="" />
</td>
</tr>
</table>
<input name="commit" type="submit" value="Save changes" />
</form>
</html>
"""
@validate(schema=NetworkForm, form='new_network')
def network(self):
return 'Your network is: %s' % self.form_result.get('new_network')
def view_hello(self):
return """
<html>
<form action="/hello" method="POST">
<table>
<tr>
<th>Hello</th>
<td>
<form:iferror name="hello">Bad Hello! </form:iferror>
<input id="hello" name="hello" type="text" value="" />
<input id="hello" name="hello" type="text" value="" />
<input id="hello" name="hello" type="text" value="" />
</td>
</tr>
</table>
<input name="commit" type="submit" value="Submit" />
</form>
</html>
"""
@validate(schema=HelloForm(), post_only=False, form='view_hello')
def hello(self):
return str(self.form_result)
@validate(schema=HelloForm(), post_only=False, form='view_hello',
auto_error_formatter=custom_error_formatter)
def hello_custom(self):
return str(self.form_result)
@validate(schema=NetworkForm, form='hello_recurse')
def hello_recurse(self, environ):
if environ['REQUEST_METHOD'] == 'GET':
return self.new_network()
else:
return 'Your network is: %s' % self.form_result.get('new_network')
class TestValidateDecorator(TestWSGIController):
def setUp(self):
TestWSGIController.setUp(self)
app = SetupCacheGlobal(ControllerWrap(ValidatingController),
self.environ)
app = RegistryManager(app)
self.app = TestApp(app)
def test_network_validated(self):
response = self.post_response(action='network',
new_network='http://pylonshq.com/')
assert 'Your network is: http://pylonshq.com/' in response
def test_network_failed_validation_non_ascii(self):
response = self.post_response(action='network', new_network='Росси́я')
assert 'That is not a valid URL' in response
assert 'Росси́я' in response
def test_recurse_validated(self):
response = self.post_response(action='hello_recurse',
new_network='http://pylonshq.com/')
assert 'Your network is: http://pylonshq.com/' in response
def test_hello(self):
self.environ['pylons.routes_dict']['action'] = 'hello'
response = self.app.post('/hello?hello=1&hello=2&hello=3',
extra_environ=self.environ)
assert "{'hello': [1, 2, 3]}" in response
def test_hello_failed(self):
self.environ['pylons.routes_dict']['action'] = 'hello'
response = self.app.post('/hello?hello=1&hello=2&hello=hi',
extra_environ=self.environ)
assert 'Bad Hello! ' in response
assert "[None, None, u'Please enter an integer value']" in response
def test_hello_custom_failed(self):
self.environ['pylons.routes_dict']['action'] = 'hello_custom'
response = \
self.app.post('/hello_custom?hello=1&hello=2&hello=hi',
extra_environ=self.environ)
assert 'Bad Hello! ' in response
assert "[None, None, u'Please enter an integer value']" in response
assert ("""<p><span class="pylons-error">[None, None, u'Please enter """
"""an integer value']</span></p>""") in response