From 355b832cc33efcab35967c1f66d53a56fb08bdd7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 23 Nov 2021 13:04:14 +0100 Subject: [PATCH] tests/e2e: fix email backend Signed-off-by: Jens Langhammer --- authentik/stages/email/models.py | 11 ++++++++--- authentik/stages/email/tasks.py | 2 +- authentik/stages/email/tests/test_sending.py | 12 +++++++++--- authentik/stages/email/tests/test_stage.py | 17 ++++++++++++----- tests/e2e/test_flows_enroll.py | 2 -- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/authentik/stages/email/models.py b/authentik/stages/email/models.py index 7584d7190..9c96a8906 100644 --- a/authentik/stages/email/models.py +++ b/authentik/stages/email/models.py @@ -4,8 +4,8 @@ from pathlib import Path from typing import Type from django.conf import settings -from django.core.mail import get_connection from django.core.mail.backends.base import BaseEmailBackend +from django.core.mail.backends.smtp import EmailBackend from django.db import models from django.utils.translation import gettext as _ from django.views import View @@ -97,12 +97,17 @@ class EmailStage(Stage): def component(self) -> str: return "ak-stage-email-form" + @property + def backend_class(self) -> Type[BaseEmailBackend]: + """Get the email backend class to use""" + return EmailBackend + @property def backend(self) -> BaseEmailBackend: """Get fully configured Email Backend instance""" if self.use_global_settings: - return get_connection() - return get_connection( + return self.backend_class() + return self.backend_class( host=self.host, port=self.port, username=self.username, diff --git a/authentik/stages/email/tasks.py b/authentik/stages/email/tasks.py index 93cec2538..16be68f40 100644 --- a/authentik/stages/email/tasks.py +++ b/authentik/stages/email/tasks.py @@ -83,7 +83,7 @@ def send_mail(self: MonitoredTask, message: dict[Any, Any], email_stage_pk: Opti message_object.extra_headers["Message-ID"] = message_id LOGGER.debug("Sending mail", to=message_object.to) - stage.backend.send_messages([message_object]) + backend.send_messages([message_object]) Event.new( EventAction.EMAIL_SENT, message=(f"Email to {', '.join(message_object.to)} sent"), diff --git a/authentik/stages/email/tests/test_sending.py b/authentik/stages/email/tests/test_sending.py index 997940a8a..b5178bd9c 100644 --- a/authentik/stages/email/tests/test_sending.py +++ b/authentik/stages/email/tests/test_sending.py @@ -1,6 +1,6 @@ """email tests""" from smtplib import SMTPException -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, PropertyMock, patch from django.core import mail from django.core.mail.backends.locmem import EmailBackend @@ -42,7 +42,10 @@ class TestEmailStageSending(APITestCase): session.save() url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) - with self.settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend"): + with patch( + "authentik.stages.email.models.EmailStage.backend_class", + PropertyMock(return_value=EmailBackend), + ): response = self.client.post(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(mail.outbox), 1) @@ -64,7 +67,10 @@ class TestEmailStageSending(APITestCase): session.save() url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) - with self.settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend"): + with patch( + "authentik.stages.email.models.EmailStage.backend_class", + PropertyMock(return_value=EmailBackend), + ): with patch( "django.core.mail.backends.locmem.EmailBackend.send_messages", MagicMock(side_effect=[SMTPException, EmailBackend.send_messages]), diff --git a/authentik/stages/email/tests/test_stage.py b/authentik/stages/email/tests/test_stage.py index 6134b3ea7..cfced006d 100644 --- a/authentik/stages/email/tests/test_stage.py +++ b/authentik/stages/email/tests/test_stage.py @@ -1,7 +1,9 @@ """email tests""" -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, PropertyMock, patch from django.core import mail +from django.core.mail.backends.locmem import EmailBackend +from django.core.mail.backends.smtp import EmailBackend as SMTPEmailBackend from django.urls import reverse from django.utils.encoding import force_str from django.utils.http import urlencode @@ -67,7 +69,10 @@ class TestEmailStage(APITestCase): session.save() url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) - with self.settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend"): + with patch( + "authentik.stages.email.models.EmailStage.backend_class", + PropertyMock(return_value=EmailBackend), + ): response = self.client.post(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(mail.outbox), 1) @@ -76,10 +81,12 @@ class TestEmailStage(APITestCase): def test_use_global_settings(self): """Test use_global_settings""" host = "some-unique-string" - with self.settings( - EMAIL_HOST=host, EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend" + with patch( + "authentik.stages.email.models.EmailStage.backend_class", + PropertyMock(return_value=SMTPEmailBackend), ): - self.assertEqual(EmailStage(use_global_settings=True).backend.host, host) + with self.settings(EMAIL_HOST=host): + self.assertEqual(EmailStage(use_global_settings=True).backend.host, host) def test_token(self): """Test with token""" diff --git a/tests/e2e/test_flows_enroll.py b/tests/e2e/test_flows_enroll.py index 1266e5826..da6d2af72 100644 --- a/tests/e2e/test_flows_enroll.py +++ b/tests/e2e/test_flows_enroll.py @@ -4,7 +4,6 @@ from time import sleep from typing import Any, Optional from unittest.case import skipUnless -from django.test import override_settings from docker.types import Healthcheck from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as ec @@ -111,7 +110,6 @@ class TestFlowsEnroll(SeleniumTestCase): @apply_migration("authentik_core", "0002_auto_20200523_1133_squashed_0011_provider_name_temp") @apply_migration("authentik_flows", "0008_default_flows") @apply_migration("authentik_flows", "0011_flow_title") - @override_settings(EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend") def test_enroll_email(self): """Test enroll with Email verification""" # First stage fields