summaryrefslogtreecommitdiff
path: root/heat/tests/test_auth_url.py
blob: 585d5b7e81035075e05ecb8eed62152c8d826dbb (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
#
# Copyright 2013 OpenStack Foundation
#
# 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.

from unittest import mock

import webob
from webob import exc

from heat.common import auth_url
from heat.tests import common


class FakeApp(object):
    """This represents a WSGI app protected by our auth middleware."""

    def __call__(self, environ, start_response):
        """Assert that headers are correctly set up when finally called."""
        resp = webob.Response()
        resp.body = 'SUCCESS'.encode('latin-1')
        return resp(environ, start_response)


class AuthUrlFilterTest(common.HeatTestCase):

    def setUp(self):
        super(AuthUrlFilterTest, self).setUp()
        self.app = FakeApp()
        self.config = {'auth_uri': 'foobar'}
        self.middleware = auth_url.AuthUrlFilter(self.app, self.config)

    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_adds_default_auth_url_from_clients_keystone(self, mock_cfg):
        self.config = {}
        mock_cfg.clients_keystone.auth_uri = 'foobar'
        mock_cfg.keystone_authtoken.www_authenticate_uri = 'should-be-ignored'
        mock_cfg.auth_password.multi_cloud = False
        with mock.patch('keystoneauth1.discover.Discover') as discover:
            class MockDiscover(object):
                def url_for(self, endpoint):
                    return 'foobar/v3'
            discover.return_value = MockDiscover()
            self.middleware = auth_url.AuthUrlFilter(self.app, self.config)
            req = webob.Request.blank('/tenant_id/')
            self.middleware(req)
            self.assertIn('X-Auth-Url', req.headers)
            self.assertEqual('foobar/v3', req.headers['X-Auth-Url'])

    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_adds_default_auth_url_from_keystone_authtoken(self, mock_cfg):
        self.config = {}
        mock_cfg.clients_keystone.auth_uri = ''
        mock_cfg.keystone_authtoken.www_authenticate_uri = 'foobar'
        mock_cfg.auth_password.multi_cloud = False
        self.middleware = auth_url.AuthUrlFilter(self.app, self.config)
        req = webob.Request.blank('/tenant_id/')
        self.middleware(req)
        self.assertIn('X-Auth-Url', req.headers)
        self.assertEqual('foobar', req.headers['X-Auth-Url'])

    def test_overwrites_auth_url_from_headers_with_local_config(self):
        req = webob.Request.blank('/tenant_id/')
        req.headers['X-Auth-Url'] = 'should_be_overwritten'
        self.middleware(req)
        self.assertEqual('foobar', req.headers['X-Auth-Url'])

    def test_reads_auth_url_from_local_config(self):
        req = webob.Request.blank('/tenant_id/')
        self.middleware(req)
        self.assertIn('X-Auth-Url', req.headers)
        self.assertEqual('foobar', req.headers['X-Auth-Url'])

    @mock.patch.object(auth_url.AuthUrlFilter, '_validate_auth_url')
    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_multicloud_reads_auth_url_from_headers(self, mock_cfg, mock_val):
        mock_cfg.auth_password.multi_cloud = True
        mock_val.return_value = True
        req = webob.Request.blank('/tenant_id/')
        req.headers['X-Auth-Url'] = 'overwrites config'
        self.middleware(req)
        self.assertIn('X-Auth-Url', req.headers)
        self.assertEqual('overwrites config', req.headers['X-Auth-Url'])

    @mock.patch.object(auth_url.AuthUrlFilter, '_validate_auth_url')
    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_multicloud_validates_auth_url(self, mock_cfg, mock_validate):
        mock_cfg.auth_password.multi_cloud = True
        req = webob.Request.blank('/tenant_id/')

        self.middleware(req)
        self.assertTrue(mock_validate.called)

    def test_validate_auth_url_with_missing_url(self):
        self.assertRaises(exc.HTTPBadRequest,
                          self.middleware._validate_auth_url,
                          auth_url='')

        self.assertRaises(exc.HTTPBadRequest,
                          self.middleware._validate_auth_url,
                          auth_url=None)

    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_validate_auth_url_with_url_not_allowed(self, mock_cfg):
        mock_cfg.auth_password.allowed_auth_uris = ['foobar']

        self.assertRaises(exc.HTTPUnauthorized,
                          self.middleware._validate_auth_url,
                          auth_url='not foobar')

    @mock.patch.object(auth_url.cfg, 'CONF')
    def test_validate_auth_url_with_valid_url(self, mock_cfg):
        mock_cfg.auth_password.allowed_auth_uris = ['foobar']

        self.assertTrue(self.middleware._validate_auth_url('foobar'))