def setUp(self):
        mock_clients = self._create_service_mock('identity_management')

        self.identity_management_service = IdentityManagementService()
        self.identity_management_service.clients = mock_clients

        # Rename to save some typing
        self.mock_create = mock_clients.resource_registry.create
        self.mock_read = mock_clients.resource_registry.read
        self.mock_update = mock_clients.resource_registry.update
        self.mock_delete = mock_clients.resource_registry.delete
        self.mock_create_association = mock_clients.resource_registry.create_association
        self.mock_delete_association = mock_clients.resource_registry.delete_association
        self.mock_find_objects = mock_clients.resource_registry.find_objects
        self.mock_find_resources = mock_clients.resource_registry.find_resources
        self.mock_find_subjects = mock_clients.resource_registry.find_subjects
        self.mock_find_associations = mock_clients.resource_registry.find_associations

        # ActorIdentity
        self.actor_identity = Mock()
        self.actor_identity._id = '111'
        self.actor_identity.name = "Foo"

        # UserCredential
        self.user_credentials = Mock()
        self.user_credentials._id = '222'
        self.user_credentials.name = "Bogus subject"

        # UserCredential
        self.user_credentials2 = Mock()
        self.user_credentials2._id = '777'
        self.user_credentials2.name = "Bogus subject"

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association = Mock()
        self.actor_identity_to_credentials_association._id = '333'
        self.actor_identity_to_credentials_association.s = "111"
        self.actor_identity_to_credentials_association.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association.o = "222"
        self.actor_identity_to_credentials_association.ot = RT.UserCredentials

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association2 = Mock()
        self.actor_identity_to_credentials_association2._id = '888'
        self.actor_identity_to_credentials_association2.s = "111"
        self.actor_identity_to_credentials_association2.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association2.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association2.o = "222"
        self.actor_identity_to_credentials_association2.ot = RT.UserCredentials

        # UserInfo
        self.user_info = Mock()
        self.user_info.name = "John Doe"
        self.user_info.email = "*****@*****.**"
        self.user_info.phone = "555-555-5555"
        self.user_info.variables = [{"name": "subscribeToMailingList", "value": "False"}]

        self.user_info2 = Mock()
        self.user_info2.name = "John Doe"
        self.user_info2.email = "*****@*****.**"
        self.user_info2.phone = "555-555-5556"
        self.user_info2.variables = [{"name": "subscribeToMailingList", "value": "True"}]

        # ActorIdentity to UserInfo association
        self.actor_identity_to_info_association = Mock()
        self.actor_identity_to_info_association._id = '555'
        self.actor_identity_to_info_association.s = "111"
        self.actor_identity_to_info_association.st = RT.ActorIdentity
        self.actor_identity_to_info_association.p = PRED.hasInfo
        self.actor_identity_to_info_association.o = "444"
        self.actor_identity_to_info_association.ot = RT.UserInfo
Beispiel #2
0
    def setUp(self):
        mock_clients = self._create_service_mock('identity_management')

        self.identity_management_service = IdentityManagementService()
        self.identity_management_service.clients = mock_clients

        # Rename to save some typing
        self.mock_create = mock_clients.resource_registry.create
        self.mock_read = mock_clients.resource_registry.read
        self.mock_update = mock_clients.resource_registry.update
        self.mock_delete = mock_clients.resource_registry.delete
        self.mock_create_association = mock_clients.resource_registry.create_association
        self.mock_delete_association = mock_clients.resource_registry.delete_association
        self.mock_find_objects = mock_clients.resource_registry.find_objects
        self.mock_find_resources = mock_clients.resource_registry.find_resources
        self.mock_find_subjects = mock_clients.resource_registry.find_subjects
        self.mock_find_associations = mock_clients.resource_registry.find_associations

        # ActorIdentity
        self.actor_identity = Mock()
        self.actor_identity._id = '111'
        self.actor_identity.name = "Foo"

        # UserCredential
        self.user_credentials = Mock()
        self.user_credentials._id = '222'
        self.user_credentials.name = "Bogus subject"

        # UserCredential
        self.user_credentials2 = Mock()
        self.user_credentials2._id = '777'
        self.user_credentials2.name = "Bogus subject"

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association = Mock()
        self.actor_identity_to_credentials_association._id = '333'
        self.actor_identity_to_credentials_association.s = "111"
        self.actor_identity_to_credentials_association.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association.o = "222"
        self.actor_identity_to_credentials_association.ot = RT.UserCredentials

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association2 = Mock()
        self.actor_identity_to_credentials_association2._id = '888'
        self.actor_identity_to_credentials_association2.s = "111"
        self.actor_identity_to_credentials_association2.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association2.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association2.o = "222"
        self.actor_identity_to_credentials_association2.ot = RT.UserCredentials

        # UserInfo
        self.user_info = Mock()
        self.user_info.name = "John Doe"
        self.user_info.email = "*****@*****.**"
        self.user_info.phone = "555-555-5555"
        self.user_info.variables = [{
            "name": "subscribeToMailingList",
            "value": "False"
        }]

        self.user_info2 = Mock()
        self.user_info2.name = "John Doe"
        self.user_info2.email = "*****@*****.**"
        self.user_info2.phone = "555-555-5556"
        self.user_info2.variables = [{
            "name": "subscribeToMailingList",
            "value": "True"
        }]

        # ActorIdentity to UserInfo association
        self.actor_identity_to_info_association = Mock()
        self.actor_identity_to_info_association._id = '555'
        self.actor_identity_to_info_association.s = "111"
        self.actor_identity_to_info_association.st = RT.ActorIdentity
        self.actor_identity_to_info_association.p = PRED.hasInfo
        self.actor_identity_to_info_association.o = "444"
        self.actor_identity_to_info_association.ot = RT.UserInfo
class TestIdentityManagementService(PyonTestCase):

    def setUp(self):
        mock_clients = self._create_service_mock('identity_management')

        self.identity_management_service = IdentityManagementService()
        self.identity_management_service.clients = mock_clients

        # Rename to save some typing
        self.mock_create = mock_clients.resource_registry.create
        self.mock_read = mock_clients.resource_registry.read
        self.mock_update = mock_clients.resource_registry.update
        self.mock_delete = mock_clients.resource_registry.delete
        self.mock_create_association = mock_clients.resource_registry.create_association
        self.mock_delete_association = mock_clients.resource_registry.delete_association
        self.mock_find_objects = mock_clients.resource_registry.find_objects
        self.mock_find_resources = mock_clients.resource_registry.find_resources
        self.mock_find_subjects = mock_clients.resource_registry.find_subjects
        self.mock_find_associations = mock_clients.resource_registry.find_associations

        # ActorIdentity
        self.actor_identity = Mock()
        self.actor_identity._id = '111'
        self.actor_identity.name = "Foo"

        # UserCredential
        self.user_credentials = Mock()
        self.user_credentials._id = '222'
        self.user_credentials.name = "Bogus subject"

        # UserCredential
        self.user_credentials2 = Mock()
        self.user_credentials2._id = '777'
        self.user_credentials2.name = "Bogus subject"

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association = Mock()
        self.actor_identity_to_credentials_association._id = '333'
        self.actor_identity_to_credentials_association.s = "111"
        self.actor_identity_to_credentials_association.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association.o = "222"
        self.actor_identity_to_credentials_association.ot = RT.UserCredentials

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association2 = Mock()
        self.actor_identity_to_credentials_association2._id = '888'
        self.actor_identity_to_credentials_association2.s = "111"
        self.actor_identity_to_credentials_association2.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association2.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association2.o = "222"
        self.actor_identity_to_credentials_association2.ot = RT.UserCredentials

        # UserInfo
        self.user_info = Mock()
        self.user_info.name = "John Doe"
        self.user_info.email = "*****@*****.**"
        self.user_info.phone = "555-555-5555"
        self.user_info.variables = [{"name": "subscribeToMailingList", "value": "False"}]

        self.user_info2 = Mock()
        self.user_info2.name = "John Doe"
        self.user_info2.email = "*****@*****.**"
        self.user_info2.phone = "555-555-5556"
        self.user_info2.variables = [{"name": "subscribeToMailingList", "value": "True"}]

        # ActorIdentity to UserInfo association
        self.actor_identity_to_info_association = Mock()
        self.actor_identity_to_info_association._id = '555'
        self.actor_identity_to_info_association.s = "111"
        self.actor_identity_to_info_association.st = RT.ActorIdentity
        self.actor_identity_to_info_association.p = PRED.hasInfo
        self.actor_identity_to_info_association.o = "444"
        self.actor_identity_to_info_association.ot = RT.UserInfo

    def test_create_actor_identity(self):
        self.mock_create.return_value = ['111', 1]
        
        user_id = self.identity_management_service.create_actor_identity(self.actor_identity)

        assert user_id == '111'
        self.mock_create.assert_called_once_with(self.actor_identity)        

    def test_read_and_update_actor_identity(self):
        self.mock_read.return_value = self.actor_identity
        
        actor_identity = self.identity_management_service.read_actor_identity('111')

        assert actor_identity is self.mock_read.return_value
        self.mock_read.assert_called_once_with('111', '')
        
        actor_identity.name = 'Bar'

        self.mock_update.return_value = ['111', 2]
        
        self.identity_management_service.update_actor_identity(actor_identity)

        self.mock_update.assert_called_once_with(actor_identity)        

    def test_delete_actor_identity(self):
        self.mock_read.return_value = self.actor_identity
        
        self.identity_management_service.delete_actor_identity('111')

        self.mock_delete.assert_called_once_with('111')
 
    def test_read_actor_identity_not_found(self):
        self.mock_read.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.read_actor_identity('bad')

        self.mock_read.assert_called_once_with('bad', '')
 
    def test_delete_actor_identity_not_found(self):
        self.mock_delete.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_actor_identity('bad')

    def test_register_user_credentials(self):
        self.mock_create.return_value = ['222', 1]

        self.mock_create_association.return_value = ['333', 1]
        
        self.identity_management_service.register_user_credentials('111', self.user_credentials)

        self.mock_create.assert_called_once_with(self.user_credentials)
        self.mock_create_association.assert_called_once_with('111', PRED.hasCredentials, '222', None)

    def test_unregister_user_credentials(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association])
        self.mock_find_associations.return_value = [self.actor_identity_to_credentials_association]
        
        self.identity_management_service.unregister_user_credentials('111', "Bogus subject")

        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, self.user_credentials.name, False)
        self.mock_find_associations.assert_called_once_with('111', PRED.hasCredentials, '222', None, False, '', 0, 0, False)
        self.mock_delete_association.assert_called_once_with('333')
        self.mock_delete.assert_called_once_with('222')

    def test_unregister_user_credentials_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.unregister_user_credentials('bad', 'bad')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserCredentials bad does not exist')
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, 'bad', False)

    def test_unregister_user_credentials_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association])
        self.mock_find_associations.return_value = None

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.unregister_user_credentials('111', "Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, 'ActorIdentity to UserCredentials association for user id 111 to credential Bogus subject does not exist')
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, 'Bogus subject', False)
        self.mock_find_associations.assert_called_once_with('111', PRED.hasCredentials, '222', None, False, '', 0, 0, False)

    def test_create_user_info(self):
        self.mock_find_objects.return_value = (None, None)

        self.mock_create.return_value = ['444', 1]

        self.mock_create_association.return_value = ['555', 1]
        
        result = self.identity_management_service.create_user_info('111', self.user_info)

        assert result == '444'
        self.mock_create.assert_called_once_with(self.user_info)
        self.mock_create_association.assert_called_once_with('111', PRED.hasInfo, '444', None)

    def test_create_user_info_fail_already_exists(self):
        self.mock_find_objects.return_value = ([self.user_info], [self.actor_identity_to_info_association])

        # TEST: Execute the service operation call
        with self.assertRaises(Conflict) as cm:
            self.identity_management_service.create_user_info('111', self.user_info)

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo already exists for user id 111')
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo, RT.UserInfo, False, 0, 0, False)

    def test_read_and_update_user_info(self):
        self.mock_read.return_value = self.user_info
        
        user_info = self.identity_management_service.read_user_info('444')

        assert user_info is self.mock_read.return_value
        self.mock_read.assert_called_once_with('444', '')
        
        user_info.name = 'Jane Doe'

        self.mock_update.return_value = ['444', 2]
        
        self.identity_management_service.update_user_info(user_info)

        self.mock_update.assert_called_once_with(user_info)        

    def test_delete_user_info(self):
        self.mock_read.return_value = self.user_info

        self.mock_find_subjects.return_value = ([self.actor_identity], [self.actor_identity_to_info_association])

        self.mock_find_associations.return_value = ([self.actor_identity_to_info_association])
        
        self.identity_management_service.delete_user_info('444')

        self.mock_find_subjects.assert_called_once_with(RT.ActorIdentity, PRED.hasInfo, '444', False, 0, 0, False)
        self.mock_find_associations.assert_called_once_with('111', PRED.hasInfo, '444', None, False, '', 0, 0, False)
        self.mock_delete_association.assert_called_once_with('555')
        self.mock_delete.assert_called_once_with('444')
 
    def test_read_user_info_not_found(self):
        self.mock_read.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.read_user_info('bad')

        self.mock_read.assert_called_once_with('bad', '')
 
    def test_delete_user_info_not_found(self):
        self.mock_read.return_value = None

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_user_info('bad')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo bad does not exist')
        self.mock_read.assert_called_once_with('bad', '')

    def test_delete_user_info_association_not_found(self):
        self.mock_read.return_value = self.user_info

        self.mock_find_subjects.return_value = (None, None)
        
        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_user_info('444')

        ex = cm.exception
        self.assertEqual(ex.message, 'ActorIdentity to UserInfo association for user info id 444 does not exist')
        self.mock_read.assert_called_once_with('444', '')
        self.mock_find_subjects.assert_called_once_with(RT.ActorIdentity, PRED.hasInfo, '444', False, 0, 0, False)

    def test_find_user_info_by_id(self):
        self.mock_find_objects.return_value = ([self.user_info], [self.actor_identity_to_info_association])
        
        result = self.identity_management_service.find_user_info_by_id('111')

        assert result == self.user_info
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo, RT.UserInfo, False, 0, 0, False)

    def test_find_user_info_by_id_association_not_found(self):
        self.mock_find_objects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_id('111')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo for user id 111 does not exist')
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo, RT.UserInfo, False, 0, 0, False)

    def test_find_user_info_by_name(self):
        self.mock_find_resources.return_value = ([self.user_info], [self.actor_identity_to_info_association])
        
        result = self.identity_management_service.find_user_info_by_name("John Doe")

        assert result == self.user_info
        self.mock_find_resources.assert_called_once_with(RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_name_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_name("John Doe")

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo with name John Doe does not exist')
        self.mock_find_resources.assert_called_once_with(RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_name_multiple_found(self):
        self.mock_find_resources.return_value = ([self.user_info, self.user_info2], [self.actor_identity_to_info_association])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_name("John Doe")

        ex = cm.exception
        self.assertEqual(ex.message, 'Multiple UserInfo objects with name John Doe exist')
        self.mock_find_resources.assert_called_once_with(RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_subject(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association])
        self.mock_find_subjects.return_value = ([self.actor_identity], [self.actor_identity_to_credentials_association])
        self.mock_find_objects.return_value = ([self.user_info], [self.actor_identity_to_info_association])
        
        result = self.identity_management_service.find_user_info_by_subject("Bogus subject")

        assert result == self.user_info
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo, RT.UserInfo, False, 0, 0, False)

    def test_find_user_info_by_subject_identity_credentials_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "UserCredentials with subject Bogus subject does not exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_multiple_credentials_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials, self.user_credentials2], [self.actor_identity_to_credentials_association, self.actor_identity_to_credentials_association2])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "Multiple UserCredentials with subject Bogus subject exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], None)
        self.mock_find_subjects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "ActorIdentity to UserCredentials association for subject Bogus subject does not exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_multiple_associations_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association, self.actor_identity_to_credentials_association2])
        self.mock_find_subjects.return_value = ([self.user_info, self.user_info2], [self.actor_identity_to_credentials_association, self.actor_identity_to_credentials_association2])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "Multiple ActorIdentity to UserCredentials associations for subject Bogus subject exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_info_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association])
        self.mock_find_subjects.return_value = ([self.actor_identity], [self.actor_identity_to_credentials_association])
        self.mock_find_objects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "UserInfo for subject Bogus subject does not exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo, RT.UserInfo, False, 0, 0, False)

    def test_find_user_info_by_subject_info_multiple_associations_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [self.actor_identity_to_credentials_association])
        self.mock_find_subjects.return_value = ([self.user_info], [self.actor_identity_to_credentials_association])
        self.mock_find_objects.return_value = ([self.user_info, self.user_info2], [self.actor_identity_to_info_association])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject("Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message, "Multiple UserInfos for subject Bogus subject exist")
        self.mock_find_resources.assert_called_once_with(RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_subjects.assert_called_once_with(RT.ActorIdentity, PRED.hasCredentials, '222', False, 0, 0, False)
Beispiel #4
0
class TestIdentityManagementService(PyonTestCase):
    def setUp(self):
        mock_clients = self._create_service_mock('identity_management')

        self.identity_management_service = IdentityManagementService()
        self.identity_management_service.clients = mock_clients

        # Rename to save some typing
        self.mock_create = mock_clients.resource_registry.create
        self.mock_read = mock_clients.resource_registry.read
        self.mock_update = mock_clients.resource_registry.update
        self.mock_delete = mock_clients.resource_registry.delete
        self.mock_create_association = mock_clients.resource_registry.create_association
        self.mock_delete_association = mock_clients.resource_registry.delete_association
        self.mock_find_objects = mock_clients.resource_registry.find_objects
        self.mock_find_resources = mock_clients.resource_registry.find_resources
        self.mock_find_subjects = mock_clients.resource_registry.find_subjects
        self.mock_find_associations = mock_clients.resource_registry.find_associations

        # ActorIdentity
        self.actor_identity = Mock()
        self.actor_identity._id = '111'
        self.actor_identity.name = "Foo"

        # UserCredential
        self.user_credentials = Mock()
        self.user_credentials._id = '222'
        self.user_credentials.name = "Bogus subject"

        # UserCredential
        self.user_credentials2 = Mock()
        self.user_credentials2._id = '777'
        self.user_credentials2.name = "Bogus subject"

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association = Mock()
        self.actor_identity_to_credentials_association._id = '333'
        self.actor_identity_to_credentials_association.s = "111"
        self.actor_identity_to_credentials_association.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association.o = "222"
        self.actor_identity_to_credentials_association.ot = RT.UserCredentials

        # ActorIdentity to UserCredential association
        self.actor_identity_to_credentials_association2 = Mock()
        self.actor_identity_to_credentials_association2._id = '888'
        self.actor_identity_to_credentials_association2.s = "111"
        self.actor_identity_to_credentials_association2.st = RT.ActorIdentity
        self.actor_identity_to_credentials_association2.p = PRED.hasCredentials
        self.actor_identity_to_credentials_association2.o = "222"
        self.actor_identity_to_credentials_association2.ot = RT.UserCredentials

        # UserInfo
        self.user_info = Mock()
        self.user_info.name = "John Doe"
        self.user_info.email = "*****@*****.**"
        self.user_info.phone = "555-555-5555"
        self.user_info.variables = [{
            "name": "subscribeToMailingList",
            "value": "False"
        }]

        self.user_info2 = Mock()
        self.user_info2.name = "John Doe"
        self.user_info2.email = "*****@*****.**"
        self.user_info2.phone = "555-555-5556"
        self.user_info2.variables = [{
            "name": "subscribeToMailingList",
            "value": "True"
        }]

        # ActorIdentity to UserInfo association
        self.actor_identity_to_info_association = Mock()
        self.actor_identity_to_info_association._id = '555'
        self.actor_identity_to_info_association.s = "111"
        self.actor_identity_to_info_association.st = RT.ActorIdentity
        self.actor_identity_to_info_association.p = PRED.hasInfo
        self.actor_identity_to_info_association.o = "444"
        self.actor_identity_to_info_association.ot = RT.UserInfo

    def test_create_actor_identity(self):
        self.mock_create.return_value = ['111', 1]

        user_id = self.identity_management_service.create_actor_identity(
            self.actor_identity)

        assert user_id == '111'
        self.mock_create.assert_called_once_with(self.actor_identity)

    def test_read_and_update_actor_identity(self):
        self.mock_read.return_value = self.actor_identity

        actor_identity = self.identity_management_service.read_actor_identity(
            '111')

        assert actor_identity is self.mock_read.return_value
        self.mock_read.assert_called_once_with('111', '')

        actor_identity.name = 'Bar'

        self.mock_update.return_value = ['111', 2]

        self.identity_management_service.update_actor_identity(actor_identity)

        self.mock_update.assert_called_once_with(actor_identity)

    def test_delete_actor_identity(self):
        self.mock_read.return_value = self.actor_identity

        self.identity_management_service.delete_actor_identity('111')

        self.mock_delete.assert_called_once_with('111')

    def test_read_actor_identity_not_found(self):
        self.mock_read.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.read_actor_identity('bad')

        self.mock_read.assert_called_once_with('bad', '')

    def test_delete_actor_identity_not_found(self):
        self.mock_delete.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_actor_identity('bad')

    def test_register_user_credentials(self):
        self.mock_create.return_value = ['222', 1]

        self.mock_create_association.return_value = ['333', 1]

        self.identity_management_service.register_user_credentials(
            '111', self.user_credentials)

        self.mock_create.assert_called_once_with(self.user_credentials)
        self.mock_create_association.assert_called_once_with(
            '111', PRED.hasCredentials, '222', None)

    def test_unregister_user_credentials(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_associations.return_value = [
            self.actor_identity_to_credentials_association
        ]

        self.identity_management_service.unregister_user_credentials(
            '111', "Bogus subject")

        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, self.user_credentials.name, False)
        self.mock_find_associations.assert_called_once_with(
            '111', PRED.hasCredentials, '222', None, False, 0, 0, False)
        self.mock_delete_association.assert_called_once_with('333')
        self.mock_delete.assert_called_once_with('222')

    def test_unregister_user_credentials_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.unregister_user_credentials(
                'bad', 'bad')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserCredentials bad does not exist')
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, 'bad', False)

    def test_unregister_user_credentials_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_associations.return_value = None

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.unregister_user_credentials(
                '111', "Bogus subject")

        ex = cm.exception
        self.assertEqual(
            ex.message,
            'ActorIdentity to UserCredentials association for user id 111 to credential Bogus subject does not exist'
        )
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, 'Bogus subject', False)
        self.mock_find_associations.assert_called_once_with(
            '111', PRED.hasCredentials, '222', None, False, 0, 0, False)

    def test_create_user_info(self):
        self.mock_find_objects.return_value = (None, None)

        self.mock_create.return_value = ['444', 1]

        self.mock_create_association.return_value = ['555', 1]

        result = self.identity_management_service.create_user_info(
            '111', self.user_info)

        assert result == '444'
        self.mock_create.assert_called_once_with(self.user_info)
        self.mock_create_association.assert_called_once_with(
            '111', PRED.hasInfo, '444', None)

    def test_create_user_info_fail_already_exists(self):
        self.mock_find_objects.return_value = ([self.user_info], [
            self.actor_identity_to_info_association
        ])

        # TEST: Execute the service operation call
        with self.assertRaises(Conflict) as cm:
            self.identity_management_service.create_user_info(
                '111', self.user_info)

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo already exists for user id 111')
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo,
                                                       RT.UserInfo, False, 0,
                                                       0, False)

    def test_read_and_update_user_info(self):
        self.mock_read.return_value = self.user_info

        user_info = self.identity_management_service.read_user_info('444')

        assert user_info is self.mock_read.return_value
        self.mock_read.assert_called_once_with('444', '')

        user_info.name = 'Jane Doe'

        self.mock_update.return_value = ['444', 2]

        self.identity_management_service.update_user_info(user_info)

        self.mock_update.assert_called_once_with(user_info)

    def test_delete_user_info(self):
        self.mock_read.return_value = self.user_info

        self.mock_find_subjects.return_value = ([self.actor_identity], [
            self.actor_identity_to_info_association
        ])

        self.mock_find_associations.return_value = ([
            self.actor_identity_to_info_association
        ])

        self.identity_management_service.delete_user_info('444')

        self.mock_find_subjects.assert_called_once_with(
            RT.ActorIdentity, PRED.hasInfo, '444', False, 0, 0, False)
        self.mock_find_associations.assert_called_once_with(
            '111', PRED.hasInfo, '444', None, False, 0, 0, False)
        self.mock_delete_association.assert_called_once_with('555')
        self.mock_delete.assert_called_once_with('444')

    def test_read_user_info_not_found(self):
        self.mock_read.side_effect = NotFound("bad")

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.read_user_info('bad')

        self.mock_read.assert_called_once_with('bad', '')

    def test_delete_user_info_not_found(self):
        self.mock_read.return_value = None

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_user_info('bad')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo bad does not exist')
        self.mock_read.assert_called_once_with('bad', '')

    def test_delete_user_info_association_not_found(self):
        self.mock_read.return_value = self.user_info

        self.mock_find_subjects.return_value = (None, None)

        # TEST: Execute the service operation call
        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.delete_user_info('444')

        ex = cm.exception
        self.assertEqual(
            ex.message,
            'ActorIdentity to UserInfo association for user info id 444 does not exist'
        )
        self.mock_read.assert_called_once_with('444', '')
        self.mock_find_subjects.assert_called_once_with(
            RT.ActorIdentity, PRED.hasInfo, '444', False, 0, 0, False)

    def test_find_user_info_by_id(self):
        self.mock_find_objects.return_value = ([self.user_info], [
            self.actor_identity_to_info_association
        ])

        result = self.identity_management_service.find_user_info_by_id('111')

        assert result == self.user_info
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo,
                                                       RT.UserInfo, False, 0,
                                                       0, False)

    def test_find_user_info_by_id_association_not_found(self):
        self.mock_find_objects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_id('111')

        ex = cm.exception
        self.assertEqual(ex.message, 'UserInfo for user id 111 does not exist')
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo,
                                                       RT.UserInfo, False, 0,
                                                       0, False)

    def test_find_user_info_by_name(self):
        self.mock_find_resources.return_value = ([self.user_info], [
            self.actor_identity_to_info_association
        ])

        result = self.identity_management_service.find_user_info_by_name(
            "John Doe")

        assert result == self.user_info
        self.mock_find_resources.assert_called_once_with(
            RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_name_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_name("John Doe")

        ex = cm.exception
        self.assertEqual(ex.message,
                         'UserInfo with name John Doe does not exist')
        self.mock_find_resources.assert_called_once_with(
            RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_name_multiple_found(self):
        self.mock_find_resources.return_value = ([
            self.user_info, self.user_info2
        ], [self.actor_identity_to_info_association])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_name("John Doe")

        ex = cm.exception
        self.assertEqual(ex.message,
                         'Multiple UserInfo objects with name John Doe exist')
        self.mock_find_resources.assert_called_once_with(
            RT.UserInfo, None, "John Doe", False)

    def test_find_user_info_by_subject(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_subjects.return_value = ([self.actor_identity], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_objects.return_value = ([self.user_info], [
            self.actor_identity_to_info_association
        ])

        result = self.identity_management_service.find_user_info_by_subject(
            "Bogus subject")

        assert result == self.user_info
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo,
                                                       RT.UserInfo, False, 0,
                                                       0, False)

    def test_find_user_info_by_subject_identity_credentials_not_found(self):
        self.mock_find_resources.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(
            ex.message,
            "UserCredentials with subject Bogus subject does not exist")
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_multiple_credentials_found(
            self):
        self.mock_find_resources.return_value = ([
            self.user_credentials, self.user_credentials2
        ], [
            self.actor_identity_to_credentials_association,
            self.actor_identity_to_credentials_association2
        ])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(
            ex.message,
            "Multiple UserCredentials with subject Bogus subject exist")
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], None)
        self.mock_find_subjects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(
            ex.message,
            "ActorIdentity to UserCredentials association for subject Bogus subject does not exist"
        )
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_identity_multiple_associations_found(
            self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association,
            self.actor_identity_to_credentials_association2
        ])
        self.mock_find_subjects.return_value = ([
            self.user_info, self.user_info2
        ], [
            self.actor_identity_to_credentials_association,
            self.actor_identity_to_credentials_association2
        ])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(
            ex.message,
            "Multiple ActorIdentity to UserCredentials associations for subject Bogus subject exist"
        )
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)

    def test_find_user_info_by_subject_info_association_not_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_subjects.return_value = ([self.actor_identity], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_objects.return_value = (None, None)

        with self.assertRaises(NotFound) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message,
                         "UserInfo for subject Bogus subject does not exist")
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_objects.assert_called_once_with('111', PRED.hasInfo,
                                                       RT.UserInfo, False, 0,
                                                       0, False)

    def test_find_user_info_by_subject_info_multiple_associations_found(self):
        self.mock_find_resources.return_value = ([self.user_credentials], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_subjects.return_value = ([self.user_info], [
            self.actor_identity_to_credentials_association
        ])
        self.mock_find_objects.return_value = ([
            self.user_info, self.user_info2
        ], [self.actor_identity_to_info_association])

        with self.assertRaises(Inconsistent) as cm:
            self.identity_management_service.find_user_info_by_subject(
                "Bogus subject")

        ex = cm.exception
        self.assertEqual(ex.message,
                         "Multiple UserInfos for subject Bogus subject exist")
        self.mock_find_resources.assert_called_once_with(
            RT.UserCredentials, None, "Bogus subject", False)
        self.mock_find_subjects.assert_called_once_with(
            RT.ActorIdentity, PRED.hasCredentials, '222', False, 0, 0, False)