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_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_set_add_resource_url_for(api_v1, client): characters_resource = Resource('characters', __name__, url_prefix='/characters') characters_resource.add_endpoint(CharactersEndpoint) api_v1.register_resource(characters_resource) url = url_for('characters.list') assert url == '/v1/characters'
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_set_add_multiple_resources_with_no_api_prefix(app, client): api = ArrestedAPI(app) characters_resource = Resource('characters', __name__, url_prefix='/characters') 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) resp = client.get('/characters') assert resp.data == bytes( json.dumps({ 'payload': _get_character_objects() }).encode('utf-8')) resp = client.get('/planets') assert resp.data == bytes( json.dumps({ 'payload': _get_planet_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_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_register_all(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') example_resource_2 = Resource('example_2', __name__, url_prefix='/example-2') api_v1.register_all([example_resource, example_resource_2]) assert app.blueprints == { 'example': example_resource, 'example_2': example_resource_2 }
def test_set_add_resource_endpoint_with_prefix(api_v1, client): characters_resource = Resource('characters', __name__, url_prefix='/characters') characters_resource.add_endpoint(CharactersEndpoint) api_v1.register_resource(characters_resource) resp = client.get('/v1/characters') assert resp.data == bytes( json.dumps({ 'payload': _get_character_objects() }).encode('utf-8'))
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_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 }
def test_set_add_resource_endpoint_with_url_params(api_v1, client): characters_resource = Resource('characters', __name__, url_prefix='/jedis') class JediObjectEndpoint(CharacterEndpoint): url = '/<string:obj_id>' def get_object(self): return _get_character_objects()[1] characters_resource.add_endpoint(JediObjectEndpoint) api_v1.register_resource(characters_resource) resp = client.get('/v1/jedis/1') assert resp.data == bytes( json.dumps({ 'payload': _get_character_objects()[1] }).encode('utf-8'))
def test_set_add_resource_endpoint_with_no_prefix(api_v1, client): characters_resource = Resource('characters', __name__) class JedisEndpoint(CharactersEndpoint): url = '/jedis' def get_objects(self): return [_get_character_objects()[1]] characters_resource.add_endpoint(JedisEndpoint) api_v1.register_resource(characters_resource) resp = client.get('/v1/jedis') assert resp.data == bytes( json.dumps({ 'payload': [_get_character_objects()[1]] }).encode('utf-8'))
from arrested import Resource, Endpoint, GetObjectMixin from resume_api.models import db, Project, EmploymentExperience, School, TechnicalExperience, WeaponOfChoice resume_resource = Resource('resume', __name__, url_prefix='/resume') class ResumeIndexEndpoint(Endpoint, GetObjectMixin): name = 'object' def get_object(self): stmt = { 'projects': [i.serialize for i in Project.query.all()], 'employmentExperiences': [i.serialize for i in EmploymentExperience.query.all()], 'schools': [i.serialize for i in School.query.all()], 'technicalExperiences': [i.serialize for i in TechnicalExperience.query.all()], 'weaponsOfChoice': [i.serialize for i in WeaponOfChoice.query.all()] } return stmt resume_resource.add_endpoint(ResumeIndexEndpoint)
from arrested import Resource, Endpoint, GetObjectMixin from flask import g token_resource = Resource('token', __name__, url_prefix='/token') class TokenIndexEndpoint(Endpoint, GetObjectMixin): name = 'object' def get_object(self): token = g.user.generate_auth_token() return {'token': token.decode('ascii')} token_resource.add_endpoint(TokenIndexEndpoint)
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') vac_certification_resource = Resource('vac_certification', __name__, url_prefix='/vac-certification') assignment_and_relocation_resource = Resource('assignment_and_relocation', __name__, url_prefix='/assignment-and-relocation') vacation_resource = Resource('vacation', __name__, url_prefix='/vacation') retraining_resource = Resource('retraining', __name__, url_prefix='/retraining') kvalif_up_resource = Resource('kvalif_up', __name__, url_prefix='/kvalif-up') users_resource.add_endpoint(UsersIndexEndpoint) users_resource.add_endpoint(UsersObjectEndpoint) worker_resource.add_endpoint(WorkerIndexEndpoint) worker_resource.add_endpoint(WorkerObjectEndpoint) vac_certification_resource.add_endpoint(Vac_certificationIndexEndpoint)
Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() """ user = Users('admin','*****@*****.**','admin') session.add(user) session.commit() """ otdels_resource = Resource('otdel', __name__, url_prefix='/otdel') users_resource = Resource('user', __name__, url_prefix='/user') specials_resource = Resource('special', __name__, url_prefix='/special') groups_resource = Resource('group', __name__, url_prefix='/group') students_resource = Resource('student', __name__, url_prefix='/student') teachers_resource = Resource('teacher', __name__, url_prefix='/teacher') rooms_resource = Resource('room', __name__, url_prefix='/room') predmets_resource = Resource('predmet', __name__, url_prefix='/predmet') posts_resource = Resource('post',__name__,url_prefix='/post') users_resource.add_endpoint(UsersIndexEndpoint) users_resource.add_endpoint(UsersObjectEndpoint) otdels_resource.add_endpoint(OtdelsIndexEndpoint)
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from resume_api.models import db, TechnicalExperience from .mappers import TechnicalExperienceMapper technical_experiences_resource = Resource('technical_experience', __name__, url_prefix='/technical_experiences') class TechnicalExperiencesIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = TechnicalExperienceMapper model = TechnicalExperience def get_query(self): stmt = db.session.query(TechnicalExperience) return stmt class TechnicalExperienceObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = TechnicalExperienceMapper model = TechnicalExperience def get_query(self):
def test_set_resource_name(): cr = Resource('characters', __name__, url_prefix='/characters') assert cr.name == 'characters'
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from resume_api.models import db, Project from .mappers import ProjectMapper projects_resource = Resource('projects', __name__, url_prefix='/projects') class ProjectsIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = ProjectMapper model = Project def get_query(self): stmt = db.session.query(Project) return stmt class ProjectObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = ProjectMapper model = Project def get_query(self): stmt = db.session.query(Project) return stmt
def test_set_resource_url_prefix(api_v1): cr = Resource('characters', __name__, url_prefix='/characters') assert cr.url_prefix == '/characters'
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from API.models import db, User from .mappers import UserMapper users_resource = Resource('users', __name__, url_prefix='/users') class UsersIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = UserMapper model = User def get_query(self): stmt = db.session.query(User) return stmt class UserObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' mapper_class = UserMapper model = User def get_query(self):
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from resume_api.models import db, School from .mappers import SchoolMapper schools_resource = Resource('schools', __name__, url_prefix='/schools') class SchoolsIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = SchoolMapper model = School def get_query(self): stmt = db.session.query(School) return stmt class SchoolObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = SchoolMapper model = School def get_query(self): stmt = db.session.query(School) return stmt
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from resume_api.models import db, EmploymentExperience from .mappers import EmploymentExperienceMapper employment_experiences_resource = Resource( 'employment_experiences', __name__, url_prefix='/employment_experiences') class EmploymentExperiencesIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = EmploymentExperienceMapper model = EmploymentExperience def get_query(self): stmt = db.session.query(EmploymentExperience) return stmt class EmploymentExperienceObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = EmploymentExperienceMapper model = EmploymentExperience def get_query(self): stmt = db.session.query(EmploymentExperience)
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from favourite_books.models import db, Book from .mappers import BookMapper books_resource = Resource('books', __name__, url_prefix='/books') class BooksIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = BookMapper model = Book def get_query(self): stmt = db.session.query(Book) return stmt class BookObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = BookMapper model = Book def get_query(self):
from arrested import Resource from arrested.contrib.kim_arrested import KimEndpoint from arrested.contrib.sql_alchemy import DBListMixin, DBCreateMixin, DBObjectMixin from python_star_wars_api.models import db, Character from .mappers import CharacterMapper characters_resource = Resource('characters', __name__, url_prefix = '/characters') class CharactersIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin): name = 'list' many = True mapper_class = CharacterMapper model = Character def get_query(self): stmt = db.session.query(Character) return stmt class CharactersObjectEndpoint(KimEndpoint, DBObjectMixin): name = 'object' url = '/<string:obj_id>' mapper_class = CharacterMapper model = Character def get_query(self): stmt = db.session.query(Character) return stmt characters_resource.add_endpoint(CharactersIndexEndpoint) characters_resource.add_endpoint(CharactersObjectEndpoint)