blob: 578af4a54c4ff891f9685d9c5f2715c4bb5e04d1 [file] [log] [blame]
Jay Pipesf38eaac2012-06-21 13:37:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Rohit Karajgid2d3f792012-05-14 10:28:43 -070018import unittest2 as unittest
Jay Pipesf38eaac2012-06-21 13:37:35 -040019
Attila Fazekas40ec1232013-01-08 11:45:32 +010020from nose.plugins.attrib import attr
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070021from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest import exceptions
Vincent Hou6b8a7b72012-08-25 01:24:33 +080023from tempest.tests.identity import base
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070024
25
Vincent Hou6b8a7b72012-08-25 01:24:33 +080026class TenantsTestBase(object):
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070027
Rohit Karajgid2d3f792012-05-14 10:28:43 -070028 def test_list_tenants_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050029 # Non-admin user should not be able to list tenants
Rohit Karajgid2d3f792012-05-14 10:28:43 -070030 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080031 self.non_admin_client.list_tenants)
Rohit Karajgid2d3f792012-05-14 10:28:43 -070032
33 def test_list_tenant_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050034 # Request to list tenants without a valid token should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070035 token = self.client.get_auth()
36 self.client.delete_token(token)
37 self.assertRaises(exceptions.Unauthorized, self.client.list_tenants)
38 self.client.clear_auth()
39
Attila Fazekas40ec1232013-01-08 11:45:32 +010040 def test_tenant_list_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050041 # Create several tenants and delete them
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070042 tenants = []
Attila Fazekas40ec1232013-01-08 11:45:32 +010043 for _ in xrange(3):
44 resp, tenant = self.client.create_tenant(rand_name('tenant-new'))
45 self.data.tenants.append(tenant)
46 tenants.append(tenant)
47 tenant_ids = map(lambda x: x['id'], tenants)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070048 resp, body = self.client.list_tenants()
Attila Fazekas40ec1232013-01-08 11:45:32 +010049 self.assertTrue(resp['status'].startswith('2'))
50 found = [tenant for tenant in body if tenant['id'] in tenant_ids]
51 self.assertEqual(len(found), len(tenants), 'Tenants not created')
52
53 for tenant in tenants:
54 resp, body = self.client.delete_tenant(tenant['id'])
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070055 self.assertTrue(resp['status'].startswith('2'))
Attila Fazekas40ec1232013-01-08 11:45:32 +010056 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070057
58 resp, body = self.client.list_tenants()
Attila Fazekas40ec1232013-01-08 11:45:32 +010059 found = [tenant for tenant in body if tenant['id'] in tenant_ids]
60 self.assertFalse(any(found), 'Tenants failed to delete')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070061
Attila Fazekas40ec1232013-01-08 11:45:32 +010062 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070063 def test_tenant_delete_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050064 # Non-admin user should not be able to delete a tenant
Rohit Karajgid2d3f792012-05-14 10:28:43 -070065 tenant_name = rand_name('tenant-')
66 resp, tenant = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +010067 self.data.tenants.append(tenant)
Rohit Karajgid2d3f792012-05-14 10:28:43 -070068 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080069 self.non_admin_client.delete_tenant, tenant['id'])
Rohit Karajgid2d3f792012-05-14 10:28:43 -070070
Attila Fazekas40ec1232013-01-08 11:45:32 +010071 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070072 def test_tenant_delete_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050073 # Request to delete a tenant without a valid token should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070074 tenant_name = rand_name('tenant-')
75 resp, tenant = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +010076 self.data.tenants.append(tenant)
Rohit Karajgid2d3f792012-05-14 10:28:43 -070077 token = self.client.get_auth()
78 self.client.delete_token(token)
79 self.assertRaises(exceptions.Unauthorized, self.client.delete_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080080 tenant['id'])
Rohit Karajgid2d3f792012-05-14 10:28:43 -070081 self.client.clear_auth()
82
Attila Fazekas40ec1232013-01-08 11:45:32 +010083 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070084 def test_delete_non_existent_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050085 # Attempt to delete a non existent tenant should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070086 self.assertRaises(exceptions.NotFound, self.client.delete_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080087 'junk_tenant_123456abc')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070088
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070089 def test_tenant_create_with_description(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050090 # Create tenant with a description
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070091 tenant_name = rand_name('tenant-')
92 tenant_desc = rand_name('desc-')
93 resp, body = self.client.create_tenant(tenant_name,
94 description=tenant_desc)
Attila Fazekas40ec1232013-01-08 11:45:32 +010095 tenant = body
96 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070097 st1 = resp['status']
98 tenant_id = body['id']
99 desc1 = body['description']
100 self.assertTrue(st1.startswith('2'))
101 self.assertEqual(desc1, tenant_desc, 'Description should have '
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800102 'been sent in response for create')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700103 resp, body = self.client.get_tenant(tenant_id)
104 desc2 = body['description']
105 self.assertEqual(desc2, tenant_desc, 'Description does not appear'
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800106 'to be set')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700107 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100108 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700109
110 def test_tenant_create_enabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500111 # Create a tenant that is enabled
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700112 tenant_name = rand_name('tenant-')
113 resp, body = self.client.create_tenant(tenant_name, enabled=True)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100114 tenant = body
115 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700116 tenant_id = body['id']
117 st1 = resp['status']
118 en1 = body['enabled']
119 self.assertTrue(st1.startswith('2'))
120 self.assertTrue(en1, 'Enable should be True in response')
121 resp, body = self.client.get_tenant(tenant_id)
122 en2 = body['enabled']
123 self.assertTrue(en2, 'Enable should be True in lookup')
124 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100125 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700126
127 def test_tenant_create_not_enabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500128 # Create a tenant that is not enabled
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700129 tenant_name = rand_name('tenant-')
130 resp, body = self.client.create_tenant(tenant_name, enabled=False)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100131 tenant = body
132 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700133 tenant_id = body['id']
134 st1 = resp['status']
135 en1 = body['enabled']
136 self.assertTrue(st1.startswith('2'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800137 self.assertEqual('false', str(en1).lower(),
138 'Enable should be False in response')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700139 resp, body = self.client.get_tenant(tenant_id)
140 en2 = body['enabled']
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800141 self.assertEqual('false', str(en2).lower(),
142 'Enable should be False in lookup')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700143 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100144 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700145
Attila Fazekas40ec1232013-01-08 11:45:32 +0100146 @attr(type='negative')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700147 def test_tenant_create_duplicate(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500148 # Tenant names should be unique
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700149 tenant_name = rand_name('tenant-dup-')
150 resp, body = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100151 tenant = body
152 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700153 tenant1_id = body.get('id')
154
155 try:
156 resp, body = self.client.create_tenant(tenant_name)
157 # this should have raised an exception
158 self.fail('Should not be able to create a duplicate tenant name')
159 except exceptions.Duplicate:
160 pass
Attila Fazekas40ec1232013-01-08 11:45:32 +0100161 self.client.delete_tenant(tenant1_id)
162 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700163
Attila Fazekas40ec1232013-01-08 11:45:32 +0100164 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700165 def test_create_tenant_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500166 # Non-admin user should not be authorized to create a tenant
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700167 tenant_name = rand_name('tenant-')
168 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800169 self.non_admin_client.create_tenant, tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700170
Attila Fazekas40ec1232013-01-08 11:45:32 +0100171 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700172 def test_create_tenant_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500173 # Create tenant request without a token should not be authorized
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700174 tenant_name = rand_name('tenant-')
175 token = self.client.get_auth()
176 self.client.delete_token(token)
177 self.assertRaises(exceptions.Unauthorized, self.client.create_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800178 tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700179 self.client.clear_auth()
180
Attila Fazekas40ec1232013-01-08 11:45:32 +0100181 @attr(type='negative')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700182 def test_create_tenant_with_empty_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500183 # Tenant name should not be empty
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700184 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
185 name='')
186
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700187 def test_create_tenants_name_length_over_64(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500188 # Tenant name length should not be greater than 64 characters
David Kranz28e35c52012-07-10 10:14:38 -0400189 tenant_name = 'a' * 65
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700190 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800191 tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700192
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700193 def test_tenant_update_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500194 # Update name attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700195 t_name1 = rand_name('tenant-')
196 resp, body = self.client.create_tenant(t_name1)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100197 tenant = body
198 self.data.tenants.append(tenant)
199
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700200 t_id = body['id']
201 resp1_name = body['name']
202
203 t_name2 = rand_name('tenant2-')
204 resp, body = self.client.update_tenant(t_id, name=t_name2)
205 st2 = resp['status']
206 resp2_name = body['name']
207 self.assertTrue(st2.startswith('2'))
208 self.assertNotEqual(resp1_name, resp2_name)
209
210 resp, body = self.client.get_tenant(t_id)
211 resp3_name = body['name']
212
213 self.assertNotEqual(resp1_name, resp3_name)
214 self.assertEqual(t_name1, resp1_name)
215 self.assertEqual(resp2_name, resp3_name)
216
217 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100218 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700219
220 def test_tenant_update_desc(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500221 # Update description attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700222 t_name = rand_name('tenant-')
223 t_desc = rand_name('desc-')
224 resp, body = self.client.create_tenant(t_name, description=t_desc)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100225 tenant = body
226 self.data.tenants.append(tenant)
227
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700228 t_id = body['id']
229 resp1_desc = body['description']
230
231 t_desc2 = rand_name('desc2-')
232 resp, body = self.client.update_tenant(t_id, description=t_desc2)
233 st2 = resp['status']
234 resp2_desc = body['extra']['description']
235 self.assertTrue(st2.startswith('2'))
236 self.assertNotEqual(resp1_desc, resp2_desc)
237
238 resp, body = self.client.get_tenant(t_id)
239 resp3_desc = body['description']
240
241 self.assertNotEqual(resp1_desc, resp3_desc)
242 self.assertEqual(t_desc, resp1_desc)
243 self.assertEqual(resp2_desc, resp3_desc)
244
245 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100246 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700247
248 def test_tenant_update_enable(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500249 # Update the enabled attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700250 t_name = rand_name('tenant-')
251 t_en = False
252 resp, body = self.client.create_tenant(t_name, enabled=t_en)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100253 tenant = body
254 self.data.tenants.append(tenant)
255
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700256 t_id = body['id']
257 resp1_en = body['enabled']
258
259 t_en2 = True
260 resp, body = self.client.update_tenant(t_id, enabled=t_en2)
261 st2 = resp['status']
262 resp2_en = body['extra']['enabled']
263 self.assertTrue(st2.startswith('2'))
264 self.assertNotEqual(resp1_en, resp2_en)
265
266 resp, body = self.client.get_tenant(t_id)
267 resp3_en = body['enabled']
268
269 self.assertNotEqual(resp1_en, resp3_en)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800270 self.assertEqual('false', str(resp1_en).lower())
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700271 self.assertEqual(resp2_en, resp3_en)
272
273 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100274 self.data.tenants.remove(tenant)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800275
276
277class TenantsTestJSON(base.BaseIdentityAdminTestJSON,
278 TenantsTestBase):
279
280 @classmethod
281 def setUpClass(cls):
282 super(TenantsTestJSON, cls).setUpClass()
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800283
284
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800285class TenantsTestXML(base.BaseIdentityAdminTestXML, TenantsTestBase):
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800286
287 @classmethod
288 def setUpClass(cls):
289 super(TenantsTestXML, cls).setUpClass()