blob: 1bebad4a9a3501ad0774e3b2a0e1279ccbd15587 [file] [log] [blame]
Steven Hardybf70c5c2013-10-30 21:55:16 +00001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Steven Hardyc234ada2013-12-10 17:00:41 +000013import datetime
14import re
Steven Hardybf70c5c2013-10-30 21:55:16 +000015from tempest.api.identity import base
16from tempest import clients
17from tempest.common.utils.data_utils import rand_name
Matthew Treinishd5021a72014-01-09 18:42:51 +000018from tempest import config
Steven Hardybf70c5c2013-10-30 21:55:16 +000019from tempest import exceptions
Steven Hardyc234ada2013-12-10 17:00:41 +000020from tempest.openstack.common import timeutils
Steven Hardybf70c5c2013-10-30 21:55:16 +000021from tempest.test import attr
22
Matthew Treinishd5021a72014-01-09 18:42:51 +000023CONF = config.CONF
24
Steven Hardybf70c5c2013-10-30 21:55:16 +000025
26class BaseTrustsV3Test(base.BaseIdentityAdminTest):
27
28 def setUp(self):
29 super(BaseTrustsV3Test, self).setUp()
30 # Use alt_username as the trustee
Matthew Treinishd5021a72014-01-09 18:42:51 +000031 if not CONF.identity_feature_enabled.trust:
32 raise self.skipException("Trusts aren't enabled")
33
Matthew Treinish5ba84e32014-01-29 16:52:57 +000034 self.trustee_username = CONF.identity.alt_username
Steven Hardybf70c5c2013-10-30 21:55:16 +000035 self.trust_id = None
Steven Hardy776f4572013-12-23 21:42:48 +000036
37 def tearDown(self):
38 if self.trust_id:
39 # Do the delete in tearDown not addCleanup - we want the test to
40 # fail in the event there is a bug which causes undeletable trusts
41 self.delete_trust()
42 super(BaseTrustsV3Test, self).tearDown()
Steven Hardybf70c5c2013-10-30 21:55:16 +000043
44 def create_trustor_and_roles(self):
45 # Get trustor project ID, use the admin project
46 self.trustor_project_name = self.v3_client.tenant_name
47 self.trustor_project_id = self.get_tenant_by_name(
48 self.trustor_project_name)['id']
49 self.assertIsNotNone(self.trustor_project_id)
50
51 # Create a trustor User
52 self.trustor_username = rand_name('user-')
53 u_desc = self.trustor_username + 'description'
Steven Hardy68f95282014-01-10 17:40:31 +000054 u_email = self.trustor_username + '@testmail.xx'
Steven Hardybf70c5c2013-10-30 21:55:16 +000055 self.trustor_password = rand_name('pass-')
56 resp, user = self.v3_client.create_user(
57 self.trustor_username,
58 description=u_desc,
59 password=self.trustor_password,
60 email=u_email,
61 project_id=self.trustor_project_id)
62 self.assertEqual(resp['status'], '201')
63 self.trustor_user_id = user['id']
64
65 # And two roles, one we'll delegate and one we won't
66 self.delegated_role = rand_name('DelegatedRole-')
67 self.not_delegated_role = rand_name('NotDelegatedRole-')
68
69 resp, role = self.v3_client.create_role(self.delegated_role)
70 self.assertEqual(resp['status'], '201')
71 self.delegated_role_id = role['id']
72
73 resp, role = self.v3_client.create_role(self.not_delegated_role)
74 self.assertEqual(resp['status'], '201')
75 self.not_delegated_role_id = role['id']
76
77 # Assign roles to trustor
78 self.v3_client.assign_user_role(self.trustor_project_id,
79 self.trustor_user_id,
80 self.delegated_role_id)
81 self.v3_client.assign_user_role(self.trustor_project_id,
82 self.trustor_user_id,
83 self.not_delegated_role_id)
84
85 # Get trustee user ID, use the demo user
86 trustee_username = self.v3_non_admin_client.user
87 self.trustee_user_id = self.get_user_by_name(trustee_username)['id']
88 self.assertIsNotNone(self.trustee_user_id)
89
90 # Initialize a new client with the trustor credentials
91 os = clients.Manager(username=self.trustor_username,
92 password=self.trustor_password,
93 tenant_name=self.trustor_project_name,
94 interface=self._interface)
95 self.trustor_v3_client = os.identity_v3_client
96
Steven Hardy776f4572013-12-23 21:42:48 +000097 def cleanup_user_and_roles(self):
Steven Hardybf70c5c2013-10-30 21:55:16 +000098 if self.trustor_user_id:
99 self.v3_client.delete_user(self.trustor_user_id)
100 if self.delegated_role_id:
101 self.v3_client.delete_role(self.delegated_role_id)
102 if self.not_delegated_role_id:
103 self.v3_client.delete_role(self.not_delegated_role_id)
104
105 def create_trust(self, impersonate=True, expires=None):
106
107 resp, trust_create = self.trustor_v3_client.create_trust(
108 trustor_user_id=self.trustor_user_id,
109 trustee_user_id=self.trustee_user_id,
110 project_id=self.trustor_project_id,
111 role_names=[self.delegated_role],
112 impersonation=impersonate,
113 expires_at=expires)
114 self.assertEqual('201', resp['status'])
115 self.trust_id = trust_create['id']
116 return trust_create
117
118 def validate_trust(self, trust, impersonate=True, expires=None,
119 summary=False):
120 self.assertIsNotNone(trust['id'])
121 self.assertEqual(impersonate, trust['impersonation'])
Steven Hardyc234ada2013-12-10 17:00:41 +0000122 # FIXME(shardy): ref bug #1246383 we can't check the
123 # microsecond component of the expiry time, because mysql
124 # <5.6.4 doesn't support microseconds.
125 # expected format 2013-12-20T16:08:36.036987Z
126 if expires is not None:
127 expires_nousec = re.sub(r'\.([0-9]){6}Z', '', expires)
128 self.assertTrue(trust['expires_at'].startswith(expires_nousec))
129 else:
130 self.assertIsNone(trust['expires_at'])
Steven Hardybf70c5c2013-10-30 21:55:16 +0000131 self.assertEqual(self.trustor_user_id, trust['trustor_user_id'])
132 self.assertEqual(self.trustee_user_id, trust['trustee_user_id'])
133 self.assertIn('v3/OS-TRUST/trusts', trust['links']['self'])
134 self.assertEqual(self.trustor_project_id, trust['project_id'])
135 if not summary:
136 self.assertEqual(self.delegated_role, trust['roles'][0]['name'])
137 self.assertEqual(1, len(trust['roles']))
138
139 def get_trust(self):
140 resp, trust_get = self.trustor_v3_client.get_trust(self.trust_id)
141 self.assertEqual('200', resp['status'])
142 return trust_get
143
144 def validate_role(self, role):
145 self.assertEqual(self.delegated_role_id, role['id'])
146 self.assertEqual(self.delegated_role, role['name'])
147 self.assertIn('v3/roles/%s' % self.delegated_role_id,
148 role['links']['self'])
149 self.assertNotEqual(self.not_delegated_role_id, role['id'])
150 self.assertNotEqual(self.not_delegated_role, role['name'])
151 self.assertNotIn('v3/roles/%s' % self.not_delegated_role_id,
152 role['links']['self'])
153
154 def check_trust_roles(self):
155 # Check we find the delegated role
156 resp, roles_get = self.trustor_v3_client.get_trust_roles(
157 self.trust_id)
158 self.assertEqual('200', resp['status'])
159 self.assertEqual(1, len(roles_get))
160 self.validate_role(roles_get[0])
161
162 resp, role_get = self.trustor_v3_client.get_trust_role(
163 self.trust_id, self.delegated_role_id)
164 self.assertEqual('200', resp['status'])
165 self.validate_role(role_get)
166
167 resp, role_get = self.trustor_v3_client.check_trust_role(
168 self.trust_id, self.delegated_role_id)
169 self.assertEqual('204', resp['status'])
170
171 # And that we don't find not_delegated_role
172 self.assertRaises(exceptions.NotFound,
173 self.trustor_v3_client.get_trust_role,
174 self.trust_id,
175 self.not_delegated_role_id)
176
177 self.assertRaises(exceptions.NotFound,
178 self.trustor_v3_client.check_trust_role,
179 self.trust_id,
180 self.not_delegated_role_id)
181
182 def delete_trust(self):
183 resp, trust_delete = self.trustor_v3_client.delete_trust(self.trust_id)
184 self.assertEqual('204', resp['status'])
185 self.assertRaises(exceptions.NotFound,
186 self.trustor_v3_client.get_trust,
187 self.trust_id)
188 self.trust_id = None
189
190
191class TrustsV3TestJSON(BaseTrustsV3Test):
192 _interface = 'json'
193
194 def setUp(self):
195 super(TrustsV3TestJSON, self).setUp()
196 self.create_trustor_and_roles()
Steven Hardy68f95282014-01-10 17:40:31 +0000197 self.addCleanup(self.cleanup_user_and_roles)
Steven Hardybf70c5c2013-10-30 21:55:16 +0000198
199 @attr(type='smoke')
200 def test_trust_impersonate(self):
201 # Test case to check we can create, get and delete a trust
202 # updates are not supported for trusts
203 trust = self.create_trust()
204 self.validate_trust(trust)
205
206 trust_get = self.get_trust()
207 self.validate_trust(trust_get)
208
209 self.check_trust_roles()
210
Steven Hardybf70c5c2013-10-30 21:55:16 +0000211 @attr(type='smoke')
212 def test_trust_noimpersonate(self):
213 # Test case to check we can create, get and delete a trust
214 # with impersonation=False
215 trust = self.create_trust(impersonate=False)
216 self.validate_trust(trust, impersonate=False)
217
218 trust_get = self.get_trust()
219 self.validate_trust(trust_get, impersonate=False)
220
221 self.check_trust_roles()
222
Steven Hardybf70c5c2013-10-30 21:55:16 +0000223 @attr(type='smoke')
Steven Hardyc234ada2013-12-10 17:00:41 +0000224 def test_trust_expire(self):
225 # Test case to check we can create, get and delete a trust
226 # with an expiry specified
227 expires_at = timeutils.utcnow() + datetime.timedelta(hours=1)
228 expires_str = timeutils.isotime(at=expires_at, subsecond=True)
229
230 trust = self.create_trust(expires=expires_str)
231 self.validate_trust(trust, expires=expires_str)
232
233 trust_get = self.get_trust()
234
235 self.validate_trust(trust_get, expires=expires_str)
236
237 self.check_trust_roles()
238
Steven Hardyc234ada2013-12-10 17:00:41 +0000239 @attr(type='smoke')
Steven Hardybf70c5c2013-10-30 21:55:16 +0000240 def test_trust_expire_invalid(self):
241 # Test case to check we can check an invlaid expiry time
242 # is rejected with the correct error
243 # with an expiry specified
244 expires_str = 'bad.123Z'
245 self.assertRaises(exceptions.BadRequest,
246 self.create_trust,
247 expires=expires_str)
Steven Hardyf31fd2d2013-12-10 17:02:36 +0000248
249 @attr(type='smoke')
250 def test_get_trusts_query(self):
251 self.create_trust()
252 resp, trusts_get = self.trustor_v3_client.get_trusts(
253 trustor_user_id=self.trustor_user_id)
254 self.assertEqual('200', resp['status'])
255 self.assertEqual(1, len(trusts_get))
256 self.validate_trust(trusts_get[0], summary=True)
257
258 @attr(type='smoke')
259 def test_get_trusts_all(self):
260 self.create_trust()
261 resp, trusts_get = self.v3_client.get_trusts()
262 self.assertEqual('200', resp['status'])
263 trusts = [t for t in trusts_get
264 if t['id'] == self.trust_id]
265 self.assertEqual(1, len(trusts))
266 self.validate_trust(trusts[0], summary=True)