Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/test_apidoc.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import textwrap | |||||
import pytest | import pytest | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from swh.storage.exc import StorageDBError, StorageAPIError | from swh.storage.exc import StorageDBError, StorageAPIError | ||||
from swh.web.api.apidoc import api_doc, _parse_httpdomain_doc | from swh.web.api.apidoc import api_doc, _parse_httpdomain_doc | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.common.exc import BadInputExc, ForbiddenExc, NotFoundExc | from swh.web.common.exc import BadInputExc, ForbiddenExc, NotFoundExc | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse, prettify_html | ||||
from swh.web.tests.django_asserts import assert_template_used, assert_contains | from swh.web.tests.django_asserts import assert_template_used | ||||
_httpdomain_doc = """ | _httpdomain_doc = """ | ||||
.. http:get:: /api/1/revision/(sha1_git)/ | .. http:get:: /api/1/revision/(sha1_git)/ | ||||
Get information about a revision in the archive. | Get information about a revision in the archive. | ||||
Revisions are identified by **sha1** checksums, compatible with Git commit | Revisions are identified by **sha1** checksums, compatible with Git commit | ||||
identifiers. | identifiers. | ||||
▲ Show 20 Lines • Show All 199 Lines • ▼ Show 20 Lines | def test_api_doc_parse_httpdomain(): | ||||
assert doc_data['args'] == expected_args | assert doc_data['args'] == expected_args | ||||
expected_params = [] | expected_params = [] | ||||
assert 'params' in doc_data | assert 'params' in doc_data | ||||
assert doc_data['params'] == expected_params | assert doc_data['params'] == expected_params | ||||
expected_reqheaders = [{ | expected_reqheaders = [{ | ||||
'doc': ('the requested response content type, either ' | 'doc': ('the requested response content type, either ' | ||||
'``application/json`` or ``application/yaml``'), | '``application/json`` (default) or ``application/yaml``'), | ||||
'name': 'Accept' | 'name': 'Accept' | ||||
}] | }] | ||||
assert 'reqheaders' in doc_data | assert 'reqheaders' in doc_data | ||||
assert doc_data['reqheaders'] == expected_reqheaders | assert doc_data['reqheaders'] == expected_reqheaders | ||||
expected_resheaders = [{ | expected_resheaders = [{ | ||||
'doc': 'this depends on **Accept** header of request', | 'doc': 'this depends on **Accept** header of request', | ||||
▲ Show 20 Lines • Show All 132 Lines • ▼ Show 20 Lines | |||||
def apidoc_test_post_endpoint(request): | def apidoc_test_post_endpoint(request): | ||||
""" | """ | ||||
.. http:post:: /api/1/post/endpoint/ | .. http:post:: /api/1/post/endpoint/ | ||||
Endpoint documentation | Endpoint documentation | ||||
:<jsonarr string -: Input array of pids | :<jsonarr string -: Input array of pids | ||||
:>jsonarr string type: swh object type | :>json object <swh_pid>: an object whose keys are input persistent | ||||
:>jsonarr string sha1_git: swh object sha1_git | identifiers and values objects with the following keys: | ||||
:>jsonarr boolean found: whether the object was found or not | |||||
* **known (bool)**: whether the object was found | |||||
""" | """ | ||||
pass | pass | ||||
def test_apidoc_input_output_doc(client): | def test_apidoc_input_output_doc(client): | ||||
url = reverse('api-1-post-endpoint-doc') | url = reverse('api-1-post-endpoint-doc') | ||||
rv = client.get(url, HTTP_ACCEPT='text/html') | rv = client.get(url, HTTP_ACCEPT='text/html') | ||||
assert rv.status_code == 200, rv.content | assert rv.status_code == 200, rv.content | ||||
assert_template_used(rv, 'api/apidoc.html') | assert_template_used(rv, 'api/apidoc.html') | ||||
input_html_doc = ( | input_html_doc = textwrap.indent(( | ||||
' <dl class="row">\n' | '<dl class="row">\n' | ||||
' <dt class="col col-md-2 text-right"> array </dt>\n' | ' <dt class="col col-md-2 text-right">\n' | ||||
' array\n' | |||||
' </dt>\n' | |||||
' <dd class="col col-md-9">\n' | ' <dd class="col col-md-9">\n' | ||||
' <p>\n' | ' <p>\n' | ||||
' \n' | |||||
' Input array of pids\n' | ' Input array of pids\n' | ||||
' \n' | |||||
' \n' | |||||
' </p>\n' | ' </p>\n' | ||||
' </dd>\n' | ' </dd>\n' | ||||
' </dl>\n' | '</dl>\n' | ||||
) | ), ' '*7) | ||||
output_html_doc = ( | output_html_doc = textwrap.indent(( | ||||
' <dl class="row">\n' | '<dl class="row">\n' | ||||
' <dt class="col col-md-2 text-right"> array </dt>\n' | ' <dt class="col col-md-2 text-right">\n' | ||||
' object\n' | |||||
' </dt>\n' | |||||
' <dd class="col col-md-9">\n' | ' <dd class="col col-md-9">\n' | ||||
' <p>\n' | ' <p>\n' | ||||
' \n' | ' an object containing the following keys:\n' | ||||
' an array of objects containing the following keys:\n' | ' </p>\n' | ||||
' \n' | ' <div class="swh-rst">\n' | ||||
' \n' | ' <blockquote>\n' | ||||
' <div class="swh-rst"><ul class="simple">\n' | ' <ul>\n' | ||||
'<li><p><strong>type (string)</strong>: swh object type</p></li>\n' | ' <li>\n' | ||||
'<li><p><strong>sha1_git (string)</strong>: swh object sha1_git</p></li>\n' # noqa | ' <p>\n' | ||||
'<li><p><strong>found (boolean)</strong>: whether the object was found or not</p></li>\n' # noqa | ' <strong>\n' | ||||
' <swh_pid> (object)\n' | |||||
' </strong>\n' | |||||
' : an object whose keys are input persistent identifiers' | |||||
' and values objects with the following keys:\n' | |||||
' </p>\n' | |||||
' <blockquote>\n' | |||||
' <ul class="simple">\n' | |||||
' <li>\n' | |||||
' <p>\n' | |||||
' <strong>\n' | |||||
' known (bool)\n' | |||||
' </strong>\n' | |||||
' : whether the object was found\n' | |||||
' </p>\n' | |||||
' </li>\n' | |||||
' </ul>\n' | |||||
' </blockquote>\n' | |||||
' </li>\n' | |||||
'</ul>\n' | ' </ul>\n' | ||||
' </blockquote>\n' | |||||
'</div>\n' | ' </div>\n' | ||||
' \n' | |||||
' </p>\n' | |||||
' </dd>\n' | ' </dd>\n' | ||||
' </dl>' | '</dl>\n' | ||||
) | ), ' '*7) | ||||
html = prettify_html(rv.content) | |||||
assert_contains(rv, input_html_doc) | assert input_html_doc in html | ||||
assert_contains(rv, output_html_doc) | assert output_html_doc in html |