def test_document_with_link_named_data(self):
        """
        Ref #5395: Doc's `document.data` would fail with a Link named "data".
            As per #4972, use templatetag instead.
        """
        document = coreapi.Document(title='Data Endpoint API',
                                    url='https://api.example.org/',
                                    content={
                                        'data':
                                        coreapi.Link(
                                            url='/data/',
                                            action='get',
                                            fields=[],
                                            description='Return data.')
                                    })

        factory = APIRequestFactory()
        request = factory.get('/')

        renderer = DocumentationRenderer()

        html = renderer.render(document,
                               accepted_media_type="text/html",
                               renderer_context={"request": request})
        assert '<h1>Data Endpoint API</h1>' in html
    def test_encode_coreapi_raises_error(self):
        """
        Tests encoding a coreapi objects raises proper error
        """
        with pytest.raises(RuntimeError):
            self.encoder.default(coreapi.Document())

        with pytest.raises(RuntimeError):
            self.encoder.default(coreapi.Error())
 def test_shell_code_example_rendering(self):
     template = loader.get_template('mind_core/docs/langs/shell.html')
     context = {
         'document': coreapi.Document(url='https://api.example.org/'),
         'link_key': 'testcases > list',
         'link': coreapi.Link(url='/data/', action='get', fields=[]),
     }
     html = template.render(context)
     assert 'testcases list' in html
 def test_schema_with_empty_links(self):
     schema = coreapi.Document(url='',
                               title='Example API',
                               content={'users': {
                                   'list': {}
                               }})
     section = schema['users']
     flat_links = schema_links(section)
     assert len(flat_links) is 0
 def test_multiple_nested_routes(self):
     schema = coreapi.Document(
         url='',
         title='Example API',
         content={
             'animals': {
                 'dog': {
                     'vet': {
                         'list':
                         coreapi.Link(url='/animals/dog/{id}/vet',
                                      action='get',
                                      fields=[
                                          coreapi.Field(
                                              'id',
                                              required=True,
                                              location='path',
                                              schema=coreschema.String())
                                      ])
                     },
                     'read':
                     coreapi.Link(url='/animals/dog/{id}',
                                  action='get',
                                  fields=[
                                      coreapi.Field(
                                          'id',
                                          required=True,
                                          location='path',
                                          schema=coreschema.String())
                                  ])
                 },
                 'cat': {
                     'list':
                     coreapi.Link(url='/animals/cat/',
                                  action='get',
                                  fields=[
                                      coreapi.Field(
                                          'id',
                                          required=True,
                                          location='path',
                                          schema=coreschema.String())
                                  ]),
                     'create':
                     coreapi.Link(url='/aniamls/cat',
                                  action='post',
                                  fields=[])
                 }
             }
         })
     section = schema['animals']
     flat_links = schema_links(section)
     assert len(flat_links) is 4
     assert 'cat > create' in flat_links
     assert 'cat > list' in flat_links
     assert 'dog > read' in flat_links
     assert 'dog > vet > list' in flat_links
 def test_single_action(self):
     schema = coreapi.Document(url='',
                               title='Example API',
                               content={
                                   'users': {
                                       'list':
                                       coreapi.Link(url='/users/',
                                                    action='get',
                                                    fields=[])
                                   }
                               })
     section = schema['users']
     flat_links = schema_links(section)
     assert len(flat_links) is 1
     assert 'list' in flat_links
 def test_default_actions_and_single_custom_action(self):
     schema = coreapi.Document(
         url='',
         title='Example API',
         content={
             'users': {
                 'create':
                 coreapi.Link(url='/users/', action='post', fields=[]),
                 'list':
                 coreapi.Link(url='/users/', action='get', fields=[]),
                 'read':
                 coreapi.Link(url='/users/{id}/',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path',
                                                schema=coreschema.String())
                              ]),
                 'update':
                 coreapi.Link(url='/users/{id}/',
                              action='patch',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path',
                                                schema=coreschema.String())
                              ]),
                 'friends':
                 coreapi.Link(url='/users/{id}/friends',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path',
                                                schema=coreschema.String())
                              ])
             }
         })
     section = schema['users']
     flat_links = schema_links(section)
     assert len(flat_links) is 5
     assert 'list' in flat_links
     assert 'create' in flat_links
     assert 'read' in flat_links
     assert 'update' in flat_links
     assert 'friends' in flat_links
def get_schema():
    return coreapi.Document(
        url='https://api.example.com/',
        title='Example API',
        content={
            'simple_link': coreapi.Link('/example/', description='example link'),
            'headers': coreapi.Link('/headers/'),
            'location': {
                'query': coreapi.Link('/example/', fields=[
                    coreapi.Field(name='example', schema=coreschema.String(description='example field'))
                ]),
                'form': coreapi.Link('/example/', action='post', fields=[
                    coreapi.Field(name='example')
                ]),
                'body': coreapi.Link('/example/', action='post', fields=[
                    coreapi.Field(name='example', location='body')
                ]),
                'path': coreapi.Link('/example/{id}', fields=[
                    coreapi.Field(name='id', location='path')
                ])
            },
            'encoding': {
                'multipart': coreapi.Link('/example/', action='post', encoding='multipart/form-data', fields=[
                    coreapi.Field(name='example')
                ]),
                'multipart-body': coreapi.Link('/example/', action='post', encoding='multipart/form-data', fields=[
                    coreapi.Field(name='example', location='body')
                ]),
                'urlencoded': coreapi.Link('/example/', action='post', encoding='application/x-www-form-urlencoded', fields=[
                    coreapi.Field(name='example')
                ]),
                'urlencoded-body': coreapi.Link('/example/', action='post', encoding='application/x-www-form-urlencoded', fields=[
                    coreapi.Field(name='example', location='body')
                ]),
                'raw_upload': coreapi.Link('/upload/', action='post', encoding='application/octet-stream', fields=[
                    coreapi.Field(name='example', location='body')
                ]),
            },
            'response': {
                'download': coreapi.Link('/download/'),
                'text': coreapi.Link('/text/')
            }
        }
    )
Пример #9
0
    def get_schema(self, request=None, public=False):
        """
        Generate a `coreapi.Document` representing the API schema.
        """
        if self.endpoints is None:
            inspector = self.endpoint_inspector_cls(self.patterns, self.urlconf)
            self.endpoints = inspector.get_api_endpoints()

        links = self.get_links(None if public else request)
        if not links:
            return None

        url = self.url
        if not url and request is not None:
            url = request.build_absolute_uri()

        distribute_links(links)
        return coreapi.Document(
            title=self.title, description=self.description,
            url=url, content=links
        )