From 0ad53ad260b31db91041ebddfe20cbc88ed83728 Mon Sep 17 00:00:00 2001 From: Stepan Henek Date: Tue, 13 Feb 2024 09:39:29 +0100 Subject: [PATCH] chore: remove NtfyBackendExponentialRateLimitBackend and SmtpExponentialRateLimitEmailBackend They can be easily replaced by ExponentialRateLimitBackends where the list of backends is NtfyBackend or Emailbackend --- django_ntfy/__init__.py | 9 -------- tests/conftest.py | 12 ----------- tests/test_ntfy_backend.py | 6 +++++- tests/test_smtp_backend.py | 43 -------------------------------------- 4 files changed, 5 insertions(+), 65 deletions(-) delete mode 100644 tests/test_smtp_backend.py diff --git a/django_ntfy/__init__.py b/django_ntfy/__init__.py index 507712a..3c1eac3 100644 --- a/django_ntfy/__init__.py +++ b/django_ntfy/__init__.py @@ -7,7 +7,6 @@ from django.core.cache import cache from django.core.mail import EmailMessage from django.core.mail.backends.base import BaseEmailBackend -from django.core.mail.backends.smtp import EmailBackend from django.utils.module_loading import import_string from django.utils.text import slugify @@ -149,14 +148,6 @@ def send_messages(self, email_messages: typing.List[EmailMessage]): return count -class NtfyBackendExponentialRateLimitBackend(ExponentialRateLimitMixin, NtfyBackend): - pass - - -class SmtpExponentialRateLimitEmailBackend(ExponentialRateLimitMixin, EmailBackend): - pass - - class ExponentialRateLimitBackends(BaseEmailBackend): def __init__(self, *args, **kwargs): backend_strings = getattr( diff --git a/tests/conftest.py b/tests/conftest.py index 852604c..0be0b23 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,18 +9,6 @@ def use_ntfy_backend(settings): settings.EMAIL_BACKEND = 'django_ntfy.NtfyBackend' -@pytest.fixture -def use_smtp_exponential_ratelimit_backend(settings): - # we need to explicitly override it here - settings.EMAIL_BACKEND = 'django_ntfy.SmtpExponentialRateLimitEmailBackend' - - -@pytest.fixture -def use_ntfy_exponential_ratelimit_backend(settings): - # we need to explicitly override it here - settings.EMAIL_BACKEND = 'django_ntfy.NtfyBackendExponentialRateLimitBackend' - - @pytest.fixture def use_exponential_ratelimit_backends(settings): # we need to explicitly override it here diff --git a/tests/test_ntfy_backend.py b/tests/test_ntfy_backend.py index 361b08e..fcef516 100644 --- a/tests/test_ntfy_backend.py +++ b/tests/test_ntfy_backend.py @@ -247,7 +247,11 @@ def test_priority_signal(settings, use_ntfy_backend, priority_signal): assert mail.send_mail("Sub", "Body", "from@example.com", ["to@example.com"]) == 1 -def test_exponential_rate_limit(settings, use_ntfy_exponential_ratelimit_backend, clear_cache): +def test_exponential_rate_limit(settings, use_exponential_ratelimit_backends, clear_cache): + settings.EMAIL_EXPONENTIAL_RATE_LIMIT_BACKENDS = [ + 'django_ntfy.NtfyBackend', + ] + def check(sent: bool, subject_suffix: str): if sent: with responses.RequestsMock() as rsps: diff --git a/tests/test_smtp_backend.py b/tests/test_smtp_backend.py deleted file mode 100644 index a9b5d24..0000000 --- a/tests/test_smtp_backend.py +++ /dev/null @@ -1,43 +0,0 @@ -from django.core import mail - -from django_ntfy import EmailBackend - - -def test_exponential_rate_limit( - settings, use_smtp_exponential_ratelimit_backend, monkeypatch, clear_cache -): - def check(sent: bool, subject_suffix: str): - tmp = {"called": False} - - def hook(self, email_messages): - assert len(email_messages) == 1 - assert email_messages[0].subject == "Sub" + subject_suffix - tmp["called"] = True - return 1 - - with monkeypatch.context() as m: - m.setattr(EmailBackend, 'send_messages', hook) - - if sent: - assert mail.send_mail("Sub", "Body", "from@example.com", ["to@example.com"]) == 1 - assert tmp["called"] is True - else: - assert mail.send_mail("Sub", "Body", "from@example.com", ["to@example.com"]) == 0 - assert tmp["called"] is False - - check(True, '') - check(True, '') - check(False, '') - check(True, ' (2)') - check(False, '') - check(False, '') - check(False, '') - check(True, ' (4)') - check(False, '') - check(False, '') - check(False, '') - check(False, '') - check(False, '') - check(False, '') - check(False, '') - check(True, ' (8)')