Use base.setup_test_project to create project

This is to add **kwargs in base.setup_test_project, in order
that it can be more widely used in testcases to create test
project, so to make code clean.

Change-Id: Ife6e6645a6ddb5d5f291641620333d12afae7d86
This commit is contained in:
zhufl 2017-04-20 15:08:02 +08:00
parent b274c5a558
commit f2f47055b3
7 changed files with 33 additions and 87 deletions

View File

@ -42,13 +42,10 @@ class TestDefaultProjectId (base.BaseIdentityV3AdminTest):
self.addCleanup(self._delete_domain, dom_id)
# create a project in the domain
proj_name = data_utils.rand_name('proj')
proj_body = self.projects_client.create_project(
proj_name, domain_id=dom_id)['project']
proj_body = self.setup_test_project(domain_id=dom_id)
proj_id = proj_body['id']
self.addCleanup(self.projects_client.delete_project, proj_id)
self.assertEqual(proj_body['domain_id'], dom_id,
"project " + proj_name +
"project " + proj_body['name'] +
"doesn't have domain id " + dom_id)
# create a user in the domain, with the previous project as his

View File

@ -147,12 +147,8 @@ class InheritsV3TestJSON(BaseInheritsV3Test):
src_role = self.setup_test_role()
# Create a project hierarchy
leaf_project_name = data_utils.rand_name('project')
leaf_project = self.projects_client.create_project(
leaf_project_name, domain_id=self.domain['id'],
parent_id=self.project['id'])['project']
self.addCleanup(
self.projects_client.delete_project, leaf_project['id'])
leaf_project = self.setup_test_project(domain_id=self.domain['id'],
parent_id=self.project['id'])
# Assign role on domain
self.inherited_roles_client.create_inherited_role_on_domains_user(
@ -195,12 +191,8 @@ class InheritsV3TestJSON(BaseInheritsV3Test):
src_role = self.setup_test_role()
# Create a project hierarchy
leaf_project_name = data_utils.rand_name('project')
leaf_project = self.projects_client.create_project(
leaf_project_name, domain_id=self.domain['id'],
parent_id=self.project['id'])['project']
self.addCleanup(
self.projects_client.delete_project, leaf_project['id'])
leaf_project = self.setup_test_project(domain_id=self.domain['id'],
parent_id=self.project['id'])
# Assign role on parent project
self.inherited_roles_client.create_inherited_role_on_projects_user(

View File

@ -26,11 +26,8 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('0ecf465c-0dc4-4532-ab53-91ffeb74d12d')
def test_project_create_with_description(self):
# Create project with a description
project_name = data_utils.rand_name('project')
project_desc = data_utils.rand_name('desc')
project = self.projects_client.create_project(
project_name, description=project_desc)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(description=project_desc)
project_id = project['id']
desc1 = project['description']
self.assertEqual(desc1, project_desc, 'Description should have '
@ -45,9 +42,8 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
# Create project with a domain
domain = self.setup_test_domain()
project_name = data_utils.rand_name('project')
project = self.projects_client.create_project(
project_name, domain_id=domain['id'])['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(
name=project_name, domain_id=domain['id'])
project_id = project['id']
self.assertEqual(project_name, project['name'])
self.assertEqual(domain['id'], project['domain_id'])
@ -62,10 +58,8 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
domain_id = domain['id']
root_project_name = data_utils.rand_name('root_project')
root_project = self.projects_client.create_project(
root_project_name, domain_id=domain_id)['project']
self.addCleanup(
self.projects_client.delete_project, root_project['id'])
root_project = self.setup_test_project(
name=root_project_name, domain_id=domain_id)
root_project_id = root_project['id']
parent_id = root_project['parent_id']
@ -76,21 +70,16 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
# Create a project using root_project_id as parent_id
project_name = data_utils.rand_name('project')
project = self.projects_client.create_project(
project_name, domain_id=domain_id,
parent_id=root_project_id)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(
name=project_name, domain_id=domain_id, parent_id=root_project_id)
parent_id = project['parent_id']
self.assertEqual(project_name, project['name'])
self.assertEqual(root_project_id, parent_id)
@decorators.idempotent_id('a7eb9416-6f9b-4dbb-b71b-7f73aaef59d5')
def test_create_is_domain_project(self):
project_name = data_utils.rand_name('is_domain_project')
project = self.projects_client.create_project(
project_name, domain_id=None, is_domain=True)['project']
project = self.setup_test_project(domain_id=None, is_domain=True)
# To delete a domain, we need to disable it first
self.addCleanup(self.projects_client.delete_project, project['id'])
self.addCleanup(self.projects_client.update_project, project['id'],
enabled=False)
@ -109,10 +98,7 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('1f66dc76-50cc-4741-a200-af984509e480')
def test_project_create_enabled(self):
# Create a project that is enabled
project_name = data_utils.rand_name('project')
project = self.projects_client.create_project(
project_name, enabled=True)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(enabled=True)
project_id = project['id']
en1 = project['enabled']
self.assertTrue(en1, 'Enable should be True in response')
@ -123,10 +109,7 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('78f96a9c-e0e0-4ee6-a3ba-fbf6dfd03207')
def test_project_create_not_enabled(self):
# Create a project that is not enabled
project_name = data_utils.rand_name('project')
project = self.projects_client.create_project(
project_name, enabled=False)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(enabled=False)
en1 = project['enabled']
self.assertEqual('false', str(en1).lower(),
'Enable should be False in response')
@ -139,8 +122,7 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
def test_project_update_name(self):
# Update name attribute of a project
p_name1 = data_utils.rand_name('project')
project = self.projects_client.create_project(p_name1)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(name=p_name1)
resp1_name = project['name']
@ -160,11 +142,8 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('f138b715-255e-4a7d-871d-351e1ef2e153')
def test_project_update_desc(self):
# Update description attribute of a project
p_name = data_utils.rand_name('project')
p_desc = data_utils.rand_name('desc')
project = self.projects_client.create_project(
p_name, description=p_desc)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(description=p_desc)
resp1_desc = project['description']
p_desc2 = data_utils.rand_name('desc2')
@ -183,11 +162,8 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('b6b25683-c97f-474d-a595-55d410b68100')
def test_project_update_enable(self):
# Update the enabled attribute of a project
p_name = data_utils.rand_name('project')
p_en = False
project = self.projects_client.create_project(p_name,
enabled=p_en)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project(enabled=p_en)
resp1_en = project['enabled']
@ -208,9 +184,7 @@ class ProjectsTestJSON(base.BaseIdentityV3AdminTest):
def test_associate_user_to_project(self):
# Associate a user to a project
# Create a Project
p_name = data_utils.rand_name('project')
project = self.projects_client.create_project(p_name)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project()
# Create a User
u_name = data_utils.rand_name('user')

View File

@ -33,8 +33,7 @@ class ProjectsNegativeTestJSON(base.BaseIdentityV3AdminTest):
def test_project_create_duplicate(self):
# Project names should be unique
project_name = data_utils.rand_name('project-dup')
project = self.projects_client.create_project(project_name)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
self.setup_test_project(name=project_name)
self.assertRaises(lib_exc.Conflict,
self.projects_client.create_project, project_name)
@ -67,9 +66,7 @@ class ProjectsNegativeTestJSON(base.BaseIdentityV3AdminTest):
@decorators.idempotent_id('8d68c012-89e0-4394-8d6b-ccd7196def97')
def test_project_delete_by_unauthorized_user(self):
# Non-admin user should not be able to delete a project
project_name = data_utils.rand_name('project')
project = self.projects_client.create_project(project_name)['project']
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project()
self.assertRaises(
lib_exc.Forbidden, self.non_admin_projects_client.delete_project,
project['id'])

View File

@ -65,13 +65,10 @@ class TokensV3TestJSON(base.BaseIdentityV3AdminTest):
# Create a couple projects
project1_name = data_utils.rand_name(name='project')
project1 = self.projects_client.create_project(
project1_name)['project']
self.addCleanup(self.projects_client.delete_project, project1['id'])
project1 = self.setup_test_project(name=project1_name)
project2_name = data_utils.rand_name(name='project')
project2 = self.projects_client.create_project(
project2_name)['project']
project2 = self.setup_test_project(name=project2_name)
self.addCleanup(self.projects_client.delete_project, project2['id'])
# Create a role

View File

@ -42,11 +42,7 @@ class UsersV3TestJSON(base.BaseIdentityV3AdminTest):
# Delete the User at the end of this method
self.addCleanup(self.users_client.delete_user, user['id'])
# Creating second project for updation
project = self.projects_client.create_project(
data_utils.rand_name('project'),
description=data_utils.rand_name('project-desc'))['project']
# Delete the Project at the end of this method
self.addCleanup(self.projects_client.delete_project, project['id'])
project = self.setup_test_project()
# Updating user details with new values
u_name2 = data_utils.rand_name('user2')
u_email2 = u_name2 + '@testmail.tm'
@ -102,11 +98,7 @@ class UsersV3TestJSON(base.BaseIdentityV3AdminTest):
# List the projects that a user has access upon
assigned_project_ids = list()
fetched_project_ids = list()
u_project = self.projects_client.create_project(
data_utils.rand_name('project'),
description=data_utils.rand_name('project-desc'))['project']
# Delete the Project at the end of this method
self.addCleanup(self.projects_client.delete_project, u_project['id'])
u_project = self.setup_test_project()
# Create a user.
u_name = data_utils.rand_name('user')
u_desc = u_name + 'description'
@ -124,14 +116,9 @@ class UsersV3TestJSON(base.BaseIdentityV3AdminTest):
role = self.roles_client.show_role(role_body['id'])['role']
for _ in range(2):
# Creating project so as to assign role
project_body = self.projects_client.create_project(
data_utils.rand_name('project'),
description=data_utils.rand_name('project-desc'))['project']
project_body = self.setup_test_project()
project = self.projects_client.show_project(
project_body['id'])['project']
# Delete the Project at the end of this method
self.addCleanup(
self.projects_client.delete_project, project_body['id'])
# Assigning roles to user on project
self.roles_client.create_user_role_on_project(project['id'],
user['id'],

View File

@ -247,11 +247,13 @@ class BaseIdentityV3AdminTest(BaseIdentityV3Test):
password=password)
return user
def setup_test_project(self):
def setup_test_project(self, **kwargs):
"""Set up a test project."""
project = self.projects_client.create_project(
name=data_utils.rand_name('test_project'),
description=data_utils.rand_name('desc'))['project']
if 'name' not in kwargs:
kwargs['name'] = data_utils.rand_name('test_project')
if 'description' not in kwargs:
kwargs['description'] = data_utils.rand_name('test_description')
project = self.projects_client.create_project(**kwargs)['project']
# Delete the project at the end of the test
self.addCleanup(
test_utils.call_and_ignore_notfound_exc,