def test_resource_schema_get_request_body_success_case(
    get_create_view_func,
    resource_default_ref,
    resource_another_ref,
):
    create_view = get_create_view_func('test',
                                       DefaultAnotherResourceViewSet,
                                       'test',
                                       router=ResourceRouter())
    resource_default_wq_ref = resource_default_ref('WQ')
    resource_another_wq_ref = resource_another_ref('WQ')

    view = create_view('/test/', 'POST')
    view.resource_discriminate_methods = ['POST']
    request_body = view.schema.get_request_body('/test/', 'POST')
    content = request_body['content']

    assert content['application/vnd.vendor']['schema']['oneOf'] == [
        resource_default_wq_ref, resource_another_wq_ref
    ]
    assert content['application/vnd.vendor.v1-default'][
        'schema'] == resource_default_wq_ref
    assert content['application/vnd.vendor.v1-another'][
        'schema'] == resource_another_wq_ref
    assert content['application/vnd.vendor.v1-actions'][
        'schema'] == resource_default_wq_ref
예제 #2
0
def test_resource_get_tags_from_default_success_case(get_create_view_func, resource_class, settings):
    settings.API_RESOURCE_DEFAULT = 'default'
    viewset_class = resource_class('default', ViewSetWithTags)
    create_view = get_create_view_func('test', viewset_class, 'test', router=ResourceRouter())

    view = create_view('/test/', 'GET')
    operation = view.schema.get_operation('/test/', 'GET')

    assert operation['tags'] == ['tag1', 'tag2']
예제 #3
0
def test_resource_schema_get_request_body_schema_success_case(get_create_view_func):
    create_view = get_create_view_func(
        'test', DefaultAnotherResourceViewSet, 'test', router=ResourceRouter())

    view = create_view('/test/', 'POST')
    view.resource_discriminate_methods = ['POST']
    request_body = view.schema.get_resources_request_body_schema('/test/', 'POST')

    assert len(request_body.get('oneOf', [])) == 2
예제 #4
0
def test_resource_schema_query_params_from_default_view_success_case(get_create_view_func):
    create_view = get_create_view_func(
        'test', DefaultAnotherResourceViewSet, 'test', router=ResourceRouter())

    view = create_view('/test/', 'GET')
    view.pagination_class = None
    operation = view.schema.get_operation('/test/', 'GET')
    parameters = operation.get('parameters', [])

    assert parameters != []
예제 #5
0
def test_get_response_schema_success_case(get_create_view_func, method):
    create_view = get_create_view_func('test',
                                       DefaultAnotherResourceViewSet,
                                       'test',
                                       router=ResourceRouter())

    view = create_view('/test/', method)
    response_schema = view.schema.get_resources_response_schema(
        '/test/', method)

    assert len(response_schema.get('oneOf', [])) == 2
def test_resource_schema_get_response_schema_right_method_success_case(
        settings, get_create_view_func):
    create_view = get_create_view_func('test',
                                       DefaultAnotherResourceViewSet,
                                       'test',
                                       router=ResourceRouter())

    view = create_view('/test/', 'GET')
    response_schema = view.schema.get_resources_response_schema(
        '/test/', 'GET')

    assert len(response_schema.get('oneOf', [])) == 2
예제 #7
0
def test_query_params_for_viewset_success_case(get_create_view_func,
                                               viewset_class):
    create_view = get_create_view_func('test',
                                       viewset_class,
                                       'test',
                                       router=ResourceRouter())

    view = create_view('/test/', 'GET')
    view.pagination_class = None
    operation = view.schema.get_operation('/test/', 'GET')
    parameters = operation.get('parameters', [])

    assert parameters != []
예제 #8
0
def test_resource_schema_get_action_responses_success_case(
    get_create_view_func, resource_default_schema, resource_another_schema,
):
    create_view = get_create_view_func(
        'test', DefaultAnotherResourceViewSet, 'test', router=ResourceRouter())

    view = create_view('/test/123/custom_action/', 'PUT')
    view.resource_discriminate_methods = ['PUT']
    responses = view.schema.get_responses('/test/123/custom_action/', 'PUT')
    content = responses['201']['content']

    assert content['application/vnd.vendor']['schema'] == resource_default_schema
    assert 'application/vnd.vendor.v1-default' not in content
    assert 'application/vnd.vendor.v1-another' not in content
    assert content['application/vnd.vendor.v1-actions']['schema'] == resource_default_schema
예제 #9
0
def test_get_action_responses_success_case(get_create_view_func,
                                           resource_default_rq_schema):
    create_view = get_create_view_func('test',
                                       DefaultAnotherResourceViewSet,
                                       'test',
                                       router=ResourceRouter())

    view = create_view('/test/123/custom_action/', 'PUT')
    responses = view.schema.get_responses('/test/123/custom_action/', 'PUT')
    responses_codes = set(responses.keys())
    content = responses['201']['content']

    assert responses_codes == {'201', '400', '404'}
    assert content['application/vnd.vendor'][
        'schema'] == resource_default_rq_schema
    assert 'application/vnd.vendor.v1-default' not in content
    assert 'application/vnd.vendor.v1-another' not in content
    assert content['application/vnd.vendor.v1-actions'][
        'schema'] == resource_default_rq_schema
예제 #10
0
from __future__ import annotations

import typing

from django.urls import path, include

from restdoctor.rest_framework.routers import ResourceRouter
from restdoctor.utils.api_prefix import get_api_path_prefixes
from tests.stubs.views import EmptyView, MyModelResourceViewSet

if typing.TYPE_CHECKING:
    from restdoctor.django.custom_types import URLPatternList

api_prefixes = get_api_path_prefixes()

router = ResourceRouter()
router.register('mymodel', MyModelResourceViewSet, basename='my-model')

api_urlpatterns: URLPatternList = [
    path('', EmptyView.as_view(), name='empty_view'),
    path('empty_v1', EmptyView.as_view(), name='empty_view_with_version'),
    *router.urls,
]

urlpatterns: URLPatternList = []

for api_prefix in api_prefixes:
    prefix_urlpattern = path(api_prefix, include((api_urlpatterns, 'api')))
    urlpatterns.append(prefix_urlpattern)
예제 #11
0
def test_resource_schema_get_response_schema_wrong_method_success_case(get_create_view_func):
    create_view = get_create_view_func('test', DefaultAnotherResourceViewSet, 'test', router=ResourceRouter())

    view = create_view('/test/', 'POST')
    response_schema = view.schema._get_response_schema('/test/', 'POST')

    print(view.schema._get_resources('POST'))
    print(response_schema)

    assert len(response_schema.get('oneOf', [])) == 0