register in auth.py and change username to userid in tests

This commit is contained in:
2021-10-12 13:45:31 -06:00
parent d6094c9118
commit 4d850864dd
2 changed files with 53 additions and 11 deletions
+42
View File
@@ -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
View File
@@ -5,24 +5,24 @@ from flaskfdx.db import get_db
def test_register(client, app):
assert client.get('/auth/register').status_code == 200
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']
with app.app_context():
assert get_db().execute(
"SELECT * FROM users WHERE username = 'a'",
"SELECT * FROM users WHERE userid = 'a'",
).fetchone() is not None
@pytest.mark.parametrize(('username', 'password', 'message'), (
('', '', b'Username is required.'),
@pytest.mark.parametrize(('userid', 'password', 'message'), (
('', '', b'Userid is required.'),
('a', '', b'Password is required.'),
('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(
'/auth/register',
data={'username': username, 'password': password}
data={'userid': userid, 'password': password}
)
assert message in response.data
@@ -34,14 +34,14 @@ def test_login(client, auth):
with client:
client.get('/')
assert session['user_id'] == 1
assert g.user['username'] == 'test'
assert g.user['userid'] == 'test'
@pytest.mark.parametrize(('username', 'password', 'message'), (
('a', 'test', b'Incorrect username'),
@pytest.mark.parametrize(('userid', 'password', 'message'), (
('a', 'test', b'Incorrect userid'),
('test', 'a', b'Incorrect password.'),
))
def test_login_validate_input(auth, username, password, message):
response = auth.login(username, password)
def test_login_validate_input(auth, userid, password, message):
response = auth.login(userid, password)
assert message in response.data
def testz_logout(client, auth):