2020-04-04 17:22:27 +02:00
|
|
|
from app.config import (
|
|
|
|
MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS,
|
|
|
|
MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX,
|
|
|
|
)
|
2020-04-04 16:27:22 +02:00
|
|
|
from app.extensions import db
|
2021-06-24 09:47:01 +02:00
|
|
|
from app.email.rate_limit import (
|
|
|
|
rate_limited_forward_phase,
|
|
|
|
rate_limited_for_alias,
|
|
|
|
rate_limited_for_mailbox,
|
|
|
|
rate_limited_reply_phase,
|
2020-04-04 16:27:22 +02:00
|
|
|
)
|
|
|
|
from app.models import User, Alias, EmailLog, Contact
|
|
|
|
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
def test_rate_limited_forward_phase_for_alias(flask_client):
|
2020-04-04 16:27:22 +02:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
# no rate limiting for a new alias
|
2020-04-04 16:27:22 +02:00
|
|
|
alias = Alias.create_new_random(user)
|
|
|
|
db.session.commit()
|
2021-06-24 09:47:01 +02:00
|
|
|
assert not rate_limited_for_alias(alias)
|
2020-04-04 16:27:22 +02:00
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
# rate limit when there's a previous activity on alias
|
2020-04-04 16:27:22 +02:00
|
|
|
contact = Contact.create(
|
|
|
|
user_id=user.id,
|
|
|
|
alias_id=alias.id,
|
|
|
|
website_email="contact@example.com",
|
|
|
|
reply_email="rep@sl.local",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
2020-04-04 17:22:27 +02:00
|
|
|
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS + 1):
|
2021-07-11 12:28:23 +02:00
|
|
|
EmailLog.create(
|
|
|
|
user_id=user.id,
|
|
|
|
contact_id=contact.id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-04-04 17:22:27 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
assert rate_limited_for_alias(alias)
|
2020-04-04 16:27:22 +02:00
|
|
|
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
def test_rate_limited_forward_phase_for_mailbox(flask_client):
|
2020-04-04 16:27:22 +02:00
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
2020-04-04 17:22:27 +02:00
|
|
|
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_MAILBOX + 1):
|
2021-07-11 12:28:23 +02:00
|
|
|
EmailLog.create(
|
|
|
|
user_id=user.id,
|
|
|
|
contact_id=contact.id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-04-04 17:22:27 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2021-07-11 12:28:23 +02:00
|
|
|
EmailLog.create(
|
|
|
|
user_id=user.id,
|
|
|
|
contact_id=contact.id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-04-04 16:27:22 +02:00
|
|
|
|
|
|
|
# Create another alias with the same mailbox
|
2021-06-24 09:47:01 +02:00
|
|
|
# will be rate limited as there's a previous activity on mailbox
|
2020-04-04 17:22:27 +02:00
|
|
|
alias2 = Alias.create_new_random(user)
|
2020-04-04 16:27:22 +02:00
|
|
|
db.session.commit()
|
2021-06-24 09:47:01 +02:00
|
|
|
assert rate_limited_for_mailbox(alias2)
|
2020-04-04 16:27:22 +02:00
|
|
|
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
def test_rate_limited_forward_phase(flask_client):
|
|
|
|
# no rate limiting when alias does not exist
|
|
|
|
assert not rate_limited_forward_phase("not-exist@alias.com")
|
2020-04-04 16:27:22 +02:00
|
|
|
|
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
def test_rate_limited_reply_phase(flask_client):
|
|
|
|
# no rate limiting when reply_email does not exist
|
|
|
|
assert not rate_limited_reply_phase("not-exist-reply@alias.com")
|
2020-04-04 16:27:22 +02:00
|
|
|
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
db.session.commit()
|
2020-04-04 17:22:27 +02:00
|
|
|
for _ in range(MAX_ACTIVITY_DURING_MINUTE_PER_ALIAS + 1):
|
2021-07-11 12:28:23 +02:00
|
|
|
EmailLog.create(
|
|
|
|
user_id=user.id,
|
|
|
|
contact_id=contact.id,
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
)
|
2020-04-04 17:22:27 +02:00
|
|
|
db.session.commit()
|
2020-04-04 16:27:22 +02:00
|
|
|
|
2021-06-24 09:47:01 +02:00
|
|
|
assert rate_limited_reply_phase("rep@sl.local")
|