-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user authentication and test case for user
- Loading branch information
1 parent
c418afe
commit d9c300b
Showing
15 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from flask import Blueprint | ||
auth = Blueprint('auth', __name__) | ||
from . import views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, PasswordField, BooleanField, SubmitField | ||
from wtforms.validators import DataRequired, Length, Email | ||
|
||
|
||
class LoginForm(FlaskForm): | ||
email = StringField( | ||
'Email', validators=[DataRequired(), | ||
Length(1, 64), | ||
Email()]) | ||
password = PasswordField('Password', validators=[DataRequired()]) | ||
remember_me = BooleanField('Keep me logged in') | ||
submit = SubmitField('Log In') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from flask import render_template, redirect, request, url_for, flash | ||
from . import auth | ||
from flask_login import login_user | ||
from ..models import User | ||
from .forms import LoginForm | ||
|
||
|
||
@auth.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
form = LoginForm() | ||
if form.validate_on_submit(): | ||
user = User.query.filter_by(email=form.email.data).first() | ||
if user and user.verify_password(form.password.data): | ||
login_user(user, form.remember_me.data) | ||
next = request.args.get('next') | ||
if not next or not next.startswith('/'): | ||
next = url_for('main.index') | ||
return redirect(next) | ||
flash('Invalid username or password') | ||
return render_template('auth/login.html', form=form) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""empty message | ||
Revision ID: 1245c4761d44 | ||
Revises: 968188240638 | ||
Create Date: 2018-08-23 11:48:42.441637 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '1245c4761d44' | ||
down_revision = '968188240638' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('users', sa.Column('password_hash', sa.String(length=128), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('users', 'password_hash') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""empty message | ||
Revision ID: 8c77319f915c | ||
Revises: 1245c4761d44 | ||
Create Date: 2018-08-23 12:34:15.969979 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '8c77319f915c' | ||
down_revision = '1245c4761d44' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('users', sa.Column('email', sa.String(length=64), nullable=True)) | ||
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_users_email'), table_name='users') | ||
op.drop_column('users', 'email') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import unittest | ||
from app.models import User | ||
|
||
|
||
class UserModelTestCase(unittest.TestCase): | ||
def test_password_setter(self): | ||
u = User(password='cat') | ||
self.assertTrue(u.password_hash is not None) | ||
|
||
def test_no_password_getter(self): | ||
u = User(password='cat') | ||
with self.assertRaises(AttributeError): | ||
u.password | ||
|
||
def test_password_verification(self): | ||
u = User(password='cat') | ||
self.assertTrue(u.verify_password('cat')) | ||
self.assertFalse(u.verify_password('dog')) | ||
|
||
def test_password_salts_are_random(self): | ||
u = User(password='cat') | ||
u2 = User(password='cat') | ||
self.assertTrue(u.password_hash != u2.password_hash) |