def test_register_resource(app): """Test that Resources are properly reigstered as a blueprint when ArrestedAPI.register_resource is called. """ api_v1 = ArrestedAPI(app) example_resource = Resource('example', __name__, url_prefix='/example') api_v1.register_resource(example_resource) assert app.blueprints == {'example': example_resource}
def test_api_request_middleware(app, client): evts = [] def api_before_func(*args, **kwarsg): evts.append('api_before') return None def api_after_func(endpoint, response): response.data += b'|api_after' evts.append('api_after') return response def resource_before_func(endpoint): evts.append('resource_before') return None def resource_after_func(endpoint, response): response.data += b'|resource_after' evts.append('resource_after') return response api_v1 = ArrestedAPI(app, url_prefix='/v1', before_all_hooks=[api_before_func], after_all_hooks=[api_after_func]) example_resource = Resource('example', __name__, url_prefix='/example', before_all_hooks=[resource_before_func], after_all_hooks=[resource_after_func]) class MyEndpoint(Endpoint): name = 'test' def get(self, *args, **kwargs): assert 'api_before' in evts assert 'api_after' not in evts assert 'resource_before' in evts assert 'resource_after' not in evts return 'request' example_resource.add_endpoint(MyEndpoint) api_v1.register_resource(example_resource, ) resp = client.get(url_for('example.test')) assert resp.data == b'request|resource_after|api_after' assert evts == [ 'api_before', 'resource_before', 'resource_after', 'api_after' ]
def test_register_resource_with_url_prefix(app): """Test that the url_prefix is correctly applied to all resources when provided """ api_v1 = ArrestedAPI(app, url_prefix='/v1') example_resource = Resource('example', __name__, url_prefix='/example') class MyEndpoint(Endpoint): name = 'test' example_resource.add_endpoint(MyEndpoint) api_v1.register_resource(example_resource) assert url_for('example.test') == '/v1/example'
def test_resource_before_request_middleware(app, client): api = ArrestedAPI(app) log_request = MagicMock(return_value=None) characters_resource = Resource('characters', __name__, url_prefix='/characters', before_all_hooks=[log_request]) characters_resource.add_endpoint(CharactersEndpoint) api.register_resource(characters_resource) client.get('/characters') assert log_request.called
def test_set_add_resource_endpoint_with_no_api_prefix(app, client): api = ArrestedAPI(app) characters_resource = Resource('characters', __name__, url_prefix='/characters') characters_resource.add_endpoint(CharactersEndpoint) api.register_resource(characters_resource) resp = client.get('/characters') assert resp.data == bytes( json.dumps({ 'payload': _get_character_objects() }).encode('utf-8'))
def test_middleware_only_applied_to_resources_endpoints(app, client): log_request = MagicMock(return_value=None) def log_middleware(endpoint, response): log_request() return response api = ArrestedAPI(app) characters_resource = Resource('characters', __name__, url_prefix='/characters', after_all_hooks=[log_middleware]) planets_resource = Resource('planets', __name__, url_prefix='/planets') characters_resource.add_endpoint(CharactersEndpoint) planets_resource.add_endpoint(PlanetsEndpoint) api.register_resource(characters_resource) api.register_resource(planets_resource) client.get('/characters') assert log_request.call_count == 1 client.get('/planets') assert log_request.call_count == 1
def test_process_before_request_hooks_processed_in_order(app): call_sequence = [] def test_hook(type_, sequence): def hook(endpoint): sequence.append(type_) return hook api_hook = test_hook('api', call_sequence) resource_hook = test_hook('resource', call_sequence) endpoint_hook = test_hook('endpoint', call_sequence) endpoint_get_hook = test_hook('endpoint_get', call_sequence) my_api = ArrestedAPI(before_all_hooks=[api_hook], url_prefix='/') my_api.init_app(app) my_resource = Resource('test', __name__, before_all_hooks=[resource_hook]) class MyEndpoint(Endpoint): before_all_hooks = [endpoint_hook] before_get_hooks = [endpoint_get_hook] url = '' my_resource.add_endpoint(Endpoint) my_api.register_resource(my_resource) endpoint = MyEndpoint() endpoint.resource = my_resource endpoint.meth = 'get' endpoint.process_before_request_hooks() assert call_sequence == ['api', 'resource', 'endpoint', 'endpoint_get']
def test_process_after_request_hooks_processed_in_order(app): call_sequence = [] def test_hook(type_, sequence): def hook(endpoint, resp): sequence.append(type_) return hook api_hook = test_hook('api', call_sequence) resource_hook = test_hook('resource', call_sequence) endpoint_hook = test_hook('endpoint', call_sequence) endpoint_get_hook = test_hook('endpoint_get', call_sequence) my_api = ArrestedAPI(after_all_hooks=[api_hook], url_prefix='/') my_api.init_app(app) my_resource = Resource('test', __name__, after_all_hooks=[resource_hook]) class MyEndpoint(Endpoint): after_all_hooks = [endpoint_hook] after_get_hooks = [endpoint_get_hook] url = '' my_resource.add_endpoint(Endpoint) my_api.register_resource(my_resource) endpoint = MyEndpoint() endpoint.resource = my_resource endpoint.meth = 'get' resp = MagicMock(spec=Response()) endpoint.process_after_request_hooks(resp) assert call_sequence == ['endpoint_get', 'endpoint', 'resource', 'api']
def test_defer_resource_registration(app): """Test that Resources are properly reigstered as a blueprint when ArrestedAPI.register_resource is called. """ api_v1 = ArrestedAPI() example_resource = Resource('example', __name__, url_prefix='/example') example_resource_2 = Resource('example_2', __name__, url_prefix='/example-2') api_v1.register_resource(example_resource, defer=True) api_v1.register_resource(example_resource_2, defer=True) assert app.blueprints == {} api_v1.init_app(app) assert app.blueprints == { 'example': example_resource, 'example_2': example_resource_2 }
from arrested import ArrestedAPI from flask import jsonify, g, make_response, request from flask_httpauth import HTTPBasicAuth from resume_api.models import User from .users import users_resource from .projects import projects_resource from .schools import schools_resource from .weapons_of_choice import weapons_of_choice_resource from .employment_experiences import employment_experiences_resource from .technical_experiences import technical_experiences_resource from .resume import resume_resource from .token import token_resource from .middleware import verify_password api_v1 = ArrestedAPI(url_prefix='/v1') api_v1_w_auth = ArrestedAPI(url_prefix='/v1', before_all_hooks=[verify_password]) api_v1_w_auth.register_resource(users_resource, defer=True) api_v1_w_auth.register_resource(projects_resource, defer=True) api_v1_w_auth.register_resource(schools_resource, defer=True) api_v1_w_auth.register_resource(weapons_of_choice_resource, defer=True) api_v1_w_auth.register_resource(employment_experiences_resource, defer=True) api_v1_w_auth.register_resource(technical_experiences_resource, defer=True) api_v1_w_auth.register_resource(token_resource, defer=True) api_v1.register_resource(resume_resource, defer=True)
from arrested import ArrestedAPI from .users import users_resource from .middleware import get_client_token, get_api_client_from_request from .books import books_resource api_v1 = ArrestedAPI( url_prefix='/v1', before_all_hooks=[get_api_client_from_request, get_client_token]) api_v1.register_resource(users_resource, defer=True) api_v1.register_resource(books_resource, defer=True)
from flask import Flask from arrested import (ArrestedAPI, Resource, Endpoint, GetListMixin, CreateMixin, GetObjectMixin, PutObjectMixin, DeleteObjectMixin, ResponseHandler) from example.models import db, Character app = Flask(__name__) api_v1 = ArrestedAPI(app, url_prefix='/v1') app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'sqlite:////opt/code/example/starwars.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) characters_resource = Resource('characters', __name__, url_prefix='/characters') def character_serializer(obj): return { 'id': obj.id, 'name': obj.name, 'created_at': obj.created_at.isoformat() } class DBResponseHandler(ResponseHandler): def __init__(self, endpoint, *args, **params): super(DBResponseHandler, self).__init__(endpoint, *args, **params)
from arrested import ( ArrestedAPI, Resource, Endpoint, GetListMixin, CreateMixin, GetObjectMixin, PutObjectMixin, DeleteObjectMixin, ResponseHandler ) from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, DateTime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker #from kim import field from table_class import user, vac_certification from config import SQLALCHEMY_DB_URI api_v1 = ArrestedAPI(app,url_prefix="/v1") app.config["SQLALCHEMY_DB_URI"] = SQLALCHEMY_DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' engine = create_engine(app.config['SQLALCHEMY_DB_URI'], echo=True) metadata = MetaData() Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() users_resource = Resource('user', __name__, url_prefix='/user') worker_resource = Resource('worker', __name__, url_prefix='/worker')
def initialise_app_via_constructor(app): """Test instantiating ArrestedAPI obj passing flask app object directly """ api_v1 = ArrestedAPI(app) assert api_v1.app == app
def defer_app_initialisation(app): """Test deferring initialising the flask app object using init_app method. """ api_v1 = ArrestedAPI() api_v1.init_app(app) assert api_v1.app == app
) from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, DateTime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker #from kim import field from table_class import group,otdel,room,special,student,teacher,user,connectJournal, journalToDay,journalToWeek, ball, post from config import SQLALCHEMY_DB_URI # <option value="{{ conJ.id }}" >{{ predmet.name + ' | '+ room.name + ' | '+ userr.lastname + ' ' + userr.firstname + ' ' + userr.patr}}</option> #curl -X DELETE localhost:5000/v1/users/1 api_v1 = ArrestedAPI(app,url_prefix="/v1") app.config["SQLALCHEMY_DB_URI"] = SQLALCHEMY_DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' engine = create_engine(app.config['SQLALCHEMY_DB_URI'], echo=True) metadata = MetaData() Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() """ user = Users('admin','*****@*****.**','admin')
from arrested import ArrestedAPI from .users import users_resource from .middleware import basic_auth api_v1 = ArrestedAPI(url_prefix='/v1', before_all_hooks=[basic_auth]) api_v1.register_resource(users_resource, defer=True)
def api_v1(app): api_v1 = ArrestedAPI(app, url_prefix='/v1') return api_v1