]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/tests/test_docs.py
import ceph pacific 16.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_docs.py
CommitLineData
11fdf7f2
TL
1# # -*- coding: utf-8 -*-
2from __future__ import absolute_import
3
f67539c2
TL
4from ..api.doc import SchemaType
5from ..controllers import ApiController, ControllerDoc, Endpoint, EndpointDoc, RESTController
11fdf7f2 6from ..controllers.docs import Docs
f67539c2 7from . import ControllerTestCase # pylint: disable=no-name-in-module
11fdf7f2
TL
8
9
10# Dummy controller and endpoint that can be assigned with @EndpointDoc and @GroupDoc
11@ControllerDoc("Group description", group="FooGroup")
12@ApiController("/doctest/", secure=False)
13class DecoratedController(RESTController):
f67539c2
TL
14 RESOURCE_ID = 'doctest'
15
11fdf7f2
TL
16 @EndpointDoc(
17 description="Endpoint description",
18 group="BarGroup",
19 parameters={
20 'parameter': (int, "Description of parameter"),
21 },
22 responses={
f67539c2
TL
23 200: [{
24 'my_prop': (str, '200 property desc.')
25 }],
26 202: {
27 'my_prop': (str, '202 property desc.')
11fdf7f2
TL
28 },
29 },
30 )
31 @Endpoint(json_response=False)
b3b6e05e 32 @RESTController.Resource('PUT', version='0.1')
11fdf7f2
TL
33 def decorated_func(self, parameter):
34 pass
35
b3b6e05e
TL
36 @RESTController.MethodMap(version='0.1')
37 def list(self):
38 pass
39
11fdf7f2
TL
40
41# To assure functionality of @EndpointDoc, @GroupDoc
42class DocDecoratorsTest(ControllerTestCase):
43 @classmethod
44 def setup_server(cls):
45 cls.setup_controllers([DecoratedController, Docs], "/test")
46
47 def test_group_info_attr(self):
48 test_ctrl = DecoratedController()
49 self.assertTrue(hasattr(test_ctrl, 'doc_info'))
50 self.assertIn('tag_descr', test_ctrl.doc_info)
51 self.assertIn('tag', test_ctrl.doc_info)
52
53 def test_endpoint_info_attr(self):
54 test_ctrl = DecoratedController()
55 test_endpoint = test_ctrl.decorated_func
56 self.assertTrue(hasattr(test_endpoint, 'doc_info'))
57 self.assertIn('summary', test_endpoint.doc_info)
58 self.assertIn('tag', test_endpoint.doc_info)
59 self.assertIn('parameters', test_endpoint.doc_info)
60 self.assertIn('response', test_endpoint.doc_info)
61
62
63# To assure functionality of Docs.py
64# pylint: disable=protected-access
65class DocsTest(ControllerTestCase):
66 @classmethod
67 def setup_server(cls):
f67539c2 68 cls.setup_controllers([DecoratedController, Docs], "/test")
11fdf7f2
TL
69
70 def test_type_to_str(self):
f67539c2
TL
71 self.assertEqual(Docs()._type_to_str(str), str(SchemaType.STRING))
72 self.assertEqual(Docs()._type_to_str(int), str(SchemaType.INTEGER))
73 self.assertEqual(Docs()._type_to_str(bool), str(SchemaType.BOOLEAN))
74 self.assertEqual(Docs()._type_to_str(list), str(SchemaType.ARRAY))
75 self.assertEqual(Docs()._type_to_str(tuple), str(SchemaType.ARRAY))
76 self.assertEqual(Docs()._type_to_str(float), str(SchemaType.NUMBER))
77 self.assertEqual(Docs()._type_to_str(object), str(SchemaType.OBJECT))
78 self.assertEqual(Docs()._type_to_str(None), str(SchemaType.OBJECT))
11fdf7f2
TL
79
80 def test_gen_paths(self):
b3b6e05e 81 outcome = Docs().gen_paths(False)['/api/doctest//{doctest}/decorated_func']['put']
11fdf7f2
TL
82 self.assertIn('tags', outcome)
83 self.assertIn('summary', outcome)
84 self.assertIn('parameters', outcome)
85 self.assertIn('responses', outcome)
86
f67539c2
TL
87 expected_response_content = {
88 '200': {
b3b6e05e 89 'application/vnd.ceph.api.v0.1+json': {
f67539c2
TL
90 'schema': {'type': 'array',
91 'items': {'type': 'object', 'properties': {
92 'my_prop': {
93 'type': 'string',
94 'description': '200 property desc.'}}},
95 'required': ['my_prop']}}},
96 '202': {
b3b6e05e 97 'application/vnd.ceph.api.v0.1+json': {
f67539c2
TL
98 'schema': {'type': 'object',
99 'properties': {'my_prop': {
100 'type': 'string',
101 'description': '202 property desc.'}},
102 'required': ['my_prop']}}
103 }
104 }
105 # Check that a schema of type 'array' is received in the response.
106 self.assertEqual(expected_response_content['200'], outcome['responses']['200']['content'])
107 # Check that a schema of type 'object' is received in the response.
108 self.assertEqual(expected_response_content['202'], outcome['responses']['202']['content'])
109
b3b6e05e
TL
110 def test_gen_method_paths(self):
111 outcome = Docs().gen_paths(False)['/api/doctest/']['get']
112
113 self.assertEqual({'application/vnd.ceph.api.v0.1+json': {'type': 'object'}},
114 outcome['responses']['200']['content'])
115
f6b5b4d7 116 def test_gen_paths_all(self):
b3b6e05e 117 paths = Docs().gen_paths(False)
f6b5b4d7
TL
118 for key in paths:
119 self.assertTrue(any(base in key.split('/')[1] for base in ['api', 'ui-api']))
120
11fdf7f2
TL
121 def test_gen_tags(self):
122 outcome = Docs()._gen_tags(False)[0]
123 self.assertEqual({'description': 'Group description', 'name': 'FooGroup'}, outcome)