2020-11-03 11:13:43 +01:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
Contact,
|
|
|
|
)
|
2024-09-27 16:04:32 +02:00
|
|
|
from tests.utils import login, random_email
|
2020-11-03 11:13:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_contact_success(flask_client):
|
2022-04-18 11:17:10 +02:00
|
|
|
user = login(flask_client)
|
|
|
|
alias = Alias.filter(Alias.user_id == user.id).first()
|
2020-11-03 11:13:43 +01:00
|
|
|
|
2022-04-18 11:17:10 +02:00
|
|
|
assert Contact.filter_by(user_id=user.id).count() == 0
|
2020-11-03 11:13:43 +01:00
|
|
|
|
2024-09-27 16:04:32 +02:00
|
|
|
email = random_email()
|
2020-11-03 11:13:43 +01:00
|
|
|
# <<< Create a new contact >>>
|
|
|
|
flask_client.post(
|
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias.id),
|
|
|
|
data={
|
|
|
|
"form-name": "create",
|
2024-09-27 16:04:32 +02:00
|
|
|
"email": email,
|
2020-11-03 11:13:43 +01:00
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
# a new contact is added
|
2022-04-18 11:17:10 +02:00
|
|
|
assert Contact.filter_by(user_id=user.id).count() == 1
|
|
|
|
contact = Contact.filter_by(user_id=user.id).first()
|
2024-09-27 16:04:32 +02:00
|
|
|
assert contact.website_email == email
|
2020-11-03 11:13:43 +01:00
|
|
|
|
|
|
|
# <<< Create a new contact using a full email format >>>
|
2024-09-27 16:04:32 +02:00
|
|
|
email = random_email()
|
2020-11-03 11:13:43 +01:00
|
|
|
flask_client.post(
|
|
|
|
url_for("dashboard.alias_contact_manager", alias_id=alias.id),
|
|
|
|
data={
|
|
|
|
"form-name": "create",
|
2024-09-27 16:04:32 +02:00
|
|
|
"email": f"First Last <{email}>",
|
2020-11-03 11:13:43 +01:00
|
|
|
},
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
# a new contact is added
|
2022-04-18 11:17:10 +02:00
|
|
|
assert Contact.filter_by(user_id=user.id).count() == 2
|
|
|
|
contact = (
|
|
|
|
Contact.filter_by(user_id=user.id).filter(Contact.id != contact.id).first()
|
|
|
|
)
|
2024-09-27 16:04:32 +02:00
|
|
|
assert contact.website_email == email
|
2020-11-03 11:13:43 +01:00
|
|
|
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
|
2022-04-18 11:17:10 +02:00
|
|
|
assert Contact.filter_by(user_id=user.id).count() == 2
|
2020-11-03 11:13:43 +01:00
|
|
|
assert "Invalid email format. Email must be either email@example.com" in str(r.data)
|