def test_create_user_and_check(self): u = User(email="*****@*****.**", username="******", full_name="Dash Stack", password="******", avatar="/static/img/user2-160x160.jpg", created_at=datetime.datetime.now()) db.session.add(u) u = User.query.filter_by(email="*****@*****.**").first() self.assertTrue(u.email == "*****@*****.**")
def setUp(self): self.dash = create_app('testing') self.app_context = self.dash.app_context() self.app_context.push() self.app_test_client = self.dash.test_client() db.create_all() # create user for testing u = User(email="*****@*****.**", username="******", full_name="Dash Stack", password="******", avatar="/static/img/user2-160x160.jpg", created_at=datetime.datetime.now()) db.session.add(u)
def create_user(): "Creates a normal user" user_login = prompt("Provide a login name") user_email = prompt("Provide an email") user_password = prompt("Provide a password") db.session.add( User( login=user_login, email=user_email, password=user_password, admin=False, confirmed=False, )) db.session.commit()
def create_admin(email=None, password=None): """Creates the admin user.""" if email is None: admin_email = prompt("Provide an email") else: admin_email = email if password is None: admin_password = prompt("Provide a password") else: admin_password = password db.session.add( User(email=admin_email, password=admin_password, admin=True, active=True, roles='admin', confirmed_at=datetime.datetime.now())) db.session.commit()
def callback(): # Redirect user to home page if already logged in. if current_user is not None and current_user.is_authenticated: return redirect(url_for('base.index')) if 'error' in request.args: if request.args.get('error') == 'access_denied': return 'You denied access.' return 'Error encountered.' if 'code' not in request.args and 'state' not in request.args: return redirect(url_for('login')) else: # Execution reaches here when user has successfully authenticated our app. try: token = Google.get_google_auth_token(state=session['oauth_state'], request=request.url) except HTTPError: return 'HTTPError occurred.' resp = Google.get_google_user_info(token=token) if resp.status_code == 200: user_data = resp.json() email = user_data['email'] user = User.query.filter_by(email=email).first() # Create account if it does not exist if user is None: user = User() user.email = email # Update attributes with data pulled from Google user.name = user_data['name'] user.tokens = json.dumps(token) user.avatar = user_data['picture'] db.session.add(user) # Update the user's last login time updated_user = user.login() db.session.commit() # Login User login_user(updated_user) flash('You were successfully logged in') return redirect(url_for('base.index')) return 'Could not fetch your information.'
def test_password_salts_are_random(self): u = User(password='******') u2 = User(password='******') self.assertTrue(u.password_hash != u2.password_hash)
def test_password_verification(self): u = User(password='******') self.assertTrue(u.verify_password('cat')) self.assertFalse(u.verify_password('dog'))
def test_no_password_getter(self): u = User(password='******') with self.assertRaises(AttributeError): u.password
def test_password_setter(self): u = User(password='******') self.assertTrue(u.password_hash is not None)