示例#1
0
 def post(self):
     '''
     Handles post requests
     '''
     self.response.headers['Content-Type'] = 'text/html'
     username = self.request.get('username')
     password = self.request.get('password')
     verify = self.request.get('verify')
     email = self.request.get('email')
     values = {'username':username, 'password':password,'verify':verify,'email':email}
     cookie = ''
     service = UserService()
     #Validate that user is not already registered
     isValid = False
     try:
         cookie = service.register(username=username, password=password, verify=verify, email=email)
         isValid = True
     except UserRegistrationError as error:
         path = os.path.join(os.path.dirname(__file__), '../templates/usersignup.html')
         values.update(error.validation_messages)
         self.response.out.write(template.render(path, values))
     if isValid:    
         self.response.headers.add_header('Set-Cookie', 'user_id=%s; Path=/' % cookie)
         self.redirect('/unit6/welcome')
示例#2
0
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.service = UserService()
        factory = UserDataStoreFactory()
        self.storage = factory.get_storage()
        self.crypt = Crypt()

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

    def testLogin(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.storage.save(user)
        user_id = user.key().id()
        cookie = self.crypt.make_cookie(user_id)
        new_cookie = self.service.login(username, password, cookie)
        self.assertFalse(new_cookie == None)
        self.assertEquals(user.key().id(), 1)

    def testLogin_Failed(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.storage.save(user)
        user_id = user.key().id()
        cookie = self.crypt.make_cookie(user_id)
        try:
            self.service.login(username + 'foo', password, cookie)
            self.fail(
                'Should not get here because LoginError should be raised due to bad usernames'
            )
        except LoginError as error:
            self.assertEquals(error.validation_messages['login_error'],
                              'Invalid login')
            print error

    def testRegister(self):
        username = '******'
        password = '******'
        verify = password
        cookie = self.service.register(username=username,
                                       password=password,
                                       verify=verify)
        print cookie
        user = self.storage.fetchByUsername(username)
        self.assertFalse(cookie == None)
        user_id = user.key().id()
        self.assertEquals(user_id, 1)
        cookie_split = cookie.split('|')
        self.assertTrue(cookie_split[0] == str(user_id))
        self.assertFalse(cookie_split[1] == None)
        self.assertFalse(cookie_split[1] == '')

    def testRegister_failed(self):
        username = '******'
        password = '******'
        verify = password
        user = UserData(username=username, password=password)
        self.storage.save(user)
        try:
            self.service.register(username=username,
                                  password=password,
                                  verify=verify)
            self.fail(
                'Should not get here because UserRegistrationError should be raised due to the fact that user already exists'
            )
        except UserRegistrationError as error:
            self.assertEquals(error.validation_messages['user_exists_error'],
                              'That user already exists.')
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.service = UserService()
        factory = UserDataStoreFactory()
        self.storage = factory.get_storage()
        self.crypt = Crypt()


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


    def testLogin(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.storage.save(user)
        user_id = user.key().id()
        cookie = self.crypt.make_cookie(user_id)
        new_cookie = self.service.login(username, password, cookie)
        self.assertFalse(new_cookie == None)
        self.assertEquals(user.key().id(), 1)

    def testLogin_Failed(self):
        username = '******'
        password = '******'
        hashed_password = self.crypt.crypt_password(username, password)
        user = UserData(username=username, password=hashed_password)
        self.storage.save(user)
        user_id = user.key().id()
        cookie = self.crypt.make_cookie(user_id)
        try: 
            self.service.login(username + 'foo', password, cookie)
            self.fail('Should not get here because LoginError should be raised due to bad usernames')
        except LoginError as error:
            self.assertEquals(error.validation_messages['login_error'], 'Invalid login')
            print error


    def testRegister(self):
        username = '******'
        password = '******'
        verify = password
        cookie = self.service.register(username=username, password=password, verify=verify)
        print cookie
        user = self.storage.fetchByUsername(username)
        self.assertFalse(cookie == None)
        user_id = user.key().id()
        self.assertEquals(user_id, 1)
        cookie_split = cookie.split('|')
        self.assertTrue(cookie_split[0] == str(user_id))
        self.assertFalse(cookie_split[1] == None)
        self.assertFalse(cookie_split[1] == '' )

    def testRegister_failed(self):
        username = '******'
        password = '******'
        verify = password
        user = UserData(username=username, password=password)
        self.storage.save(user)
        try:
            self.service.register(username=username, password=password, verify=verify)
            self.fail('Should not get here because UserRegistrationError should be raised due to the fact that user already exists')
        except UserRegistrationError as error:
            self.assertEquals(error.validation_messages['user_exists_error'], 'That user already exists.')