summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/tests/test_docs.py
blob: 5291edb3bb77f6ef1ecebf351ea3503b091f5a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# # -*- coding: utf-8 -*-
from __future__ import absolute_import

from ..api.doc import SchemaType
from ..controllers import ENDPOINT_MAP, APIDoc, APIRouter, Endpoint, EndpointDoc, RESTController
from ..controllers._version import APIVersion
from ..controllers.docs import Docs
from ..tests import ControllerTestCase


# Dummy controller and endpoint that can be assigned with @EndpointDoc and @GroupDoc
@APIDoc("Group description", group="FooGroup")
@APIRouter("/doctest/", secure=False)
class DecoratedController(RESTController):
    RESOURCE_ID = 'doctest'

    @EndpointDoc(
        description="Endpoint description",
        group="BarGroup",
        parameters={
            'parameter': (int, "Description of parameter"),
        },
        responses={
            200: [{
                'my_prop': (str, '200 property desc.')
            }],
            202: {
                'my_prop': (str, '202 property desc.')
            },
        },
    )
    @Endpoint(json_response=False)
    @RESTController.Resource('PUT', version=APIVersion(0, 1))
    def decorated_func(self, parameter):
        pass

    @RESTController.MethodMap(version=APIVersion(0, 1))
    def list(self):
        pass


# To assure functionality of @EndpointDoc, @GroupDoc
class DocDecoratorsTest(ControllerTestCase):
    @classmethod
    def setup_server(cls):
        cls.setup_controllers([DecoratedController, Docs], "/test")

    def test_group_info_attr(self):
        test_ctrl = DecoratedController()
        self.assertTrue(hasattr(test_ctrl, 'doc_info'))
        self.assertIn('tag_descr', test_ctrl.doc_info)
        self.assertIn('tag', test_ctrl.doc_info)

    def test_endpoint_info_attr(self):
        test_ctrl = DecoratedController()
        test_endpoint = test_ctrl.decorated_func
        self.assertTrue(hasattr(test_endpoint, 'doc_info'))
        self.assertIn('summary', test_endpoint.doc_info)
        self.assertIn('tag', test_endpoint.doc_info)
        self.assertIn('parameters', test_endpoint.doc_info)
        self.assertIn('response', test_endpoint.doc_info)


# To assure functionality of Docs.py
# pylint: disable=protected-access
class DocsTest(ControllerTestCase):
    @classmethod
    def setup_server(cls):
        ENDPOINT_MAP.clear()
        cls.setup_controllers([DecoratedController, Docs], "/test")

    def test_type_to_str(self):
        self.assertEqual(Docs()._type_to_str(str), str(SchemaType.STRING))
        self.assertEqual(Docs()._type_to_str(int), str(SchemaType.INTEGER))
        self.assertEqual(Docs()._type_to_str(bool), str(SchemaType.BOOLEAN))
        self.assertEqual(Docs()._type_to_str(list), str(SchemaType.ARRAY))
        self.assertEqual(Docs()._type_to_str(tuple), str(SchemaType.ARRAY))
        self.assertEqual(Docs()._type_to_str(float), str(SchemaType.NUMBER))
        self.assertEqual(Docs()._type_to_str(object), str(SchemaType.OBJECT))
        self.assertEqual(Docs()._type_to_str(None), str(SchemaType.OBJECT))

    def test_gen_paths(self):
        outcome = Docs().gen_paths(False)['/api/doctest//{doctest}/decorated_func']['put']
        self.assertIn('tags', outcome)
        self.assertIn('summary', outcome)
        self.assertIn('parameters', outcome)
        self.assertIn('responses', outcome)

        expected_response_content = {
            '200': {
                APIVersion(0, 1).to_mime_type(): {
                    'schema': {'type': 'array',
                               'items': {'type': 'object', 'properties': {
                                   'my_prop': {
                                       'type': 'string',
                                       'description': '200 property desc.'}}},
                               'required': ['my_prop']}}},
            '202': {
                APIVersion(0, 1).to_mime_type(): {
                    'schema': {'type': 'object',
                               'properties': {'my_prop': {
                                   'type': 'string',
                                   'description': '202 property desc.'}},
                               'required': ['my_prop']}}
            }
        }
        # Check that a schema of type 'array' is received in the response.
        self.assertEqual(expected_response_content['200'], outcome['responses']['200']['content'])
        # Check that a schema of type 'object' is received in the response.
        self.assertEqual(expected_response_content['202'], outcome['responses']['202']['content'])

    def test_gen_method_paths(self):
        outcome = Docs().gen_paths(False)['/api/doctest/']['get']

        self.assertEqual({APIVersion(0, 1).to_mime_type(): {'type': 'object'}},
                         outcome['responses']['200']['content'])

    def test_gen_paths_all(self):
        paths = Docs().gen_paths(False)
        for key in paths:
            self.assertTrue(any(base in key.split('/')[1] for base in ['api', 'ui-api']))

    def test_gen_tags(self):
        outcome = Docs._gen_tags(False)
        self.assertEqual([{'description': 'Group description', 'name': 'FooGroup'}], outcome)