def post(self): ''' Processes user login ''' self.response.headers['Content-Type'] = 'text/html' username = self.request.get('username') password = self.request.get('password') values = {'username': username, 'password': password} cookie = self.request.cookies.get('user_id') service = UserService() new_cookie = '' isError = False try: new_cookie = service.login(username=username,password=password, cookie=cookie) except InvalidCookieError as error: isError = True self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/') self.redirect('/final/signup') except LoginError as error: isError = True messages = error.validation_messages logging.info("LoginError msgs: " + str(messages)) values.update(messages) path = os.path.join(os.path.dirname(__file__), '../templates/login.html') self.response.out.write(template.render(path, values)) if not isError: self.response.headers.add_header('Set-Cookie', 'user_id=%s; Path=/' % new_cookie) self.redirect('/final/welcome')
def post(self): ''' Processes user login ''' self.response.headers['Content-Type'] = 'text/html' username = self.request.get('username') password = self.request.get('password') values = {'username': username, 'password': password} cookie = self.request.cookies.get('user_id') service = UserService() new_cookie = '' isError = False try: new_cookie = service.login(username=username, password=password, cookie=cookie) except InvalidCookieError as error: isError = True self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/') self.redirect('/final/signup') except LoginError as error: isError = True messages = error.validation_messages logging.info("LoginError msgs: " + str(messages)) values.update(messages) path = os.path.join(os.path.dirname(__file__), '../templates/login.html') self.response.out.write(template.render(path, values)) if not isError: self.response.headers.add_header('Set-Cookie', 'user_id=%s; Path=/' % new_cookie) self.redirect('/final/welcome')
def check_cookie(request_handler): try: cookie = request_handler.request.cookies.get("user_id") user_service = UserService() user_service.check_cookie(cookie) except InvalidCookieError as error: request_handler.response.headers.add_header('Set-Cookie', 'user_id=; Path=/') request_handler.redirect('/final/signup')
def get(self): ''' Handles get requests ''' self.response.headers['Content-Type'] = 'text/html' #delete user from data store user_id = self.request.cookies.get("user_id") if user_id: user_id = user_id[0:user_id.find('|')] service = UserService() logging.info("User %s logged out? %s" % (user_id, str(service.logout(user_id)))) #clear cookie by setting it to an empty string self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/') self.redirect('/final/signup')
def get(self): ''' Handles get request ''' # values = {'username':self.request.get('username')} values = {} cookie = self.request.cookies.get('user_id') service = UserService() user = None try: user = service.welcome(cookie) values['username'] = user.username except InvalidCookieError as error: logging.error('Invalid cookie in UserSignupWelcome: ' + str(error)) self.redirect('/final/signup') path = os.path.join(os.path.dirname(__file__), '../templates/welcome.html') self.response.out.write(template.render(path, values))
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 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('/final/welcome')
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.')