register in auth.py and change username to userid in tests
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
import functools
|
||||||
|
|
||||||
|
from flask import(
|
||||||
|
Blueprint, flash, g, redirect, render_template, request, session, url_for
|
||||||
|
)
|
||||||
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
|
from flaskfdx.db import get_db
|
||||||
|
|
||||||
|
bp = Blueprint('auth', __name__, url_prefix='/auth')
|
||||||
|
|
||||||
|
@bp.route('/register', methods=('GET', 'POST'))
|
||||||
|
def register():
|
||||||
|
if request.method == 'POST':
|
||||||
|
userid = request.form['username']
|
||||||
|
email = request.form['email']
|
||||||
|
password = request.form['password']
|
||||||
|
db = get_db()
|
||||||
|
error = None
|
||||||
|
|
||||||
|
if not userid:
|
||||||
|
error = 'Username if required.'
|
||||||
|
elif not email:
|
||||||
|
error = 'Email is required.'
|
||||||
|
elif not password:
|
||||||
|
error = 'Password is required.'
|
||||||
|
|
||||||
|
if error is None:
|
||||||
|
try:
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO users (userid, email, password) VALUES (?, ?, ?)",
|
||||||
|
(userid, email, generate_password_hash(password)),
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
except db.IntegrityError:
|
||||||
|
error = f"User {userid} is already registered."
|
||||||
|
else:
|
||||||
|
return redirect(url_for("auth.login"))
|
||||||
|
|
||||||
|
flash(error)
|
||||||
|
|
||||||
|
return render_template('auth/register.html')
|
||||||
+11
-11
@@ -5,24 +5,24 @@ from flaskfdx.db import get_db
|
|||||||
def test_register(client, app):
|
def test_register(client, app):
|
||||||
assert client.get('/auth/register').status_code == 200
|
assert client.get('/auth/register').status_code == 200
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'auth/register', data={'username': 'a', 'password': 'a'}
|
'auth/register', data={'userid': 'a', 'email': 'a@b.c', 'password': 'a'}
|
||||||
)
|
)
|
||||||
assert 'http://localhost/auth/login' == response.headers['Location']
|
assert 'http://localhost/auth/login' == response.headers['Location']
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
assert get_db().execute(
|
assert get_db().execute(
|
||||||
"SELECT * FROM users WHERE username = 'a'",
|
"SELECT * FROM users WHERE userid = 'a'",
|
||||||
).fetchone() is not None
|
).fetchone() is not None
|
||||||
|
|
||||||
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
@pytest.mark.parametrize(('userid', 'password', 'message'), (
|
||||||
('', '', b'Username is required.'),
|
('', '', b'Userid is required.'),
|
||||||
('a', '', b'Password is required.'),
|
('a', '', b'Password is required.'),
|
||||||
('test', 'test', b'already registered'),
|
('test', 'test', b'already registered'),
|
||||||
))
|
))
|
||||||
def test_register_validate_input(client, username, password, message):
|
def test_register_validate_input(client, userid, password, message):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/auth/register',
|
'/auth/register',
|
||||||
data={'username': username, 'password': password}
|
data={'userid': userid, 'password': password}
|
||||||
)
|
)
|
||||||
assert message in response.data
|
assert message in response.data
|
||||||
|
|
||||||
@@ -34,14 +34,14 @@ def test_login(client, auth):
|
|||||||
with client:
|
with client:
|
||||||
client.get('/')
|
client.get('/')
|
||||||
assert session['user_id'] == 1
|
assert session['user_id'] == 1
|
||||||
assert g.user['username'] == 'test'
|
assert g.user['userid'] == 'test'
|
||||||
|
|
||||||
@pytest.mark.parametrize(('username', 'password', 'message'), (
|
@pytest.mark.parametrize(('userid', 'password', 'message'), (
|
||||||
('a', 'test', b'Incorrect username'),
|
('a', 'test', b'Incorrect userid'),
|
||||||
('test', 'a', b'Incorrect password.'),
|
('test', 'a', b'Incorrect password.'),
|
||||||
))
|
))
|
||||||
def test_login_validate_input(auth, username, password, message):
|
def test_login_validate_input(auth, userid, password, message):
|
||||||
response = auth.login(username, password)
|
response = auth.login(userid, password)
|
||||||
assert message in response.data
|
assert message in response.data
|
||||||
|
|
||||||
def testz_logout(client, auth):
|
def testz_logout(client, auth):
|
||||||
|
|||||||
Reference in New Issue
Block a user