blob: 253e52ee5e9c10ac7c6429629886e7b2e1d1feb3 [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
Jay Pipesf38eaac2012-06-21 13:37:35 -040022from tempest.tests.identity.base import BaseIdentityAdminTest
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070023
24
Jay Pipesf38eaac2012-06-21 13:37:35 -040025class TenantsTest(BaseIdentityAdminTest):
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070026
27 @classmethod
28 def setUpClass(cls):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070029 super(TenantsTest, cls).setUpClass()
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070030
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070031 for _ in xrange(5):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070032 resp, tenant = cls.client.create_tenant(rand_name('tenant-'))
33 cls.data.tenants.append(tenant)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070034
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070035 def test_list_tenants(self):
36 """Return a list of all tenants"""
37 resp, body = self.client.list_tenants()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070038 found = [tenant for tenant in body if tenant in self.data.tenants]
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070039 self.assertTrue(any(found), 'List did not return newly created '
40 'tenants')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070041 self.assertEqual(len(found), len(self.data.tenants))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070042 self.assertTrue(resp['status'].startswith('2'))
43
Rohit Karajgid2d3f792012-05-14 10:28:43 -070044 def test_list_tenants_by_unauthorized_user(self):
45 """Non-admin user should not be able to list tenants"""
46 self.assertRaises(exceptions.Unauthorized,
47 self.non_admin_client.list_tenants)
48
49 def test_list_tenant_request_without_token(self):
50 """Request to list tenants without a valid token should fail"""
51 token = self.client.get_auth()
52 self.client.delete_token(token)
53 self.assertRaises(exceptions.Unauthorized, self.client.list_tenants)
54 self.client.clear_auth()
55
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070056 def test_tenant_delete(self):
57 """Create several tenants and delete them"""
58 tenants = []
59 for _ in xrange(5):
60 resp, body = self.client.create_tenant(rand_name('tenant-new'))
61 tenants.append(body['id'])
62
63 resp, body = self.client.list_tenants()
64 found_1 = [tenant for tenant in body if tenant['id'] in tenants]
65 for tenant_id in tenants:
66 resp, body = self.client.delete_tenant(tenant_id)
67 self.assertTrue(resp['status'].startswith('2'))
68
69 resp, body = self.client.list_tenants()
70 found_2 = [tenant for tenant in body if tenant['id'] in tenants]
71 self.assertTrue(any(found_1), 'Tenants not created')
72 self.assertFalse(any(found_2), 'Tenants failed to delete')
73
Rohit Karajgid2d3f792012-05-14 10:28:43 -070074 def test_tenant_delete_by_unauthorized_user(self):
75 """Non-admin user should not be able to delete a tenant"""
76 tenant_name = rand_name('tenant-')
77 resp, tenant = self.client.create_tenant(tenant_name)
78 self.assertRaises(exceptions.Unauthorized,
79 self.non_admin_client.delete_tenant, tenant['id'])
80
81 def test_tenant_delete_request_without_token(self):
82 """Request to delete a tenant without a valid token should fail"""
83 tenant_name = rand_name('tenant-')
84 resp, tenant = self.client.create_tenant(tenant_name)
85 token = self.client.get_auth()
86 self.client.delete_token(token)
87 self.assertRaises(exceptions.Unauthorized, self.client.delete_tenant,
88 tenant['id'])
89 self.client.clear_auth()
90
91 def test_delete_non_existent_tenant(self):
92 """Attempt to delete a non existent tenant should fail"""
93 self.assertRaises(exceptions.NotFound, self.client.delete_tenant,
94 'junk_tenant_123456abc')
95
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070096 def test_tenant_create_with_description(self):
97 """Create tenant with a description"""
98 tenant_name = rand_name('tenant-')
99 tenant_desc = rand_name('desc-')
100 resp, body = self.client.create_tenant(tenant_name,
101 description=tenant_desc)
102 st1 = resp['status']
103 tenant_id = body['id']
104 desc1 = body['description']
105 self.assertTrue(st1.startswith('2'))
106 self.assertEqual(desc1, tenant_desc, 'Description should have '
107 'been sent in response for create')
108 resp, body = self.client.get_tenant(tenant_id)
109 desc2 = body['description']
110 self.assertEqual(desc2, tenant_desc, 'Description does not appear'
111 'to be set')
112 self.client.delete_tenant(tenant_id)
113
114 def test_tenant_create_enabled(self):
115 """Create a tenant that is enabled"""
116 tenant_name = rand_name('tenant-')
117 resp, body = self.client.create_tenant(tenant_name, enabled=True)
118 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)
127
128 def test_tenant_create_not_enabled(self):
129 """Create a tenant that is not enabled"""
130 tenant_name = rand_name('tenant-')
131 resp, body = self.client.create_tenant(tenant_name, enabled=False)
132 tenant_id = body['id']
133 st1 = resp['status']
134 en1 = body['enabled']
135 self.assertTrue(st1.startswith('2'))
136 self.assertFalse(en1, 'Enable should be False in response')
137 resp, body = self.client.get_tenant(tenant_id)
138 en2 = body['enabled']
139 self.assertFalse(en2, 'Enable should be False in lookup')
140 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)
249 self.assertEqual(t_en, resp1_en)
250 self.assertEqual(resp2_en, resp3_en)
251
252 self.client.delete_tenant(t_id)