summaryrefslogtreecommitdiff
path: root/keystoneclient/tests/unit/auth/utils.py
blob: 1e346e35c2c0f9e9babd661619ca773cff94c580 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# 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.

import functools
from unittest import mock
import uuid

from keystoneauth1 import fixture
from oslo_config import cfg

from keystoneclient import access
from keystoneclient.auth import base
from keystoneclient import exceptions
from keystoneclient import session
from keystoneclient.tests.unit import utils


class MockPlugin(base.BaseAuthPlugin):

    INT_DESC = 'test int'
    FLOAT_DESC = 'test float'
    BOOL_DESC = 'test bool'
    STR_DESC = 'test str'
    STR_DEFAULT = uuid.uuid4().hex

    def __init__(self, **kwargs):
        self._data = kwargs

    def __getitem__(self, key):
        """Get the data of the key."""
        return self._data[key]

    def get_token(self, *args, **kwargs):
        return 'aToken'

    def get_endpoint(self, *args, **kwargs):
        return 'http://test'

    @classmethod
    def get_options(cls):
        return [
            cfg.IntOpt('a-int', default='3', help=cls.INT_DESC),
            cfg.BoolOpt('a-bool', help=cls.BOOL_DESC),
            cfg.FloatOpt('a-float', help=cls.FLOAT_DESC),
            cfg.StrOpt('a-str', help=cls.STR_DESC, default=cls.STR_DEFAULT),
        ]


class MockManager(object):

    def __init__(self, driver):
        self.driver = driver


def mock_plugin(f):
    @functools.wraps(f)
    def inner(*args, **kwargs):
        with mock.patch.object(base, 'get_plugin_class') as m:
            m.return_value = MockPlugin
            args = list(args) + [m]
            return f(*args, **kwargs)

    return inner


class TestCase(utils.TestCase):

    GROUP = 'auth'
    V2PASS = 'v2password'
    V3TOKEN = 'v3token'

    a_int = 88
    a_float = 88.8
    a_bool = False

    TEST_VALS = {'a_int': a_int,
                 'a_float': a_float,
                 'a_bool': a_bool}

    def assertTestVals(self, plugin, vals=TEST_VALS):
        for k, v in vals.items():
            self.assertEqual(v, plugin[k])


class GenericPluginTestCase(utils.TestCase):

    TEST_URL = 'http://keystone.host:5000/'

    # OVERRIDE THESE IN SUB CLASSES
    PLUGIN_CLASS = None
    V2_PLUGIN_CLASS = None
    V3_PLUGIN_CLASS = None

    def setUp(self):
        super(GenericPluginTestCase, self).setUp()

        self.token_v2 = fixture.V2Token()
        self.token_v3 = fixture.V3Token()
        self.token_v3_id = uuid.uuid4().hex

        self.deprecations.expect_deprecations()
        self.session = session.Session()

        self.stub_url('POST', ['v2.0', 'tokens'], json=self.token_v2)
        self.stub_url('POST', ['v3', 'auth', 'tokens'],
                      headers={'X-Subject-Token': self.token_v3_id},
                      json=self.token_v3)

    def new_plugin(self, **kwargs):
        kwargs.setdefault('auth_url', self.TEST_URL)
        return self.PLUGIN_CLASS(**kwargs)

    def stub_discovery(self, base_url=None, **kwargs):
        kwargs.setdefault('href', self.TEST_URL)
        disc = fixture.DiscoveryList(**kwargs)
        self.stub_url('GET', json=disc, base_url=base_url, status_code=300)
        return disc

    def assertCreateV3(self, **kwargs):
        auth = self.new_plugin(**kwargs)
        auth_ref = auth.get_auth_ref(self.session)
        self.assertIsInstance(auth_ref, access.AccessInfoV3)
        self.assertEqual(self.TEST_URL + 'v3/auth/tokens',
                         self.requests_mock.last_request.url)
        self.assertIsInstance(auth._plugin, self.V3_PLUGIN_CLASS)
        return auth

    def assertCreateV2(self, **kwargs):
        auth = self.new_plugin(**kwargs)
        auth_ref = auth.get_auth_ref(self.session)
        self.assertIsInstance(auth_ref, access.AccessInfoV2)
        self.assertEqual(self.TEST_URL + 'v2.0/tokens',
                         self.requests_mock.last_request.url)
        self.assertIsInstance(auth._plugin, self.V2_PLUGIN_CLASS)
        return auth

    def assertDiscoveryFailure(self, **kwargs):
        plugin = self.new_plugin(**kwargs)
        self.assertRaises(exceptions.DiscoveryFailure,
                          plugin.get_auth_ref,
                          self.session)

    def test_create_v3_if_domain_params(self):
        self.stub_discovery()

        self.assertCreateV3(domain_id=uuid.uuid4().hex)
        self.assertCreateV3(domain_name=uuid.uuid4().hex)
        self.assertCreateV3(project_name=uuid.uuid4().hex,
                            project_domain_name=uuid.uuid4().hex)
        self.assertCreateV3(project_name=uuid.uuid4().hex,
                            project_domain_id=uuid.uuid4().hex)

    def test_create_v2_if_no_domain_params(self):
        self.stub_discovery()
        self.assertCreateV2()
        self.assertCreateV2(project_id=uuid.uuid4().hex)
        self.assertCreateV2(project_name=uuid.uuid4().hex)
        self.assertCreateV2(tenant_id=uuid.uuid4().hex)
        self.assertCreateV2(tenant_name=uuid.uuid4().hex)

    def test_v3_params_v2_url(self):
        self.stub_discovery(v3=False)
        self.assertDiscoveryFailure(domain_name=uuid.uuid4().hex)

    def test_v2_params_v3_url(self):
        self.stub_discovery(v2=False)
        self.assertCreateV3()

    def test_no_urls(self):
        self.stub_discovery(v2=False, v3=False)
        self.assertDiscoveryFailure()

    def test_path_based_url_v2(self):
        self.stub_url('GET', ['v2.0'], status_code=403)
        self.assertCreateV2(auth_url=self.TEST_URL + 'v2.0')

    def test_path_based_url_v3(self):
        self.stub_url('GET', ['v3'], status_code=403)
        self.assertCreateV3(auth_url=self.TEST_URL + 'v3')

    def test_disc_error_for_failure(self):
        self.stub_url('GET', [], status_code=403)
        self.assertDiscoveryFailure()

    def test_v3_plugin_from_failure(self):
        url = self.TEST_URL + 'v3'
        self.stub_url('GET', [], base_url=url, status_code=403)
        self.assertCreateV3(auth_url=url)

    def test_unknown_discovery_version(self):
        # make a v4 entry that's mostly the same as a v3
        self.stub_discovery(v2=False, v3_id='v4.0')
        self.assertDiscoveryFailure()