def test_create_and_get_user(self): email = '*****@*****.**' password = hashlib.md5('admin-password').hexdigest() nicename = 'Admin' admin = store.create_user(store.ROLE_ADMINISTRATOR, email, password, nicename) self.assertFalse(admin is None) self.assertEquals(store.ROLE_ADMINISTRATOR, admin.role) self.assertEquals(email, admin.email) self.assertEquals(password, admin.password) self.assertEquals(nicename, admin.nicename) # get by email: u = store.get_user_by_email(email) self.assertFalse(u is None) self.assertEquals(store.ROLE_ADMINISTRATOR, u.role) self.assertEquals(email, u.email) self.assertEquals(password, u.password) self.assertEquals(nicename, u.nicename) # get by key: u = store.get_user_by_key(admin.id) self.assertFalse(u is None) self.assertEquals(store.ROLE_ADMINISTRATOR, u.role) self.assertEquals(email, u.email) self.assertEquals(password, u.password) self.assertEquals(nicename, u.nicename) # load non-exist user: u = store.get_user_by_email('*****@*****.**') self.assertTrue(u is None)
def test_create_and_get_user(self): email = "*****@*****.**" password = hashlib.md5("admin-password").hexdigest() nicename = "Admin" admin = store.create_user(store.ROLE_ADMINISTRATOR, email, password, nicename) self.assertFalse(admin is None) self.assertEquals(store.ROLE_ADMINISTRATOR, admin.role) self.assertEquals(email, admin.email) self.assertEquals(password, admin.password) self.assertEquals(nicename, admin.nicename) # get by email: u = store.get_user_by_email(email) self.assertFalse(u is None) self.assertEquals(store.ROLE_ADMINISTRATOR, u.role) self.assertEquals(email, u.email) self.assertEquals(password, u.password) self.assertEquals(nicename, u.nicename) # get by key: u = store.get_user_by_key(admin.id) self.assertFalse(u is None) self.assertEquals(store.ROLE_ADMINISTRATOR, u.role) self.assertEquals(email, u.email) self.assertEquals(password, u.password) self.assertEquals(nicename, u.nicename) # load non-exist user: u = store.get_user_by_email("*****@*****.**") self.assertTrue(u is None)
def do_google_signin(**kw): ctx = kw['context'] # get google user: gu = users.get_current_user() if gu is None: logging.error('Google account info is not found. Exit g_signin...') raise ApplicationError('Cannot find user information') ctx.delete_cookie(cookie.AUTO_SIGNIN_COOKIE) email = gu.email().lower() nicename = gu.nickname() # check if user exist: user = store.get_user_by_email(email) if user is None: # auto-create new user: role = store.ROLE_SUBSCRIBER if users.is_current_user_admin(): role = store.ROLE_ADMINISTRATOR user = store.create_user(role, email, '', nicename) elif users.is_current_user_admin() and user.role!=store.ROLE_ADMINISTRATOR: user.role = store.ROLE_ADMINISTRATOR user.put() ctx.set_cookie(cookie.IS_FROM_GOOGLE_COOKIE, 'yes', 31536000) redirect = ctx.get_argument('redirect', '/') logging.info('Sign in successfully with Google account and redirect to %s...' % redirect) return 'redirect:%s' % redirect
def do_forgot(**kw): ip = kw['request'].remote_addr ctx = kw['context'] # verify captcha: challenge = ctx.get_argument('recaptcha_challenge_field', '') response = ctx.get_argument('recaptcha_response_field', '') email = ctx.get_argument('email', '') user = store.get_user_by_email(email) if user is None: return { '__view__' : 'forgot', 'email' : email, 'error' : 'Email is not exist', 'recaptcha_public_key' : recaptcha.get_public_key(), 'site' : _get_site_info(), 'version' : get_version(), } result, error = recaptcha.verify_captcha(challenge, response, recaptcha.get_private_key(), ip) if result: token = model.create_reset_password_token(user.id) sender = store.get_setting('sender', 'mail', '') if not sender: raise ApplicationError('Cannot send mail: mail sender address is not configured.') appid = kw['environ']['APPLICATION_ID'] body = r'''Dear %s You received this mail because you have requested reset your password. Please paste the following link to the address bar of the browser, then press ENTER: https://%s.appspot.com/manage/reset?token=%s ''' % (user.nicename, appid, token) html = r'''<html> <body> <p>Dear %s</p> <p>You received this mail because you have requested reset your password.<p> <p>Please paste the following link to reset your password:</p> <p><a href="https://%s.appspot.com/manage/reset?token=%s">https://%s.appspot.com/manage/reset?token=%s</a></p> <p>If you have trouble in clicking the URL above, please paste the following link to the address bar of the browser, then press ENTER:</p> <p>https://%s.appspot.com/manage/reset?token=%s</p> </body> </html> ''' % (urllib.quote(user.nicename), appid, token, appid, token, appid, token) mail.send(sender, email, 'Reset your password', body, html) return { '__view__' : 'sent', 'email' : email, 'site' : _get_site_info(), 'version' : get_version(), } return { '__view__' : 'forgot', 'email' : email, 'error' : error, 'recaptcha_public_key' : recaptcha.get_public_key(), 'site' : _get_site_info(), 'version' : get_version(), }
def do_signin(**kw): ctx = kw['context'] redirect = ctx.get_argument('redirect', '/') email = ctx.get_argument('email', '').lower() password = ctx.get_argument('password') user = None error = '' try: user = store.get_user_by_email(email) if user is None or user.password != password: error = r'Bad email or password' except StandardError, e: logging.exception('failed to sign in') error = 'Unexpected error occurred: %s' % e.message
def do_signin(**kw): ctx = kw['context'] redirect = ctx.get_argument('redirect', '/') email = ctx.get_argument('email', '').lower() password = ctx.get_argument('password') user = None error = '' try: user = store.get_user_by_email(email) if user is None or user.password!=password: error = r'Bad email or password' except StandardError, e: logging.exception('failed to sign in') error = 'Unexpected error occurred: %s' % e.message
def test_create_duplicate_users(self): email = "*****@*****.**" password = hashlib.md5("random-password").hexdigest() bob1 = store.create_user(store.ROLE_EDITOR, email, password, "Bob1") self.assertFalse(bob1 is None) func = lambda: store.create_user(store.ROLE_CONTRIBUTOR, email, password, "Bob2") self.assertRaises(ValidationError, func) self.assertRaises(ValidationError, func) # get by email, should be only one: Bob1 us = store.User.all().filter("email =", email).fetch(100) self.assertEquals(1, len(us)) u = store.get_user_by_email(email) self.assertEquals(store.ROLE_EDITOR, u.role) self.assertEquals(email, u.email) self.assertEquals("Bob1", u.nicename)
def _detect_current_user(kw): kw['current_user'] = None ctx = kw['context'] auto_signin_cookie = ctx.get_cookie(cookie.AUTO_SIGNIN_COOKIE) if auto_signin_cookie: user = cookie.validate_sign_in_cookie(auto_signin_cookie, store.get_user_by_key) if user: kw['current_user'] = user return from google.appengine.api import users gu = users.get_current_user() if gu is not None: email = gu.email().lower() user = store.get_user_by_email(email) if user: kw['current_user'] = user
def test_create_duplicate_users(self): email = '*****@*****.**' password = hashlib.md5('random-password').hexdigest() bob1 = store.create_user(store.ROLE_EDITOR, email, password, 'Bob1') self.assertFalse(bob1 is None) func = lambda: store.create_user(store.ROLE_CONTRIBUTOR, email, password, 'Bob2') self.assertRaises(ValidationError, func) self.assertRaises(ValidationError, func) # get by email, should be only one: Bob1 us = store.User.all().filter('email =', email).fetch(100) self.assertEquals(1, len(us)) u = store.get_user_by_email(email) self.assertEquals(store.ROLE_EDITOR, u.role) self.assertEquals(email, u.email) self.assertEquals('Bob1', u.nicename)