Debian Codegraph @

os_api_ref/tests/test_basic_example.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 """
14 test_os_api_ref
15 ----------------------------------
16
17 Tests for `os_api_ref` module.
18 """
19
20 from bs4 import BeautifulSoup
21
22 from os_api_ref.tests import base
23
24
25 class TestBasicExample(base.TestCase):
26 """Test basic rendering.
27
28 This can be used to test that basic rendering works for these
29 examples, so if someone breaks something we know.
30 """
31
32 @base.with_app(buildername='html', srcdir=base.example_dir('basic'))
33 def setUp(self, app, status, warning):
34 super().setUp()
35 self.app = app
36 self.status = status
37 self.warning = warning
38 self.app.build()
39 self.html = (app.outdir / 'index.html').read_text(encoding='utf-8')
40 self.soup = BeautifulSoup(self.html, 'html.parser')
41 self.content = str(self.soup)
42
43 def test_expand_all(self):
44 """Do we get an expand all button like we expect."""
45 content = str(self.soup.find(id='expand-all'))
46 example_button = ('<button class="btn btn-info btn-sm btn-expand-all" '
47 'data-toggle="collapse" id="expand-all">'
48 'Show All</button>')
49 self.assertEqual(
50 example_button,
51 content)
52
53 def test_rest_method(self):
54 """Do we get a REST method call block"""
55
56 # TODO(sdague): it probably would make sense to do this as a
57 # whole template instead of parts.
58 content = str(self.soup.find_all(class_='operation-grp'))
59 self.assertIn(
60 '<span class="fa fa-link"></span>',
61 str(content))
62 self.assertIn(
63 '<span class="badge label-GET">GET</span>',
64 str(content))
65 self.assertIn(
66 '<div class="endpoint-url">/servers</div>',
67 str(content))
68 self.assertIn(
69 ('<button class="btn btn-info btn-sm btn-detail" '
70 'data-bs-target="#list-servers-detail" data-bs-toggle="collapse" '
71 'id="list-servers-detail-btn">detail</button>'),
72 str(content))
73
74 def test_parameters(self):
75 """Do we get some parameters table"""
76 table = """<table class="docutils align-default">
77 <thead>
78 <tr class="row-odd"><th class="head"><p>Name</p></th>
79 <th class="head"><p>In</p></th>
80 <th class="head"><p>Type</p></th>
81 <th class="head"><p>Description</p></th>
82 </tr>
83 </thead>
84 <tbody>
85 <tr class="row-even"><td><p>name</p></td>
86 <td><p>body</p></td>
87 <td><p>string</p></td>
88 <td><p>The name of things</p></td>
89 </tr>
90 </tbody>
91 </table>"""
92 self.assertIn(table, self.content)
93
94 def test_rest_response(self):
95 success_table = """<table class="docutils align-default">
96 <thead>
97 <tr class="row-odd"><th class="head"><p>Code</p></th>
98 <th class="head"><p>Reason</p></th>
99 </tr>
100 </thead>
101 <tbody>
102 <tr class="row-even"><td><code>200 - OK</code></td>
103 <td><p>Request was successful.</p></td>
104 </tr>
105 <tr class="row-odd"><td><code>100 - Continue</code></td>
106 <td><p>An unusual code for an API</p></td>
107 </tr>
108 <tr class="row-even"><td><code>201 - Created</code></td>
109 <td><p>Resource was created and is ready to use.</p></td>
110 </tr>
111 </tbody>
112 </table>"""
113
114 error_table = """<table class="docutils align-default">
115 <thead>
116 <tr class="row-odd"><th class="head"><p>Code</p></th>
117 <th class="head"><p>Reason</p></th>
118 </tr>
119 </thead>
120 <tbody>
121 <tr class="row-even"><td><code>405 - Method Not Allowed</code></td>
122 <td><p>Method is not valid for this endpoint.</p></td>
123 </tr>
124 <tr class="row-odd"><td><code>403 - Forbidden</code></td>
125 <td><p>Policy does not allow current user to do this operation.</p></td>
126 </tr>
127 <tr class="row-even"><td><code>401 - Unauthorized</code></td>
128 <td><p>User must authenticate before making a request</p></td>
129 </tr>
130 <tr class="row-odd"><td><code>400 - Bad Request</code></td>
131 <td><p>Some content in the request was invalid</p></td>
132 </tr>
133 <tr class="row-even"><td><code>500 - Internal Server Error</code></td>
134 <td><p>Something went wrong inside the service.</p></td>
135 </tr>
136 <tr class="row-odd"><td><code>409 - Conflict</code></td>
137 <td><p>There is already a zone with this name.</p></td>
138 </tr>
139 </tbody>
140 </table>"""
141
142 self.assertIn(success_table, self.content)
143 self.assertIn(error_table, self.content)