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()
        self.user_datastore = UserAppengineDataStore()

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


    def testFetch(self):
        user = UserData(username='******', password='******')
        user.put()
        user_id = user.key().id()
        actual = self.user_datastore.fetch(user_id)
        self.assertEquals(user.username, actual.username, "Found user: "******"Found user_id: " + str(actual.user_id))

    def testSave(self):
        user = UserData(username='******', password='******')
        new_user = self.user_datastore.save(user)
        user_id = user.key().id()
        actual = self.user_datastore.fetch(user_id)
        self.assertEquals(user.username, actual.username, "Found user: "******"User count: " + str(users.count()))
        self.assertTrue(new_user1.user_id ==1, 'New user_id for user1: ' + str(new_user1.user_id))
        self.assertTrue(new_user2.user_id ==2, 'New user_id for user2: ' + str(new_user2.user_id))
#        count = 0
#         for user in users:
#            count += 1
#            self.assertTrue(user.user_id == count)
           
    def testFetchByUsername(self): 
        user1 = UserData(username='******', password='******')
        self.user_datastore.save(user1)
        user2 = UserData(username='******', password='******')
        self.user_datastore.save(user2)
        userfound1 = self.user_datastore.fetchByUsername('cdoremus1')
        self.assertEquals(userfound1.password, user1.password, 'Found user' + str(userfound1))
        self.assertTrue(userfound1.user_id == 1,"User id is not 1: " + str(userfound1.user_id))

    def testFetchByUsername_NotFound(self): 
        user1 = UserData(username='******', password='******')
        self.user_datastore.save(user1)
        user2 = UserData(username='******', password='******')
        self.user_datastore.save(user2)
        userfound1 = self.user_datastore.fetchByUsername('foo')
        self.assertTrue(userfound1 == None, 'Found user' + str(userfound1))
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()
        self.user_datastore = UserAppengineDataStore()

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

    def testFetch(self):
        user = UserData(username='******', password='******')
        user.put()
        user_id = user.key().id()
        actual = self.user_datastore.fetch(user_id)
        self.assertEquals(user.username, actual.username,
                          "Found user: "******"Found user_id: " + str(actual.user_id))

    def testSave(self):
        user = UserData(username='******', password='******')
        new_user = self.user_datastore.save(user)
        user_id = user.key().id()
        actual = self.user_datastore.fetch(user_id)
        self.assertEquals(user.username, actual.username,
                          "Found user: "******"User count: " + str(users.count()))
        self.assertTrue(new_user1.user_id == 1,
                        'New user_id for user1: ' + str(new_user1.user_id))
        self.assertTrue(new_user2.user_id == 2,
                        'New user_id for user2: ' + str(new_user2.user_id))


#        count = 0
#         for user in users:
#            count += 1
#            self.assertTrue(user.user_id == count)

    def testFetchByUsername(self):
        user1 = UserData(username='******', password='******')
        self.user_datastore.save(user1)
        user2 = UserData(username='******', password='******')
        self.user_datastore.save(user2)
        userfound1 = self.user_datastore.fetchByUsername('cdoremus1')
        self.assertEquals(userfound1.password, user1.password,
                          'Found user' + str(userfound1))
        self.assertTrue(userfound1.user_id == 1,
                        "User id is not 1: " + str(userfound1.user_id))

    def testFetchByUsername_NotFound(self):
        user1 = UserData(username='******', password='******')
        self.user_datastore.save(user1)
        user2 = UserData(username='******', password='******')
        self.user_datastore.save(user2)
        userfound1 = self.user_datastore.fetchByUsername('foo')
        self.assertTrue(userfound1 == None, 'Found user' + str(userfound1))