Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
apryor6 committed May 3, 2019
0 parents commit ae426ad
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .circleci/config.yml
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/
124 changes: 124 additions & 0 deletions .gitignore
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# flask_accepts

1 change: 1 addition & 0 deletions flask_accepts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from flask_accepts.decorators import accepts # noqa
1 change: 1 addition & 0 deletions flask_accepts/decorators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .decorators import accepts # noqa
17 changes: 17 additions & 0 deletions flask_accepts/decorators/decorators.py
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
63 changes: 63 additions & 0 deletions flask_accepts/decorators/decorators_test.py
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 added flask_accepts/test/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions flask_accepts/test/fixtures.py
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()
5 changes: 5 additions & 0 deletions pytest.ini
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
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask==1.0.2
Flask-RESTful==0.3.7
13 changes: 13 additions & 0 deletions setup.py
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'],
)

0 comments on commit ae426ad

Please sign in to comment.