# 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.assert_called_with(
            "No valid DukeDS user or affiliate found for email address fakeuser@duke.edu.")

    def test_user_or_affiliate_exists_for_username__dds_user_exists(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = {"id": "123"}

        self.assertTrue(self.user_util.user_or_affiliate_exists_for_username("fakeuser"))
        self.user_util.find_user_by_username.assert_called_with("fakeuser")
        self.logging_func.assert_called_with(
            "Found DukeDS user for username fakeuser.")

    def test_user_or_affiliate_exists_for_username__valid_affiliate_exists(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = None
        self.user_util.find_affiliate_by_username = Mock()
        self.user_util.find_affiliate_by_username.return_value = {"uid": "123"}

        self.assertTrue(self.user_util.user_or_affiliate_exists_for_username("fakeuser"))
        self.user_util.find_user_by_username.assert_called_with("fakeuser")
        self.user_util.find_affiliate_by_username.assert_called_with("fakeuser")
        self.logging_func.assert_called_with(
            "Found affiliate for username fakeuser.")

    def test_valid_dds_user_or_affiliate_exists_for_username__no_user_found(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = None
        self.user_util.find_affiliate_by_username = Mock()
        self.user_util.find_affiliate_by_username.return_value = None


# 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.assert_called_with(
            "No valid DukeDS user or affiliate found for email address fakeuser@duke.edu.")

    def test_user_or_affiliate_exists_for_username__dds_user_exists(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = {"id": "123"}

        self.assertTrue(self.user_util.user_or_affiliate_exists_for_username("fakeuser"))
        self.user_util.find_user_by_username.assert_called_with("fakeuser")
        self.logging_func.assert_called_with(
            "Found DukeDS user for username fakeuser.")

    def test_user_or_affiliate_exists_for_username__valid_affiliate_exists(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = None
        self.user_util.find_affiliate_by_username = Mock()
        self.user_util.find_affiliate_by_username.return_value = {"uid": "123"}

        self.assertTrue(self.user_util.user_or_affiliate_exists_for_username("fakeuser"))
        self.user_util.find_user_by_username.assert_called_with("fakeuser")
        self.user_util.find_affiliate_by_username.assert_called_with("fakeuser")
        self.logging_func.assert_called_with(
            "Found affiliate for username fakeuser.")

    def test_valid_dds_user_or_affiliate_exists_for_username__no_user_found(self):
        self.user_util.find_user_by_username = Mock()
        self.user_util.find_user_by_username.return_value = None
        self.user_util.find_affiliate_by_username = Mock()
        self.user_util.find_affiliate_by_username.return_value = None


        self.assertFalse(self.user_util.user_or_affiliate_exists_for_username("fakeuser"))