2020-01-28 04:50:25 +00:00
|
|
|
from flask import url_for
|
|
|
|
|
2022-11-30 16:19:55 +00:00
|
|
|
from app import config
|
2022-10-14 15:35:34 +00:00
|
|
|
from app.db import Session
|
2022-11-30 16:19:55 +00:00
|
|
|
from app.models import DailyMetric, User
|
|
|
|
from app.utils import canonicalize_email
|
|
|
|
from tests.utils import create_new_user, random_email
|
|
|
|
|
|
|
|
|
|
|
|
def setup_module():
|
|
|
|
config.SKIP_MX_LOOKUP_ON_CHECK = True
|
|
|
|
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
config.SKIP_MX_LOOKUP_ON_CHECK = False
|
2022-10-14 15:35:34 +00:00
|
|
|
|
2020-01-28 04:50:25 +00:00
|
|
|
|
|
|
|
def test_register_success(flask_client):
|
2022-11-30 16:19:55 +00:00
|
|
|
email = random_email()
|
2020-01-28 04:50:25 +00:00
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.register"),
|
2022-11-30 16:19:55 +00:00
|
|
|
data={"email": email, "password": "password"},
|
2020-01-28 04:50:25 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
2022-10-14 15:35:34 +00:00
|
|
|
# User arrives at the waiting activation page.
|
2020-01-28 04:50:25 +00:00
|
|
|
assert b"An email to validate your email is on its way" in r.data
|
|
|
|
|
|
|
|
|
2022-10-14 15:35:34 +00:00
|
|
|
def test_register_increment_nb_new_web_non_proton_user(flask_client):
|
|
|
|
daily_metric = DailyMetric.get_or_create_today_metric()
|
|
|
|
Session.commit()
|
|
|
|
nb_new_web_non_proton_user = daily_metric.nb_new_web_non_proton_user
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.register"),
|
2022-11-30 16:19:55 +00:00
|
|
|
data={"email": random_email(), "password": "password"},
|
2022-10-14 15:35:34 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
new_daily_metric = DailyMetric.get_or_create_today_metric()
|
|
|
|
assert new_daily_metric.nb_new_web_non_proton_user == nb_new_web_non_proton_user + 1
|
|
|
|
|
|
|
|
|
2020-01-28 04:50:25 +00:00
|
|
|
def test_register_disabled(flask_client):
|
|
|
|
"""User cannot create new account when DISABLE_REGISTRATION."""
|
|
|
|
|
|
|
|
config.DISABLE_REGISTRATION = True
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.register"),
|
2020-04-27 21:15:30 +00:00
|
|
|
data={"email": "abcd@gmail.com", "password": "password"},
|
2020-01-28 04:50:25 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
2022-11-30 16:19:55 +00:00
|
|
|
config.DISABLE_REGISTRATION = False
|
2020-01-28 04:50:25 +00:00
|
|
|
assert b"Registration is closed" in r.data
|
2022-11-30 16:19:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_register_non_canonical_if_canonical_exists_is_not_allowed(flask_client):
|
|
|
|
"""User cannot create new account if the canonical name clashes"""
|
|
|
|
email = f"noncan.{random_email()}"
|
|
|
|
canonical_email = canonicalize_email(email)
|
|
|
|
create_new_user(email=canonical_email)
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.register"),
|
|
|
|
data={"email": email, "password": "password"},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert f"Email {canonical_email} already used".encode("utf-8") in r.data
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_non_canonical_is_canonicalized(flask_client):
|
|
|
|
"""User cannot create new account if the canonical name clashes"""
|
|
|
|
email = f"noncan.{random_email()}"
|
|
|
|
|
|
|
|
r = flask_client.post(
|
|
|
|
url_for("auth.register"),
|
|
|
|
data={"email": email, "password": "password"},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert b"An email to validate your email is on its way" in r.data
|
|
|
|
assert User.get_by(email=canonicalize_email(email)) is not None
|