make sure alias contact address is valid
This commit is contained in:
parent
04a418e655
commit
a989545505
|
@ -185,6 +185,15 @@ def alias_contact_manager(alias_id):
|
|||
)
|
||||
)
|
||||
|
||||
if not is_valid_email(contact_email):
|
||||
flash(f"{contact_email} is invalid", "error")
|
||||
return redirect(
|
||||
url_for(
|
||||
"dashboard.alias_contact_manager",
|
||||
alias_id=alias_id,
|
||||
)
|
||||
)
|
||||
|
||||
contact = Contact.get_by(alias_id=alias.id, website_email=contact_email)
|
||||
# already been added
|
||||
if contact:
|
||||
|
|
|
@ -83,6 +83,10 @@ class ModelMixin(object):
|
|||
def delete(cls, obj_id):
|
||||
cls.query.filter(cls.id == obj_id).delete()
|
||||
|
||||
@classmethod
|
||||
def first(cls):
|
||||
return cls.query.first()
|
||||
|
||||
def __repr__(self):
|
||||
values = ", ".join(
|
||||
"%s=%r" % (n, getattr(self, n))
|
||||
|
|
58
tests/dashboard/test_alias_contact_manager.py
Normal file
58
tests/dashboard/test_alias_contact_manager.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
from flask import url_for
|
||||
|
||||
from app.config import EMAIL_DOMAIN
|
||||
from app.models import (
|
||||
Alias,
|
||||
Contact,
|
||||
)
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_add_contact_success(flask_client):
|
||||
login(flask_client)
|
||||
alias = Alias.first()
|
||||
|
||||
assert Contact.query.count() == 0
|
||||
|
||||
# <<< Create a new contact >>>
|
||||
flask_client.post(
|
||||
url_for("dashboard.alias_contact_manager", alias_id=alias.id),
|
||||
data={
|
||||
"form-name": "create",
|
||||
"email": "abcd@gmail.com",
|
||||
},
|
||||
follow_redirects=True,
|
||||
)
|
||||
# a new contact is added
|
||||
assert Contact.query.count() == 1
|
||||
contact = Contact.first()
|
||||
assert contact.website_email == "abcd@gmail.com"
|
||||
|
||||
# <<< Create a new contact using a full email format >>>
|
||||
flask_client.post(
|
||||
url_for("dashboard.alias_contact_manager", alias_id=alias.id),
|
||||
data={
|
||||
"form-name": "create",
|
||||
"email": "First Last <another@gmail.com>",
|
||||
},
|
||||
follow_redirects=True,
|
||||
)
|
||||
# a new contact is added
|
||||
assert Contact.query.count() == 2
|
||||
contact = Contact.get(2)
|
||||
assert contact.website_email == "another@gmail.com"
|
||||
assert contact.name == "First Last"
|
||||
|
||||
# <<< Create a new contact with invalid email address >>>
|
||||
r = flask_client.post(
|
||||
url_for("dashboard.alias_contact_manager", alias_id=alias.id),
|
||||
data={
|
||||
"form-name": "create",
|
||||
"email": "with space@gmail.com",
|
||||
},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
# no new contact is added
|
||||
assert Contact.query.count() == 2
|
||||
assert "Invalid email format. Email must be either email@example.com" in str(r.data)
|
Loading…
Reference in a new issue