From 441e4e963a131b9cd323e22c9d0af17a1a033957 Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Mon, 4 Jul 2016 09:42:14 +0800 Subject: [PATCH] Refactor unit tests for project and domain with fake classes in identityv3 Add FakeProject and FakeDomain classes and update unit tests for project and domain. Change-Id: Ifeed5dcba03155daa3b7b46b34d49c333ab19135 Partially-Implements: blueprint refactor-identity-unit-test --- openstackclient/tests/identity/v3/fakes.py | 64 +++ .../tests/identity/v3/test_domain.py | 136 +++--- .../tests/identity/v3/test_project.py | 441 +++++++++--------- 3 files changed, 343 insertions(+), 298 deletions(-) diff --git a/openstackclient/tests/identity/v3/fakes.py b/openstackclient/tests/identity/v3/fakes.py index dd918616ac..df532df401 100644 --- a/openstackclient/tests/identity/v3/fakes.py +++ b/openstackclient/tests/identity/v3/fakes.py @@ -15,6 +15,7 @@ import copy import mock +import uuid from keystoneauth1 import access from keystoneauth1 import fixture @@ -575,3 +576,66 @@ class TestOAuth1(utils.TestCommand): endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN ) + + +class FakeProject(object): + """Fake one or more project.""" + + @staticmethod + def create_one_project(attrs=None): + """Create a fake project. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object, with id, name, and so on + """ + + attrs = attrs or {} + + # set default attributes. + project_info = { + 'id': 'project-id-' + uuid.uuid4().hex, + 'name': 'project-name-' + uuid.uuid4().hex, + 'description': 'project-description-' + uuid.uuid4().hex, + 'enabled': True, + 'is_domain': False, + 'domain_id': 'domain-id-' + uuid.uuid4().hex, + 'parent_id': 'parent-id-' + uuid.uuid4().hex, + 'links': 'links-' + uuid.uuid4().hex, + } + project_info.update(attrs) + + project = fakes.FakeResource(info=copy.deepcopy(project_info), + loaded=True) + return project + + +class FakeDomain(object): + """Fake one or more domain.""" + + @staticmethod + def create_one_domain(attrs=None): + """Create a fake domain. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object, with id, name, and so on + """ + + attrs = attrs or {} + + # set default attributes. + domain_info = { + 'id': 'domain-id-' + uuid.uuid4().hex, + 'name': 'domain-name-' + uuid.uuid4().hex, + 'description': 'domain-description-' + uuid.uuid4().hex, + 'enabled': True, + 'links': 'links-' + uuid.uuid4().hex, + } + domain_info.update(attrs) + + domain = fakes.FakeResource(info=copy.deepcopy(domain_info), + loaded=True) + return domain diff --git a/openstackclient/tests/identity/v3/test_domain.py b/openstackclient/tests/identity/v3/test_domain.py index e06e06812c..9229ddd0a9 100644 --- a/openstackclient/tests/identity/v3/test_domain.py +++ b/openstackclient/tests/identity/v3/test_domain.py @@ -10,10 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import copy - from openstackclient.identity.v3 import domain -from openstackclient.tests import fakes from openstackclient.tests.identity.v3 import fakes as identity_fakes @@ -35,20 +32,17 @@ class TestDomainCreate(TestDomain): 'id', 'name', ) - datalist = ( - identity_fakes.domain_description, - True, - identity_fakes.domain_id, - identity_fakes.domain_name, - ) def setUp(self): super(TestDomainCreate, self).setUp() - self.domains_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, + self.domain = identity_fakes.FakeDomain.create_one_domain() + self.domains_mock.create.return_value = self.domain + self.datalist = ( + self.domain.description, + True, + self.domain.id, + self.domain.name, ) # Get the command object to test @@ -56,10 +50,10 @@ class TestDomainCreate(TestDomain): def test_domain_create_no_options(self): arglist = [ - identity_fakes.domain_name, + self.domain.name, ] verifylist = [ - ('name', identity_fakes.domain_name), + ('name', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -70,7 +64,7 @@ class TestDomainCreate(TestDomain): # Set expected values kwargs = { - 'name': identity_fakes.domain_name, + 'name': self.domain.name, 'description': None, 'enabled': True, } @@ -84,11 +78,11 @@ class TestDomainCreate(TestDomain): def test_domain_create_description(self): arglist = [ '--description', 'new desc', - identity_fakes.domain_name, + self.domain.name, ] verifylist = [ ('description', 'new desc'), - ('name', identity_fakes.domain_name), + ('name', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -99,7 +93,7 @@ class TestDomainCreate(TestDomain): # Set expected values kwargs = { - 'name': identity_fakes.domain_name, + 'name': self.domain.name, 'description': 'new desc', 'enabled': True, } @@ -113,11 +107,11 @@ class TestDomainCreate(TestDomain): def test_domain_create_enable(self): arglist = [ '--enable', - identity_fakes.domain_name, + self.domain.name, ] verifylist = [ ('enable', True), - ('name', identity_fakes.domain_name), + ('name', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -128,7 +122,7 @@ class TestDomainCreate(TestDomain): # Set expected values kwargs = { - 'name': identity_fakes.domain_name, + 'name': self.domain.name, 'description': None, 'enabled': True, } @@ -142,11 +136,11 @@ class TestDomainCreate(TestDomain): def test_domain_create_disable(self): arglist = [ '--disable', - identity_fakes.domain_name, + self.domain.name, ] verifylist = [ ('disable', True), - ('name', identity_fakes.domain_name), + ('name', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -157,7 +151,7 @@ class TestDomainCreate(TestDomain): # Set expected values kwargs = { - 'name': identity_fakes.domain_name, + 'name': self.domain.name, 'description': None, 'enabled': False, } @@ -171,15 +165,13 @@ class TestDomainCreate(TestDomain): class TestDomainDelete(TestDomain): + domain = identity_fakes.FakeDomain.create_one_domain() + def setUp(self): super(TestDomainDelete, self).setUp() # This is the return value for utils.find_resource() - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain self.domains_mock.delete.return_value = None # Get the command object to test @@ -187,33 +179,29 @@ class TestDomainDelete(TestDomain): def test_domain_delete(self): arglist = [ - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.domains_mock.delete.assert_called_with( - identity_fakes.domain_id, + self.domain.id, ) self.assertIsNone(result) class TestDomainList(TestDomain): + domain = identity_fakes.FakeDomain.create_one_domain() + def setUp(self): super(TestDomainList, self).setUp() - self.domains_mock.list.return_value = [ - fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ), - ] + self.domains_mock.list.return_value = [self.domain] # Get the command object to test self.cmd = domain.ListDomain(self.app, None) @@ -232,40 +220,34 @@ class TestDomainList(TestDomain): collist = ('ID', 'Name', 'Enabled', 'Description') self.assertEqual(collist, columns) datalist = (( - identity_fakes.domain_id, - identity_fakes.domain_name, + self.domain.id, + self.domain.name, True, - identity_fakes.domain_description, + self.domain.description, ), ) self.assertEqual(datalist, tuple(data)) class TestDomainSet(TestDomain): + domain = identity_fakes.FakeDomain.create_one_domain() + def setUp(self): super(TestDomainSet, self).setUp() - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain - self.domains_mock.update.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.update.return_value = self.domain # Get the command object to test self.cmd = domain.SetDomain(self.app, None) def test_domain_set_no_options(self): arglist = [ - identity_fakes.domain_name, + self.domain.name, ] verifylist = [ - ('domain', identity_fakes.domain_name), + ('domain', self.domain.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -277,11 +259,11 @@ class TestDomainSet(TestDomain): def test_domain_set_name(self): arglist = [ '--name', 'qwerty', - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ ('name', 'qwerty'), - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -292,7 +274,7 @@ class TestDomainSet(TestDomain): 'name': 'qwerty', } self.domains_mock.update.assert_called_with( - identity_fakes.domain_id, + self.domain.id, **kwargs ) self.assertIsNone(result) @@ -300,11 +282,11 @@ class TestDomainSet(TestDomain): def test_domain_set_description(self): arglist = [ '--description', 'new desc', - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ ('description', 'new desc'), - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -315,7 +297,7 @@ class TestDomainSet(TestDomain): 'description': 'new desc', } self.domains_mock.update.assert_called_with( - identity_fakes.domain_id, + self.domain.id, **kwargs ) self.assertIsNone(result) @@ -323,11 +305,11 @@ class TestDomainSet(TestDomain): def test_domain_set_enable(self): arglist = [ '--enable', - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ ('enable', True), - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -338,7 +320,7 @@ class TestDomainSet(TestDomain): 'enabled': True, } self.domains_mock.update.assert_called_with( - identity_fakes.domain_id, + self.domain.id, **kwargs ) self.assertIsNone(result) @@ -346,11 +328,11 @@ class TestDomainSet(TestDomain): def test_domain_set_disable(self): arglist = [ '--disable', - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ ('disable', True), - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -361,7 +343,7 @@ class TestDomainSet(TestDomain): 'enabled': False, } self.domains_mock.update.assert_called_with( - identity_fakes.domain_id, + self.domain.id, **kwargs ) self.assertIsNone(result) @@ -372,21 +354,17 @@ class TestDomainShow(TestDomain): def setUp(self): super(TestDomainShow, self).setUp() - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) - + self.domain = identity_fakes.FakeDomain.create_one_domain() + self.domains_mock.get.return_value = self.domain # Get the command object to test self.cmd = domain.ShowDomain(self.app, None) def test_domain_show(self): arglist = [ - identity_fakes.domain_id, + self.domain.id, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.domain.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.app.client_manager.identity.tokens.get_token_data.return_value = \ @@ -405,15 +383,15 @@ class TestDomainShow(TestDomain): # data to be shown. columns, data = self.cmd.take_action(parsed_args) self.domains_mock.get.assert_called_with( - identity_fakes.domain_id, + self.domain.id, ) collist = ('description', 'enabled', 'id', 'name') self.assertEqual(collist, columns) datalist = ( - identity_fakes.domain_description, + self.domain.description, True, - identity_fakes.domain_id, - identity_fakes.domain_name, + self.domain.id, + self.domain.name, ) self.assertEqual(datalist, data) diff --git a/openstackclient/tests/identity/v3/test_project.py b/openstackclient/tests/identity/v3/test_project.py index 93bf18afbc..4c122e3d90 100644 --- a/openstackclient/tests/identity/v3/test_project.py +++ b/openstackclient/tests/identity/v3/test_project.py @@ -13,13 +13,11 @@ # under the License. # -import copy import mock from osc_lib import exceptions from openstackclient.identity.v3 import project -from openstackclient.tests import fakes from openstackclient.tests.identity.v3 import fakes as identity_fakes @@ -39,48 +37,46 @@ class TestProject(identity_fakes.TestIdentityv3): class TestProjectCreate(TestProject): + domain = identity_fakes.FakeDomain.create_one_domain() + columns = ( 'description', 'domain_id', 'enabled', 'id', - 'name' - ) - datalist = ( - identity_fakes.project_description, - identity_fakes.domain_id, - True, - identity_fakes.project_id, - identity_fakes.project_name, + 'is_domain', + 'name', + 'parent_id', ) def setUp(self): super(TestProjectCreate, self).setUp() - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, + self.project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': self.domain.id}) + self.domains_mock.get.return_value = self.domain + self.projects_mock.create.return_value = self.project + self.datalist = ( + self.project.description, + self.project.domain_id, + True, + self.project.id, + False, + self.project.name, + self.project.parent_id, ) - - self.projects_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) - # Get the command object to test self.cmd = project.CreateProject(self.app, None) def test_project_create_no_options(self): arglist = [ - identity_fakes.project_name, + self.project.name, ] verifylist = [ ('parent', None), ('enable', False), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -91,7 +87,7 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, + 'name': self.project.name, 'domain': None, 'description': None, 'enabled': True, @@ -103,27 +99,37 @@ class TestProjectCreate(TestProject): **kwargs ) - collist = ('description', 'domain_id', 'enabled', 'id', 'name') + collist = ( + 'description', + 'domain_id', + 'enabled', + 'id', + 'is_domain', + 'name', + 'parent_id', + ) self.assertEqual(collist, columns) datalist = ( - identity_fakes.project_description, - identity_fakes.domain_id, + self.project.description, + self.project.domain_id, True, - identity_fakes.project_id, - identity_fakes.project_name, + self.project.id, + False, + self.project.name, + self.project.parent_id, ) self.assertEqual(datalist, data) def test_project_create_description(self): arglist = [ '--description', 'new desc', - identity_fakes.project_name, + self.project.name, ] verifylist = [ ('description', 'new desc'), ('enable', False), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ('parent', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -135,7 +141,7 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, + 'name': self.project.name, 'domain': None, 'description': 'new desc', 'enabled': True, @@ -152,14 +158,14 @@ class TestProjectCreate(TestProject): def test_project_create_domain(self): arglist = [ - '--domain', identity_fakes.domain_name, - identity_fakes.project_name, + '--domain', self.project.domain_id, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_name), + ('domain', self.project.domain_id), ('enable', False), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ('parent', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -171,8 +177,8 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, - 'domain': identity_fakes.domain_id, + 'name': self.project.name, + 'domain': self.project.domain_id, 'description': None, 'enabled': True, 'parent': None, @@ -188,14 +194,14 @@ class TestProjectCreate(TestProject): def test_project_create_domain_no_perms(self): arglist = [ - '--domain', identity_fakes.domain_id, - identity_fakes.project_name, + '--domain', self.project.domain_id, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('enable', False), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ('parent', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -207,8 +213,8 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, - 'domain': identity_fakes.domain_id, + 'name': self.project.name, + 'domain': self.project.domain_id, 'description': None, 'enabled': True, 'parent': None, @@ -222,12 +228,12 @@ class TestProjectCreate(TestProject): def test_project_create_enable(self): arglist = [ '--enable', - identity_fakes.project_name, + self.project.name, ] verifylist = [ ('enable', True), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ('parent', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -239,7 +245,7 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, + 'name': self.project.name, 'domain': None, 'description': None, 'enabled': True, @@ -257,12 +263,12 @@ class TestProjectCreate(TestProject): def test_project_create_disable(self): arglist = [ '--disable', - identity_fakes.project_name, + self.project.name, ] verifylist = [ ('enable', False), ('disable', True), - ('name', identity_fakes.project_name), + ('name', self.project.name), ('parent', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -274,7 +280,7 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, + 'name': self.project.name, 'domain': None, 'description': None, 'enabled': False, @@ -293,11 +299,11 @@ class TestProjectCreate(TestProject): arglist = [ '--property', 'fee=fi', '--property', 'fo=fum', - identity_fakes.project_name, + self.project.name, ] verifylist = [ ('property', {'fee': 'fi', 'fo': 'fum'}), - ('name', identity_fakes.project_name), + ('name', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -308,7 +314,7 @@ class TestProjectCreate(TestProject): # Set expected values kwargs = { - 'name': identity_fakes.project_name, + 'name': self.project.name, 'domain': None, 'description': None, 'enabled': True, @@ -326,37 +332,32 @@ class TestProjectCreate(TestProject): self.assertEqual(self.datalist, data) def test_project_create_parent(self): - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) - self.projects_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT_WITH_PARENT), - loaded=True, - ) + self.parent = identity_fakes.FakeProject.create_one_project() + self.project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': self.domain.id, 'parent_id': self.parent.id}) + self.projects_mock.get.return_value = self.parent + self.projects_mock.create.return_value = self.project arglist = [ - '--domain', identity_fakes.PROJECT_WITH_PARENT['domain_id'], - '--parent', identity_fakes.PROJECT['name'], - identity_fakes.PROJECT_WITH_PARENT['name'], + '--domain', self.project.domain_id, + '--parent', self.parent.name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.PROJECT_WITH_PARENT['domain_id']), - ('parent', identity_fakes.PROJECT['name']), + ('domain', self.project.domain_id), + ('parent', self.parent.name), ('enable', False), ('disable', False), - ('name', identity_fakes.PROJECT_WITH_PARENT['name']), + ('name', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) kwargs = { - 'name': identity_fakes.PROJECT_WITH_PARENT['name'], - 'domain': identity_fakes.PROJECT_WITH_PARENT['domain_id'], - 'parent': identity_fakes.PROJECT['id'], + 'name': self.project.name, + 'domain': self.project.domain_id, + 'parent': self.parent.id, 'description': None, 'enabled': True, } @@ -370,17 +371,19 @@ class TestProjectCreate(TestProject): 'domain_id', 'enabled', 'id', + 'is_domain', 'name', 'parent_id', ) self.assertEqual(columns, collist) datalist = ( - identity_fakes.PROJECT_WITH_PARENT['description'], - identity_fakes.PROJECT_WITH_PARENT['domain_id'], - identity_fakes.PROJECT_WITH_PARENT['enabled'], - identity_fakes.PROJECT_WITH_PARENT['id'], - identity_fakes.PROJECT_WITH_PARENT['name'], - identity_fakes.PROJECT['id'], + self.project.description, + self.project.domain_id, + self.project.enabled, + self.project.id, + self.project.is_domain, + self.project.name, + self.parent.id, ) self.assertEqual(data, datalist) @@ -392,16 +395,16 @@ class TestProjectCreate(TestProject): 'Invalid parent') arglist = [ - '--domain', identity_fakes.domain_name, + '--domain', self.project.domain_id, '--parent', 'invalid', - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_name), + ('domain', self.project.domain_id), ('parent', 'invalid'), ('enable', False), ('disable', False), - ('name', identity_fakes.project_name), + ('name', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -414,15 +417,13 @@ class TestProjectCreate(TestProject): class TestProjectDelete(TestProject): + project = identity_fakes.FakeProject.create_one_project() + def setUp(self): super(TestProjectDelete, self).setUp() # This is the return value for utils.find_resource() - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project self.projects_mock.delete.return_value = None # Get the command object to test @@ -430,44 +431,42 @@ class TestProjectDelete(TestProject): def test_project_delete_no_options(self): arglist = [ - identity_fakes.project_id, + self.project.id, ] verifylist = [ - ('projects', [identity_fakes.project_id]), + ('projects', [self.project.id]), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.projects_mock.delete.assert_called_with( - identity_fakes.project_id, + self.project.id, ) self.assertIsNone(result) class TestProjectList(TestProject): + domain = identity_fakes.FakeDomain.create_one_domain() + project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': domain.id}) + columns = ( 'ID', 'Name', ) datalist = ( ( - identity_fakes.project_id, - identity_fakes.project_name, + project.id, + project.name, ), ) def setUp(self): super(TestProjectList, self).setUp() - self.projects_mock.list.return_value = [ - fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ), - ] + self.projects_mock.list.return_value = [self.project] # Get the command object to test self.cmd = project.ListProject(self.app, None) @@ -504,27 +503,23 @@ class TestProjectList(TestProject): collist = ('ID', 'Name', 'Domain ID', 'Description', 'Enabled') self.assertEqual(collist, columns) datalist = (( - identity_fakes.project_id, - identity_fakes.project_name, - identity_fakes.domain_id, - identity_fakes.project_description, + self.project.id, + self.project.name, + self.project.domain_id, + self.project.description, True, ), ) self.assertEqual(datalist, tuple(data)) def test_project_list_domain(self): arglist = [ - '--domain', identity_fakes.domain_name, + '--domain', self.project.domain_id, ] verifylist = [ - ('domain', identity_fakes.domain_name), + ('domain', self.project.domain_id), ] - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -533,17 +528,17 @@ class TestProjectList(TestProject): # containing the data to be listed. columns, data = self.cmd.take_action(parsed_args) self.projects_mock.list.assert_called_with( - domain=identity_fakes.domain_id) + domain=self.project.domain_id) self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, tuple(data)) def test_project_list_domain_no_perms(self): arglist = [ - '--domain', identity_fakes.domain_id, + '--domain', self.project.domain_id, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) mocker = mock.Mock() @@ -553,42 +548,34 @@ class TestProjectList(TestProject): columns, data = self.cmd.take_action(parsed_args) self.projects_mock.list.assert_called_with( - domain=identity_fakes.domain_id) + domain=self.project.domain_id) self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, tuple(data)) class TestProjectSet(TestProject): + domain = identity_fakes.FakeDomain.create_one_domain() + project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': domain.id}) + def setUp(self): super(TestProjectSet, self).setUp() - self.domains_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.DOMAIN), - loaded=True, - ) + self.domains_mock.get.return_value = self.domain - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) - self.projects_mock.update.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.projects_mock.get.return_value = self.project + self.projects_mock.update.return_value = self.project # Get the command object to test self.cmd = project.SetProject(self.app, None) def test_project_set_no_options(self): arglist = [ - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('project', identity_fakes.project_name), + ('project', self.project.name), ('enable', False), ('disable', False), ] @@ -601,15 +588,15 @@ class TestProjectSet(TestProject): def test_project_set_name(self): arglist = [ '--name', 'qwerty', - '--domain', identity_fakes.domain_id, - identity_fakes.project_name, + '--domain', self.project.domain_id, + self.project.name, ] verifylist = [ ('name', 'qwerty'), - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('enable', False), ('disable', False), - ('project', identity_fakes.project_name), + ('project', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -622,23 +609,23 @@ class TestProjectSet(TestProject): # ProjectManager.update(project, name=, domain=, description=, # enabled=, **kwargs) self.projects_mock.update.assert_called_with( - identity_fakes.project_id, + self.project.id, **kwargs ) self.assertIsNone(result) def test_project_set_description(self): arglist = [ - '--domain', identity_fakes.domain_id, + '--domain', self.project.domain_id, '--description', 'new desc', - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('description', 'new desc'), ('enable', False), ('disable', False), - ('project', identity_fakes.project_name), + ('project', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -649,22 +636,22 @@ class TestProjectSet(TestProject): 'description': 'new desc', } self.projects_mock.update.assert_called_with( - identity_fakes.project_id, + self.project.id, **kwargs ) self.assertIsNone(result) def test_project_set_enable(self): arglist = [ - '--domain', identity_fakes.domain_id, + '--domain', self.project.domain_id, '--enable', - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('enable', True), ('disable', False), - ('project', identity_fakes.project_name), + ('project', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -675,22 +662,22 @@ class TestProjectSet(TestProject): 'enabled': True, } self.projects_mock.update.assert_called_with( - identity_fakes.project_id, + self.project.id, **kwargs ) self.assertIsNone(result) def test_project_set_disable(self): arglist = [ - '--domain', identity_fakes.domain_id, + '--domain', self.project.domain_id, '--disable', - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('enable', False), ('disable', True), - ('project', identity_fakes.project_name), + ('project', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -701,22 +688,22 @@ class TestProjectSet(TestProject): 'enabled': False, } self.projects_mock.update.assert_called_with( - identity_fakes.project_id, + self.project.id, **kwargs ) self.assertIsNone(result) def test_project_set_property(self): arglist = [ - '--domain', identity_fakes.domain_id, + '--domain', self.project.domain_id, '--property', 'fee=fi', '--property', 'fo=fum', - identity_fakes.project_name, + self.project.name, ] verifylist = [ - ('domain', identity_fakes.domain_id), + ('domain', self.project.domain_id), ('property', {'fee': 'fi', 'fo': 'fum'}), - ('project', identity_fakes.project_name), + ('project', self.project.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -728,7 +715,7 @@ class TestProjectSet(TestProject): 'fo': 'fum', } self.projects_mock.update.assert_called_with( - identity_fakes.project_id, + self.project.id, **kwargs ) self.assertIsNone(result) @@ -736,14 +723,14 @@ class TestProjectSet(TestProject): class TestProjectShow(TestProject): + domain = identity_fakes.FakeDomain.create_one_domain() + def setUp(self): super(TestProjectShow, self).setUp() - self.projects_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.PROJECT), - loaded=True, - ) + self.project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': self.domain.id}) + self.projects_mock.get.return_value = self.project # Get the command object to test self.cmd = project.ShowProject(self.app, None) @@ -751,10 +738,10 @@ class TestProjectShow(TestProject): def test_project_show(self): arglist = [ - identity_fakes.project_id, + self.project.id, ] verifylist = [ - ('project', identity_fakes.project_id), + ('project', self.project.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -773,37 +760,47 @@ class TestProjectShow(TestProject): # data to be shown. columns, data = self.cmd.take_action(parsed_args) self.projects_mock.get.assert_called_with( - identity_fakes.project_id, + self.project.id, parents_as_list=False, subtree_as_list=False, ) - collist = ('description', 'domain_id', 'enabled', 'id', 'name') + collist = ( + 'description', + 'domain_id', + 'enabled', + 'id', + 'is_domain', + 'name', + 'parent_id', + ) self.assertEqual(collist, columns) datalist = ( - identity_fakes.project_description, - identity_fakes.domain_id, + self.project.description, + self.project.domain_id, True, - identity_fakes.project_id, - identity_fakes.project_name, + self.project.id, + False, + self.project.name, + self.project.parent_id, ) self.assertEqual(datalist, data) def test_project_show_parents(self): - project = copy.deepcopy(identity_fakes.PROJECT_WITH_GRANDPARENT) - project['parents'] = identity_fakes.grandparents - self.projects_mock.get.return_value = fakes.FakeResource( - None, - project, - loaded=True, + self.project = identity_fakes.FakeProject.create_one_project( + attrs={ + 'parent_id': self.project.parent_id, + 'parents': [{'project': {'id': self.project.parent_id}}] + } ) + self.projects_mock.get.return_value = self.project arglist = [ - identity_fakes.PROJECT_WITH_GRANDPARENT['id'], + self.project.id, '--parents', ] verifylist = [ - ('project', identity_fakes.PROJECT_WITH_GRANDPARENT['id']), + ('project', self.project.id), ('parents', True), ('children', False), ] @@ -820,7 +817,7 @@ class TestProjectShow(TestProject): columns, data = self.cmd.take_action(parsed_args) self.projects_mock.get.assert_called_with( - identity_fakes.PROJECT_WITH_GRANDPARENT['id'], + self.project.id, parents_as_list=True, subtree_as_list=False, ) @@ -830,37 +827,39 @@ class TestProjectShow(TestProject): 'domain_id', 'enabled', 'id', + 'is_domain', 'name', 'parent_id', 'parents', ) self.assertEqual(columns, collist) datalist = ( - identity_fakes.PROJECT_WITH_GRANDPARENT['description'], - identity_fakes.PROJECT_WITH_GRANDPARENT['domain_id'], - identity_fakes.PROJECT_WITH_GRANDPARENT['enabled'], - identity_fakes.PROJECT_WITH_GRANDPARENT['id'], - identity_fakes.PROJECT_WITH_GRANDPARENT['name'], - identity_fakes.PROJECT_WITH_GRANDPARENT['parent_id'], - identity_fakes.ids_for_parents_and_grandparents, + self.project.description, + self.project.domain_id, + self.project.enabled, + self.project.id, + self.project.is_domain, + self.project.name, + self.project.parent_id, + [self.project.parent_id], ) self.assertEqual(data, datalist) def test_project_show_subtree(self): - project = copy.deepcopy(identity_fakes.PROJECT_WITH_PARENT) - project['subtree'] = identity_fakes.children - self.projects_mock.get.return_value = fakes.FakeResource( - None, - project, - loaded=True, + self.project = identity_fakes.FakeProject.create_one_project( + attrs={ + 'parent_id': self.project.parent_id, + 'subtree': [{'project': {'id': 'children-id'}}] + } ) + self.projects_mock.get.return_value = self.project arglist = [ - identity_fakes.PROJECT_WITH_PARENT['id'], + self.project.id, '--children', ] verifylist = [ - ('project', identity_fakes.PROJECT_WITH_PARENT['id']), + ('project', self.project.id), ('parents', False), ('children', True), ] @@ -877,7 +876,7 @@ class TestProjectShow(TestProject): columns, data = self.cmd.take_action(parsed_args) self.projects_mock.get.assert_called_with( - identity_fakes.PROJECT_WITH_PARENT['id'], + self.project.id, parents_as_list=False, subtree_as_list=True, ) @@ -887,39 +886,41 @@ class TestProjectShow(TestProject): 'domain_id', 'enabled', 'id', + 'is_domain', 'name', 'parent_id', 'subtree', ) self.assertEqual(columns, collist) datalist = ( - identity_fakes.PROJECT_WITH_PARENT['description'], - identity_fakes.PROJECT_WITH_PARENT['domain_id'], - identity_fakes.PROJECT_WITH_PARENT['enabled'], - identity_fakes.PROJECT_WITH_PARENT['id'], - identity_fakes.PROJECT_WITH_PARENT['name'], - identity_fakes.PROJECT_WITH_PARENT['parent_id'], - identity_fakes.ids_for_children, + self.project.description, + self.project.domain_id, + self.project.enabled, + self.project.id, + self.project.is_domain, + self.project.name, + self.project.parent_id, + ['children-id'], ) self.assertEqual(data, datalist) def test_project_show_parents_and_children(self): - project = copy.deepcopy(identity_fakes.PROJECT_WITH_PARENT) - project['subtree'] = identity_fakes.children - project['parents'] = identity_fakes.parents - self.projects_mock.get.return_value = fakes.FakeResource( - None, - project, - loaded=True, + self.project = identity_fakes.FakeProject.create_one_project( + attrs={ + 'parent_id': self.project.parent_id, + 'parents': [{'project': {'id': self.project.parent_id}}], + 'subtree': [{'project': {'id': 'children-id'}}] + } ) + self.projects_mock.get.return_value = self.project arglist = [ - identity_fakes.PROJECT_WITH_PARENT['id'], + self.project.id, '--parents', '--children', ] verifylist = [ - ('project', identity_fakes.PROJECT_WITH_PARENT['id']), + ('project', self.project.id), ('parents', True), ('children', True), ] @@ -936,7 +937,7 @@ class TestProjectShow(TestProject): columns, data = self.cmd.take_action(parsed_args) self.projects_mock.get.assert_called_with( - identity_fakes.PROJECT_WITH_PARENT['id'], + self.project.id, parents_as_list=True, subtree_as_list=True, ) @@ -946,6 +947,7 @@ class TestProjectShow(TestProject): 'domain_id', 'enabled', 'id', + 'is_domain', 'name', 'parent_id', 'parents', @@ -953,13 +955,14 @@ class TestProjectShow(TestProject): ) self.assertEqual(columns, collist) datalist = ( - identity_fakes.PROJECT_WITH_PARENT['description'], - identity_fakes.PROJECT_WITH_PARENT['domain_id'], - identity_fakes.PROJECT_WITH_PARENT['enabled'], - identity_fakes.PROJECT_WITH_PARENT['id'], - identity_fakes.PROJECT_WITH_PARENT['name'], - identity_fakes.PROJECT_WITH_PARENT['parent_id'], - identity_fakes.ids_for_parents, - identity_fakes.ids_for_children, + self.project.description, + self.project.domain_id, + self.project.enabled, + self.project.id, + self.project.is_domain, + self.project.name, + self.project.parent_id, + [self.project.parent_id], + ['children-id'], ) self.assertEqual(data, datalist)