Exemplo n.º 1
0
 def test_schema_for_regular_views(self):
     """
     Ensure that schema generation with an API that is not at the URL
     root continues to use correct structure for link keys.
     """
     generator = SchemaGenerator(title='Example API',
                                 patterns=self.patterns)
     schema = generator.get_schema()
     expected = coreapi.Document(
         url='',
         title='Example API',
         content={
             'example': {
                 'create':
                 coreapi.Link(url='/api/v1/example/',
                              action='post',
                              fields=[]),
                 'list':
                 coreapi.Link(url='/api/v1/example/',
                              action='get',
                              fields=[]),
                 'read':
                 coreapi.Link(url='/api/v1/example/{id}/',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path')
                              ]),
                 'sub': {
                     'list':
                     coreapi.Link(url='/api/v1/example/{id}/sub/',
                                  action='get',
                                  fields=[
                                      coreapi.Field('id',
                                                    required=True,
                                                    location='path')
                                  ])
                 }
             }
         })
     self.assertEqual(schema, expected)
Exemplo n.º 2
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()

        return coreapi.Document(
            title=self.title, description=self.description,
            url=url, content=links
        )
Exemplo n.º 3
0
 def test_schema_for_regular_views(self):
     """
     Ensure that schema generation works for ViewSet classes
     with permission classes raising exceptions.
     """
     generator = SchemaGenerator(title='Example API',
                                 patterns=self.patterns)
     request = factory.get('/')
     schema = generator.get_schema(Request(request))
     expected = coreapi.Document(url='http://testserver/',
                                 title='Example API',
                                 content={
                                     'example': {
                                         'list':
                                         coreapi.Link(url='/example/',
                                                      action='get',
                                                      fields=[]),
                                     },
                                 })
     assert schema == expected
 def test_schema_for_regular_views(self):
     """
     Ensure that schema generation works for APIView classes.
     """
     generator = SchemaGenerator(title='Example API', patterns=self.patterns)
     schema = generator.get_schema()
     expected = coreapi.Document(
         url='',
         title='Example API',
         content={
             'example': {
                 'create': coreapi.Link(
                     url='/example/',
                     action='post',
                     fields=[]
                 ),
                 'list': coreapi.Link(
                     url='/example/',
                     action='get',
                     fields=[]
                 ),
                 'read': coreapi.Link(
                     url='/example/{id}/',
                     action='get',
                     fields=[
                         coreapi.Field('id', required=True, location='path', schema=coreschema.String())
                     ]
                 ),
                 'sub': {
                     'list': coreapi.Link(
                         url='/example/{id}/sub/',
                         action='get',
                         fields=[
                             coreapi.Field('id', required=True, location='path', schema=coreschema.String())
                         ]
                     )
                 }
             }
         }
     )
     assert schema == expected
 def test_anonymous_request(self):
     client = APIClient()
     response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
     assert response.status_code == 200
     expected = coreapi.Document(
         url='http://testserver/',
         title='Example API',
         content={
             'example': {
                 'list': coreapi.Link(
                     url='/example/',
                     action='get',
                     fields=[
                         coreapi.Field('page', required=False, location='query', schema=coreschema.Integer(title='Page', description='A page number within the paginated result set.')),
                         coreapi.Field('page_size', required=False, location='query', schema=coreschema.Integer(title='Page size', description='Number of results to return per page.')),
                         coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
                     ]
                 ),
                 'custom_list_action': coreapi.Link(
                     url='/example/custom_list_action/',
                     action='get'
                 ),
                 'custom_list_action_multiple_methods': {
                     'read': coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='get'
                     )
                 },
                 'read': coreapi.Link(
                     url='/example/{id}/',
                     action='get',
                     fields=[
                         coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
                         coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
                     ]
                 )
             }
         }
     )
     assert response.data == expected
 def test_default_actions(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())
                     ]
                 )
             }
         }
     )
     section = schema['users']
     flat_links = schema_links(section)
     assert len(flat_links) is 4
     assert 'list' in flat_links
     assert 'create' in flat_links
     assert 'read' in flat_links
     assert 'update' in flat_links
Exemplo n.º 7
0
 def test_anonymous_request(self):
     client = APIClient()
     response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
     self.assertEqual(response.status_code, 200)
     expected = coreapi.Document(
         url='',
         title='Example API',
         content={
             'example': {
                 'list':
                 coreapi.Link(url='/example/',
                              action='get',
                              fields=[
                                  coreapi.Field('page',
                                                required=False,
                                                location='query'),
                                  coreapi.Field('ordering',
                                                required=False,
                                                location='query')
                              ]),
                 'custom_list_action':
                 coreapi.Link(url='/example/custom_list_action/',
                              action='get'),
                 'custom_list_action_multiple_methods': {
                     'read':
                     coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='get')
                 },
                 'read':
                 coreapi.Link(url='/example/{id}/',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path')
                              ])
             }
         })
     self.assertEqual(response.data, expected)
Exemplo n.º 8
0
    def test_manually_routing_nested_routes(self):
        patterns = [
            url(r'^test', simple_fbv),
            url(r'^test/list/', simple_fbv),
        ]

        generator = SchemaGenerator(title='Naming Colisions', patterns=patterns)
        schema = generator.get_schema()

        expected = coreapi.Document(
            url='',
            title='Naming Colisions',
            content={
                'test': {
                    'list': {
                        'list': coreapi.Link(url='/test/list/', action='get')
                    },
                    'list_0': coreapi.Link(url='/test', action='get')
                }
            }
        )

        assert expected == schema
Exemplo n.º 9
0
 def test_authenticated_request(self):
     client = APIClient()
     client.force_authenticate(MockUser())
     response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
     self.assertEqual(response.status_code, 200)
     expected = coreapi.Document(
         url='',
         title='Example API',
         content={
             'example': {
                 'list':
                 coreapi.Link(url='/example/',
                              action='get',
                              fields=[
                                  coreapi.Field('page',
                                                required=False,
                                                location='query'),
                                  coreapi.Field('ordering',
                                                required=False,
                                                location='query')
                              ]),
                 'create':
                 coreapi.Link(url='/example/',
                              action='post',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field('a',
                                                required=True,
                                                location='form'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form')
                              ]),
                 'retrieve':
                 coreapi.Link(url='/example/{pk}/',
                              action='get',
                              fields=[
                                  coreapi.Field('pk',
                                                required=True,
                                                location='path')
                              ]),
                 'update':
                 coreapi.Link(url='/example/{pk}/',
                              action='put',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field('pk',
                                                required=True,
                                                location='path'),
                                  coreapi.Field('a',
                                                required=True,
                                                location='form'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form')
                              ]),
                 'partial_update':
                 coreapi.Link(url='/example/{pk}/',
                              action='patch',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field('pk',
                                                required=True,
                                                location='path'),
                                  coreapi.Field('a',
                                                required=False,
                                                location='form'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form')
                              ]),
                 'destroy':
                 coreapi.Link(url='/example/{pk}/',
                              action='delete',
                              fields=[
                                  coreapi.Field('pk',
                                                required=True,
                                                location='path')
                              ])
             }
         })
     self.assertEqual(response.data, expected)
Exemplo n.º 10
0
    def test_schema_for_regular_views(self):
        """
        Ensure that schema generation works for ViewSet classes
        with method limitation by Django CBV's http_method_names attribute
        """
        generator = SchemaGenerator(title='Example API',
                                    patterns=self.patterns)
        request = factory.get('/example1/')
        schema = generator.get_schema(Request(request))

        expected = coreapi.Document(
            url='http://testserver/example1/',
            title='Example API',
            content={
                'example1': {
                    'list':
                    coreapi.Link(
                        url='/example1/',
                        action='get',
                        fields=[
                            coreapi.Field(
                                'page',
                                required=False,
                                location='query',
                                schema=coreschema.Integer(
                                    title='Page',
                                    description=
                                    'A page number within the paginated result set.'
                                )),
                            coreapi.Field(
                                'page_size',
                                required=False,
                                location='query',
                                schema=coreschema.Integer(
                                    title='Page size',
                                    description=
                                    'Number of results to return per page.')),
                            coreapi.Field(
                                'ordering',
                                required=False,
                                location='query',
                                schema=coreschema.String(
                                    title='Ordering',
                                    description=
                                    'Which field to use when ordering the results.'
                                ))
                        ]),
                    'custom_list_action':
                    coreapi.Link(url='/example1/custom_list_action/',
                                 action='get'),
                    'custom_list_action_multiple_methods': {
                        'read':
                        coreapi.Link(
                            url=
                            '/example1/custom_list_action_multiple_methods/',
                            action='get')
                    },
                    'read':
                    coreapi.Link(url='/example1/{id}/',
                                 action='get',
                                 fields=[
                                     coreapi.Field('id',
                                                   required=True,
                                                   location='path',
                                                   schema=coreschema.String())
                                 ])
                }
            })
        assert schema == expected
Exemplo n.º 11
0
 def test_authenticated_request(self):
     client = APIClient()
     client.force_authenticate(MockUser())
     response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
     assert response.status_code == 200
     expected = coreapi.Document(
         url='http://testserver/',
         title='Example API',
         content={
             'example': {
                 'list':
                 coreapi.Link(
                     url='/example/',
                     action='get',
                     fields=[
                         coreapi.Field(
                             'page',
                             required=False,
                             location='query',
                             schema=coreschema.Integer(
                                 title='Page',
                                 description=
                                 'A page number within the paginated result set.'
                             )),
                         coreapi.Field(
                             'page_size',
                             required=False,
                             location='query',
                             schema=coreschema.Integer(
                                 title='Page size',
                                 description=
                                 'Number of results to return per page.')),
                         coreapi.Field(
                             'ordering',
                             required=False,
                             location='query',
                             schema=coreschema.String(
                                 title='Ordering',
                                 description=
                                 'Which field to use when ordering the results.'
                             ))
                     ]),
                 'create':
                 coreapi.Link(
                     url='/example/',
                     action='post',
                     encoding='application/json',
                     fields=[
                         coreapi.Field(
                             'a',
                             required=True,
                             location='form',
                             schema=coreschema.String(
                                 title='A',
                                 description='A field description')),
                         coreapi.Field('b',
                                       required=False,
                                       location='form',
                                       schema=coreschema.String(title='B'))
                     ]),
                 'read':
                 coreapi.Link(url='/example/{id}/',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path',
                                                schema=coreschema.String())
                              ]),
                 'custom_action':
                 coreapi.Link(
                     url='/example/{id}/custom_action/',
                     action='post',
                     encoding='application/json',
                     description='A description of custom action.',
                     fields=[
                         coreapi.Field('id',
                                       required=True,
                                       location='path',
                                       schema=coreschema.String()),
                         coreapi.Field('c',
                                       required=True,
                                       location='form',
                                       schema=coreschema.String(title='C')),
                         coreapi.Field('d',
                                       required=False,
                                       location='form',
                                       schema=coreschema.String(title='D')),
                     ]),
                 'custom_list_action':
                 coreapi.Link(url='/example/custom_list_action/',
                              action='get'),
                 'custom_list_action_multiple_methods': {
                     'read':
                     coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='get'),
                     'create':
                     coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='post')
                 },
                 'update':
                 coreapi.Link(
                     url='/example/{id}/',
                     action='put',
                     encoding='application/json',
                     fields=[
                         coreapi.Field('id',
                                       required=True,
                                       location='path',
                                       schema=coreschema.String()),
                         coreapi.Field(
                             'a',
                             required=True,
                             location='form',
                             schema=coreschema.String(
                                 title='A',
                                 description=('A field description'))),
                         coreapi.Field('b',
                                       required=False,
                                       location='form',
                                       schema=coreschema.String(title='B'))
                     ]),
                 'partial_update':
                 coreapi.Link(
                     url='/example/{id}/',
                     action='patch',
                     encoding='application/json',
                     fields=[
                         coreapi.Field('id',
                                       required=True,
                                       location='path',
                                       schema=coreschema.String()),
                         coreapi.Field(
                             'a',
                             required=False,
                             location='form',
                             schema=coreschema.String(
                                 title='A',
                                 description='A field description')),
                         coreapi.Field('b',
                                       required=False,
                                       location='form',
                                       schema=coreschema.String(title='B'))
                     ]),
                 'delete':
                 coreapi.Link(url='/example/{id}/',
                              action='delete',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path',
                                                schema=coreschema.String())
                              ])
             }
         })
     assert response.data == expected
Exemplo n.º 12
0
 def test_authenticated_request(self):
     client = APIClient()
     client.force_authenticate(MockUser())
     response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
     self.assertEqual(response.status_code, 200)
     expected = coreapi.Document(
         url='',
         title='Example API',
         content={
             'example': {
                 'list':
                 coreapi.Link(url='/example/',
                              action='get',
                              fields=[
                                  coreapi.Field('page',
                                                required=False,
                                                location='query'),
                                  coreapi.Field('page_size',
                                                required=False,
                                                location='query'),
                                  coreapi.Field('ordering',
                                                required=False,
                                                location='query')
                              ]),
                 'create':
                 coreapi.Link(url='/example/',
                              action='post',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field(
                                      'a',
                                      required=True,
                                      location='form',
                                      type='string',
                                      description='A field description'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form',
                                                type='string')
                              ]),
                 'read':
                 coreapi.Link(url='/example/{id}/',
                              action='get',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path')
                              ]),
                 'custom_action':
                 coreapi.Link(url='/example/{id}/custom_action/',
                              action='post',
                              encoding='application/json',
                              description='A description of custom action.',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path'),
                                  coreapi.Field('c',
                                                required=True,
                                                location='form',
                                                type='string'),
                                  coreapi.Field('d',
                                                required=False,
                                                location='form',
                                                type='string'),
                              ]),
                 'custom_list_action':
                 coreapi.Link(url='/example/custom_list_action/',
                              action='get'),
                 'custom_list_action_multiple_methods': {
                     'read':
                     coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='get'),
                     'create':
                     coreapi.Link(
                         url='/example/custom_list_action_multiple_methods/',
                         action='post')
                 },
                 'update':
                 coreapi.Link(url='/example/{id}/',
                              action='put',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path'),
                                  coreapi.Field(
                                      'a',
                                      required=True,
                                      location='form',
                                      type='string',
                                      description='A field description'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form',
                                                type='string')
                              ]),
                 'partial_update':
                 coreapi.Link(url='/example/{id}/',
                              action='patch',
                              encoding='application/json',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path'),
                                  coreapi.Field(
                                      'a',
                                      required=False,
                                      location='form',
                                      type='string',
                                      description='A field description'),
                                  coreapi.Field('b',
                                                required=False,
                                                location='form',
                                                type='string')
                              ]),
                 'delete':
                 coreapi.Link(url='/example/{id}/',
                              action='delete',
                              fields=[
                                  coreapi.Field('id',
                                                required=True,
                                                location='path')
                              ])
             }
         })
     self.assertEqual(response.data, expected)
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/')
            }
        })
    def test_multiple_resources_with_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=[])
                    }
                },
                'farmers': {
                    'silo': {
                        'soy': {
                            'list':
                            coreapi.Link(url='/farmers/silo/{id}/soy',
                                         action='get',
                                         fields=[
                                             coreapi.Field(
                                                 'id',
                                                 required=True,
                                                 location='path',
                                                 schema=coreschema.String())
                                         ])
                        },
                        'list':
                        coreapi.Link(url='/farmers/silo',
                                     action='get',
                                     fields=[
                                         coreapi.Field(
                                             'id',
                                             required=True,
                                             location='path',
                                             schema=coreschema.String())
                                     ])
                    }
                }
            })
        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

        section = schema['farmers']
        flat_links = schema_links(section)
        assert len(flat_links) is 2
        assert 'silo > list' in flat_links
        assert 'silo > soy > list' in flat_links