238 lines
8.1 KiB
Python
238 lines
8.1 KiB
Python
"""
|
|
Unit tests for email service functionality.
|
|
"""
|
|
|
|
import unittest
|
|
import os
|
|
import sys
|
|
from unittest.mock import patch, MagicMock, mock_open
|
|
|
|
# Add the util module to the Python path
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'util'))
|
|
|
|
from util.email.email_service import (
|
|
EmailService,
|
|
send_credential_notification,
|
|
send_user_guide,
|
|
send_log_files,
|
|
send_vpn_config
|
|
)
|
|
|
|
|
|
class TestEmailService(unittest.TestCase):
|
|
"""Test email service functionality."""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures."""
|
|
self.email_service = EmailService()
|
|
|
|
@patch('smtplib.SMTP')
|
|
def test_send_email_success(self, mock_smtp_class):
|
|
"""Test successful email sending."""
|
|
# Mock SMTP connection
|
|
mock_server = MagicMock()
|
|
mock_smtp_class.return_value = mock_server
|
|
|
|
# Test sending email
|
|
result = self.email_service.send_email(
|
|
to_email='test@example.com',
|
|
subject='Test Subject',
|
|
body='<p>Test body</p>'
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_smtp_class.assert_called_once_with('smtp.gmail.com', 587)
|
|
mock_server.starttls.assert_called_once()
|
|
mock_server.login.assert_called_once()
|
|
mock_server.sendmail.assert_called_once()
|
|
mock_server.quit.assert_called_once()
|
|
|
|
@patch('smtplib.SMTP')
|
|
def test_send_email_with_attachments(self, mock_smtp_class):
|
|
"""Test email sending with file attachments."""
|
|
# Mock SMTP connection
|
|
mock_server = MagicMock()
|
|
mock_smtp_class.return_value = mock_server
|
|
|
|
# Create a temporary test file
|
|
test_file = '/tmp/test_attachment.txt'
|
|
with open(test_file, 'w') as f:
|
|
f.write('test content')
|
|
|
|
try:
|
|
# Test sending email with attachment
|
|
result = self.email_service.send_email(
|
|
to_email='test@example.com',
|
|
subject='Test Subject',
|
|
body='<p>Test body</p>',
|
|
attachments=[test_file]
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_server.sendmail.assert_called_once()
|
|
finally:
|
|
# Clean up
|
|
if os.path.exists(test_file):
|
|
os.remove(test_file)
|
|
|
|
@patch('smtplib.SMTP')
|
|
def test_send_email_failure(self, mock_smtp_class):
|
|
"""Test email sending failure."""
|
|
# Mock SMTP to raise exception
|
|
mock_smtp_class.side_effect = Exception("SMTP error")
|
|
|
|
# Test sending email
|
|
result = self.email_service.send_email(
|
|
to_email='test@example.com',
|
|
subject='Test Subject',
|
|
body='<p>Test body</p>'
|
|
)
|
|
|
|
self.assertFalse(result)
|
|
|
|
@patch('tests.test_email.EmailService.send_email')
|
|
def test_send_credential_notification(self, mock_send_email):
|
|
"""Test sending credential notification."""
|
|
mock_send_email.return_value = True
|
|
|
|
result = send_credential_notification(
|
|
to_email='user@example.com',
|
|
username='testuser',
|
|
temp_password='temppass123',
|
|
vpn_config_url='http://example.com/config'
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_send_email.assert_called_once()
|
|
args, kwargs = mock_send_email.call_args
|
|
self.assertEqual(args[0], 'user@example.com') # to_email
|
|
self.assertEqual(args[1], 'Your VPN Access Credentials') # subject
|
|
self.assertIn('testuser', args[2]) # body
|
|
self.assertIn('temppass123', args[2]) # body
|
|
self.assertIn('http://example.com/config', args[2]) # body
|
|
|
|
@patch('tests.test_email.EmailService.send_email')
|
|
def test_send_user_guide(self, mock_send_email):
|
|
"""Test sending user guide with attachments."""
|
|
mock_send_email.return_value = True
|
|
|
|
# Create a temporary test file
|
|
test_file = '/tmp/test_guide.pdf'
|
|
with open(test_file, 'w') as f:
|
|
f.write('test guide content')
|
|
|
|
try:
|
|
result = send_user_guide(
|
|
to_email='user@example.com',
|
|
username='testuser',
|
|
guide_attachments=[test_file]
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_send_email.assert_called_once()
|
|
args, kwargs = mock_send_email.call_args
|
|
self.assertEqual(args[0], 'user@example.com') # to_email
|
|
self.assertEqual(args[1], 'VPN Access User Guide and Materials') # subject
|
|
self.assertEqual(args[3], [test_file]) # attachments
|
|
finally:
|
|
# Clean up
|
|
if os.path.exists(test_file):
|
|
os.remove(test_file)
|
|
|
|
@patch('tests.test_email.EmailService.send_email')
|
|
def test_send_log_files(self, mock_send_email):
|
|
"""Test sending log files."""
|
|
mock_send_email.return_value = True
|
|
|
|
# Create a temporary test log file
|
|
test_log = '/tmp/test_log.txt'
|
|
with open(test_log, 'w') as f:
|
|
f.write('test log content')
|
|
|
|
try:
|
|
result = send_log_files(
|
|
to_email='admin@example.com',
|
|
username='admin',
|
|
log_files=[test_log],
|
|
date_range='2024-01-01 to 2024-01-31'
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_send_email.assert_called_once()
|
|
args, kwargs = mock_send_email.call_args
|
|
self.assertEqual(args[0], 'admin@example.com') # to_email
|
|
self.assertEqual(args[1], 'VPN Access Server Log Files') # subject
|
|
self.assertEqual(args[3], [test_log]) # attachments
|
|
self.assertIn('2024-01-01 to 2024-01-31', args[2]) # body
|
|
finally:
|
|
# Clean up
|
|
if os.path.exists(test_log):
|
|
os.remove(test_log)
|
|
|
|
@patch('tests.test_email.EmailService.send_email')
|
|
def test_send_vpn_config(self, mock_send_email):
|
|
"""Test sending VPN configuration and user guide."""
|
|
mock_send_email.return_value = True
|
|
|
|
# Create temporary files
|
|
vpn_config = '/tmp/test_config.ovpn'
|
|
user_guide = '/tmp/test_guide.pptx'
|
|
|
|
with open(vpn_config, 'w') as f:
|
|
f.write('client config content')
|
|
with open(user_guide, 'w') as f:
|
|
f.write('guide content')
|
|
|
|
try:
|
|
result = send_vpn_config(
|
|
to_email='user@example.com',
|
|
username='testuser',
|
|
vpn_config_path=vpn_config,
|
|
user_guide_path=user_guide
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_send_email.assert_called_once()
|
|
args, kwargs = mock_send_email.call_args
|
|
self.assertEqual(args[0], 'user@example.com') # to_email
|
|
self.assertEqual(args[1], 'Your VPN Client Configuration and User Guide') # subject
|
|
self.assertEqual(args[3], [vpn_config, user_guide]) # attachments
|
|
self.assertIn('testuser', args[2]) # body contains username
|
|
finally:
|
|
# Clean up
|
|
for file_path in [vpn_config, user_guide]:
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
@patch('tests.test_email.EmailService.send_email')
|
|
def test_send_vpn_config_without_user_guide(self, mock_send_email):
|
|
"""Test sending VPN configuration without user guide."""
|
|
mock_send_email.return_value = True
|
|
|
|
# Create temporary VPN config file
|
|
vpn_config = '/tmp/test_config.ovpn'
|
|
|
|
with open(vpn_config, 'w') as f:
|
|
f.write('client config content')
|
|
|
|
try:
|
|
result = send_vpn_config(
|
|
to_email='user@example.com',
|
|
username='testuser',
|
|
vpn_config_path=vpn_config,
|
|
user_guide_path=None # No user guide
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
mock_send_email.assert_called_once()
|
|
args, kwargs = mock_send_email.call_args
|
|
self.assertEqual(args[3], [vpn_config]) # Only VPN config attached
|
|
finally:
|
|
# Clean up
|
|
if os.path.exists(vpn_config):
|
|
os.remove(vpn_config)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|