2022-09-01 14:59:16 +02:00
|
|
|
import random
|
2022-04-14 18:46:11 +02:00
|
|
|
from email.message import Message
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from app.config import (
|
|
|
|
ALERT_COMPLAINT_FORWARD_PHASE,
|
|
|
|
ALERT_COMPLAINT_REPLY_PHASE,
|
2022-05-02 11:53:32 +02:00
|
|
|
ALERT_COMPLAINT_TRANSACTIONAL_PHASE,
|
2022-05-10 17:54:51 +02:00
|
|
|
POSTMASTER,
|
2022-04-14 18:46:11 +02:00
|
|
|
)
|
|
|
|
from app.db import Session
|
2022-05-10 23:34:57 +02:00
|
|
|
from app.email_utils import generate_verp_email
|
2022-04-25 14:40:42 +02:00
|
|
|
from app.handler.provider_complaint import (
|
2022-04-14 18:46:11 +02:00
|
|
|
handle_hotmail_complaint,
|
|
|
|
handle_yahoo_complaint,
|
|
|
|
)
|
2022-05-19 12:24:33 +02:00
|
|
|
from app.mail_sender import mail_sender
|
2022-05-10 23:34:57 +02:00
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
ProviderComplaint,
|
|
|
|
SentAlert,
|
|
|
|
EmailLog,
|
|
|
|
VerpType,
|
|
|
|
Contact,
|
|
|
|
)
|
2022-05-10 17:54:51 +02:00
|
|
|
from tests.utils import create_new_user, load_eml_file
|
2022-04-14 18:46:11 +02:00
|
|
|
|
|
|
|
origins = [
|
2022-05-10 17:54:51 +02:00
|
|
|
[handle_yahoo_complaint, "yahoo"],
|
|
|
|
[handle_hotmail_complaint, "hotmail"],
|
2022-04-14 18:46:11 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-05-10 17:54:51 +02:00
|
|
|
def prepare_complaint(
|
2022-05-10 23:34:57 +02:00
|
|
|
provider_name: str, alias: Alias, rcpt_address: str, sender_address: str
|
2022-05-10 17:54:51 +02:00
|
|
|
) -> Message:
|
2022-05-10 23:34:57 +02:00
|
|
|
contact = Contact.create(
|
|
|
|
user_id=alias.user.id,
|
|
|
|
alias_id=alias.id,
|
2022-09-01 14:59:16 +02:00
|
|
|
website_email=f"contact{random.random()}@mailbox.test",
|
2022-05-10 23:34:57 +02:00
|
|
|
reply_email="d@e.f",
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
elog = EmailLog.create(
|
|
|
|
user_id=alias.user.id,
|
|
|
|
mailbox_id=alias.user.default_mailbox_id,
|
|
|
|
contact_id=contact.id,
|
|
|
|
commit=True,
|
|
|
|
bounced=True,
|
|
|
|
)
|
|
|
|
return_path = generate_verp_email(VerpType.bounce_forward, elog.id)
|
2022-05-10 17:54:51 +02:00
|
|
|
return load_eml_file(
|
|
|
|
f"{provider_name}_complaint.eml",
|
|
|
|
{
|
|
|
|
"postmaster": POSTMASTER,
|
2022-05-10 23:34:57 +02:00
|
|
|
"return_path": return_path,
|
2022-05-10 17:54:51 +02:00
|
|
|
"rcpt": rcpt_address,
|
|
|
|
"sender": sender_address,
|
|
|
|
"rcpt_comma_list": f"{rcpt_address},other_rcpt@somwhere.net",
|
|
|
|
},
|
|
|
|
)
|
2022-04-14 18:46:11 +02:00
|
|
|
|
|
|
|
|
2022-05-30 11:52:10 +02:00
|
|
|
@mail_sender.store_emails_test_decorator
|
2022-05-10 17:54:51 +02:00
|
|
|
@pytest.mark.parametrize("handle_ftor,provider", origins)
|
|
|
|
def test_provider_to_user(flask_client, handle_ftor, provider):
|
2022-04-14 18:46:11 +02:00
|
|
|
user = create_new_user()
|
2022-05-10 23:34:57 +02:00
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
Session.commit()
|
|
|
|
complaint = prepare_complaint(provider, alias, user.email, "nobody@nowhere.net")
|
2022-04-14 18:46:11 +02:00
|
|
|
assert handle_ftor(complaint)
|
2022-04-25 14:40:42 +02:00
|
|
|
found = ProviderComplaint.filter_by(user_id=user.id).all()
|
2022-04-14 18:46:11 +02:00
|
|
|
assert len(found) == 0
|
|
|
|
alerts = SentAlert.filter_by(user_id=user.id).all()
|
|
|
|
assert len(alerts) == 1
|
2022-05-19 12:24:33 +02:00
|
|
|
sent_mails = mail_sender.get_stored_emails()
|
|
|
|
assert len(sent_mails) == 1
|
2022-05-02 11:53:32 +02:00
|
|
|
assert alerts[0].alert_type == f"{ALERT_COMPLAINT_TRANSACTIONAL_PHASE}_{provider}"
|
2022-04-14 18:46:11 +02:00
|
|
|
|
|
|
|
|
2022-05-10 17:54:51 +02:00
|
|
|
@pytest.mark.parametrize("handle_ftor,provider", origins)
|
|
|
|
def test_provider_forward_phase(flask_client, handle_ftor, provider):
|
2022-04-14 18:46:11 +02:00
|
|
|
user = create_new_user()
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
Session.commit()
|
2022-05-10 23:34:57 +02:00
|
|
|
complaint = prepare_complaint(provider, alias, "nobody@nowhere.net", alias.email)
|
2022-04-14 18:46:11 +02:00
|
|
|
assert handle_ftor(complaint)
|
2022-04-25 14:40:42 +02:00
|
|
|
found = ProviderComplaint.filter_by(user_id=user.id).all()
|
2022-04-14 18:46:11 +02:00
|
|
|
assert len(found) == 1
|
|
|
|
alerts = SentAlert.filter_by(user_id=user.id).all()
|
|
|
|
assert len(alerts) == 1
|
|
|
|
assert alerts[0].alert_type == f"{ALERT_COMPLAINT_REPLY_PHASE}_{provider}"
|
|
|
|
|
|
|
|
|
2022-05-30 11:52:10 +02:00
|
|
|
@mail_sender.store_emails_test_decorator
|
2022-05-10 17:54:51 +02:00
|
|
|
@pytest.mark.parametrize("handle_ftor,provider", origins)
|
|
|
|
def test_provider_reply_phase(flask_client, handle_ftor, provider):
|
2022-05-19 12:24:33 +02:00
|
|
|
mail_sender.store_emails_instead_of_sending()
|
|
|
|
mail_sender.purge_stored_emails()
|
2022-04-14 18:46:11 +02:00
|
|
|
user = create_new_user()
|
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
Session.commit()
|
2022-05-10 23:34:57 +02:00
|
|
|
complaint = prepare_complaint(provider, alias, alias.email, "no@no.no")
|
2022-04-14 18:46:11 +02:00
|
|
|
assert handle_ftor(complaint)
|
2022-04-25 14:40:42 +02:00
|
|
|
found = ProviderComplaint.filter_by(user_id=user.id).all()
|
2022-04-14 18:46:11 +02:00
|
|
|
assert len(found) == 0
|
|
|
|
alerts = SentAlert.filter_by(user_id=user.id).all()
|
|
|
|
assert len(alerts) == 1
|
2022-05-19 12:24:33 +02:00
|
|
|
sent_mails = mail_sender.get_stored_emails()
|
|
|
|
assert len(sent_mails) == 1
|
2022-04-14 18:46:11 +02:00
|
|
|
assert alerts[0].alert_type == f"{ALERT_COMPLAINT_FORWARD_PHASE}_{provider}"
|