# Path: ddsc/core/userutil.py
# class UserUtil(object):
#     def __init__(self, data_service, logging_func=logging.info):
#         self.data_service = data_service
#         self.auth_provider_id = data_service.get_default_auth_provider_id()
#         self.logging_func = logging_func
# 
#     def find_user_by_username(self, username):
#         response = self.data_service.get_users(username=username)
#         return self._get_single_user_or_none(response, username)
# 
#     def register_user_by_username(self, username):
#         response = self.data_service.auth_provider_add_user(self.auth_provider_id, username)
#         return response.json()
# 
#     def find_user_by_email(self, email_address):
#         response = self.data_service.get_users(email=email_address)
#         return self._get_single_user_or_none(response, email_address)
# 
#     def find_affiliate_by_email(self, email_address):
#         response = self.data_service.get_auth_provider_affiliates(self.auth_provider_id, email=email_address)
#         return self._get_single_user_or_none(response, email_address)
# 
#     def find_affiliate_by_username(self, username):
#         response = self.data_service.get_auth_provider_affiliates(self.auth_provider_id, username=username)
#         return self._get_single_user_or_none(response, username)
# 
#     def user_or_affiliate_exists_for_email(self, email_address):
#         if self.find_user_by_email(email_address):
#             self.logging_func("Found DukeDS user for email address {}.".format(email_address))
#             return True
#         if self.find_affiliate_by_email(email_address):
#             self.logging_func("Found affiliate for email address {}.".format(email_address))
#             return True
#         self.logging_func("No valid DukeDS user or affiliate found for email address {}.".format(email_address))
#         return False
# 
#     def user_or_affiliate_exists_for_username(self, username):
#         if self.find_user_by_username(username):
#             self.logging_func("Found DukeDS user for username {}.".format(username))
#             return True
#         if self.find_affiliate_by_username(username):
#             self.logging_func("Found affiliate for username {}.".format(username))
#             return True
#         self.logging_func("No valid DukeDS user or affiliate found for username {}.".format(username))
#         return False
# 
#     @staticmethod
#     def _get_single_user_or_none(response, lookup_value):
#         results = response.json()['results']
#         if not results:
#             return None
#         if len(results) == 1:
#             return results[0]
#         raise DDSUserException("Found multiple users for {}.".format(lookup_value))
# 
# Path: ddsc/exceptions.py
# class DDSUserException(Exception):
#     """
#     Exception with an error message to be displayed to the user on the command line.
#     """
#     pass


from unittest import TestCase
from mock import Mock
from ddsc.core.userutil import UserUtil
from ddsc.exceptions import DDSUserException
        self.logging_func = Mock()
        self.user_util = UserUtil(self.data_service, self.logging_func)

    def test_constructor(self):
        self.assertEqual(self.user_util.auth_provider_id,
                         self.data_service.get_default_auth_provider_id.return_value)

    def test_find_user_by_username(self):
        self.data_service.get_users.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_user_by_username("fakeuser")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_users.assert_called_with(username="fakeuser")

    def test_register_user_by_username(self):
        self.data_service.auth_provider_add_user.return_value.json.return_value = {"id": "123"}
        result = self.user_util.register_user_by_username("fakeuser")
        self.assertEqual(result, {"id": "123"})
        self.data_service.auth_provider_add_user.assert_called_with(self.user_util.auth_provider_id, "fakeuser")

    def test_find_user_by_email(self):
        self.data_service.get_users.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_user_by_email("fakeuser@duke.edu")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_users.assert_called_with(email="fakeuser@duke.edu")

    def test_find_affiliate_by_email(self):
        self.data_service.get_auth_provider_affiliates.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_affiliate_by_email("fakeuser@duke.edu")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_auth_provider_affiliates.assert_called_with(self.user_util.auth_provider_id,

# Path: ddsc/core/userutil.py
# class UserUtil(object):
#     def __init__(self, data_service, logging_func=logging.info):
#         self.data_service = data_service
#         self.auth_provider_id = data_service.get_default_auth_provider_id()
#         self.logging_func = logging_func
# 
#     def find_user_by_username(self, username):
#         response = self.data_service.get_users(username=username)
#         return self._get_single_user_or_none(response, username)
# 
#     def register_user_by_username(self, username):
#         response = self.data_service.auth_provider_add_user(self.auth_provider_id, username)
#         return response.json()
# 
#     def find_user_by_email(self, email_address):
#         response = self.data_service.get_users(email=email_address)
#         return self._get_single_user_or_none(response, email_address)
# 
#     def find_affiliate_by_email(self, email_address):
#         response = self.data_service.get_auth_provider_affiliates(self.auth_provider_id, email=email_address)
#         return self._get_single_user_or_none(response, email_address)
# 
#     def find_affiliate_by_username(self, username):
#         response = self.data_service.get_auth_provider_affiliates(self.auth_provider_id, username=username)
#         return self._get_single_user_or_none(response, username)
# 
#     def user_or_affiliate_exists_for_email(self, email_address):
#         if self.find_user_by_email(email_address):
#             self.logging_func("Found DukeDS user for email address {}.".format(email_address))
#             return True
#         if self.find_affiliate_by_email(email_address):
#             self.logging_func("Found affiliate for email address {}.".format(email_address))
#             return True
#         self.logging_func("No valid DukeDS user or affiliate found for email address {}.".format(email_address))
#         return False
# 
#     def user_or_affiliate_exists_for_username(self, username):
#         if self.find_user_by_username(username):
#             self.logging_func("Found DukeDS user for username {}.".format(username))
#             return True
#         if self.find_affiliate_by_username(username):
#             self.logging_func("Found affiliate for username {}.".format(username))
#             return True
#         self.logging_func("No valid DukeDS user or affiliate found for username {}.".format(username))
#         return False
# 
#     @staticmethod
#     def _get_single_user_or_none(response, lookup_value):
#         results = response.json()['results']
#         if not results:
#             return None
#         if len(results) == 1:
#             return results[0]
#         raise DDSUserException("Found multiple users for {}.".format(lookup_value))
# 
# Path: ddsc/exceptions.py
# class DDSUserException(Exception):
#     """
#     Exception with an error message to be displayed to the user on the command line.
#     """
#     pass


# Path: ddsc/core/tests/test_userutil.py
from unittest import TestCase
from mock import Mock
from ddsc.core.userutil import UserUtil
from ddsc.exceptions import DDSUserException

        self.logging_func = Mock()
        self.user_util = UserUtil(self.data_service, self.logging_func)

    def test_constructor(self):
        self.assertEqual(self.user_util.auth_provider_id,
                         self.data_service.get_default_auth_provider_id.return_value)

    def test_find_user_by_username(self):
        self.data_service.get_users.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_user_by_username("fakeuser")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_users.assert_called_with(username="fakeuser")

    def test_register_user_by_username(self):
        self.data_service.auth_provider_add_user.return_value.json.return_value = {"id": "123"}
        result = self.user_util.register_user_by_username("fakeuser")
        self.assertEqual(result, {"id": "123"})
        self.data_service.auth_provider_add_user.assert_called_with(self.user_util.auth_provider_id, "fakeuser")

    def test_find_user_by_email(self):
        self.data_service.get_users.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_user_by_email("fakeuser@duke.edu")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_users.assert_called_with(email="fakeuser@duke.edu")

    def test_find_affiliate_by_email(self):
        self.data_service.get_auth_provider_affiliates.return_value.json.return_value = {"results": [{"id": "123"}]}
        result = self.user_util.find_affiliate_by_email("fakeuser@duke.edu")
        self.assertEqual(result, {"id": "123"})
        self.data_service.get_auth_provider_affiliates.assert_called_with(self.user_util.auth_provider_id,

                                                                          email="fakeuser@duke.edu")