validators are now decorators instead
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
# coding=UTF-8
|
||||
|
||||
import flask_testing
|
||||
import json
|
||||
import base64
|
||||
from api import create_app
|
||||
from flask import Flask
|
||||
from api.validators import ValidationError, validate_exists, validate_number, validate_territory, validate_jsonschema
|
||||
|
||||
|
||||
class TestHttpBasicAuth(flask_testing.TestCase):
|
||||
@@ -35,3 +38,75 @@ class TestHttpBasicAuth(flask_testing.TestCase):
|
||||
def test_basic_auth_disabled(self):
|
||||
response = self.client.get('/')
|
||||
self.assert200(response)
|
||||
|
||||
|
||||
class TestValidators(flask_testing.TestCase):
|
||||
|
||||
def create_app(self):
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.errorhandler(ValidationError)
|
||||
def on_error(error):
|
||||
return error.message, 400
|
||||
return app
|
||||
|
||||
def test_validate_exists(self):
|
||||
app = self.app
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
@validate_exists('a', 'b')
|
||||
def index():
|
||||
return 'ok'
|
||||
|
||||
self.assertEqual(b'No b is specified', self.client.get('/?a=1').data)
|
||||
self.assertEqual(b'ok', self.client.get('/?a=1&b=2').data)
|
||||
|
||||
def test_validate_number(self):
|
||||
app = self.app
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
@validate_number('age')
|
||||
def index():
|
||||
return 'ok'
|
||||
|
||||
self.assertEqual(b'No age is specified', self.client.get('/').data)
|
||||
self.assertEqual(b'age is not a number',
|
||||
self.client.get('/?age=aaa').data)
|
||||
self.assertEqual(b'ok', self.client.get('/?age=1').data)
|
||||
|
||||
def test_validate_territory(self):
|
||||
app = self.app
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
@validate_territory('territory')
|
||||
def index():
|
||||
return 'ok'
|
||||
|
||||
self.assertEqual(b'No territory is specified',
|
||||
self.client.get('/').data)
|
||||
self.assertEqual(b'abc is not a valid territory',
|
||||
self.client.get('/?territory=abc').data)
|
||||
self.assertEqual(b'ok', self.client.get('/?territory=SE').data)
|
||||
|
||||
def test_validate_jsonschema(self):
|
||||
app = self.app
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "number"}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
@validate_jsonschema(schema)
|
||||
def index():
|
||||
return 'ok'
|
||||
|
||||
self.assertEqual(b'Payload is not valid json',
|
||||
self.client.post('/').data)
|
||||
self.assertEqual(b"'name' is a required property", self.client.post(
|
||||
'/', data=json.dumps({'foo': 'bar'}), content_type='application/json').data)
|
||||
self.assertEqual(b"'abc' is not of type 'number'", self.client.post(
|
||||
'/', data=json.dumps({'name': 'bar', 'age': 'abc'}), content_type='application/json').data)
|
||||
|
||||
Reference in New Issue
Block a user