Commit 0f3a3792 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

validators WIP

parent 7d4f05aa
......@@ -3,20 +3,29 @@ Argument extraction tests
@author: jacekf
'''
from corepost.web import CorePost
from corepost.web import CorePost, validate
from corepost.enums import Http
from formencode import Schema, validators
app = CorePost()
class TestSchema(Schema):
allow_extra_fields = True
childId = validators.Regex(regex="^jacekf|test$")
@app.route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs):
args = (intarg,floatarg,stringarg)
return "%s" % map(lambda x: (type(x),x),args)
@app.route("/validate/<int:rootId>/children",Http.POST)
@app.validate(childid=validators.String(not_empty=True))
def post(request,rootId,childId,**kwargs):
@app.route("/validate/<int:rootId>/schema",Http.POST)
@validate(schema=TestSchema)
def postValidateSchema(request,rootId,childId,**kwargs):
return "%s - %s - %s" % (rootId,childId,kwargs)
@app.route("/validate/<int:rootId>/custom",Http.POST)
@validate(childId=validators.Regex(regex="^jacekf|test$"))
def postValidateCustom(request,rootId,childId,**kwargs):
return "%s - %s - %s" % (rootId,childId,kwargs)
def run_app_arguments():
......
......@@ -5,14 +5,23 @@ Feature: Argument Validators
CorePost should be able to correctly validate path, query and form arguments
@validate
Scenario Outline: Path argument extraction
Scenario Outline: Form argument validation
Given 'arguments' is running
When as user 'None:None' I POST 'http://127.0.0.1:8082/validate/23/children' with '<args>'
# childId accepts only jacekf or test, via Regex validator
When as user 'None:None' I POST 'http://127.0.0.1:8082/validate/23/<url>' with '<args>'
Then I expect HTTP code <code>
And I expect content contains '<content>'
Examples:
| args | code | content |
| childId=jacekf | 201 | 23 - jacekf - {} |
| childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} |
| url | args | code | content |
# validates using argument-specific validators
| custom | childId=jacekf | 201 | 23 - jacekf - {} |
| custom | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} |
| custom | childId=test | 201 | 23 - test - {} |
| custom | childId=wrong | 400 | 'wrong' is not a valid value for 'childId': The input is not valid |
# validates using Schema
| schema | childId=jacekf | 201 | 23 - jacekf - {} |
| schema | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} |
| schema | childId=test | 201 | 23 - test - {} |
| schema | childId=wrong | 400 | 'wrong' is not a valid value for 'childId': The input is not valid |
\ No newline at end of file
......@@ -12,7 +12,7 @@ from collections import defaultdict
from enums import MediaType
from corepost.enums import Http
from corepost.utils import getMandatoryArgumentNames
from formencode import validators, Schema, Validator
from formencode import FancyValidator, Invalid
class RequestRouter:
''' Common class for containing info related to routing a request to a function '''
......@@ -148,15 +148,6 @@ class CorePost(Resource):
return f
return wrap
def validate(self,schema=None,**kwargs):
'''
Main decorator for registering additional validators for incoming URL arguments
'''
def wrap(f,**kwargs):
print kwargs
return f
return wrap
def render_GET(self,request):
""" Handles all GET requests """
return self.__renderUrl(request)
......@@ -247,4 +238,40 @@ class CorePost(Resource):
reactor.run() #@UndefinedVariable
##################################################################################################
#
# DECORATORS
#
##################################################################################################
def validate(schema=None,**vKwargs):
'''
Main decorator for registering additional validators for incoming URL arguments
'''
def fn(realfn):
def wrap(*args,**kwargs):
# first run schema validation, then the custom validators
errors = ()
if schema != None:
try:
schema.to_python(kwargs)
except Invalid as ex:
errors = ()
for error in ex.error_dict.keys():
errors.append()
raise TypeError("%s" % ex)
for arg in vKwargs.keys():
validator = vKwargs[arg]
if arg in kwargs:
val = kwargs[arg]
try:
validator.to_python(val)
except Invalid as ex:
raise TypeError("'%s' is not a valid value for '%s': %s" % (val,arg,ex))
else:
if isinstance(validator,FancyValidator) and validator.not_empty:
raise TypeError("Missing mandatory argument '%s'" % arg)
return realfn(*args,**kwargs)
return wrap
return fn
......@@ -49,7 +49,7 @@ def read(fname):
setup(
name="CorePost",
version="0.0.4",
version="0.0.5",
author="Jacek Furmankiewicz",
author_email="jacek99@gmail.com",
description=("A Twisted Web REST micro-framework"),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment