more detailed error message when an alias can't be created

This commit is contained in:
Son NK 2020-10-09 11:48:52 +02:00
parent 859bc7976e
commit 8dbd3c1c68
3 changed files with 38 additions and 12 deletions

View file

@ -101,16 +101,35 @@ def custom_alias():
if verify_prefix_suffix(current_user, alias_prefix, alias_suffix):
full_alias = alias_prefix + alias_suffix
if (
Alias.get_by(email=full_alias)
or DeletedAlias.get_by(email=full_alias)
or DomainDeletedAlias.get_by(email=full_alias)
):
LOG.d("full alias already used %s", full_alias)
flash(
f"Alias {full_alias} already exists, please choose another one",
"warning",
general_error_msg = f"{full_alias} cannot be used"
if Alias.get_by(email=full_alias):
alias = Alias.get_by(email=full_alias)
if alias.user_id == current_user.id:
flash(f"You already have this alias {full_alias}", "error")
else:
flash(general_error_msg, "error")
elif DomainDeletedAlias.get_by(email=full_alias):
domain_deleted_alias: DomainDeletedAlias = DomainDeletedAlias.get_by(
email=full_alias
)
custom_domain = domain_deleted_alias.domain
if domain_deleted_alias.user_id == current_user.id:
flash(
f"You have deleted this alias before. You can restore it on "
f"{custom_domain.domain} 'Deleted Alias' page",
"error",
)
else:
# should never happen as user can only choose their domains
LOG.exception(
"Deleted Alias %s does not belong to user %s",
domain_deleted_alias,
)
elif DeletedAlias.get_by(email=full_alias):
flash(general_error_msg, "error")
else:
custom_domain_id = None
# get the custom_domain_id if alias is created with a custom domain

View file

@ -1478,10 +1478,15 @@ class DomainDeletedAlias(db.Model, ModelMixin):
)
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
domain = db.relationship(CustomDomain)
@classmethod
def create(cls, **kw):
raise Exception("should use delete_alias(alias,user) instead")
def __repr__(self):
return f"<DomainDeletedAlias {self.id} {self.email}>"
class LifetimeCoupon(db.Model, ModelMixin):
code = db.Column(db.String(128), nullable=False, unique=True)

View file

@ -152,7 +152,7 @@ def test_add_already_existed_alias(flask_client):
follow_redirects=True,
)
assert r.status_code == 200
assert b"already exists, please choose another one" in r.data
assert f"prefix{suffix} cannot be used" in r.get_data(True)
def test_add_alias_in_global_trash(flask_client):
@ -194,7 +194,7 @@ def test_add_alias_in_global_trash(flask_client):
follow_redirects=True,
)
assert r.status_code == 200
assert b"already exists, please choose another one" in r.data
assert f"prefix{suffix} cannot be used" in r.get_data(True)
def test_add_alias_in_custom_domain_trash(flask_client):
@ -231,4 +231,6 @@ def test_add_alias_in_custom_domain_trash(flask_client):
follow_redirects=True,
)
assert r.status_code == 200
assert b"already exists, please choose another one" in r.data
assert "You have deleted this alias before. You can restore it on" in r.get_data(
True
)