summaryrefslogtreecommitdiff
path: root/nova/tests/functional/test_middleware.py
blob: 3a4d8d64ef028e93cf4f433bb264a096f73a21c0 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Tests to assert that various incorporated middleware works as expected.
"""

from oslo_config import cfg

from nova.tests.functional.api_sample_tests import api_sample_base


class TestCORSMiddleware(api_sample_base.ApiSampleTestBaseV21):
    '''Provide a basic smoke test to ensure CORS middleware is active.

    The tests below provide minimal confirmation that the CORS middleware
    is active, and may be configured. For comprehensive tests, please consult
    the test suite in oslo_middleware.
    '''

    def setUp(self):
        # Here we monkeypatch GroupAttr.__getattr__, necessary because the
        # paste.ini method of initializing this middleware creates its own
        # ConfigOpts instance, bypassing the regular config fixture.
        # Mocking also does not work, as accessing an attribute on a mock
        # object will return a MagicMock instance, which will fail
        # configuration type checks.
        def _mock_getattr(instance, key):
            if key != 'allowed_origin':
                return self._original_call_method(instance, key)
            return "http://valid.example.com"

        self._original_call_method = cfg.ConfigOpts.GroupAttr.__getattr__
        cfg.ConfigOpts.GroupAttr.__getattr__ = _mock_getattr

        # With the project_id in the URL, we get the 300 'multiple choices'
        # response from nova.api.openstack.compute.versions.Versions.
        self.exp_version_status = 300 if self.USE_PROJECT_ID else 200

        # Initialize the application after all the config overrides are in
        # place.
        super(TestCORSMiddleware, self).setUp()

    def tearDown(self):
        super(TestCORSMiddleware, self).tearDown()

        # Reset the configuration overrides.
        cfg.ConfigOpts.GroupAttr.__getattr__ = self._original_call_method

    def test_valid_cors_options_request(self):
        response = self._do_options('servers',
                                    headers={
                                        'Origin': 'http://valid.example.com',
                                        'Access-Control-Request-Method': 'GET'
                                    })

        self.assertEqual(response.status_code, 200)
        self.assertIn('Access-Control-Allow-Origin', response.headers)
        self.assertEqual('http://valid.example.com',
                         response.headers['Access-Control-Allow-Origin'])

    def test_invalid_cors_options_request(self):
        response = self._do_options('servers',
                                    headers={
                                        'Origin': 'http://invalid.example.com',
                                        'Access-Control-Request-Method': 'GET'
                                    })

        self.assertEqual(response.status_code, 200)
        self.assertNotIn('Access-Control-Allow-Origin', response.headers)

    def test_valid_cors_get_request(self):
        response = self._do_get('servers',
                                headers={
                                    'Origin': 'http://valid.example.com',
                                    'Access-Control-Request-Method': 'GET'
                                })

        self.assertEqual(response.status_code, 200)
        self.assertIn('Access-Control-Allow-Origin', response.headers)
        self.assertEqual('http://valid.example.com',
                         response.headers['Access-Control-Allow-Origin'])

    def test_invalid_cors_get_request(self):
        response = self._do_get('servers',
                                headers={
                                    'Origin': 'http://invalid.example.com',
                                    'Access-Control-Request-Method': 'GET'
                                })

        self.assertEqual(response.status_code, 200)
        self.assertNotIn('Access-Control-Allow-Origin', response.headers)

    def test_valid_cors_get_versions_request(self):
        response = self._do_get('',
                                strip_version=True,
                                headers={
                                    'Origin': 'http://valid.example.com',
                                    'Access-Control-Request-Method': 'GET'
                                })

        self.assertEqual(response.status_code, self.exp_version_status)
        self.assertIn('Access-Control-Allow-Origin', response.headers)
        self.assertEqual('http://valid.example.com',
                         response.headers['Access-Control-Allow-Origin'])

    def test_invalid_cors_get_versions_request(self):
        response = self._do_get('',
                                strip_version=True,
                                headers={
                                    'Origin': 'http://invalid.example.com',
                                    'Access-Control-Request-Method': 'GET'
                                })

        self.assertEqual(response.status_code, self.exp_version_status)
        self.assertNotIn('Access-Control-Allow-Origin', response.headers)

    def test_api_version_request_headers(self):
        # Verify allow_headers
        headers = {
            'Origin': 'http://valid.example.com',
            'Access-Control-Request-Method': 'GET',
            'Access-Control-Request-Headers':
                'OpenStack-API-Version, X-OpenStack-Nova-API-Version'
        }
        response = self._do_options('', strip_version=True, headers=headers)

        self.assertEqual(response.status_code, 200)
        self.assertIn('Access-Control-Allow-Headers', response.headers)
        self.assertIn('OpenStack-API-Version',
                      response.headers['Access-Control-Allow-Headers'])
        self.assertIn('X-OpenStack-Nova-API-Version',
                      response.headers['Access-Control-Allow-Headers'])

        # Verify expose_headers
        headers = {'Origin': 'http://valid.example.com'}
        response = self._do_get('', strip_version=True, headers=headers)

        self.assertEqual(response.status_code, self.exp_version_status)
        self.assertIn('Access-Control-Expose-Headers', response.headers)
        self.assertIn('OpenStack-API-Version',
                      response.headers['Access-Control-Expose-Headers'])
        self.assertIn('X-OpenStack-Nova-API-Version',
                      response.headers['Access-Control-Expose-Headers'])