Skip to content

Commit 5125b6f

Browse files
author
Huanxuan Ao
committed
Refactor identity v3 unit tests with fake class
Refactor unit tests in identity v3 for "user", "endpoint", "group" and "service" with fake classes. Change-Id: I57316bbf762c805f8e9ae225b394bbe58ebdd416 Partially-Implements: blueprint refactor-identity-unit-test
1 parent 2a1c2b2 commit 5125b6f

5 files changed

Lines changed: 572 additions & 609 deletions

File tree

openstackclient/tests/identity/v3/fakes.py

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,133 @@ def create_one_user(attrs=None):
730730
'default_project_id': 'project-' + uuid.uuid4().hex,
731731
'email': 'user-email-' + uuid.uuid4().hex,
732732
'enabled': True,
733-
'domain_id': 'domain-id' + uuid.uuid4().hex,
733+
'domain_id': 'domain-id-' + uuid.uuid4().hex,
734734
'links': 'links-' + uuid.uuid4().hex,
735735
}
736736
user_info.update(attrs)
737737

738738
user = fakes.FakeResource(info=copy.deepcopy(user_info),
739739
loaded=True)
740740
return user
741+
742+
743+
class FakeGroup(object):
744+
"""Fake one or more group."""
745+
746+
@staticmethod
747+
def create_one_group(attrs=None):
748+
"""Create a fake group.
749+
750+
:param Dictionary attrs:
751+
A dictionary with all attributes
752+
:return:
753+
A FakeResource object, with id, name, and so on
754+
"""
755+
756+
attrs = attrs or {}
757+
758+
# set default attributes.
759+
group_info = {
760+
'id': 'group-id-' + uuid.uuid4().hex,
761+
'name': 'group-name-' + uuid.uuid4().hex,
762+
'links': 'links-' + uuid.uuid4().hex,
763+
'domain_id': 'domain-id-' + uuid.uuid4().hex,
764+
'description': 'group-description-' + uuid.uuid4().hex,
765+
}
766+
group_info.update(attrs)
767+
768+
group = fakes.FakeResource(info=copy.deepcopy(group_info),
769+
loaded=True)
770+
return group
771+
772+
773+
class FakeEndpoint(object):
774+
"""Fake one or more endpoint."""
775+
776+
@staticmethod
777+
def create_one_endpoint(attrs=None):
778+
"""Create a fake endpoint.
779+
780+
:param Dictionary attrs:
781+
A dictionary with all attributes
782+
:return:
783+
A FakeResource object, with id, url, and so on
784+
"""
785+
786+
attrs = attrs or {}
787+
788+
# set default attributes.
789+
endpoint_info = {
790+
'id': 'endpoint-id-' + uuid.uuid4().hex,
791+
'url': 'url-' + uuid.uuid4().hex,
792+
'region': 'endpoint-region-' + uuid.uuid4().hex,
793+
'interface': 'admin',
794+
'service_id': 'service-id-' + uuid.uuid4().hex,
795+
'enabled': True,
796+
'links': 'links-' + uuid.uuid4().hex,
797+
}
798+
endpoint_info.update(attrs)
799+
800+
endpoint = fakes.FakeResource(info=copy.deepcopy(endpoint_info),
801+
loaded=True)
802+
return endpoint
803+
804+
805+
class FakeService(object):
806+
"""Fake one or more service."""
807+
808+
@staticmethod
809+
def create_one_service(attrs=None):
810+
"""Create a fake service.
811+
812+
:param Dictionary attrs:
813+
A dictionary with all attributes
814+
:return:
815+
A FakeResource object, with id, name, and so on
816+
"""
817+
818+
attrs = attrs or {}
819+
820+
# set default attributes.
821+
service_info = {
822+
'id': 'service-id-' + uuid.uuid4().hex,
823+
'name': 'service-name-' + uuid.uuid4().hex,
824+
'type': 'service-type-' + uuid.uuid4().hex,
825+
'description': 'service-description-' + uuid.uuid4().hex,
826+
'enabled': True,
827+
'links': 'links-' + uuid.uuid4().hex,
828+
}
829+
service_info.update(attrs)
830+
831+
service = fakes.FakeResource(info=copy.deepcopy(service_info),
832+
loaded=True)
833+
return service
834+
835+
836+
class FakeRoleAssignment(object):
837+
"""Fake one or more role assignment."""
838+
839+
@staticmethod
840+
def create_one_role_assignment(attrs=None):
841+
"""Create a fake role assignment.
842+
843+
:param Dictionary attrs:
844+
A dictionary with all attributes
845+
:return:
846+
A FakeResource object, with scope, user, and so on
847+
"""
848+
849+
attrs = attrs or {}
850+
851+
# set default attributes.
852+
role_assignment_info = {
853+
'scope': {'project': {'id': 'project-id-' + uuid.uuid4().hex}},
854+
'user': {'id': 'user-id-' + uuid.uuid4().hex},
855+
'role': {'id': 'role-id-' + uuid.uuid4().hex},
856+
}
857+
role_assignment_info.update(attrs)
858+
859+
role_assignment = fakes.FakeResource(
860+
info=copy.deepcopy(role_assignment_info), loaded=True)
861+
862+
return role_assignment

0 commit comments

Comments
 (0)