blob: d8b6c338c3ab073d153e45ce7ea0f4971aec54e2 [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
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070020from tempest import exceptions
21from tempest.common.utils.data_utils import rand_name
Vincent Hou6b8a7b72012-08-25 01:24:33 +080022from tempest.tests.identity import base
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070023
24
Vincent Hou6b8a7b72012-08-25 01:24:33 +080025class TenantsTestBase(object):
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070026
Vincent Hou6b8a7b72012-08-25 01:24:33 +080027 @staticmethod
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070028 def setUpClass(cls):
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070029 for _ in xrange(5):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070030 resp, tenant = cls.client.create_tenant(rand_name('tenant-'))
31 cls.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070032
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070033 def test_list_tenants(self):
34 """Return a list of all tenants"""
35 resp, body = self.client.list_tenants()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070036 found = [tenant for tenant in body if tenant in self.data.tenants]
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070037 self.assertTrue(any(found), 'List did not return newly created '
38 'tenants')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070039 self.assertEqual(len(found), len(self.data.tenants))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070040 self.assertTrue(resp['status'].startswith('2'))
41
Rohit Karajgid2d3f792012-05-14 10:28:43 -070042 def test_list_tenants_by_unauthorized_user(self):
43 """Non-admin user should not be able to list tenants"""
44 self.assertRaises(exceptions.Unauthorized,
45 self.non_admin_client.list_tenants)
46
47 def test_list_tenant_request_without_token(self):
48 """Request to list tenants without a valid token should fail"""
49 token = self.client.get_auth()
50 self.client.delete_token(token)
51 self.assertRaises(exceptions.Unauthorized, self.client.list_tenants)
52 self.client.clear_auth()
53
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070054 def test_tenant_delete(self):
55 """Create several tenants and delete them"""
56 tenants = []
57 for _ in xrange(5):
58 resp, body = self.client.create_tenant(rand_name('tenant-new'))
59 tenants.append(body['id'])
60
61 resp, body = self.client.list_tenants()
62 found_1 = [tenant for tenant in body if tenant['id'] in tenants]
63 for tenant_id in tenants:
64 resp, body = self.client.delete_tenant(tenant_id)
65 self.assertTrue(resp['status'].startswith('2'))
66
67 resp, body = self.client.list_tenants()
68 found_2 = [tenant for tenant in body if tenant['id'] in tenants]
69 self.assertTrue(any(found_1), 'Tenants not created')
70 self.assertFalse(any(found_2), 'Tenants failed to delete')
71
Rohit Karajgid2d3f792012-05-14 10:28:43 -070072 def test_tenant_delete_by_unauthorized_user(self):
73 """Non-admin user should not be able to delete a tenant"""
74 tenant_name = rand_name('tenant-')
75 resp, tenant = self.client.create_tenant(tenant_name)
76 self.assertRaises(exceptions.Unauthorized,
77 self.non_admin_client.delete_tenant, tenant['id'])
78
79 def test_tenant_delete_request_without_token(self):
80 """Request to delete a tenant without a valid token should fail"""
81 tenant_name = rand_name('tenant-')
82 resp, tenant = self.client.create_tenant(tenant_name)
83 token = self.client.get_auth()
84 self.client.delete_token(token)
85 self.assertRaises(exceptions.Unauthorized, self.client.delete_tenant,
86 tenant['id'])
87 self.client.clear_auth()
88
89 def test_delete_non_existent_tenant(self):
90 """Attempt to delete a non existent tenant should fail"""
91 self.assertRaises(exceptions.NotFound, self.client.delete_tenant,
92 'junk_tenant_123456abc')
93
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070094 def test_tenant_create_with_description(self):
95 """Create tenant with a description"""
96 tenant_name = rand_name('tenant-')
97 tenant_desc = rand_name('desc-')
98 resp, body = self.client.create_tenant(tenant_name,
99 description=tenant_desc)
100 st1 = resp['status']
101 tenant_id = body['id']
102 desc1 = body['description']
103 self.assertTrue(st1.startswith('2'))
104 self.assertEqual(desc1, tenant_desc, 'Description should have '
105 'been sent in response for create')
106 resp, body = self.client.get_tenant(tenant_id)
107 desc2 = body['description']
108 self.assertEqual(desc2, tenant_desc, 'Description does not appear'
109 'to be set')
110 self.client.delete_tenant(tenant_id)
111
112 def test_tenant_create_enabled(self):
113 """Create a tenant that is enabled"""
114 tenant_name = rand_name('tenant-')
115 resp, body = self.client.create_tenant(tenant_name, enabled=True)
116 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)
125
126 def test_tenant_create_not_enabled(self):
127 """Create a tenant that is not enabled"""
128 tenant_name = rand_name('tenant-')
129 resp, body = self.client.create_tenant(tenant_name, enabled=False)
130 tenant_id = body['id']
131 st1 = resp['status']
132 en1 = body['enabled']
133 self.assertTrue(st1.startswith('2'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800134 self.assertEqual('false', str(en1).lower(),
135 'Enable should be False in response')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700136 resp, body = self.client.get_tenant(tenant_id)
137 en2 = body['enabled']
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800138 self.assertEqual('false', str(en2).lower(),
139 'Enable should be False in lookup')
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700140 self.client.delete_tenant(tenant_id)
141
142 def test_tenant_create_duplicate(self):
143 """Tenant names should be unique"""
144 tenant_name = rand_name('tenant-dup-')
145 resp, body = self.client.create_tenant(tenant_name)
146 tenant1_id = body.get('id')
147
148 try:
149 resp, body = self.client.create_tenant(tenant_name)
150 # this should have raised an exception
151 self.fail('Should not be able to create a duplicate tenant name')
152 except exceptions.Duplicate:
153 pass
154 if tenant1_id:
155 self.client.delete_tenant(tenant1_id)
156
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700157 def test_create_tenant_by_unauthorized_user(self):
158 """Non-admin user should not be authorized to create a tenant"""
159 tenant_name = rand_name('tenant-')
160 self.assertRaises(exceptions.Unauthorized,
161 self.non_admin_client.create_tenant, tenant_name)
162
163 def test_create_tenant_request_without_token(self):
164 """Create tenant request without a token should not be authorized"""
165 tenant_name = rand_name('tenant-')
166 token = self.client.get_auth()
167 self.client.delete_token(token)
168 self.assertRaises(exceptions.Unauthorized, self.client.create_tenant,
169 tenant_name)
170 self.client.clear_auth()
171
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700172 def test_create_tenant_with_empty_name(self):
173 """Tenant name should not be empty"""
174 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
175 name='')
176
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700177 def test_create_tenants_name_length_over_64(self):
178 """Tenant name length should not be greater than 64 characters"""
David Kranz28e35c52012-07-10 10:14:38 -0400179 tenant_name = 'a' * 65
Rohit Karajgid2d3f792012-05-14 10:28:43 -0700180 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,
181 tenant_name)
182
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700183 def test_tenant_update_name(self):
184 """Update name attribute of a tenant"""
185 t_name1 = rand_name('tenant-')
186 resp, body = self.client.create_tenant(t_name1)
187 t_id = body['id']
188 resp1_name = body['name']
189
190 t_name2 = rand_name('tenant2-')
191 resp, body = self.client.update_tenant(t_id, name=t_name2)
192 st2 = resp['status']
193 resp2_name = body['name']
194 self.assertTrue(st2.startswith('2'))
195 self.assertNotEqual(resp1_name, resp2_name)
196
197 resp, body = self.client.get_tenant(t_id)
198 resp3_name = body['name']
199
200 self.assertNotEqual(resp1_name, resp3_name)
201 self.assertEqual(t_name1, resp1_name)
202 self.assertEqual(resp2_name, resp3_name)
203
204 self.client.delete_tenant(t_id)
205
206 def test_tenant_update_desc(self):
207 """Update description attribute of a tenant"""
208 t_name = rand_name('tenant-')
209 t_desc = rand_name('desc-')
210 resp, body = self.client.create_tenant(t_name, description=t_desc)
211 t_id = body['id']
212 resp1_desc = body['description']
213
214 t_desc2 = rand_name('desc2-')
215 resp, body = self.client.update_tenant(t_id, description=t_desc2)
216 st2 = resp['status']
217 resp2_desc = body['extra']['description']
218 self.assertTrue(st2.startswith('2'))
219 self.assertNotEqual(resp1_desc, resp2_desc)
220
221 resp, body = self.client.get_tenant(t_id)
222 resp3_desc = body['description']
223
224 self.assertNotEqual(resp1_desc, resp3_desc)
225 self.assertEqual(t_desc, resp1_desc)
226 self.assertEqual(resp2_desc, resp3_desc)
227
228 self.client.delete_tenant(t_id)
229
230 def test_tenant_update_enable(self):
231 """Update the enabled attribute of a tenant"""
232 t_name = rand_name('tenant-')
233 t_en = False
234 resp, body = self.client.create_tenant(t_name, enabled=t_en)
235 t_id = body['id']
236 resp1_en = body['enabled']
237
238 t_en2 = True
239 resp, body = self.client.update_tenant(t_id, enabled=t_en2)
240 st2 = resp['status']
241 resp2_en = body['extra']['enabled']
242 self.assertTrue(st2.startswith('2'))
243 self.assertNotEqual(resp1_en, resp2_en)
244
245 resp, body = self.client.get_tenant(t_id)
246 resp3_en = body['enabled']
247
248 self.assertNotEqual(resp1_en, resp3_en)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800249 self.assertEqual('false', str(resp1_en).lower())
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700250 self.assertEqual(resp2_en, resp3_en)
251
252 self.client.delete_tenant(t_id)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800253
254
255class TenantsTestJSON(base.BaseIdentityAdminTestJSON,
256 TenantsTestBase):
257
258 @classmethod
259 def setUpClass(cls):
260 super(TenantsTestJSON, cls).setUpClass()
261 TenantsTestBase.setUpClass(cls)
262
263
264class TenantsTestXML(base.BaseIdentityAdminTestXML,
265 TenantsTestBase):
266
267 @classmethod
268 def setUpClass(cls):
269 super(TenantsTestXML, cls).setUpClass()
270 TenantsTestBase.setUpClass(cls)