Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/test_apidoc.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 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 | ||||
from nose.tools import istest, nottest | |||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
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.tests.testcase import SWHWebTestCase | from swh.web.tests.testcase import SWHWebTestCase | ||||
# flake8: noqa | # flake8: noqa | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/revision/(sha1_git)/ | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
$ curl -i :swh_web_api:`revision/aafb16d69fd30ff58afdd69036a26047f3aebdc6/` | $ curl -i :swh_web_api:`revision/aafb16d69fd30ff58afdd69036a26047f3aebdc6/` | ||||
""" | """ | ||||
class APIDocTestCase(SWHWebTestCase, APITestCase): | class APIDocTestCase(SWHWebTestCase, APITestCase): | ||||
@istest | def test_apidoc_nodoc_failure(self): | ||||
def apidoc_nodoc_failure(self): | |||||
with self.assertRaises(Exception): | with self.assertRaises(Exception): | ||||
@api_doc('/my/nodoc/url/') | @api_doc('/my/nodoc/url/') | ||||
def apidoc_nodoc_tester(request, arga=0, argb=0): | def apidoc_nodoc_tester(request, arga=0, argb=0): | ||||
return Response(arga + argb) | return Response(arga + argb) | ||||
@staticmethod | @staticmethod | ||||
@api_route(r'/some/(?P<myarg>[0-9]+)/(?P<myotherarg>[0-9]+)/', | @api_route(r'/some/(?P<myarg>[0-9]+)/(?P<myotherarg>[0-9]+)/', | ||||
'some-doc-route') | 'some-doc-route') | ||||
@api_doc('/some/doc/route/') | @api_doc('/some/doc/route/') | ||||
@nottest | def apidoc_route(request, myarg, myotherarg, akw=0): | ||||
def apidoc_route_tester(request, myarg, myotherarg, akw=0): | |||||
""" | """ | ||||
Sample doc | Sample doc | ||||
""" | """ | ||||
return {'result': int(myarg) + int(myotherarg) + akw} | return {'result': int(myarg) + int(myotherarg) + akw} | ||||
@istest | def test_apidoc_route_doc(self): | ||||
def apidoc_route_doc(self): | |||||
# when | # when | ||||
rv = self.client.get('/api/1/some/doc/route/') | rv = self.client.get('/api/1/some/doc/route/') | ||||
# then | # then | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
self.assertTemplateUsed('api/apidoc.html') | self.assertTemplateUsed('api/apidoc.html') | ||||
@istest | def test_apidoc_route_fn(self): | ||||
def apidoc_route_fn(self): | |||||
# when | # when | ||||
rv = self.client.get('/api/1/some/1/1/') | rv = self.client.get('/api/1/some/1/1/') | ||||
# then | # then | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
@staticmethod | @staticmethod | ||||
@api_route(r'/some/full/(?P<myarg>[0-9]+)/(?P<myotherarg>[0-9]+)/', | @api_route(r'/some/full/(?P<myarg>[0-9]+)/(?P<myotherarg>[0-9]+)/', | ||||
'some-complete-doc-route') | 'some-complete-doc-route') | ||||
@api_doc('/some/complete/doc/route/') | @api_doc('/some/complete/doc/route/') | ||||
@nottest | def apidoc_full_stack(request, myarg, myotherarg, akw=0): | ||||
def apidoc_full_stack_tester(request, myarg, myotherarg, akw=0): | |||||
""" | """ | ||||
Sample doc | Sample doc | ||||
""" | """ | ||||
return {'result': int(myarg) + int(myotherarg) + akw} | return {'result': int(myarg) + int(myotherarg) + akw} | ||||
@istest | def test_apidoc_full_stack_doc(self): | ||||
def apidoc_full_stack_doc(self): | |||||
# when | # when | ||||
rv = self.client.get('/api/1/some/complete/doc/route/') | rv = self.client.get('/api/1/some/complete/doc/route/') | ||||
# then | # then | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
self.assertTemplateUsed('api/apidoc.html') | self.assertTemplateUsed('api/apidoc.html') | ||||
@istest | def test_apidoc_full_stack_fn(self): | ||||
def apidoc_full_stack_fn(self): | |||||
# when | # when | ||||
rv = self.client.get('/api/1/some/full/1/1/') | rv = self.client.get('/api/1/some/full/1/1/') | ||||
# then | # then | ||||
self.assertEqual(rv.status_code, 200) | self.assertEqual(rv.status_code, 200) | ||||
@istest | |||||
def test_api_doc_parse_httpdomain(self): | def test_api_doc_parse_httpdomain(self): | ||||
doc_data = { | doc_data = { | ||||
'description': '', | 'description': '', | ||||
'urls': [], | 'urls': [], | ||||
'args': [], | 'args': [], | ||||
'params': [], | 'params': [], | ||||
'resheaders': [], | 'resheaders': [], | ||||
'reqheaders': [], | 'reqheaders': [], | ||||
▲ Show 20 Lines • Show All 150 Lines • Show Last 20 Lines |