Coverage for manila/tests/api/test_middleware.py: 100%
37 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright (c) 2015 Hewlett Packard Enterprise Development LP
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied. See the License for the specific language governing permissions and
13# limitations under the License.
15import ddt
16from oslo_config import cfg
17from oslo_serialization import jsonutils
18import requests
20from manila.tests.integrated import integrated_helpers
23@ddt.ddt
24class TestCORSMiddleware(integrated_helpers._IntegratedTestBase):
25 '''Provide a basic smoke test to ensure CORS middleware is active.
27 The tests below provide minimal confirmation that the CORS middleware
28 is active, and may be configured. For comprehensive tests, please consult
29 the test suite in oslo_middleware.
30 '''
32 def setUp(self):
33 # Here we monkeypatch GroupAttr.__getattr__, necessary because the
34 # paste.ini method of initializing this middleware creates its own
35 # ConfigOpts instance, bypassing the regular config fixture.
36 # Mocking also does not work, as accessing an attribute on a mock
37 # object will return a MagicMock instance, which will fail
38 # configuration type checks.
39 def _mock_getattr(instance, key):
40 if key != 'allowed_origin':
41 return self._original_call_method(instance, key)
42 return ["http://valid.example.com"]
44 self._original_call_method = cfg.ConfigOpts.GroupAttr.__getattr__
45 cfg.ConfigOpts.GroupAttr.__getattr__ = _mock_getattr
47 # Initialize the application after all the config overrides are in
48 # place.
49 super(TestCORSMiddleware, self).setUp()
51 def tearDown(self):
52 super(TestCORSMiddleware, self).tearDown()
54 # Reset the configuration overrides.
55 cfg.ConfigOpts.GroupAttr.__getattr__ = self._original_call_method
57 @ddt.data(
58 ('http://valid.example.com', 'http://valid.example.com'),
59 ('http://invalid.example.com', None),
60 )
61 @ddt.unpack
62 def test_options_request(self, origin_url, acao_header_expected):
63 response = self.api.api_request(
64 '',
65 method='OPTIONS',
66 headers={
67 'Origin': origin_url,
68 'Access-Control-Request-Method': 'GET',
69 }
70 )
71 self.assertEqual(200, response.status_code)
72 self.assertEqual(acao_header_expected,
73 response.headers.get('Access-Control-Allow-Origin'))
75 @ddt.data(
76 ('http://valid.example.com', 'http://valid.example.com'),
77 ('http://invalid.example.com', None),
78 )
79 @ddt.unpack
80 def test_get_request(self, origin_url, acao_header_expected):
81 response = self.api.api_request(
82 '',
83 method='GET',
84 headers={
85 'Origin': origin_url
86 }
87 )
88 self.assertEqual(404, response.status_code)
89 self.assertEqual(acao_header_expected,
90 response.headers.get('Access-Control-Allow-Origin'))
93class TestHealthCheckMiddleware(integrated_helpers._IntegratedTestBase):
95 def test_healthcheck(self):
96 # We verify that we return a HTTP200 when calling api_get
97 url = 'http://%s:%s/healthcheck' % (self.osapi.host, self.osapi.port)
98 response = requests.request(
99 'GET',
100 url,
101 headers={'Accept': 'application/json'})
102 output = jsonutils.loads(response.content)
103 self.assertEqual(200, response.status_code)
104 self.assertEqual(['OK'], output['reasons'])