| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import uuid |
| 14 |
|
| 15 |
from keystoneauth1 import exceptions |
| 16 |
from keystoneauth1 import fixture |
| 17 |
|
| 18 |
from keystoneclient.tests.unit.v2_0 import utils |
| 19 |
from keystoneclient.v2_0 import client |
| 20 |
from keystoneclient.v2_0 import tenants |
| 21 |
from keystoneclient.v2_0 import users |
| 22 |
|
| 23 |
|
| 24 |
class TenantTests(utils.ClientTestCase): |
| 25 |
def setUp(self): |
| 26 |
super(TenantTests, self).setUp() |
| 27 |
|
| 28 |
self.INVIS_ID = uuid.uuid4().hex |
| 29 |
self.DEMO_ID = uuid.uuid4().hex |
| 30 |
self.ADMIN_ID = uuid.uuid4().hex |
| 31 |
self.EXTRAS_ID = uuid.uuid4().hex |
| 32 |
|
| 33 |
self.TEST_TENANTS = { |
| 34 |
"tenants": { |
| 35 |
"values": [ |
| 36 |
{ |
| 37 |
"enabled": True, |
| 38 |
"description": "A description change!", |
| 39 |
"name": "invisible_to_admin", |
| 40 |
"id": self.INVIS_ID, |
| 41 |
}, |
| 42 |
{ |
| 43 |
"enabled": True, |
| 44 |
"description": "None", |
| 45 |
"name": "demo", |
| 46 |
"id": self.DEMO_ID, |
| 47 |
}, |
| 48 |
{ |
| 49 |
"enabled": True, |
| 50 |
"description": "None", |
| 51 |
"name": "admin", |
| 52 |
"id": self.ADMIN_ID, |
| 53 |
}, |
| 54 |
{ |
| 55 |
"extravalue01": "metadata01", |
| 56 |
"enabled": True, |
| 57 |
"description": "For testing extras", |
| 58 |
"name": "test_extras", |
| 59 |
"id": self.EXTRAS_ID, |
| 60 |
} |
| 61 |
], |
| 62 |
"links": [], |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
def test_create(self): |
| 67 |
req_body = { |
| 68 |
"tenant": { |
| 69 |
"name": "tenantX", |
| 70 |
"description": "Like tenant 9, but better.", |
| 71 |
"enabled": True, |
| 72 |
"extravalue01": "metadata01", |
| 73 |
}, |
| 74 |
} |
| 75 |
id_ = uuid.uuid4().hex |
| 76 |
resp_body = { |
| 77 |
"tenant": { |
| 78 |
"name": "tenantX", |
| 79 |
"enabled": True, |
| 80 |
"id": id_, |
| 81 |
"description": "Like tenant 9, but better.", |
| 82 |
"extravalue01": "metadata01", |
| 83 |
} |
| 84 |
} |
| 85 |
self.stub_url('POST', ['tenants'], json=resp_body) |
| 86 |
|
| 87 |
tenant = self.client.tenants.create( |
| 88 |
req_body['tenant']['name'], |
| 89 |
req_body['tenant']['description'], |
| 90 |
req_body['tenant']['enabled'], |
| 91 |
extravalue01=req_body['tenant']['extravalue01'], |
| 92 |
name="don't overwrite priors") |
| 93 |
self.assertIsInstance(tenant, tenants.Tenant) |
| 94 |
self.assertEqual(tenant.id, id_) |
| 95 |
self.assertEqual(tenant.name, "tenantX") |
| 96 |
self.assertEqual(tenant.description, "Like tenant 9, but better.") |
| 97 |
self.assertEqual(tenant.extravalue01, "metadata01") |
| 98 |
self.assertRequestBodyIs(json=req_body) |
| 99 |
|
| 100 |
def test_duplicate_create(self): |
| 101 |
req_body = { |
| 102 |
"tenant": { |
| 103 |
"name": "tenantX", |
| 104 |
"description": "The duplicate tenant.", |
| 105 |
"enabled": True |
| 106 |
}, |
| 107 |
} |
| 108 |
resp_body = { |
| 109 |
"error": { |
| 110 |
"message": "Conflict occurred attempting to store project.", |
| 111 |
"code": 409, |
| 112 |
"title": "Conflict", |
| 113 |
} |
| 114 |
} |
| 115 |
self.stub_url('POST', ['tenants'], status_code=409, json=resp_body) |
| 116 |
|
| 117 |
def create_duplicate_tenant(): |
| 118 |
self.client.tenants.create(req_body['tenant']['name'], |
| 119 |
req_body['tenant']['description'], |
| 120 |
req_body['tenant']['enabled']) |
| 121 |
|
| 122 |
self.assertRaises(exceptions.Conflict, create_duplicate_tenant) |
| 123 |
|
| 124 |
def test_delete(self): |
| 125 |
self.stub_url('DELETE', ['tenants', self.ADMIN_ID], status_code=204) |
| 126 |
self.client.tenants.delete(self.ADMIN_ID) |
| 127 |
|
| 128 |
def test_get(self): |
| 129 |
resp = {'tenant': self.TEST_TENANTS['tenants']['values'][2]} |
| 130 |
self.stub_url('GET', ['tenants', self.ADMIN_ID], json=resp) |
| 131 |
|
| 132 |
t = self.client.tenants.get(self.ADMIN_ID) |
| 133 |
self.assertIsInstance(t, tenants.Tenant) |
| 134 |
self.assertEqual(t.id, self.ADMIN_ID) |
| 135 |
self.assertEqual(t.name, 'admin') |
| 136 |
|
| 137 |
def test_list(self): |
| 138 |
self.stub_url('GET', ['tenants'], json=self.TEST_TENANTS) |
| 139 |
|
| 140 |
tenant_list = self.client.tenants.list() |
| 141 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 142 |
|
| 143 |
def test_list_limit(self): |
| 144 |
self.stub_url('GET', ['tenants'], json=self.TEST_TENANTS) |
| 145 |
|
| 146 |
tenant_list = self.client.tenants.list(limit=1) |
| 147 |
self.assertQueryStringIs('limit=1') |
| 148 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 149 |
|
| 150 |
def test_list_marker(self): |
| 151 |
self.stub_url('GET', ['tenants'], json=self.TEST_TENANTS) |
| 152 |
|
| 153 |
tenant_list = self.client.tenants.list(marker=1) |
| 154 |
self.assertQueryStringIs('marker=1') |
| 155 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 156 |
|
| 157 |
def test_list_limit_marker(self): |
| 158 |
self.stub_url('GET', ['tenants'], json=self.TEST_TENANTS) |
| 159 |
|
| 160 |
tenant_list = self.client.tenants.list(limit=1, marker=1) |
| 161 |
self.assertQueryStringIs('marker=1&limit=1') |
| 162 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 163 |
|
| 164 |
def test_update(self): |
| 165 |
req_body = { |
| 166 |
"tenant": { |
| 167 |
"id": self.EXTRAS_ID, |
| 168 |
"name": "tenantX", |
| 169 |
"description": "I changed you!", |
| 170 |
"enabled": False, |
| 171 |
"extravalue01": "metadataChanged", |
| 172 |
|
| 173 |
}, |
| 174 |
} |
| 175 |
resp_body = { |
| 176 |
"tenant": { |
| 177 |
"name": "tenantX", |
| 178 |
"enabled": False, |
| 179 |
"id": self.EXTRAS_ID, |
| 180 |
"description": "I changed you!", |
| 181 |
"extravalue01": "metadataChanged", |
| 182 |
}, |
| 183 |
} |
| 184 |
|
| 185 |
self.stub_url('POST', ['tenants', self.EXTRAS_ID], json=resp_body) |
| 186 |
|
| 187 |
tenant = self.client.tenants.update( |
| 188 |
req_body['tenant']['id'], |
| 189 |
req_body['tenant']['name'], |
| 190 |
req_body['tenant']['description'], |
| 191 |
req_body['tenant']['enabled'], |
| 192 |
extravalue01=req_body['tenant']['extravalue01'], |
| 193 |
name="don't overwrite priors") |
| 194 |
self.assertIsInstance(tenant, tenants.Tenant) |
| 195 |
self.assertRequestBodyIs(json=req_body) |
| 196 |
self.assertEqual(tenant.id, self.EXTRAS_ID) |
| 197 |
self.assertEqual(tenant.name, "tenantX") |
| 198 |
self.assertEqual(tenant.description, "I changed you!") |
| 199 |
self.assertFalse(tenant.enabled) |
| 200 |
self.assertEqual(tenant.extravalue01, "metadataChanged") |
| 201 |
|
| 202 |
def test_update_empty_description(self): |
| 203 |
req_body = { |
| 204 |
"tenant": { |
| 205 |
"id": self.EXTRAS_ID, |
| 206 |
"name": "tenantX", |
| 207 |
"description": "", |
| 208 |
"enabled": False, |
| 209 |
}, |
| 210 |
} |
| 211 |
resp_body = { |
| 212 |
"tenant": { |
| 213 |
"name": "tenantX", |
| 214 |
"enabled": False, |
| 215 |
"id": self.EXTRAS_ID, |
| 216 |
"description": "", |
| 217 |
}, |
| 218 |
} |
| 219 |
self.stub_url('POST', ['tenants', self.EXTRAS_ID], json=resp_body) |
| 220 |
|
| 221 |
tenant = self.client.tenants.update(req_body['tenant']['id'], |
| 222 |
req_body['tenant']['name'], |
| 223 |
req_body['tenant']['description'], |
| 224 |
req_body['tenant']['enabled']) |
| 225 |
self.assertIsInstance(tenant, tenants.Tenant) |
| 226 |
self.assertRequestBodyIs(json=req_body) |
| 227 |
self.assertEqual(tenant.id, self.EXTRAS_ID) |
| 228 |
self.assertEqual(tenant.name, "tenantX") |
| 229 |
self.assertEqual(tenant.description, "") |
| 230 |
self.assertFalse(tenant.enabled) |
| 231 |
|
| 232 |
def test_add_user(self): |
| 233 |
self.stub_url('PUT', |
| 234 |
['tenants', self.EXTRAS_ID, 'users', 'foo', 'roles', |
| 235 |
'OS-KSADM', 'barrr'], |
| 236 |
status_code=204) |
| 237 |
|
| 238 |
self.client.tenants.add_user(self.EXTRAS_ID, 'foo', 'barrr') |
| 239 |
|
| 240 |
def test_remove_user(self): |
| 241 |
self.stub_url('DELETE', ['tenants', self.EXTRAS_ID, 'users', |
| 242 |
'foo', 'roles', 'OS-KSADM', 'barrr'], |
| 243 |
status_code=204) |
| 244 |
|
| 245 |
self.client.tenants.remove_user(self.EXTRAS_ID, 'foo', 'barrr') |
| 246 |
|
| 247 |
def test_tenant_add_user(self): |
| 248 |
self.stub_url('PUT', ['tenants', self.EXTRAS_ID, 'users', |
| 249 |
'foo', 'roles', 'OS-KSADM', 'barrr'], |
| 250 |
status_code=204) |
| 251 |
|
| 252 |
req_body = { |
| 253 |
"tenant": { |
| 254 |
"id": self.EXTRAS_ID, |
| 255 |
"name": "tenantX", |
| 256 |
"description": "I changed you!", |
| 257 |
"enabled": False, |
| 258 |
}, |
| 259 |
} |
| 260 |
|
| 261 |
tenant = self.client.tenants.resource_class(self.client.tenants, |
| 262 |
req_body['tenant']) |
| 263 |
tenant.add_user('foo', 'barrr') |
| 264 |
self.assertIsInstance(tenant, tenants.Tenant) |
| 265 |
|
| 266 |
def test_tenant_remove_user(self): |
| 267 |
self.stub_url('DELETE', ['tenants', self.EXTRAS_ID, 'users', |
| 268 |
'foo', 'roles', 'OS-KSADM', 'barrr'], |
| 269 |
status_code=204) |
| 270 |
|
| 271 |
req_body = { |
| 272 |
"tenant": { |
| 273 |
"id": self.EXTRAS_ID, |
| 274 |
"name": "tenantX", |
| 275 |
"description": "I changed you!", |
| 276 |
"enabled": False, |
| 277 |
}, |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
tenant = self.client.tenants.resource_class(self.client.tenants, |
| 282 |
req_body['tenant']) |
| 283 |
tenant.remove_user('foo', 'barrr') |
| 284 |
self.assertIsInstance(tenant, tenants.Tenant) |
| 285 |
|
| 286 |
def test_tenant_list_users(self): |
| 287 |
tenant_id = uuid.uuid4().hex |
| 288 |
user_id1 = uuid.uuid4().hex |
| 289 |
user_id2 = uuid.uuid4().hex |
| 290 |
|
| 291 |
tenant_resp = { |
| 292 |
'tenant': { |
| 293 |
'name': uuid.uuid4().hex, |
| 294 |
'enabled': True, |
| 295 |
'id': tenant_id, |
| 296 |
'description': 'test tenant', |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
users_resp = { |
| 301 |
'users': { |
| 302 |
'values': [ |
| 303 |
{ |
| 304 |
'email': uuid.uuid4().hex, |
| 305 |
'enabled': True, |
| 306 |
'id': user_id1, |
| 307 |
'name': uuid.uuid4().hex, |
| 308 |
}, |
| 309 |
{ |
| 310 |
'email': uuid.uuid4().hex, |
| 311 |
'enabled': True, |
| 312 |
'id': user_id2, |
| 313 |
'name': uuid.uuid4().hex, |
| 314 |
}, |
| 315 |
] |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
self.stub_url('GET', ['tenants', tenant_id], json=tenant_resp) |
| 320 |
self.stub_url('GET', |
| 321 |
['tenants', tenant_id, 'users'], |
| 322 |
json=users_resp) |
| 323 |
|
| 324 |
tenant = self.client.tenants.get(tenant_id) |
| 325 |
user_objs = tenant.list_users() |
| 326 |
|
| 327 |
for u in user_objs: |
| 328 |
self.assertIsInstance(u, users.User) |
| 329 |
|
| 330 |
self.assertEqual(set([user_id1, user_id2]), |
| 331 |
set([u.id for u in user_objs])) |
| 332 |
|
| 333 |
def test_list_tenants_use_admin_url(self): |
| 334 |
self.stub_url('GET', ['tenants'], json=self.TEST_TENANTS) |
| 335 |
|
| 336 |
tenant_list = self.client.tenants.list() |
| 337 |
self.assertEqual(self.TEST_URL + '/tenants', |
| 338 |
self.requests_mock.last_request.url) |
| 339 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 340 |
|
| 341 |
self.assertEqual(len(self.TEST_TENANTS['tenants']['values']), |
| 342 |
len(tenant_list)) |
| 343 |
|
| 344 |
def test_list_tenants_fallback_to_auth_url(self): |
| 345 |
new_auth_url = 'http://keystone.test:5000/v2.0' |
| 346 |
|
| 347 |
token = fixture.V2Token(token_id=self.TEST_TOKEN, |
| 348 |
user_name=self.TEST_USER, |
| 349 |
user_id=self.TEST_USER_ID) |
| 350 |
|
| 351 |
self.stub_auth(base_url=new_auth_url, json=token) |
| 352 |
self.stub_url('GET', ['tenants'], base_url=new_auth_url, |
| 353 |
json=self.TEST_TENANTS) |
| 354 |
|
| 355 |
|
| 356 |
with self.deprecations.expect_deprecations_here(): |
| 357 |
c = client.Client(username=self.TEST_USER, |
| 358 |
auth_url=new_auth_url, |
| 359 |
password=uuid.uuid4().hex) |
| 360 |
|
| 361 |
self.assertIsNone(c.management_url) |
| 362 |
tenant_list = c.tenants.list() |
| 363 |
[self.assertIsInstance(t, tenants.Tenant) for t in tenant_list] |
| 364 |
|
| 365 |
self.assertEqual(len(self.TEST_TENANTS['tenants']['values']), |
| 366 |
len(tenant_list)) |