2022-03-17 19:03:36 +01:00
|
|
|
import email
|
|
|
|
import os.path
|
2022-01-04 18:06:08 +01:00
|
|
|
from email.message import EmailMessage
|
|
|
|
|
|
|
|
from app.email import headers
|
2021-06-22 17:52:24 +02:00
|
|
|
from app.models import User, Alias, AuthorizedAddress, IgnoredEmail
|
2022-01-04 18:06:08 +01:00
|
|
|
from email_handler import (
|
|
|
|
get_mailbox_from_mail_from,
|
|
|
|
should_ignore,
|
|
|
|
is_automatic_out_of_office,
|
|
|
|
)
|
2022-03-17 19:03:36 +01:00
|
|
|
from tests.utils import load_eml_file
|
2020-09-28 17:41:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_mailbox_from_mail_from(flask_client):
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c",
|
|
|
|
password="password",
|
|
|
|
name="Test User",
|
|
|
|
activated=True,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
alias = Alias.create(
|
|
|
|
user_id=user.id,
|
|
|
|
email="first@d1.test",
|
|
|
|
mailbox_id=user.default_mailbox_id,
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
mb = get_mailbox_from_mail_from("a@b.c", alias)
|
|
|
|
assert mb.email == "a@b.c"
|
|
|
|
|
|
|
|
mb = get_mailbox_from_mail_from("unauthorized@gmail.com", alias)
|
|
|
|
assert mb is None
|
|
|
|
|
|
|
|
# authorized address
|
|
|
|
AuthorizedAddress.create(
|
|
|
|
user_id=user.id,
|
|
|
|
mailbox_id=user.default_mailbox_id,
|
|
|
|
email="unauthorized@gmail.com",
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
mb = get_mailbox_from_mail_from("unauthorized@gmail.com", alias)
|
|
|
|
assert mb.email == "a@b.c"
|
2021-06-22 17:52:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_ignore(flask_client):
|
|
|
|
assert should_ignore("mail_from", []) is False
|
|
|
|
|
|
|
|
assert not should_ignore("mail_from", ["rcpt_to"])
|
|
|
|
IgnoredEmail.create(mail_from="mail_from", rcpt_to="rcpt_to", commit=True)
|
|
|
|
assert should_ignore("mail_from", ["rcpt_to"])
|
2022-01-04 18:06:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_automatic_out_of_office():
|
|
|
|
msg = EmailMessage()
|
|
|
|
assert not is_automatic_out_of_office(msg)
|
|
|
|
|
2022-01-05 15:21:54 +01:00
|
|
|
msg[headers.AUTO_SUBMITTED] = "auto-replied"
|
2022-01-04 18:06:08 +01:00
|
|
|
assert is_automatic_out_of_office(msg)
|
|
|
|
|
2022-01-05 15:21:54 +01:00
|
|
|
del msg[headers.AUTO_SUBMITTED]
|
2022-01-04 18:06:08 +01:00
|
|
|
assert not is_automatic_out_of_office(msg)
|
|
|
|
|
2022-01-05 15:21:54 +01:00
|
|
|
msg[headers.AUTO_SUBMITTED] = "auto-generated"
|
2022-01-04 18:06:08 +01:00
|
|
|
assert is_automatic_out_of_office(msg)
|
2022-03-17 19:03:36 +01:00
|
|
|
|
2022-03-17 21:36:25 +01:00
|
|
|
|
2022-03-17 19:03:36 +01:00
|
|
|
def test_process_spoofed():
|
|
|
|
msg = load_eml_file("gmail_spoof.eml")
|
|
|
|
breakpoint()
|
2022-03-17 21:36:25 +01:00
|
|
|
a = msg["a"]
|
|
|
|
b = 1
|
|
|
|
c = 2
|