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