├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── app.py ├── handlers ├── __init__.py └── routes.py ├── requirements.txt └── tests ├── __init__.py └── test_routes.py /.gitignore: -------------------------------------------------------------------------------- 1 | .pytest_cache 2 | __pycache__ 3 | venv/ 4 | .idea 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Aaron J. Olson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask Pytest routing and requests example 2 | Simple sample application demonstrating how to use Pytest with Flask for testing routing and requests. 3 | The example includes a basic "hello world" route to demonstrate a GET request. The second example is 4 | a route that expects a POST request. 5 | 6 | ## Setup and run instructions 7 | Install the requirements into a virtualenv or your environment of choice 8 | 9 | pip install -r requirements.txt 10 | 11 | which includes 12 | * flask 13 | * pytest 14 | 15 | then run the `pytest` command -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aaronjolson/flask-pytest-example/e74780371fbb8372b95ad91faf9a1e7580acf072/__init__.py -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_pytest_example.handlers.routes import configure_routes 3 | 4 | app = Flask(__name__) 5 | 6 | configure_routes(app) 7 | 8 | if __name__ == '__main__': 9 | app.run() 10 | -------------------------------------------------------------------------------- /handlers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aaronjolson/flask-pytest-example/e74780371fbb8372b95ad91faf9a1e7580acf072/handlers/__init__.py -------------------------------------------------------------------------------- /handlers/routes.py: -------------------------------------------------------------------------------- 1 | from flask import request 2 | import json 3 | 4 | 5 | def configure_routes(app): 6 | 7 | @app.route('/') 8 | def hello_world(): 9 | return 'Hello, World!' 10 | 11 | @app.route('/post/test', methods=['POST']) 12 | def receive_post(): 13 | headers = request.headers 14 | 15 | auth_token = headers.get('authorization-sha256') 16 | if not auth_token: 17 | return 'Unauthorized', 401 18 | 19 | data_string = request.get_data() 20 | data = json.loads(data_string) 21 | 22 | request_id = data.get('request_id') 23 | payload = data.get('payload') 24 | 25 | if request_id and payload: 26 | return 'Ok', 200 27 | else: 28 | return 'Bad Request', 400 29 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | pytest 3 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aaronjolson/flask-pytest-example/e74780371fbb8372b95ad91faf9a1e7580acf072/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_routes.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | import json 3 | 4 | from flask_pytest_example.handlers.routes import configure_routes 5 | 6 | 7 | def test_base_route(): 8 | app = Flask(__name__) 9 | configure_routes(app) 10 | client = app.test_client() 11 | url = '/' 12 | 13 | response = client.get(url) 14 | assert response.get_data() == b'Hello, World!' 15 | assert response.status_code == 200 16 | 17 | 18 | def test_post_route__success(): 19 | app = Flask(__name__) 20 | configure_routes(app) 21 | client = app.test_client() 22 | url = '/post/test' 23 | 24 | mock_request_headers = { 25 | 'authorization-sha256': '123' 26 | } 27 | 28 | mock_request_data = { 29 | 'request_id': '123', 30 | 'payload': { 31 | 'py': 'pi', 32 | 'java': 'script' 33 | } 34 | } 35 | 36 | response = client.post(url, data=json.dumps(mock_request_data), headers=mock_request_headers) 37 | assert response.status_code == 200 38 | 39 | 40 | def test_post_route__failure__unauthorized(): 41 | app = Flask(__name__) 42 | configure_routes(app) 43 | client = app.test_client() 44 | url = '/post/test' 45 | 46 | mock_request_headers = {} 47 | 48 | mock_request_data = { 49 | 'request_id': '123', 50 | 'payload': { 51 | 'py': 'pi', 52 | 'java': 'script' 53 | } 54 | } 55 | 56 | response = client.post(url, data=json.dumps(mock_request_data), headers=mock_request_headers) 57 | assert response.status_code == 401 58 | 59 | 60 | def test_post_route__failure__bad_request(): 61 | app = Flask(__name__) 62 | configure_routes(app) 63 | client = app.test_client() 64 | url = '/post/test' 65 | 66 | mock_request_headers = { 67 | 'authorization-sha256': '123' 68 | } 69 | 70 | mock_request_data = {} 71 | 72 | response = client.post(url, data=json.dumps(mock_request_data), headers=mock_request_headers) 73 | assert response.status_code == 400 74 | --------------------------------------------------------------------------------