Esempio n. 1
0
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']
Esempio n. 2
0
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'))
Esempio n. 3
0
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
Esempio n. 4
0
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']
Esempio n. 5
0
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'
Esempio n. 6
0
def test_api_request_middleware_limited_to_api(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])
    api_v2 = ArrestedAPI(
        app,
        url_prefix='/v2',
    )
    example_resource = Resource('example',
                                __name__,
                                url_prefix='/example',
                                before_all_hooks=[resource_before_func],
                                after_all_hooks=[resource_after_func])
    example2_resource = Resource('example2', __name__, url_prefix='/example2')

    class MyEndpoint(Endpoint):

        name = 'test'

        def get(self, *args, **kwargs):

            assert 'api_before' not in evts
            assert 'api_after' not in evts
            assert 'resource_before' not in evts
            assert 'resource_after' not in evts
            return 'request'

    example_resource.add_endpoint(MyEndpoint)
    example2_resource.add_endpoint(MyEndpoint)
    api_v1.register_resource(example_resource, )
    api_v2.register_resource(example2_resource, )

    resp = client.get(url_for('example2.test'))
    assert resp.data == b'request'
Esempio n. 7
0
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'
Esempio n. 8
0
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'))
Esempio n. 9
0
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
Esempio n. 10
0
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'))
Esempio n. 11
0
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'))
Esempio n. 12
0
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)
Esempio n. 13
0
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)
Esempio n. 14
0
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)
vac_certification_resource.add_endpoint(Vac_certificationObjectEndpoint)
assignment_and_relocation_resource.add_endpoint(Assignment_and_relocationIndexEndpoint)
assignment_and_relocation_resource.add_endpoint(Assignment_and_relocationObjectEndpoint)
vacation_resource.add_endpoint(VacationIndexEndpoint)
vacation_resource.add_endpoint(VacationObjectEndpoint)
retraining_resource.add_endpoint(RetrainingIndexEndpoint)
retraining_resource.add_endpoint(RetrainingObjectEndpoint)
kvalif_up_resource.add_endpoint(Kvalif_upIndexEndpoint)
kvalif_up_resource.add_endpoint(Kvalif_upObjectEndpoint)

Esempio n. 15
0

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)
otdels_resource.add_endpoint(OtdelsObjectEndpoint)
specials_resource.add_endpoint(SpecialsIndexEndpoint)
specials_resource.add_endpoint(SpecialsObjectEndpoint)
rooms_resource.add_endpoint(RoomsIndexEndpoint)
rooms_resource.add_endpoint(RoomsObjectEndpoint)
groups_resource.add_endpoint(GroupsIndexEndpoint)
groups_resource.add_endpoint(GroupsObjectEndpoint)
students_resource.add_endpoint(StudentIndexEndpoint)
students_resource.add_endpoint(StudentObjectEndpoint)
teachers_resource.add_endpoint(TeachersIndexEndpoint)
teachers_resource.add_endpoint(TeachersObjectEndpoint)
predmets_resource.add_endpoint(PredmetsIndexEndpoint)
predmets_resource.add_endpoint(PredmetsObjectEndpoint)
Esempio n. 16
0
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):
        stmt = db.session.query(TechnicalExperience)
        return stmt


technical_experiences_resource.add_endpoint(TechnicalExperiencesIndexEndpoint)
technical_experiences_resource.add_endpoint(TechnicalExperienceObjectEndpoint)
Esempio n. 17
0
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


projects_resource.add_endpoint(ProjectsIndexEndpoint)
projects_resource.add_endpoint(ProjectObjectEndpoint)
Esempio n. 18
0
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):

        stmt = db.session.query(User)
        return stmt


users_resource.add_endpoint(UsersIndexEndpoint)
users_resource.add_endpoint(UserObjectEndpoint)
Esempio n. 19
0
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

schools_resource.add_endpoint(SchoolsIndexEndpoint)
schools_resource.add_endpoint(SchoolObjectEndpoint)
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)
        return stmt


employment_experiences_resource.add_endpoint(
    EmploymentExperiencesIndexEndpoint)
employment_experiences_resource.add_endpoint(
    EmploymentExperienceObjectEndpoint)
Esempio n. 21
0
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):

        stmt = db.session.query(Book)
        return stmt


books_resource.add_endpoint(BooksIndexEndpoint)
books_resource.add_endpoint(BookObjectEndpoint)
Esempio n. 22
0
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)