-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ae426ad
Showing
12 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
docker: | ||
- image: circleci/python:3.6.2-stretch-browsers | ||
environment: | ||
FLASK_CONFIG: testing | ||
steps: | ||
- checkout | ||
- run: mkdir test-reports | ||
- restore_cache: | ||
key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }} | ||
- run: | ||
command: | | ||
python3 -m venv venv | ||
. venv/bin/activate | ||
pip install -r requirements.txt | ||
pip install pytest | ||
- run: | ||
command: | | ||
. venv/bin/activate | ||
pytest | ||
- store_artifacts: | ||
path: test-reports/ | ||
destination: tr1 | ||
- store_test_results: | ||
path: test-reports/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
.idea/ | ||
*.pyc | ||
py3/ | ||
data/ | ||
build/ | ||
dist/ | ||
.pytest_cache/ | ||
.vscode/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
*.pkl | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.idea | ||
|
||
*.pptx | ||
|
||
misc/ | ||
.Rproj.user | ||
|
||
venv/ | ||
|
||
bundle.cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# flask_accepts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from flask_accepts.decorators import accepts # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .decorators import accepts # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def accepts(*args): | ||
from flask_restful import reqparse | ||
parser = [arg for arg in args if isinstance(arg, reqparse.RequestParser)] | ||
parser = (parser and parser[0]) or reqparse.RequestParser(bundle_errors=True) | ||
for arg in args: | ||
if isinstance(arg, dict): | ||
parser.add_argument(**arg) | ||
|
||
def decorator(func): | ||
from functools import wraps | ||
@wraps(func) | ||
def inner(*args, **kwargs): | ||
from flask import request | ||
request.parsed_args = parser.parse_args() | ||
return func(*args, **kwargs) | ||
return inner | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from flask import request | ||
|
||
from flask_accepts.decorators import accepts | ||
from flask_accepts.test.fixtures import app, client # noqa | ||
|
||
|
||
def test_arguments_are_added_to_request(app): # noqa | ||
@app.route('/test') | ||
@accepts(dict(name='foo', type=int, help='An important foo')) | ||
def test(): | ||
assert request.parsed_args | ||
return 'success' | ||
with app.test_client() as cl: | ||
resp = cl.get('/test?foo=3') | ||
assert resp.status_code == 200 | ||
|
||
|
||
def test_dict_arguments_are_correctly_added(app): # noqa | ||
@app.route('/test') | ||
@accepts( | ||
{'name': 'an_int', 'type': int, 'help': 'An important int'}, | ||
{'name': 'a_bool', 'type': bool, 'help': 'An important bool'}, | ||
{'name': 'a_str', 'type': str, 'help': 'An important str'} | ||
) | ||
def test(): | ||
print('request.parsed_args = ', request.parsed_args) | ||
assert request.parsed_args.get('an_int') == 1 | ||
assert request.parsed_args.get('a_bool') | ||
assert request.parsed_args.get('a_str') == 'faraday' | ||
return 'success' | ||
with app.test_client() as cl: | ||
resp = cl.get('/test?an_int=1&a_bool=1&a_str=faraday') | ||
assert resp.status_code == 200 | ||
|
||
|
||
def test_failure_when_required_arg_is_missing(app): # noqa | ||
@app.route('/test') | ||
@accepts(dict(name='foo', | ||
type=int, | ||
required=True, | ||
help='A required foo')) | ||
def test(): | ||
print(request.parsed_args) | ||
return 'success' | ||
with app.test_client() as cl: | ||
resp = cl.get('/test') | ||
print(resp.data) | ||
assert resp.status_code == 400 | ||
|
||
|
||
def test_failure_when_arg_is_wrong_type(app): # noqa | ||
@app.route('/test') | ||
@accepts(dict(name='foo', | ||
type=int, | ||
required=True, | ||
help='A required foo')) | ||
def test(): | ||
print(request.parsed_args) | ||
return 'success' | ||
with app.test_client() as cl: | ||
resp = cl.get('/test?foo=baz') | ||
print(resp.data) | ||
assert resp.status_code == 400 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from flask import Flask, jsonify | ||
import pytest | ||
|
||
|
||
def create_app(env=None): | ||
app = Flask(__name__) | ||
@app.route('/health') | ||
def health(): | ||
return jsonify('healthy') | ||
return app | ||
|
||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
return create_app('test') | ||
|
||
|
||
@pytest.fixture | ||
def client(app): | ||
return app.test_client() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
testpaths = flask_accepts | ||
log_cli = True | ||
log_level = INFO | ||
addopts = --disable-pytest-warnings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask==1.0.2 | ||
Flask-RESTful==0.3.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright Alan (AJ) Pryor, Jr. 2018 | ||
|
||
from setuptools import setup, find_packages | ||
|
||
setup(name='flask_accepts', | ||
author='Alan "AJ" Pryor, Jr.', | ||
author_email='apryor6@gmail.com', | ||
version='0.1.0', | ||
description='Easy Flask input validation', | ||
ext_modules=[], | ||
packages=find_packages(), | ||
install_requires=['Flask-RESTful>=0.3.7'], | ||
) |