mirror of
https://github.com/simple-login/app.git
synced 2025-02-25 00:03:03 +08:00
Improve should_disable() to take into account last 7 days bounces
This commit is contained in:
parent
0d6338b525
commit
da53b7fa00
2 changed files with 61 additions and 3 deletions
|
@ -940,8 +940,33 @@ def should_disable(alias: Alias) -> bool:
|
|||
.filter(Contact.alias_id == alias.id)
|
||||
.count()
|
||||
)
|
||||
if nb_bounced_last_24h > 5:
|
||||
# if more than 10 bounces in 24h -> disable alias
|
||||
if nb_bounced_last_24h > 10:
|
||||
LOG.debug("more than 10 bounces in the last 24h on %s", alias)
|
||||
return True
|
||||
|
||||
# if between 5-10 bounces but has bounces last week -> disable alias
|
||||
elif nb_bounced_last_24h > 5:
|
||||
one_week_ago = arrow.now().shift(days=-8)
|
||||
nb_bounced_7d_1d = (
|
||||
db.session.query(EmailLog)
|
||||
.join(Contact, EmailLog.contact_id == Contact.id)
|
||||
.filter(
|
||||
EmailLog.bounced.is_(True),
|
||||
EmailLog.is_reply.is_(False),
|
||||
EmailLog.created_at > one_week_ago,
|
||||
EmailLog.created_at < yesterday,
|
||||
)
|
||||
.filter(Contact.alias_id == alias.id)
|
||||
.count()
|
||||
)
|
||||
if nb_bounced_7d_1d > 1:
|
||||
LOG.debug(
|
||||
"more than 5 bounces in the last 24h and more than 1 bounces in the last 7 days on %s",
|
||||
alias,
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import email
|
||||
from email.message import EmailMessage
|
||||
|
||||
import arrow
|
||||
|
||||
from app.config import MAX_ALERT_24H, EMAIL_DOMAIN, BOUNCE_EMAIL
|
||||
from app.email_utils import (
|
||||
get_email_domain_part,
|
||||
|
@ -28,9 +30,8 @@ from app.email_utils import (
|
|||
)
|
||||
from app.extensions import db
|
||||
from app.models import User, CustomDomain, Alias, Contact, EmailLog
|
||||
|
||||
|
||||
# flake8: noqa: E101, W191
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_get_email_domain_part():
|
||||
|
@ -618,6 +619,38 @@ def test_should_disable(flask_client):
|
|||
assert not should_disable(alias2)
|
||||
|
||||
|
||||
def test_should_disable_bounce_consecutive_days(flask_client):
|
||||
user = login(flask_client)
|
||||
alias = Alias.create_new_random(user)
|
||||
db.session.commit()
|
||||
|
||||
contact = Contact.create(
|
||||
user_id=user.id,
|
||||
alias_id=alias.id,
|
||||
website_email="contact@example.com",
|
||||
reply_email="rep@sl.local",
|
||||
commit=True,
|
||||
)
|
||||
|
||||
# create 6 bounce on this alias in the last 24h: alias is not disabled
|
||||
for _ in range(6):
|
||||
EmailLog.create(
|
||||
user_id=user.id, contact_id=contact.id, commit=True, bounced=True
|
||||
)
|
||||
assert not should_disable(alias)
|
||||
|
||||
# create 2 bounces in the last 7 days: alias should be disabled
|
||||
for _ in range(2):
|
||||
EmailLog.create(
|
||||
user_id=user.id,
|
||||
contact_id=contact.id,
|
||||
commit=True,
|
||||
bounced=True,
|
||||
created_at=arrow.now().shift(days=-3),
|
||||
)
|
||||
assert should_disable(alias)
|
||||
|
||||
|
||||
def test_parse_email_log_id_from_bounce():
|
||||
assert parse_email_log_id_from_bounce("bounces+1234+@local") == 1234
|
||||
assert parse_email_log_id_from_bounce(BOUNCE_EMAIL.format(1234)) == 1234
|
||||
|
|
Loading…
Reference in a new issue