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

validators WIP

parent 7d4f05aa
...@@ -3,20 +3,29 @@ Argument extraction tests ...@@ -3,20 +3,29 @@ Argument extraction tests
@author: jacekf @author: jacekf
''' '''
from corepost.web import CorePost from corepost.web import CorePost, validate
from corepost.enums import Http from corepost.enums import Http
from formencode import Schema, validators from formencode import Schema, validators
app = CorePost() 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) @app.route("/int/<int:intarg>/float/<float:floatarg>/string/<stringarg>",Http.GET)
def test(request,intarg,floatarg,stringarg,**kwargs): def test(request,intarg,floatarg,stringarg,**kwargs):
args = (intarg,floatarg,stringarg) args = (intarg,floatarg,stringarg)
return "%s" % map(lambda x: (type(x),x),args) return "%s" % map(lambda x: (type(x),x),args)
@app.route("/validate/<int:rootId>/children",Http.POST) @app.route("/validate/<int:rootId>/schema",Http.POST)
@app.validate(childid=validators.String(not_empty=True)) @validate(schema=TestSchema)
def post(request,rootId,childId,**kwargs): 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) return "%s - %s - %s" % (rootId,childId,kwargs)
def run_app_arguments(): def run_app_arguments():
......
...@@ -5,14 +5,23 @@ Feature: Argument Validators ...@@ -5,14 +5,23 @@ Feature: Argument Validators
CorePost should be able to correctly validate path, query and form arguments CorePost should be able to correctly validate path, query and form arguments
@validate @validate
Scenario Outline: Path argument extraction Scenario Outline: Form argument validation
Given 'arguments' is running 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> Then I expect HTTP code <code>
And I expect content contains '<content>' And I expect content contains '<content>'
Examples: Examples:
| args | code | content | | url | args | code | content |
| childId=jacekf | 201 | 23 - jacekf - {} | # validates using argument-specific validators
| childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} | | 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 ...@@ -12,7 +12,7 @@ from collections import defaultdict
from enums import MediaType from enums import MediaType
from corepost.enums import Http from corepost.enums import Http
from corepost.utils import getMandatoryArgumentNames from corepost.utils import getMandatoryArgumentNames
from formencode import validators, Schema, Validator from formencode import FancyValidator, Invalid
class RequestRouter: class RequestRouter:
''' Common class for containing info related to routing a request to a function ''' ''' Common class for containing info related to routing a request to a function '''
...@@ -148,15 +148,6 @@ class CorePost(Resource): ...@@ -148,15 +148,6 @@ class CorePost(Resource):
return f return f
return wrap 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): def render_GET(self,request):
""" Handles all GET requests """ """ Handles all GET requests """
return self.__renderUrl(request) return self.__renderUrl(request)
...@@ -246,5 +237,41 @@ class CorePost(Resource): ...@@ -246,5 +237,41 @@ class CorePost(Resource):
reactor.listenTCP(port, factory) #@UndefinedVariable reactor.listenTCP(port, factory) #@UndefinedVariable
reactor.run() #@UndefinedVariable reactor.run() #@UndefinedVariable
##################################################################################################
#
# DECORATORS
#
##################################################################################################
def validate(schema=None,**vKwargs):
\ No newline at end of file '''
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): ...@@ -49,7 +49,7 @@ def read(fname):
setup( setup(
name="CorePost", name="CorePost",
version="0.0.4", version="0.0.5",
author="Jacek Furmankiewicz", author="Jacek Furmankiewicz",
author_email="jacek99@gmail.com", author_email="jacek99@gmail.com",
description=("A Twisted Web REST micro-framework"), 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