summaryrefslogtreecommitdiff
path: root/openstack_auth/tests/unit/test_utils.py
blob: 59f1427bf636844a09902bad5ddf625524d2100e (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
# 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 django import http
from django import test
from django.test import client
from django.test.utils import override_settings

from openstack_auth import utils


FAKE_CATALOG = [
    {
        'name': 'fake_service',
        'type': "not-identity",
        'endpoints': [
            {'region': 'RegionOne'},
            {'region': 'RegionTwo'},
            {'region': 'RegionThree'},
            {'region': 'RegionFour'},
        ]
    },
]


class RoleTestCaseAdmin(test.TestCase):

    def test_get_admin_roles_with_default_value(self):
        admin_roles = utils.get_admin_roles()
        self.assertSetEqual({'admin'}, admin_roles)

    @override_settings(OPENSTACK_KEYSTONE_ADMIN_ROLES=['foO', 'BAR', 'admin'])
    def test_get_admin_roles(self):
        admin_roles = utils.get_admin_roles()
        self.assertSetEqual({'foo', 'bar', 'admin'}, admin_roles)

    @override_settings(OPENSTACK_KEYSTONE_ADMIN_ROLES=['foO', 'BAR', 'admin'])
    def test_get_admin_permissions(self):
        admin_permissions = utils.get_admin_permissions()
        self.assertSetEqual({'openstack.roles.foo',
                             'openstack.roles.bar',
                             'openstack.roles.admin'}, admin_permissions)


class UtilsTestCase(test.TestCase):

    @override_settings(DEFAULT_SERVICE_REGIONS={
        'http://example.com': 'RegionThree', '*': 'RegionFour'})
    def test_default_services_region_precedence(self):
        request = client.RequestFactory().get('fake')

        # Cookie is valid, so should be region source
        request.COOKIES['services_region'] = "RegionTwo"
        default_region = utils.default_services_region(
            FAKE_CATALOG, request=request, ks_endpoint='http://example.com')
        self.assertEqual("RegionTwo", default_region)

        # Cookie is invalid, so ks_endpoint is source
        request.COOKIES['services_region'] = "Not_valid_region"
        default_region = utils.default_services_region(
            FAKE_CATALOG, request=request, ks_endpoint='http://example.com')
        self.assertEqual("RegionThree", default_region)

        # endpoint and cookie are invalid, so source is "*" key
        default_region = utils.default_services_region(
            FAKE_CATALOG, request=request, ks_endpoint='not_a_match')
        self.assertEqual("RegionFour", default_region)

    def test_default_services_region_fallback(self):
        # Test that first region found in catalog is returned
        default_region = utils.default_services_region(FAKE_CATALOG)
        self.assertEqual("RegionOne", default_region)


class BehindProxyTestCase(test.TestCase):

    def setUp(self):
        super().setUp()
        self.request = http.HttpRequest()

    def test_without_proxy(self):
        self.request.META['REMOTE_ADDR'] = '10.111.111.2'
        from openstack_auth.utils import get_client_ip
        self.assertEqual('10.111.111.2', get_client_ip(self.request))

    def test_with_proxy_no_settings(self):
        from openstack_auth.utils import get_client_ip
        self.request.META['REMOTE_ADDR'] = '10.111.111.2'
        self.request.META['HTTP_X_REAL_IP'] = '192.168.15.33'
        self.request.META['HTTP_X_FORWARDED_FOR'] = '172.18.0.2'
        self.assertEqual('10.111.111.2', get_client_ip(self.request))

    def test_with_settings_without_proxy(self):
        from openstack_auth.utils import get_client_ip
        self.request.META['REMOTE_ADDR'] = '10.111.111.2'
        self.assertEqual('10.111.111.2', get_client_ip(self.request))

    @override_settings(SECURE_PROXY_ADDR_HEADER='HTTP_X_FORWARDED_FOR')
    def test_with_settings_with_proxy_forwardfor(self):
        from openstack_auth.utils import get_client_ip
        self.request.META['REMOTE_ADDR'] = '10.111.111.2'
        self.request.META['HTTP_X_FORWARDED_FOR'] = '172.18.0.2'
        self.assertEqual('172.18.0.2', get_client_ip(self.request))

    @override_settings(SECURE_PROXY_ADDR_HEADER='HTTP_X_REAL_IP')
    def test_with_settings_with_proxy_real_ip(self):
        from openstack_auth.utils import get_client_ip
        self.request.META['REMOTE_ADDR'] = '10.111.111.2'
        self.request.META['HTTP_X_REAL_IP'] = '192.168.15.33'
        self.request.META['HTTP_X_FORWARDED_FOR'] = '172.18.0.2'
        self.assertEqual('192.168.15.33', get_client_ip(self.request))