authentik.stages.email.tests.test_management_commands
Test email management commands
1"""Test email management commands""" 2 3from unittest.mock import MagicMock, 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_uses_stage_settings(self): 53 """Test test_email command uses the stage's settings, not the global ones""" 54 EmailStage.objects.create( 55 name="test-stage", 56 use_global_settings=False, 57 from_address="custom@authentik.local", 58 host="stage.authentik.local", 59 port=587, 60 username="stage-user", 61 password="stage-password", # nosec 62 use_tls=True, 63 ) 64 65 backend_class = MagicMock(wraps=EmailBackend) 66 with patch("authentik.stages.email.models.EmailStage.backend_class", backend_class): 67 call_command("test_email", "test@example.com", stage="test-stage") 68 69 self.assertEqual(len(mail.outbox), 1) 70 self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local") 71 self.assertEqual( 72 backend_class.call_args.kwargs, 73 { 74 "host": "stage.authentik.local", 75 "port": 587, 76 "username": "stage-user", 77 "password": "stage-password", 78 "use_tls": True, 79 "use_ssl": False, 80 "timeout": 10, 81 }, 82 )
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_uses_stage_settings(self): 54 """Test test_email command uses the stage's settings, not the global ones""" 55 EmailStage.objects.create( 56 name="test-stage", 57 use_global_settings=False, 58 from_address="custom@authentik.local", 59 host="stage.authentik.local", 60 port=587, 61 username="stage-user", 62 password="stage-password", # nosec 63 use_tls=True, 64 ) 65 66 backend_class = MagicMock(wraps=EmailBackend) 67 with patch("authentik.stages.email.models.EmailStage.backend_class", backend_class): 68 call_command("test_email", "test@example.com", stage="test-stage") 69 70 self.assertEqual(len(mail.outbox), 1) 71 self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local") 72 self.assertEqual( 73 backend_class.call_args.kwargs, 74 { 75 "host": "stage.authentik.local", 76 "port": 587, 77 "username": "stage-user", 78 "password": "stage-password", 79 "use_tls": True, 80 "use_ssl": False, 81 "timeout": 10, 82 }, 83 )
Test email management commands
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_uses_stage_settings(self):
53 def test_test_email_command_uses_stage_settings(self): 54 """Test test_email command uses the stage's settings, not the global ones""" 55 EmailStage.objects.create( 56 name="test-stage", 57 use_global_settings=False, 58 from_address="custom@authentik.local", 59 host="stage.authentik.local", 60 port=587, 61 username="stage-user", 62 password="stage-password", # nosec 63 use_tls=True, 64 ) 65 66 backend_class = MagicMock(wraps=EmailBackend) 67 with patch("authentik.stages.email.models.EmailStage.backend_class", backend_class): 68 call_command("test_email", "test@example.com", stage="test-stage") 69 70 self.assertEqual(len(mail.outbox), 1) 71 self.assertEqual(mail.outbox[0].from_email, "custom@authentik.local") 72 self.assertEqual( 73 backend_class.call_args.kwargs, 74 { 75 "host": "stage.authentik.local", 76 "port": 587, 77 "username": "stage-user", 78 "password": "stage-password", 79 "use_tls": True, 80 "use_ssl": False, 81 "timeout": 10, 82 }, 83 )
Test test_email command uses the stage's settings, not the global ones