class TestAuthorization(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_with_token(self): @self.app.route('/resource', methods=['GET']) @self.io.authenticators(TokenAuthenticator) def test(): pass response = self.client.get('/resource', headers={'Authorization': 'token'}) self.assertEqual(response.status_code, 204) def test_missing_token(self): @self.app.route('/resource', methods=['GET']) @self.io.authenticators(TokenAuthenticator) def test(): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 401)
def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client()
class TestResponseStatus(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_unauthorized(self): @self.app.route('/resource', methods=['POST']) def test(): return self.io.unauthorized(Error('required', 'Login required', 'header', 'Authorization')) response = self.client.post('/resource') self.assertEqual(response.status_code, 401) def test_none_with_envelope(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return None response = self.client.post('/resource') data = json.loads(response.get_data(as_text=True)) self.assertEqual(data.get('users', 'missing'), None) def test_empty_list_with_envelope(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return [] response = self.client.post('/resource') data = json.loads(response.get_data(as_text=True)) self.assertEqual(data.get('users'), []) def test_no_content(self): @self.app.route('/resource', methods=['POST']) def test(): return None response = self.client.post('/resource') self.assertEqual(response.status_code, 204) self.assertTrue(response.content_type.startswith('text/html')) def test_error(self): @self.app.route('/resource', methods=['POST']) def test(): raise Exception('expected error in test_error') response = self.client.post('/resource') self.assertEqual(response.status_code, 500) self.assertTrue(response.content_type.startswith('application/json')) data = json.loads(response.get_data(as_text=True)) self.assertEqual(type(data.get('errors')), list) self.assertEqual(len(data.get('errors')), 1) def test_fields_param(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return dict(username='******', password='******') response = self.client.post('/resource?fields=username') data = json.loads(response.get_data(as_text=True)) users_data = data.get('users') self.assertEqual('foo', users_data.get('username')) self.assertIsNone(users_data.get('password')) def test_http_exception(self): @self.app.route('/resource', methods=['GET']) def test(): abort(401) response = self.client.get('/resource') self.assertEqual(401, response.status_code)
from flask import Flask from flask_io import FlaskIO from .config import Base __version__ = '1.0.0' app = Flask(__name__) app.config.from_object(Base) io = FlaskIO() io.init_app(app)
from flask_login import LoginManager from flask_sqlalchemy import SQLAlchemy from flask_webpack import Webpack from flask_socketio import SocketIO from flask_io import FlaskIO from app.plugins.flask_redis import Redis from app.plugins.flask_simplemde import SimpleMDE db = SQLAlchemy() login_manager = LoginManager() redis = Redis() webpack = Webpack() socketio = SocketIO() flaskio = FlaskIO() login_manager.login_view = 'auth.login' login_manager.login_message_category = 'warning' def register_extensions(app): db.init_app(app) login_manager.init_app(app) redis.init_app(app) webpack.init_app(app) socketio.init_app(app) # flaskio.init_app(app)
from flask import Flask from flask_io import FlaskIO, fields from datetime import datetime from example.schemas import UserSchema, PatchUserSchema, UpdateUserSchema app = Flask(__name__) app.config['DEBUG'] = True io = FlaskIO() io.init_app(app) store = dict() @app.route('/users', methods=['POST']) @io.from_body('user', UserSchema) @io.marshal_with(UserSchema) def add_user(user): if store.get(user.username): return io.conflict('User already exists: ' + user.username) user.created_at = datetime.now() store[user.username] = user return user @app.route('/users') @io.from_query('username', fields.String()) @io.from_query('max_results', fields.Integer(missing=10)) @io.marshal_with(UserSchema) def get_users(username, max_results):
from flask_io import FlaskIO from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() io = FlaskIO()
class TestResponseStatus(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_unauthorized(self): @self.app.route('/resource', methods=['POST']) def test(): return self.io.unauthorized( Error('required', 'Login required', 'header', 'Authorization')) response = self.client.post('/resource') self.assertEqual(response.status_code, 401) def test_none_with_envelope(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return None response = self.client.post('/resource') data = json.loads(response.get_data(as_text=True)) self.assertEqual(data.get('users', 'missing'), None) def test_empty_list_with_envelope(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return [] response = self.client.post('/resource') data = json.loads(response.get_data(as_text=True)) self.assertEqual(data.get('users'), []) def test_no_content(self): @self.app.route('/resource', methods=['POST']) def test(): return None response = self.client.post('/resource') self.assertEqual(response.status_code, 204) self.assertTrue(response.content_type.startswith('text/html')) def test_error(self): @self.app.route('/resource', methods=['POST']) def test(): raise Exception('expected error in test_error') response = self.client.post('/resource') self.assertEqual(response.status_code, 500) self.assertTrue(response.content_type.startswith('application/json')) data = json.loads(response.get_data(as_text=True)) self.assertEqual(type(data.get('errors')), list) self.assertEqual(len(data.get('errors')), 1) def test_fields_param(self): @self.app.route('/resource', methods=['POST']) @self.io.marshal_with(UserSchema, envelope='users') def test(): return dict(username='******', password='******') response = self.client.post('/resource?fields=username') data = json.loads(response.get_data(as_text=True)) users_data = data.get('users') self.assertEqual('foo', users_data.get('username')) self.assertIsNone(users_data.get('password')) def test_http_exception(self): @self.app.route('/resource', methods=['GET']) def test(): abort(401) response = self.client.get('/resource') self.assertEqual(401, response.status_code)
class TestRequestArgs(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_missing_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): self.assertEqual(param1, None) response = self.client.get('/resource') self.assertEqual(response.status_code, 204) def test_missing_required_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(required=True)) def test(param1): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 400) def test_empty_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): self.assertEqual(param1, None) response = self.client.get('/resource?param1=') self.assertEqual(response.status_code, 204) def test_empty_required_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(required=True)) def test(param1): pass response = self.client.get('/resource?param1=') self.assertEqual(response.status_code, 400) def test_default_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int(missing=10)) def test(param1): self.assertEqual(param1, 10) response = self.client.get('/resource') self.assertEqual(response.status_code, 204) def test_invalid_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): pass response = self.client.get('/resource?param1=a') self.assertEqual(response.status_code, 400) def test_success_validate(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(validate=lambda val: 1 <= val <= 10)) def test(param1): self.assertEqual(param1, 5) response = self.client.get('/resource?param1=5') self.assertEqual(response.status_code, 204) def test_fail_validate(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(validate=lambda val: 1 <= val <= 10)) def test(param1): pass response = self.client.get('/resource?param1=11') self.assertEqual(response.status_code, 400) def test_load_from(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param2', fields.Integer(load_from='param1')) def test(param2): self.assertEqual(param2, 10) response = self.client.get('/resource?param1=10') self.assertEqual(response.status_code, 204) def test_list(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.List(fields.Int())) def test(param1): self.assertEqual(param1[0], 10) self.assertEqual(param1[1], 20) response = self.client.get('/resource?param1=10¶m1=20') self.assertEqual(response.status_code, 204) def test_missing_required_list(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.List(fields.Int(), required=True)) def test(param1): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 400)
class TestRequestBody(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_invalid_payload_format(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): pass headers = {'content-type': 'application/json'} response = self.client.post('/resource', data='invalid data', headers=headers) self.assertEqual(response.status_code, 400) def test_invalid_field(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): self.assertEqual(type(user), User) self.assertEqual(user.username, 'user1') self.assertEqual(user.password, 'pass1') data = UserSchema().dump(User('user1', 'p')).data headers = {'content-type': 'application/json'} response = self.client.post('/resource', data=json.dumps(data), headers=headers) self.assertEqual(response.status_code, 400) def test_valid_fields(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): self.assertEqual(type(user), User) self.assertEqual(user.username, 'user1') self.assertEqual(user.password, 'pass1') data = UserSchema().dump(User('user1', 'pass1')).data headers = {'content-type': 'application/json'} response = self.client.post('/resource', data=json.dumps(data), headers=headers) self.assertEqual(response.status_code, 204) def test_missing_payload(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): pass response = self.client.post('/resource') self.assertEqual(response.status_code, 400) def test_no_content_type(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): self.assertEqual(type(user), User) self.assertEqual(user.username, 'user1') self.assertEqual(user.password, 'pass1') data = UserSchema().dump(User('user1', 'pass1')).data response = self.client.post('/resource', data=json.dumps(data)) self.assertEqual(response.status_code, 204) def test_invalid_content_type(self): @self.app.route('/resource', methods=['POST']) @self.io.from_body('user', UserSchema) def test(user): pass data = UserSchema().dump(User('user1', 'pass1')).data headers = {'content-type': 'application/data'} response = self.client.post('/resource', data=json.dumps(data), headers=headers) self.assertEqual(response.status_code, 415) def test_invalid_accept(self): @self.app.route('/resource', methods=['GET']) def test(): return 'response' headers = {'accept': 'application/data'} response = self.client.get('/resource', headers=headers) self.assertEqual(response.status_code, 406) def test_accept_with_parameters(self): @self.app.route('/resource', methods=['GET']) def test(): return 'response' headers = {'accept': 'application/json;indent=2'} response = self.client.get('/resource', headers=headers) self.assertEqual(response.status_code, 200)
Main file where app-engine runs the website """ from flask import Flask, render_template, jsonify, make_response from flask_io import FlaskIO, fields, Schema from flask_cors import CORS from flask_restful import marshal_with from flask_sqlalchemy import SQLAlchemy from sqlalchemy import desc from os import environ import models from models import Artist, Work, ArtType, Venue, Medium app = Flask(__name__) cors = CORS(app, resources={r"/*": {"origins": "*"}}) io = FlaskIO(app) app.config['SQLALCHEMY_DATABASE_URI'] = environ['DATABASE_URI'] app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) def get_info(page, entries_per_page, num_entries): """ Fill in information for responses containing page number, entries_per_page, and total pages and entries """ return { 'page': page, 'entries_per_page': entries_per_page, 'num_pages': num_entries // entries_per_page + 1, 'num_entries': num_entries
class TestRequestArgs(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.client = self.app.test_client() def test_missing_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): self.assertEqual(param1, None) response = self.client.get('/resource') self.assertEqual(response.status_code, 204) def test_missing_required_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(required=True)) def test(param1): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 400) def test_empty_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): self.assertEqual(param1, None) response = self.client.get('/resource?param1=') self.assertEqual(response.status_code, 204) def test_empty_required_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(required=True)) def test(param1): pass response = self.client.get('/resource?param1=') self.assertEqual(response.status_code, 400) def test_default_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int(missing=10)) def test(param1): self.assertEqual(param1, 10) response = self.client.get('/resource') self.assertEqual(response.status_code, 204) def test_invalid_value(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Int()) def test(param1): pass response = self.client.get('/resource?param1=a') self.assertEqual(response.status_code, 400) def test_success_validate(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(validate=lambda val: 1 <= val <= 10) ) def test(param1): self.assertEqual(param1, 5) response = self.client.get('/resource?param1=5') self.assertEqual(response.status_code, 204) def test_fail_validate(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.Integer(validate=lambda val: 1 <= val <= 10) ) def test(param1): pass response = self.client.get('/resource?param1=11') self.assertEqual(response.status_code, 400) def test_load_from(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param2', fields.Integer(load_from='param1')) def test(param2): self.assertEqual(param2, 10) response = self.client.get('/resource?param1=10') self.assertEqual(response.status_code, 204) def test_list(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.List(fields.Int())) def test(param1): self.assertEqual(param1[0], 10) self.assertEqual(param1[1], 20) response = self.client.get('/resource?param1=10¶m1=20') self.assertEqual(response.status_code, 204) def test_missing_required_list(self): @self.app.route('/resource', methods=['GET']) @self.io.from_query('param1', fields.List(fields.Int(), required=True)) def test(param1): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 400)
def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.io.tracer.enabled = True self.client = self.app.test_client()
class TestTrace(TestCase): def setUp(self): self.app = Flask(__name__) self.io = FlaskIO() self.io.init_app(self.app) self.io.tracer.enabled = True self.client = self.app.test_client() def test_default(self): @self.app.route('/resource') def test(): pass response = self.client.get('/resource') self.assertEqual(response.status_code, 204) def test_handlers(self): self.steps = 0 @self.app.route('/resource') def test(): pass @self.io.trace_inspect() def trace_inspect(data): self.steps += 1 data['entry'] = 'value' @self.io.trace_emit() def trace_emit(data): self.steps += 1 self.assertEqual(data['entry'], 'value') response = self.client.get('/resource') self.assertEqual(response.status_code, 204) self.assertEqual(self.steps, 2) def test_exception(self): self.error = None @self.app.route('/resource') def test(): raise Exception('expected error in test_exception') @self.io.trace_inspect() def trace_inspect(data): self.error = data['error'] response = self.client.get('/resource') self.assertEqual(response.status_code, 500) self.assertIsNotNone(self.error) def test_filter_endpoints(self): self.io.tracer.add_filter(endpoints=['test2']) self.steps = 0 @self.app.route('/test1') def test1(): pass @self.app.route('/test2') def test2(): pass @self.io.trace_emit() def trace_emit(data): self.steps += 1 response = self.client.get('/test1') self.assertEqual(response.status_code, 204) response = self.client.get('/test2') self.assertEqual(response.status_code, 204) self.assertEqual(self.steps, 1) def test_filter_methods(self): self.io.tracer.add_filter(['POST']) self.steps = 0 @self.app.route('/get', methods=['GET']) def get_test(): pass @self.app.route('/post', methods=['POST']) def post_test(): pass @self.io.trace_emit() def trace_emit(data): self.steps += 1 response = self.client.get('/get') self.assertEqual(response.status_code, 204) response = self.client.post('/post') self.assertEqual(response.status_code, 204) self.assertEqual(self.steps, 1)