Test utility for your Swagger documentation.
Project description
This package lets you test the integrity of your OpenAPI/Swagger documentation.
The package has three main features:
Ensuring all documentation complies with a case standard. Available standards include:
camelCase
snake_case
kebab-case
PascalCase
Django Swagger Tester is currently compatible with Swagger documentation generated using drf_yasg, or docs rendered from a schema file (yaml/json). If you’re using another method to generate your documentation and would like to use this library, feel free to add an issue, or create a PR - expanding the package is relatively simple.
Installation
Install using pip:
pip install django-swagger-tester
Configuration
Settings
To add Django Swagger Settings in your project, add a SWAGGER_TESTER object to your settings.py:
SWAGGER_TESTER = {
'CASE': 'camel case',
'PATH': BASE_DIR + '/openapi-schema.yml' # not required for drf_yasg
}
Setting parameters
- CASE
The case standard you wish to enforce for your documentation.
Needs to be one of the following:
camel case
snake case
pascal case
kebab case
None
All documentation is tested to make sure values are correctly cased, unless you specify
None
to skip this feature.Example:
SWAGGER_TESTER = { 'CASE': 'snake case', }
Default:
camel case
- PATH
The path to your OpenAPI specification.
Example:
SWAGGER_TESTER = { 'PATH': BASE_DIR + '/openapi-schema.yml', }
This setting is not required if your swagger docs are generated.
Testing with django Swagger Tester
This document contains an in-depth explanation on how the package works, and how to best implement it.
Response validation
An OpenAPI schema should generally span all APIs you provide. For each API, there may be several methods to document (GET, PUT, POST, DELETE, …), and for each method you may have several responses (200, 400, 401, 404, 5XX). Seeing that at least parts of the OpenAPI schema used for rendering your swagger docs will need to be maintained manually, it is easy to see how bugs might be introduced in the documentation over time. By testing your response documentation against your actual API responses, you can make sure that errors don’t pass silently.
This functionality is currently compatible with rendered static schema, or generated drf_yasg swagger docs.
At the core, the logic for testing an OpenAPI schema is the same, regardless of your Swagger implementation. However, because packages like drf_yasg lets you generate documentation on the fly, we need custom logic for extracting the schema. The result is that we need separate implementations for separate documentation implementations, simply for loading schemas.
The validate_response function
The validate_response function takes three required inputs:
- response
description: This should be the response object returned from an API call. Note: Make sure to pass the response object, not the response data, as we need to match both status_code and json to the OpenAPI schema. type: Response
- method
description: This should be the HTTP method used to get the reponse. type: string example: ‘GET’
- endpoint_url
description: This should be the resolvable path of your endpoint. type: string example: ‘api/v1/test’
In addition, the function also takes one optional input:
- ignore_case
description: List of keys for which we will skip case-validation. This can be useful for when you’ve made a conscious decision to, e.g., keep an acronym upper-cased although you have camelCase as a general standard. type: list of strings example: [‘API’,]
Dynamically rendered OpenAPI schema with drf_yasg
The drf_yasg implementation can be imported from its own project folder:
from django_swagger_tester.response_validation.drf_yasg import validate_response
Statically rendered OpenAPI schema
When testing a static schema (located locally in your project), make sure to point to the right file in the PATH setting.
The static schema implementation can be imported from its own project folder:
from django_swagger_tester.response_validation.static_schema import validate_response
Examples
A pytest implementation might look like this:
def test_response_documentation(client):
response = client.get('api/v1/test/')
assert response.status_code == 200
assert response.json() == expected_response
# Test Swagger documentation
validate_response(response=response, method='GET', endpoint_url='api/v1/test/', ignore_case=[])
A Django-test implementation might look like this:
class MyApiTest(APITestCase):
def setUp(self) -> None:
user, _ = User.objects.update_or_create(username='test_user')
self.client.force_authenticate(user=user)
self.path = '/api/v1/test/'
def test_get_200(self) -> None:
"""
Verifies that a 200 is returned for a valid GET request to the /test/ endpoint.
"""
response = self.client.get(self.path, headers={'Content-Type': 'application/json'})
expected_response = [...]
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected_response)
# Test Swagger documentation
validate_response(response=response, method='GET', endpoint_url=self.path)
You can also test more than a single response at the time.
def test_response_documentation(client):
# 201 - Resource created
response = client.post('api/v1/test/', data=...)
validate_response(response=response, method='POST', endpoint_url='api/v1/test/', ignore_case=[])
# 200 - Idempotency --> if an object exists, return it with a 200 without creating a new resource
response = client.post('api/v1/test/', data=...)
validate_response(response=response, method='POST', endpoint_url='api/v1/test/', ignore_case=[])
# 400 - Bad data
response = client.post('api/v1/test/', data=bad_data)
validate_response(response=response, method='POST', endpoint_url='api/v1/test/', ignore_case=[])
Input validation
Similarly to the response documentation, request body examples should be representative of a functioning request body. If you use Django Rest Framework’s Serializer class for input validation, it is simple to make sure that all your documented request bodies would pass input validation for all endpoints.
This is currently under development and will be added for v1.0.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-swagger-tester-0.2.3.tar.gz
.
File metadata
- Download URL: django-swagger-tester-0.2.3.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.0.0-1035-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0660709152111f75ebd701d62c2303ee0669e72d4a0d1a88765805ef1c057bba |
|
MD5 | 1e816a6f504ce1bc7c6239022994ac5a |
|
BLAKE2b-256 | 2b192a34fa22355dbcee54c21770fdc49086578484be8e7d338234beab7ed022 |
File details
Details for the file django_swagger_tester-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: django_swagger_tester-0.2.3-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.0.0-1035-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95d23ef5510a1cdd02482377e6d8943f9197be798359cc914179c7cbf14d419b |
|
MD5 | fc99c348f5ea2acabf14ded04bf22d06 |
|
BLAKE2b-256 | 90d4549776d8058dbc8cf3143c577d51ab2db1c691b513aea9b436d831e312b0 |