blob: c51a14a9d93545ae1a727ec61bdda1d2c096e8ff [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
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070018from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019from tempest import exceptions
Chris Yeoh01cb2792013-02-09 22:25:37 +103020from tempest.test import attr
Vincent Hou6b8a7b72012-08-25 01:24:33 +080021from tempest.tests.identity import base
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070022
23
Attila Fazekas0d0c6162013-02-24 09:14:23 +010024class TenantsTestJSON(base.BaseIdentityAdminTest):
25 _interface = 'json'
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070026
Giampaolo Lauriaea294952013-05-15 08:52:04 -040027 @attr(type='gate')
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
Giampaolo Lauriaea294952013-05-15 08:52:04 -040033 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070034 def test_list_tenant_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050035 # Request to list tenants without a valid token should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070036 token = self.client.get_auth()
37 self.client.delete_token(token)
38 self.assertRaises(exceptions.Unauthorized, self.client.list_tenants)
39 self.client.clear_auth()
40
Giampaolo Lauriaea294952013-05-15 08:52:04 -040041 @attr(type='gate')
Attila Fazekas40ec1232013-01-08 11:45:32 +010042 def test_tenant_list_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050043 # Create several tenants and delete them
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070044 tenants = []
Attila Fazekas40ec1232013-01-08 11:45:32 +010045 for _ in xrange(3):
46 resp, tenant = self.client.create_tenant(rand_name('tenant-new'))
47 self.data.tenants.append(tenant)
48 tenants.append(tenant)
49 tenant_ids = map(lambda x: x['id'], tenants)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070050 resp, body = self.client.list_tenants()
Attila Fazekas40ec1232013-01-08 11:45:32 +010051 self.assertTrue(resp['status'].startswith('2'))
52 found = [tenant for tenant in body if tenant['id'] in tenant_ids]
53 self.assertEqual(len(found), len(tenants), 'Tenants not created')
54
55 for tenant in tenants:
56 resp, body = self.client.delete_tenant(tenant['id'])
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070057 self.assertTrue(resp['status'].startswith('2'))
Attila Fazekas40ec1232013-01-08 11:45:32 +010058 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070059
60 resp, body = self.client.list_tenants()
Attila Fazekas40ec1232013-01-08 11:45:32 +010061 found = [tenant for tenant in body if tenant['id'] in tenant_ids]
62 self.assertFalse(any(found), 'Tenants failed to delete')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070063
Giampaolo Lauriaea294952013-05-15 08:52:04 -040064 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070065 def test_tenant_delete_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050066 # Non-admin user should not be able to delete a tenant
Rohit Karajgid2d3f792012-05-14 10:28:43 -070067 tenant_name = rand_name('tenant-')
68 resp, tenant = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +010069 self.data.tenants.append(tenant)
Rohit Karajgid2d3f792012-05-14 10:28:43 -070070 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080071 self.non_admin_client.delete_tenant, tenant['id'])
Rohit Karajgid2d3f792012-05-14 10:28:43 -070072
Giampaolo Lauriaea294952013-05-15 08:52:04 -040073 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070074 def test_tenant_delete_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050075 # Request to delete a tenant without a valid token should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070076 tenant_name = rand_name('tenant-')
77 resp, tenant = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +010078 self.data.tenants.append(tenant)
Rohit Karajgid2d3f792012-05-14 10:28:43 -070079 token = self.client.get_auth()
80 self.client.delete_token(token)
81 self.assertRaises(exceptions.Unauthorized, self.client.delete_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080082 tenant['id'])
Rohit Karajgid2d3f792012-05-14 10:28:43 -070083 self.client.clear_auth()
84
Giampaolo Lauriaea294952013-05-15 08:52:04 -040085 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070086 def test_delete_non_existent_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050087 # Attempt to delete a non existent tenant should fail
Rohit Karajgid2d3f792012-05-14 10:28:43 -070088 self.assertRaises(exceptions.NotFound, self.client.delete_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080089 'junk_tenant_123456abc')
Rohit Karajgid2d3f792012-05-14 10:28:43 -070090
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070091 def test_tenant_create_with_description(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050092 # Create tenant with a description
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070093 tenant_name = rand_name('tenant-')
94 tenant_desc = rand_name('desc-')
95 resp, body = self.client.create_tenant(tenant_name,
96 description=tenant_desc)
Attila Fazekas40ec1232013-01-08 11:45:32 +010097 tenant = body
98 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070099 st1 = resp['status']
100 tenant_id = body['id']
101 desc1 = body['description']
102 self.assertTrue(st1.startswith('2'))
103 self.assertEqual(desc1, tenant_desc, 'Description should have '
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800104 'been sent in response for create')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700105 resp, body = self.client.get_tenant(tenant_id)
106 desc2 = body['description']
107 self.assertEqual(desc2, tenant_desc, 'Description does not appear'
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800108 'to be set')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700109 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100110 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700111
112 def test_tenant_create_enabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500113 # Create a tenant that is enabled
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700114 tenant_name = rand_name('tenant-')
115 resp, body = self.client.create_tenant(tenant_name, enabled=True)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100116 tenant = body
117 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700118 tenant_id = body['id']
119 st1 = resp['status']
120 en1 = body['enabled']
121 self.assertTrue(st1.startswith('2'))
122 self.assertTrue(en1, 'Enable should be True in response')
123 resp, body = self.client.get_tenant(tenant_id)
124 en2 = body['enabled']
125 self.assertTrue(en2, 'Enable should be True in lookup')
126 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100127 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700128
129 def test_tenant_create_not_enabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500130 # Create a tenant that is not enabled
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700131 tenant_name = rand_name('tenant-')
132 resp, body = self.client.create_tenant(tenant_name, enabled=False)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100133 tenant = body
134 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700135 tenant_id = body['id']
136 st1 = resp['status']
137 en1 = body['enabled']
138 self.assertTrue(st1.startswith('2'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800139 self.assertEqual('false', str(en1).lower(),
140 'Enable should be False in response')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700141 resp, body = self.client.get_tenant(tenant_id)
142 en2 = body['enabled']
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800143 self.assertEqual('false', str(en2).lower(),
144 'Enable should be False in lookup')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700145 self.client.delete_tenant(tenant_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100146 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700147
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400148 @attr(type='gate')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700149 def test_tenant_create_duplicate(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500150 # Tenant names should be unique
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700151 tenant_name = rand_name('tenant-dup-')
152 resp, body = self.client.create_tenant(tenant_name)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100153 tenant = body
154 self.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700155 tenant1_id = body.get('id')
156
Chris Yeohe04628e2013-02-25 17:12:21 +1030157 self.addCleanup(self.client.delete_tenant, tenant1_id)
158 self.addCleanup(self.data.tenants.remove, tenant)
159 self.assertRaises(exceptions.Duplicate, self.client.create_tenant,
160 tenant_name)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700161
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400162 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700163 def test_create_tenant_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500164 # Non-admin user should not be authorized to create a tenant
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700165 tenant_name = rand_name('tenant-')
166 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800167 self.non_admin_client.create_tenant, tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700168
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400169 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700170 def test_create_tenant_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500171 # Create tenant request without a token should not be authorized
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700172 tenant_name = rand_name('tenant-')
173 token = self.client.get_auth()
174 self.client.delete_token(token)
175 self.assertRaises(exceptions.Unauthorized, self.client.create_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800176 tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700177 self.client.clear_auth()
178
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400179 @attr(type='gate')
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700180 def test_create_tenant_with_empty_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500181 # Tenant name should not be empty
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700182 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
183 name='')
184
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700185 def test_create_tenants_name_length_over_64(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500186 # Tenant name length should not be greater than 64 characters
David Kranz28e35c52012-07-10 10:14:38 -0400187 tenant_name = 'a' * 65
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700188 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800189 tenant_name)
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700190
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700191 def test_tenant_update_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500192 # Update name attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700193 t_name1 = rand_name('tenant-')
194 resp, body = self.client.create_tenant(t_name1)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100195 tenant = body
196 self.data.tenants.append(tenant)
197
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700198 t_id = body['id']
199 resp1_name = body['name']
200
201 t_name2 = rand_name('tenant2-')
202 resp, body = self.client.update_tenant(t_id, name=t_name2)
203 st2 = resp['status']
204 resp2_name = body['name']
205 self.assertTrue(st2.startswith('2'))
206 self.assertNotEqual(resp1_name, resp2_name)
207
208 resp, body = self.client.get_tenant(t_id)
209 resp3_name = body['name']
210
211 self.assertNotEqual(resp1_name, resp3_name)
212 self.assertEqual(t_name1, resp1_name)
213 self.assertEqual(resp2_name, resp3_name)
214
215 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100216 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700217
218 def test_tenant_update_desc(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500219 # Update description attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700220 t_name = rand_name('tenant-')
221 t_desc = rand_name('desc-')
222 resp, body = self.client.create_tenant(t_name, description=t_desc)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100223 tenant = body
224 self.data.tenants.append(tenant)
225
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700226 t_id = body['id']
227 resp1_desc = body['description']
228
229 t_desc2 = rand_name('desc2-')
230 resp, body = self.client.update_tenant(t_id, description=t_desc2)
231 st2 = resp['status']
Gordon Chungad873602013-02-18 19:26:27 -0500232 resp2_desc = body['description']
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700233 self.assertTrue(st2.startswith('2'))
234 self.assertNotEqual(resp1_desc, resp2_desc)
235
236 resp, body = self.client.get_tenant(t_id)
237 resp3_desc = body['description']
238
239 self.assertNotEqual(resp1_desc, resp3_desc)
240 self.assertEqual(t_desc, resp1_desc)
241 self.assertEqual(resp2_desc, resp3_desc)
242
243 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100244 self.data.tenants.remove(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700245
246 def test_tenant_update_enable(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500247 # Update the enabled attribute of a tenant
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700248 t_name = rand_name('tenant-')
249 t_en = False
250 resp, body = self.client.create_tenant(t_name, enabled=t_en)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100251 tenant = body
252 self.data.tenants.append(tenant)
253
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700254 t_id = body['id']
255 resp1_en = body['enabled']
256
257 t_en2 = True
258 resp, body = self.client.update_tenant(t_id, enabled=t_en2)
259 st2 = resp['status']
Gordon Chungad873602013-02-18 19:26:27 -0500260 resp2_en = body['enabled']
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700261 self.assertTrue(st2.startswith('2'))
262 self.assertNotEqual(resp1_en, resp2_en)
263
264 resp, body = self.client.get_tenant(t_id)
265 resp3_en = body['enabled']
266
267 self.assertNotEqual(resp1_en, resp3_en)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800268 self.assertEqual('false', str(resp1_en).lower())
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700269 self.assertEqual(resp2_en, resp3_en)
270
271 self.client.delete_tenant(t_id)
Attila Fazekas40ec1232013-01-08 11:45:32 +0100272 self.data.tenants.remove(tenant)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800273
274
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100275class TenantsTestXML(TenantsTestJSON):
276 _interface = 'xml'