def register(): form = RegisterForm() if form.validate_on_submit(): user = User(email=form.email.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() token = user.generate_confirmation_token() send_email(to=user.email, subject='Confirmation You Account', template='email/confirm', user=user, token=token) flash('A confirmation email has been sent to you by email') return redirect(url_for('user.login')) return render_template('user/register.html', form=form)
def password_reset(token): if not current_user.is_anonymous: return redirect(url_for('main.index')) form = PasswordResetForm() if form.validate_on_submit(): if User.reset_password(token, form.password.data): db.session.commit() flash("Your password has been updated.") return redirect(url_for('user.login')) else: return redirect(url_for('main.index')) return render_template('user/reset_password.html', form=form)
def users(count=100): fake = Faker() i = 0 while i < count: u = User(email=fake.email(), username=fake.user_name(), password='******', confirmed=True, name=fake.name(), location=fake.city(), about_me=fake.text(), member_since=fake.past_date()) db.session.add(u) try: db.session.commit() i += 1 except IndentationError: db.session.rollback()
def test_user_role(self): u = User(email='*****@*****.**', password='******') self.assertTrue(u.can(Permission.FOLLOW)) self.assertTrue(u.can(Permission.COMMENT)) self.assertTrue(u.can(Permission.ADMIN))
def test_password_salts_are_random(self): u = User(password='******') u2 = User(password='******') self.assertTrue(u._password != u2._password)
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 is not None)