class UserService(object):
    '''
    User service
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.storage = UserDataStoreFactory().get_storage()
        self.signup_validation = UserSignupValidation()
        self.login_validation = LoginValidation()
        self.crypt = Crypt()

    def register(self, username, password, verify, email=None):
        validation_msgs, isValid = self.signup_validation.validate(
            username=username, password=password, verify=verify, email=email)
        if not isValid:
            raise UserRegistrationError(validation_msgs)
        else:
            crypt_password = self._crypt_password(username, password)
            user = UserData(username=username,
                            password=crypt_password,
                            email=email)
            saved_user = self.storage.save(user)
            user_id = saved_user.user_id
        return self.crypt.make_cookie(user_id)

    def login(self, username, password, cookie):
        self.check_cookie(cookie)
        validation_msgs, login_ok = self.login_validation.validate(
            username, password)
        if not login_ok:
            raise LoginError(validation_msgs)
        user = self.storage.fetchByUsername(username)
        user_id = user.user_id
        return self.crypt.make_cookie(user_id)

    def welcome(self, cookie):
        self.check_cookie(cookie)
        user_id = cookie.split('|')[0]
        user = self.storage.fetch(int(user_id))
        return user

    def check_cookie(self, cookie):
        if not cookie:
            raise InvalidCookieError()
        else:
            cookie_ok = self.crypt.is_valid_cookie(cookie)
            if not cookie_ok:
                raise InvalidCookieError()

    def _crypt_password(self, username, password):
        return self.crypt.crypt_password(username, password)

    def _hash_user_id(self, user_id):
        return self.crypt.crypt_user_id(user_id)
class UserService(object):
    '''
    User service
    '''
    

    def __init__(self):
        '''
        Constructor
        '''
        self.storage = UserDataStoreFactory().get_storage()
        self.signup_validation = UserSignupValidation();
        self.login_validation = LoginValidation();
        self.crypt = Crypt()
        
    def register(self, username,password,verify, email=None):
        validation_msgs, isValid = self.signup_validation.validate(username=username, password=password, verify=verify, email=email)
        if not isValid:
            raise UserRegistrationError(validation_msgs)
        else:
            crypt_password = self._crypt_password(username, password)
            user = UserData(username=username,password=crypt_password,email=email)
            saved_user = self.storage.save(user)
            user_id = saved_user.user_id
        return self.crypt.make_cookie(user_id)
    
    def login(self,username,password, cookie):
        self.check_cookie(cookie)
        validation_msgs, login_ok = self.login_validation.validate(username, password)
        if not login_ok:
            raise LoginError(validation_msgs)
        user = self.storage.fetchByUsername(username)
        user_id = user.user_id
        return self.crypt.make_cookie(user_id)

    def welcome(self, cookie):
        self.check_cookie(cookie)
        user_id = cookie.split('|')[0]
        user = self.storage.fetch(int(user_id))
        return user
    
    def check_cookie(self, cookie):
        if not cookie:
            raise InvalidCookieError()
        else: 
            cookie_ok = self.crypt.is_valid_cookie(cookie)
            if not cookie_ok:
                raise InvalidCookieError()
        
    
    def _crypt_password(self, username, password):
        return self.crypt.crypt_password(username, password)
    
    def _hash_user_id(self, user_id):
        return self.crypt.crypt_user_id(user_id)
        
class Test(unittest.TestCase):


    def setUp(self):
        # Setup mock GAE Data Store
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        #instantiate classes under test
        self.validation = UserSignupValidation()
        self.validation_login = LoginValidation()
        self.user_datastore = UserAppengineDataStore()
        self.crypt = Crypt()


    def tearDown(self):
        self.testbed.deactivate()

    def testIsValidUsername(self):
        self.assertTrue(self.validation.is_valid_username("Crag"))
        self.assertTrue(self.validation.is_valid_username("Crag-Doremus"))
        self.assertTrue(self.validation.is_valid_username("Crag_Doremus"))
        self.assertTrue(self.validation.is_valid_username("Cra"))
        self.assertFalse(self.validation.is_valid_username("ca"))
        self.assertFalse(self.validation.is_valid_username("cat!"))
        self.assertTrue(self.validation.is_valid_username("abcdefghijklmnopqrst"))
        self.assertFalse(self.validation.is_valid_username("abcdefghijklmnopqrstu"))
        pass

    def testIsValidPassword(self):
        self.assertTrue(self.validation.is_valid_password("Craig"))
        self.assertTrue(self.validation.is_valid_password("abcdefghijklmnopqrst"))
        self.assertFalse(self.validation.is_valid_password("abcdefghijklmnopqrstu"))
        pass

    def testIsValidEmail(self):
        self.assertTrue(self.validation.is_valid_email("*****@*****.**"))
        self.assertTrue(self.validation.is_valid_email("*****@*****.**"))
        self.assertFalse(self.validation.is_valid_email("Craigfoocom"))
        pass

    def testValid(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "*****@*****.**"
#        user = UserData(username=username, password=password, email=email)
#        self.user_datastore.save(user)
        validMsgs, isValid = self.validation.validate(username, password, verify, email)
        self.assertTrue(isValid)
        self.assertEmptyMessage([VERIFICATION_MESSAGES_KEYS[0],VERIFICATION_MESSAGES_KEYS[1],VERIFICATION_MESSAGES_KEYS[2],VERIFICATION_MESSAGES_KEYS[3]], validMsgs)
        
    def testValid_BadUsername(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password, verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[0]], validMsgs[VERIFICATION_MESSAGES_KEYS[0]])
        self.assertEmptyMessage([VERIFICATION_MESSAGES_KEYS[1],VERIFICATION_MESSAGES_KEYS[2],VERIFICATION_MESSAGES_KEYS[3]], validMsgs)
        
    def testUserExists_True(self):
        username = '******'
        #add a record to the data store
        userData = UserData(username=username, password='******')
        self.user_datastore.save(userData)
        self.assertTrue(self.validation.user_exists(username), "User " + username + " does not exist")


    def testUserExists_False(self):
        username = '******'
        testVal ='foo'
        #add a record to the data store
        userData = UserData(username=username, password='******')
        self.user_datastore.save(userData)
        self.assertFalse(self.validation.user_exists(testVal), "User " + testVal + " exists. User added: " + userData.username)

    def testValidate_UserExists(self):
        '''
        Test validate() with a user that already exists
        '''
        username = '******'
        password = '******'
        
        #add a record to the data store
        userData = UserData(username=username, password=password)
        self.user_datastore.save(userData)
        #verify in database
        user = self.user_datastore.fetchByUsername(username)
        print user.username
        self.assertTrue(self.validation.validate(username=username, password=password, verify=password, email=None), "User " + username + " exists")

    def testValid_BadPassword(self):
        username = "******"
        password = "******"
        verify = "c1"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password, verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[1]], validMsgs[VERIFICATION_MESSAGES_KEYS[1]])
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[2]], validMsgs[VERIFICATION_MESSAGES_KEYS[2]])
        self.assertEmptyMessage([VERIFICATION_MESSAGES_KEYS[0],VERIFICATION_MESSAGES_KEYS[3]], validMsgs)

    def testValid_BadEmail(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "craigfoo.com"
        validMsgs, isValid = self.validation.validate(username, password, verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[3]], validMsgs[VERIFICATION_MESSAGES_KEYS[3]])
        self.assertEmptyMessage([VERIFICATION_MESSAGES_KEYS[0],VERIFICATION_MESSAGES_KEYS[2],VERIFICATION_MESSAGES_KEYS[1]], validMsgs)

    def testValid_PasswordsDontMatch(self):
        username = "******"
        password = "******"
        verify = "craig"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password, verify, email)
        self.assertFalse(isValid)
        self.assertEquals(MISMATCHED_PASSWORDS_MESSAGE, validMsgs[VERIFICATION_MESSAGES_KEYS[1]])
        self.assertEquals(MISMATCHED_PASSWORDS_MESSAGE, validMsgs[VERIFICATION_MESSAGES_KEYS[2]])
        self.assertEmptyMessage([VERIFICATION_MESSAGES_KEYS[0],VERIFICATION_MESSAGES_KEYS[3]], validMsgs)

    def test_is_password_and_verify_equals(self):
        self.assertTrue(self.validation.is_password_and_verify_equals("craig", "craig"))
        self.assertFalse(self.validation.is_password_and_verify_equals("craig", "craig1"))

    def test_validate_login(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.user_datastore.save(user)
        
        msgs, isValid = self.validation_login.validate(username, password)
        self.assertTrue(isValid)
        self.assertTrue(len(msgs) == 0)

    def test_validate_login_username_notfound(self):
        username = '******'
        password = '******'
        user = UserData(username=username,password=password)
        self.user_datastore.save(user)
        
        msgs, isValid = self.validation_login.validate(username + 'foo', password)
        self.assertFalse(isValid)
        self.assertFalse(len(msgs) == 0)

    def test_validate_login_invalid_password(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.user_datastore.save(user)
        
        msgs, isValid = self.validation_login.validate(username, password + 'bar')
        self.assertFalse(isValid)
        self.assertFalse(len(msgs) == 0)
        
    def assertEmptyMessage(self, key_list, messages):
        for key in key_list:
            self.assertEquals('', messages[key], "Message with key " + key + " is not empty")
class Test(unittest.TestCase):
    def setUp(self):
        # Setup mock GAE Data Store
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        #instantiate classes under test
        self.validation = UserSignupValidation()
        self.validation_login = LoginValidation()
        self.user_datastore = UserAppengineDataStore()
        self.crypt = Crypt()

    def tearDown(self):
        self.testbed.deactivate()

    def testIsValidUsername(self):
        self.assertTrue(self.validation.is_valid_username("Crag"))
        self.assertTrue(self.validation.is_valid_username("Crag-Doremus"))
        self.assertTrue(self.validation.is_valid_username("Crag_Doremus"))
        self.assertTrue(self.validation.is_valid_username("Cra"))
        self.assertFalse(self.validation.is_valid_username("ca"))
        self.assertFalse(self.validation.is_valid_username("cat!"))
        self.assertTrue(
            self.validation.is_valid_username("abcdefghijklmnopqrst"))
        self.assertFalse(
            self.validation.is_valid_username("abcdefghijklmnopqrstu"))
        pass

    def testIsValidPassword(self):
        self.assertTrue(self.validation.is_valid_password("Craig"))
        self.assertTrue(
            self.validation.is_valid_password("abcdefghijklmnopqrst"))
        self.assertFalse(
            self.validation.is_valid_password("abcdefghijklmnopqrstu"))
        pass

    def testIsValidEmail(self):
        self.assertTrue(self.validation.is_valid_email("*****@*****.**"))
        self.assertTrue(self.validation.is_valid_email("*****@*****.**"))
        self.assertFalse(self.validation.is_valid_email("Craigfoocom"))
        pass

    def testValid(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "*****@*****.**"
        #        user = UserData(username=username, password=password, email=email)
        #        self.user_datastore.save(user)
        validMsgs, isValid = self.validation.validate(username, password,
                                                      verify, email)
        self.assertTrue(isValid)
        self.assertEmptyMessage([
            VERIFICATION_MESSAGES_KEYS[0], VERIFICATION_MESSAGES_KEYS[1],
            VERIFICATION_MESSAGES_KEYS[2], VERIFICATION_MESSAGES_KEYS[3]
        ], validMsgs)

    def testValid_BadUsername(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password,
                                                      verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[0]],
                          validMsgs[VERIFICATION_MESSAGES_KEYS[0]])
        self.assertEmptyMessage([
            VERIFICATION_MESSAGES_KEYS[1], VERIFICATION_MESSAGES_KEYS[2],
            VERIFICATION_MESSAGES_KEYS[3]
        ], validMsgs)

    def testUserExists_True(self):
        username = '******'
        #add a record to the data store
        userData = UserData(username=username, password='******')
        self.user_datastore.save(userData)
        self.assertTrue(self.validation.user_exists(username),
                        "User " + username + " does not exist")

    def testUserExists_False(self):
        username = '******'
        testVal = 'foo'
        #add a record to the data store
        userData = UserData(username=username, password='******')
        self.user_datastore.save(userData)
        self.assertFalse(
            self.validation.user_exists(testVal),
            "User " + testVal + " exists. User added: " + userData.username)

    def testValidate_UserExists(self):
        '''
        Test validate() with a user that already exists
        '''
        username = '******'
        password = '******'

        #add a record to the data store
        userData = UserData(username=username, password=password)
        self.user_datastore.save(userData)
        #verify in database
        user = self.user_datastore.fetchByUsername(username)
        print user.username
        self.assertTrue(
            self.validation.validate(username=username,
                                     password=password,
                                     verify=password,
                                     email=None),
            "User " + username + " exists")

    def testValid_BadPassword(self):
        username = "******"
        password = "******"
        verify = "c1"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password,
                                                      verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[1]],
                          validMsgs[VERIFICATION_MESSAGES_KEYS[1]])
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[2]],
                          validMsgs[VERIFICATION_MESSAGES_KEYS[2]])
        self.assertEmptyMessage(
            [VERIFICATION_MESSAGES_KEYS[0], VERIFICATION_MESSAGES_KEYS[3]],
            validMsgs)

    def testValid_BadEmail(self):
        username = "******"
        password = "******"
        verify = "craig1"
        email = "craigfoo.com"
        validMsgs, isValid = self.validation.validate(username, password,
                                                      verify, email)
        self.assertFalse(isValid)
        self.assertEquals(VERIFICATION_MESSAGES[VERIFICATION_MESSAGES_KEYS[3]],
                          validMsgs[VERIFICATION_MESSAGES_KEYS[3]])
        self.assertEmptyMessage([
            VERIFICATION_MESSAGES_KEYS[0], VERIFICATION_MESSAGES_KEYS[2],
            VERIFICATION_MESSAGES_KEYS[1]
        ], validMsgs)

    def testValid_PasswordsDontMatch(self):
        username = "******"
        password = "******"
        verify = "craig"
        email = "*****@*****.**"
        validMsgs, isValid = self.validation.validate(username, password,
                                                      verify, email)
        self.assertFalse(isValid)
        self.assertEquals(MISMATCHED_PASSWORDS_MESSAGE,
                          validMsgs[VERIFICATION_MESSAGES_KEYS[1]])
        self.assertEquals(MISMATCHED_PASSWORDS_MESSAGE,
                          validMsgs[VERIFICATION_MESSAGES_KEYS[2]])
        self.assertEmptyMessage(
            [VERIFICATION_MESSAGES_KEYS[0], VERIFICATION_MESSAGES_KEYS[3]],
            validMsgs)

    def test_is_password_and_verify_equals(self):
        self.assertTrue(
            self.validation.is_password_and_verify_equals("craig", "craig"))
        self.assertFalse(
            self.validation.is_password_and_verify_equals("craig", "craig1"))

    def test_validate_login(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.user_datastore.save(user)

        msgs, isValid = self.validation_login.validate(username, password)
        self.assertTrue(isValid)
        self.assertTrue(len(msgs) == 0)

    def test_validate_login_username_notfound(self):
        username = '******'
        password = '******'
        user = UserData(username=username, password=password)
        self.user_datastore.save(user)

        msgs, isValid = self.validation_login.validate(username + 'foo',
                                                       password)
        self.assertFalse(isValid)
        self.assertFalse(len(msgs) == 0)

    def test_validate_login_invalid_password(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.user_datastore.save(user)

        msgs, isValid = self.validation_login.validate(username,
                                                       password + 'bar')
        self.assertFalse(isValid)
        self.assertFalse(len(msgs) == 0)

    def assertEmptyMessage(self, key_list, messages):
        for key in key_list:
            self.assertEquals('', messages[key],
                              "Message with key " + key + " is not empty")