authentik.stages.email.tests.test_management_commands

Test email management commands

 1"""Test email management commands"""
 2
 3from unittest.mock import patch
 4
 5from django.core import mail
 6from django.core.mail.backends.locmem import EmailBackend
 7from django.core.management import call_command
 8from django.test import TestCase
 9
10from authentik.core.tests.utils import create_test_admin_user
11from authentik.stages.email.models import EmailStage
12
13
14class TestEmailManagementCommands(TestCase):
15    """Test email management commands"""
16
17    def setUp(self):
18        self.user = create_test_admin_user()
19
20    def test_test_email_command_with_stage(self):
21        """Test test_email command with specified stage"""
22        EmailStage.objects.create(
23            name="test-stage",
24            from_address="test@authentik.local",
25            host="localhost",
26            port=25,
27        )
28
29        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
30            call_command("test_email", "test@example.com", stage="test-stage")
31
32            self.assertEqual(len(mail.outbox), 1)
33            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
34            self.assertEqual(mail.outbox[0].to, ["test@example.com"])
35
36    def test_test_email_command_with_global_settings(self):
37        """Test test_email command with global settings"""
38        # Mock the backend to use Django's locmem backend
39        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
40            call_command("test_email", "test@example.com")
41
42            self.assertEqual(len(mail.outbox), 1)
43            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
44            self.assertEqual(mail.outbox[0].to, ["test@example.com"])
45
46    def test_test_email_command_invalid_stage(self):
47        """Test test_email command with invalid stage"""
48        call_command("test_email", "test@example.com", stage="nonexistent")
49
50        self.assertEqual(len(mail.outbox), 0)
51
52    def test_test_email_command_with_custom_from(self):
53        """Test test_email command respects custom from address"""
54        EmailStage.objects.create(
55            name="test-stage",
56            from_address="custom@authentik.local",
57            host="localhost",
58            port=25,
59        )
60
61        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
62            call_command("test_email", "test@example.com", stage="test-stage")
63
64            self.assertEqual(len(mail.outbox), 1)
65            self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local")
66            self.assertEqual(mail.outbox[0].to, ["test@example.com"])
class TestEmailManagementCommands(django.test.testcases.TestCase):
15class TestEmailManagementCommands(TestCase):
16    """Test email management commands"""
17
18    def setUp(self):
19        self.user = create_test_admin_user()
20
21    def test_test_email_command_with_stage(self):
22        """Test test_email command with specified stage"""
23        EmailStage.objects.create(
24            name="test-stage",
25            from_address="test@authentik.local",
26            host="localhost",
27            port=25,
28        )
29
30        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
31            call_command("test_email", "test@example.com", stage="test-stage")
32
33            self.assertEqual(len(mail.outbox), 1)
34            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
35            self.assertEqual(mail.outbox[0].to, ["test@example.com"])
36
37    def test_test_email_command_with_global_settings(self):
38        """Test test_email command with global settings"""
39        # Mock the backend to use Django's locmem backend
40        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
41            call_command("test_email", "test@example.com")
42
43            self.assertEqual(len(mail.outbox), 1)
44            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
45            self.assertEqual(mail.outbox[0].to, ["test@example.com"])
46
47    def test_test_email_command_invalid_stage(self):
48        """Test test_email command with invalid stage"""
49        call_command("test_email", "test@example.com", stage="nonexistent")
50
51        self.assertEqual(len(mail.outbox), 0)
52
53    def test_test_email_command_with_custom_from(self):
54        """Test test_email command respects custom from address"""
55        EmailStage.objects.create(
56            name="test-stage",
57            from_address="custom@authentik.local",
58            host="localhost",
59            port=25,
60        )
61
62        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
63            call_command("test_email", "test@example.com", stage="test-stage")
64
65            self.assertEqual(len(mail.outbox), 1)
66            self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local")
67            self.assertEqual(mail.outbox[0].to, ["test@example.com"])

Test email management commands

def setUp(self):
18    def setUp(self):
19        self.user = create_test_admin_user()

Hook method for setting up the test fixture before exercising it.

def test_test_email_command_with_stage(self):
21    def test_test_email_command_with_stage(self):
22        """Test test_email command with specified stage"""
23        EmailStage.objects.create(
24            name="test-stage",
25            from_address="test@authentik.local",
26            host="localhost",
27            port=25,
28        )
29
30        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
31            call_command("test_email", "test@example.com", stage="test-stage")
32
33            self.assertEqual(len(mail.outbox), 1)
34            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
35            self.assertEqual(mail.outbox[0].to, ["test@example.com"])

Test test_email command with specified stage

def test_test_email_command_with_global_settings(self):
37    def test_test_email_command_with_global_settings(self):
38        """Test test_email command with global settings"""
39        # Mock the backend to use Django's locmem backend
40        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
41            call_command("test_email", "test@example.com")
42
43            self.assertEqual(len(mail.outbox), 1)
44            self.assertEqual(mail.outbox[0].subject, "authentik Test-Email")
45            self.assertEqual(mail.outbox[0].to, ["test@example.com"])

Test test_email command with global settings

def test_test_email_command_invalid_stage(self):
47    def test_test_email_command_invalid_stage(self):
48        """Test test_email command with invalid stage"""
49        call_command("test_email", "test@example.com", stage="nonexistent")
50
51        self.assertEqual(len(mail.outbox), 0)

Test test_email command with invalid stage

def test_test_email_command_with_custom_from(self):
53    def test_test_email_command_with_custom_from(self):
54        """Test test_email command respects custom from address"""
55        EmailStage.objects.create(
56            name="test-stage",
57            from_address="custom@authentik.local",
58            host="localhost",
59            port=25,
60        )
61
62        with patch("authentik.stages.email.models.EmailStage.backend_class", EmailBackend):
63            call_command("test_email", "test@example.com", stage="test-stage")
64
65            self.assertEqual(len(mail.outbox), 1)
66            self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local")
67            self.assertEqual(mail.outbox[0].to, ["test@example.com"])

Test test_email command respects custom from address