import formprocessor from testcase import TestCase import schema import validators import htmlview from simplehtmlgen import html def d(**kw): return kw class TestDecode(TestCase): def testDecode(self, description, schema, options, httpfields, output): if isinstance(httpfields, str): httpfields = self.cgiParse(httpfields) httpfields['_formName_'] = 'form' fp = formprocessor.FormProcessor(schema) success, data = fp.processForm(httpfields) if not success: val = fp.renderForm(options=options, httpRequest=httpfields, action='form', errors=data) output = [html.input.hidden(name='_formName_', value='form')] + output output = html.form(action='form', method='POST', c=output) self.assertHTMLEqual(val, output) else: self.assertDictEqual(data, output) params_testDecode = [] def addDecode(schema, httpfields, output, options=None): if isinstance(schema, type): name = schema.__name__ else: name = schema.__class__.__name__ TestDecode.params_testDecode.append( (name, schema, options, httpfields, output)) class Name(schema.Schema): fname = validators.String(notEmpty=True, view=htmlview.Text, description='') lname = validators.String(notEmpty=True, view=htmlview.Text, description='') class phone(validators.ForEach): number = validators.PhoneNumber(view=htmlview.Text, description='', notEmpty=True) addDecode( Name, options=d(phone=d(repetitions=2)), httpfields='fname=Bob', output= ['fname:', html.input.text(name='fname', value='Bob'), html.br, html.b('Missing value'), html.br, 'lname:', html.input.text(name='lname', value=''), html.br, 'phone:', html.input.text(name='phone-0', value=''), html.br, 'phone:', html.input.text(name='phone-1', value=''), html.br, html.input.hidden(name='phone--repetitions', value='2'), ] ) addDecode( Name, httpfields='fname=Bob&lname=Jones&phone-0=444-444-3947&phone-1=384-2934&phone--repetitions=3', output= ['fname:', html.input.text(name='fname', value='Bob'), html.br, 'lname:', html.input.text(name='lname', value='Jones'), html.br, 'phone:', html.input.text(name='phone-0', value='444-444-3947'), html.br, html.b('Please enter a number, with area code, in the form ###-###-####, optionally with "ext.####"'), html.br, 'phone:', html.input.text(name='phone-1', value='384-2934'), html.br, html.b('Please enter a value'), html.br, 'phone:', html.input.text(name='phone-2', value=''), html.br, html.input.hidden(name='phone--repetitions', value=3), ] ) class Secrets(schema.Schema): class items(validators.ForEach): item1 = htmlview.SecureHidden(secretKey='sh') required = validators.FancyValidator(notEmpty=True, view=htmlview.Hidden) addDecode( Secrets, httpfields='items-0=whatever&required=yes', output= [html.b('Invalid value (field missing)'), html.br, 'items:', html.input.hidden(name='items-0.hash', value='*'), html.input.hidden(name='items-0', value=''), html.br, html.input.hidden(name='items--repetitions', value=1), 'required:', html.input.hidden(name='required', value='yes'), html.br, ] ) import md5 hasher = md5.new() hasher.update('sh') hasher.update('test') hashvalue = hasher.hexdigest() addDecode( Secrets, httpfields='items-0=whatever&items-0.hash=%s&required=yes' % hashvalue, output= [html.b('Invalid value (not generated by server) submitted'), html.br, 'items:', html.input.hidden(name='items-0.hash', value='*'), html.input.hidden(name='items-0', value=''), html.br, html.input.hidden(name='items--repetitions', value=1), 'required:', html.input.hidden(name='required', value='yes'), html.br, ] ) addDecode( Secrets, httpfields='items-0=test&items-0.hash=%s&required=' % hashvalue, output= ['items:', html.input.hidden(name='items-0.hash', value=hashvalue), html.input.hidden(name='items-0', value='test'), html.br, html.input.hidden(name='items--repetitions', value=1), html.b('Please enter a value'), html.br, 'required:', html.input.hidden(name='required', value=''), html.br, ] )