Пример #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']
Пример #2
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
Пример #3
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'))
Пример #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']
Пример #5
0
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}
Пример #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'
Пример #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'
Пример #8
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
Пример #9
0
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
    }
Пример #10
0
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)
Пример #11
0
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)
Пример #12
0
            payload = {
                "message": "Character object not found.",
            }
            self.return_error(404, payload=payload)

        return obj

    def update_object(self, obj):

        data = self.request.data
        allowed_fields = ['name']

        for key, val in data.items():
            if key in allowed_fields:
                setattr(obj, key, val)

        db.session.add(obj)
        db.session.commit()

        return obj

    def delete_object(self, obj):

        db.session.delete(obj)
        db.session.commit()


characters_resource.add_endpoint(CharactersIndexEndpoint)
characters_resource.add_endpoint(CharacterObjectEndpoint)
api_v1.register_resource(characters_resource)
Пример #13
0
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)



api_v1.register_resource(users_resource)
api_v1.register_resource(worker_resource)
api_v1.register_resource(vac_certification_resource)
api_v1.register_resource(assignment_and_relocation_resource)
api_v1.register_resource(vacation_resource)
api_v1.register_resource(retraining_resource)
api_v1.register_resource(kvalif_up_resource)
Пример #14
0
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)




api_v1.register_resource(users_resource)
api_v1.register_resource(otdels_resource)
api_v1.register_resource(specials_resource)
api_v1.register_resource(groups_resource)
api_v1.register_resource(rooms_resource)
api_v1.register_resource(students_resource)
api_v1.register_resource(teachers_resource)
api_v1.register_resource(predmets_resource)
api_v1.register_resource(posts_resource)