2021-05-04 00:47:06 +02:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.models import User, UserEmailVerification
|
|
|
|
from .utils import login, logout, is_logged_in
|
|
|
|
from .utils import client # noqa
|
|
|
|
|
|
|
|
|
|
|
|
def test_login_logout(client):
|
|
|
|
rv = client.get("/")
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
2021-07-24 03:32:28 +02:00
|
|
|
assert b"Sign out" not in rv.data
|
2021-05-04 00:47:06 +02:00
|
|
|
rv = login(client, "rubenwardy", "tuckfrump")
|
2021-07-24 03:32:28 +02:00
|
|
|
assert b"Sign out" in rv.data
|
2021-05-04 00:47:06 +02:00
|
|
|
assert is_logged_in(rv)
|
|
|
|
|
|
|
|
rv = client.get("/")
|
|
|
|
assert is_logged_in(rv)
|
|
|
|
|
|
|
|
rv = logout(client)
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
|
|
|
rv = login(client, "rubenwardy", "wrongpass")
|
|
|
|
assert b"Incorrect password. Did you set one?" in rv.data
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
|
|
|
rv = login(client, "badname", "wrongpass")
|
|
|
|
assert b"User badname does not exist" in rv.data
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
|
|
|
rv = login(client, "bad@email.com", "wrongpass")
|
|
|
|
assert b"Incorrect email or password" in rv.data
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
|
|
|
|
|
|
|
def register(client, username, display_name, password, email, question):
|
|
|
|
return client.post("/user/register/", data=dict(
|
|
|
|
username=username,
|
|
|
|
display_name=display_name,
|
|
|
|
email=email,
|
|
|
|
password=password,
|
|
|
|
question=question,
|
|
|
|
agree=True
|
|
|
|
), follow_redirects=True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_register(client):
|
|
|
|
username = "testuser123"
|
|
|
|
assert User.query.filter_by(username=username).first() is None
|
|
|
|
|
|
|
|
rv = register(client, username, "Test User", "password", "test@example.com", "13")
|
|
|
|
assert b"Incorrect captcha answer" in rv.data
|
|
|
|
|
|
|
|
rv = register(client, "££££!!!", "Test User", "password", "test@example.com", "13")
|
|
|
|
assert b"invalid-feedback" in rv.data
|
|
|
|
assert b"Only a-zA-Z0-9._ allowed</p>" in rv.data
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_flow(client):
|
|
|
|
username = "testuser123"
|
|
|
|
|
|
|
|
assert User.query.filter_by(username=username).first() is None
|
|
|
|
|
|
|
|
rv = register(client, username, "Test User", "password", "test@example.com", "19")
|
2021-11-25 12:01:07 +01:00
|
|
|
assert b"We've sent an email to the address you specified" in rv.data
|
2021-05-04 00:47:06 +02:00
|
|
|
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
assert user is not None
|
|
|
|
assert user.username == username
|
|
|
|
assert user.display_name == "Test User"
|
|
|
|
assert not user.is_active
|
|
|
|
assert user.email_confirmed_at is None
|
|
|
|
assert user.email == "test@example.com"
|
|
|
|
|
|
|
|
rv = login(client, username, "password")
|
|
|
|
assert b"You need to confirm the registration email" in rv.data
|
|
|
|
assert not is_logged_in(rv)
|
|
|
|
|
|
|
|
email = UserEmailVerification.query.filter_by(user_id=user.id).first()
|
|
|
|
assert email is not None
|
|
|
|
|
|
|
|
rv = client.get(url_for('users.verify_email', token=email.token), follow_redirects=True)
|
|
|
|
assert b"You may now log in" in rv.data
|
|
|
|
|
2021-07-24 03:32:28 +02:00
|
|
|
assert b"Sign out" not in rv.data
|
2021-05-04 00:47:06 +02:00
|
|
|
rv = login(client, username, "password")
|
2021-07-24 03:32:28 +02:00
|
|
|
assert b"Sign out" in rv.data
|
2021-05-04 00:47:06 +02:00
|
|
|
assert is_logged_in(rv)
|