def test_random_string(self): """Test random string helper.""" string1 = random_str() string2 = random_str(30) self.assertEqual(10, len(string1)) self.assertEqual(30, len(string2)) self.assertTrue(string1 != string2)
def register(): """Register route.""" form = RegistationForm() if form.validate_on_submit(): token = random_str(30) name = request.form['name'] email = request.form['email'] password = request.form['password'] db.session.add(User(name, email, password, token)) db.session.commit() reset_url = url_for('users.confirm_account', token=token, _external=True) send_registration( { 'to': email, 'subject': 'Project confirmation email' }, values=[ name, reset_url, reset_url ] ) flash('Thanks for signing up. Please check your email to for a' ' confirmation link so we know you\'re human.', 'info') resend_url = url_for('.resend_confirmation') + '?email=' + email flash('If you do not revieve your confirmation email you can resend ' 'it by clicking <a href="' + resend_url + '">here</a>', 'info') return redirect(url_for('users.login')) return render_template('register.html', form=form)
def forgot_password(): """Forgot password route.""" form = ForgotPasswordForm() if form.validate_on_submit(): code = random_str(25) expires = datetime.utcnow() + timedelta(hours=24) email = request.form.get('email') user = User.query.filter_by(email=email).first() db.session.add(ResetPassword(user, code, expires)) db.session.commit() reset_url = url_for('users.reset_password', path=code, _external=True) # send email send_forgot_password( { 'to': user.email, 'subject': 'Project reset password' }, values=[ reset_url, reset_url ] ) flash('A password reset link has been emailed to you, please check ' 'your email.', 'info') return redirect(url_for('users.login')) return render_template('forgot_password.html', form=form)
def get_user(self): data = self.get_user_data() user = User.query.filter_by(email=data['email']).first() if user is None: # name, email, random password, no token, is google user = User(data['name'], data['email'], random_str(30), None, True) db.session.add(user) db.session.commit() login_user(user)
def create_db(): """Create database for tests.""" db.create_all() user = User('*****@*****.**', 'password', None) user2 = User('*****@*****.**', 'other_password', None) unconfirmed = User('*****@*****.**', 'unconfirmed_password', random_str(30)) db.session.add(user) db.session.add(user2) db.session.add(unconfirmed) db.session.add(ResetPassword(user, 'resetcode', datetime.utcnow() + timedelta(hours=24))) db.session.add(ResetPassword(user2, 'resetcode2', datetime.utcnow() - timedelta(hours=24))) db.session.commit()
def test_user_cant_edit_email(self, get_mock): password = random_str(30) user = User('Testname', '*****@*****.**', password, None, True) db.session.add(user) db.session.commit() with self.client: self.login('*****@*****.**', password) response = self.client.get(url_for('users.edit')) self.assertIn( '<input type="hidden" name="email" ' 'value="*****@*****.**" />', str(response.data) ) self.assertNotIn( 'Password', str(response.data) )
def create_users(self): user1 = User( 'Test User One', '*****@*****.**', 'password1', None ) db.session.add(user1) user2 = User( 'Test User Two', '*****@*****.**', 'password2', None ) db.session.add(user2) user3 = User( 'Test User Three', '*****@*****.**', 'password3', None ) db.session.add(user3) user4 = User( 'Test User Four', '*****@*****.**', 'password4', None ) db.session.add(user4) user5 = User( 'Test User Five', '*****@*****.**', 'password5', None ) db.session.add(user5) unconfirmed = User( 'Unconfirmed User', '*****@*****.**', 'unconfirmed_password', random_str(30) ) db.session.add(unconfirmed) db.session.add( ResetPassword( user1, 'resetcode', datetime.utcnow() + timedelta(hours=24) ) ) db.session.add( ResetPassword( user2, 'resetcode2', datetime.utcnow() - timedelta(hours=24) ) ) db.session.commit()