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 18:23:14 +02:00
|
|
|
from app.email import headers
|
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-04-25 14:40:42 +02:00
|
|
|
from app.models import Alias, ProviderComplaint, SentAlert
|
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(
|
|
|
|
provider_name: str, rcpt_address: str, sender_address: str
|
|
|
|
) -> Message:
|
|
|
|
return load_eml_file(
|
|
|
|
f"{provider_name}_complaint.eml",
|
|
|
|
{
|
|
|
|
"postmaster": POSTMASTER,
|
|
|
|
"return_path": "sl.something.other@simplelogin.co",
|
|
|
|
"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-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 17:54:51 +02:00
|
|
|
complaint = prepare_complaint(provider, 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-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 17:54:51 +02:00
|
|
|
complaint = prepare_complaint(provider, "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-10 17:54:51 +02:00
|
|
|
@pytest.mark.parametrize("handle_ftor,provider", origins)
|
|
|
|
def test_provider_reply_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()
|
|
|
|
original_message = Message()
|
|
|
|
original_message[headers.TO] = alias.email
|
|
|
|
original_message[headers.FROM] = "no@no.no"
|
|
|
|
original_message.set_payload("Contents")
|
|
|
|
|
2022-05-10 17:54:51 +02:00
|
|
|
complaint = prepare_complaint(provider, 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
|
|
|
|
assert alerts[0].alert_type == f"{ALERT_COMPLAINT_FORWARD_PHASE}_{provider}"
|