blob: 53460529323cfb55e47fb7da9e18dd19e0201a50 [file] [log] [blame]
Mauro S. M. Rodriguesc3e573c2014-02-19 07:59:29 -05001# Copyright 2014 IBM Corp.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import copy
17
18from tempest import auth
19from tempest.common import http
20from tempest import config
21from tempest import exceptions
22from tempest.openstack.common.fixture import mockpatch
23from tempest.tests import base
24from tempest.tests import fake_config
25from tempest.tests import fake_http
26from tempest.tests import fake_identity
27
28
29class BaseAuthTestsSetUp(base.TestCase):
30 _auth_provider_class = None
31 credentials = {
32 'username': 'fake_user',
33 'password': 'fake_pwd',
34 'tenant_name': 'fake_tenant'
35 }
36
37 def _auth(self, credentials, **params):
38 """
39 returns auth method according to keystone
40 """
41 return self._auth_provider_class(credentials, **params)
42
43 def setUp(self):
44 super(BaseAuthTestsSetUp, self).setUp()
45 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakeConfig)
46 self.fake_http = fake_http.fake_httplib2(return_type=200)
47 self.stubs.Set(http.ClosingHttp, 'request', self.fake_http.request)
48 self.auth_provider = self._auth(self.credentials)
49
50
51class TestBaseAuthProvider(BaseAuthTestsSetUp):
52 """
53 This tests auth.AuthProvider class which is base for the other so we
54 obviously don't test not implemented method or the ones which strongly
55 depends on them.
56 """
57 _auth_provider_class = auth.AuthProvider
58
59 def test_check_credentials_is_dict(self):
60 self.assertTrue(self.auth_provider.check_credentials({}))
61
62 def test_check_credentials_bad_type(self):
63 self.assertFalse(self.auth_provider.check_credentials([]))
64
65 def test_instantiate_with_bad_credentials_type(self):
66 """
67 Assure that credentials with bad type fail with TypeError
68 """
69 self.assertRaises(TypeError, self._auth, [])
70
71 def test_auth_data_property(self):
72 self.assertRaises(NotImplementedError, getattr, self.auth_provider,
73 'auth_data')
74
75 def test_auth_data_property_when_cache_exists(self):
76 self.auth_provider.cache = 'foo'
77 self.useFixture(mockpatch.PatchObject(self.auth_provider,
78 'is_expired',
79 return_value=False))
80 self.assertEqual('foo', getattr(self.auth_provider, 'auth_data'))
81
82 def test_delete_auth_data_property_through_deleter(self):
83 self.auth_provider.cache = 'foo'
84 del self.auth_provider.auth_data
85 self.assertIsNone(self.auth_provider.cache)
86
87 def test_delete_auth_data_property_through_clear_auth(self):
88 self.auth_provider.cache = 'foo'
89 self.auth_provider.clear_auth()
90 self.assertIsNone(self.auth_provider.cache)
91
92 def test_set_and_reset_alt_auth_data(self):
93 self.auth_provider.set_alt_auth_data('foo', 'bar')
94 self.assertEqual(self.auth_provider.alt_part, 'foo')
95 self.assertEqual(self.auth_provider.alt_auth_data, 'bar')
96
97 self.auth_provider.reset_alt_auth_data()
98 self.assertIsNone(self.auth_provider.alt_part)
99 self.assertIsNone(self.auth_provider.alt_auth_data)
100
101
102class TestKeystoneV2AuthProvider(BaseAuthTestsSetUp):
103 _auth_provider_class = auth.KeystoneV2AuthProvider
104
105 def setUp(self):
106 super(TestKeystoneV2AuthProvider, self).setUp()
107 self.stubs.Set(http.ClosingHttp, 'request',
108 fake_identity._fake_v2_response)
109 self.target_url = 'test_api'
110
111 def _get_fake_alt_identity(self):
112 return fake_identity.ALT_IDENTITY_V2_RESPONSE['access']
113
114 def _get_result_url_from_fake_identity(self):
115 return fake_identity.COMPUTE_ENDPOINTS_V2['endpoints'][1]['publicURL']
116
117 def _get_token_from_fake_identity(self):
118 return fake_identity.TOKEN
119
120 def _test_request_helper(self):
121 filters = {
122 'service': 'compute',
123 'endpoint_type': 'publicURL',
124 'region': 'fakeRegion'
125 }
126
127 url, headers, body = self.auth_provider.auth_request('GET',
128 self.target_url,
129 filters=filters)
130
131 result_url = self._get_result_url_from_fake_identity()
132 self.assertEqual(url, result_url + '/' + self.target_url)
133 self.assertEqual(self._get_token_from_fake_identity(),
134 headers['X-Auth-Token'])
135 self.assertEqual(body, None)
136
137 def test_request(self):
138 self._test_request_helper()
139
140 def test_request_with_alt_auth(self):
141 self.auth_provider.set_alt_auth_data(
142 'body',
143 (fake_identity.ALT_TOKEN, self._get_fake_alt_identity()))
144 self._test_request_helper()
145 # Assert alt auth data is clear after it
146 self.assertIsNone(self.auth_provider.alt_part)
147 self.assertIsNone(self.auth_provider.alt_auth_data)
148
149 def test_request_with_bad_service(self):
150 filters = {
151 'service': 'BAD_SERVICE',
152 'endpoint_type': 'publicURL',
153 'region': 'fakeRegion'
154 }
155 self.assertRaises(exceptions.EndpointNotFound,
156 self.auth_provider.auth_request, 'GET',
157 'http://fakeurl.com/fake_api', filters=filters)
158
159 def test_request_without_service(self):
160 filters = {
161 'service': None,
162 'endpoint_type': 'publicURL',
163 'region': 'fakeRegion'
164 }
165 self.assertRaises(exceptions.EndpointNotFound,
166 self.auth_provider.auth_request, 'GET',
167 'http://fakeurl.com/fake_api', filters=filters)
168
169 def test_check_credentials_missing_attribute(self):
170 for attr in ['username', 'password']:
171 cred = copy.copy(self.credentials)
172 del cred[attr]
173 self.assertFalse(self.auth_provider.check_credentials(cred))
174
175 def test_check_credentials_not_scoped_missing_tenant_name(self):
176 cred = copy.copy(self.credentials)
177 del cred['tenant_name']
178 self.assertTrue(self.auth_provider.check_credentials(cred,
179 scoped=False))
180
181 def test_check_credentials_missing_tenant_name(self):
182 cred = copy.copy(self.credentials)
183 del cred['tenant_name']
184 self.assertFalse(self.auth_provider.check_credentials(cred))
185
186
187class TestKeystoneV3AuthProvider(TestKeystoneV2AuthProvider):
188 _auth_provider_class = auth.KeystoneV3AuthProvider
189 credentials = {
190 'username': 'fake_user',
191 'password': 'fake_pwd',
192 'tenant_name': 'fake_tenant',
193 'domain_name': 'fake_domain_name',
194 }
195
196 def setUp(self):
197 super(TestKeystoneV3AuthProvider, self).setUp()
198 self.stubs.Set(http.ClosingHttp, 'request',
199 fake_identity._fake_v3_response)
200
201 def _get_fake_alt_identity(self):
202 return fake_identity.ALT_IDENTITY_V3['token']
203
204 def _get_result_url_from_fake_identity(self):
205 return fake_identity.COMPUTE_ENDPOINTS_V3['endpoints'][1]['url']
206
207 def test_check_credentials_missing_tenant_name(self):
208 cred = copy.copy(self.credentials)
209 del cred['domain_name']
210 self.assertFalse(self.auth_provider.check_credentials(cred))