Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for AI VPN module: mod_comm_send #58

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mod_comm_send/mod_comm_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from email.mime.text import MIMEText
from email.encoders import encode_base64
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from telegram.ext import MessageHandler, filters
from telegram.ext import Updater

def send_mime_msg_via_email(msg_task,profile_name,msg_addr,msg_vpn_type,config):
Expand Down
52 changes: 52 additions & 0 deletions tests/test_mod_comm_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from unittest.mock import MagicMock, patch
from mod_comm_send.mod_comm_send import send_mime_msg_via_email


class TestSendMimeMsgViaEmail(unittest.TestCase):
@patch('mod_comm_send.mod_comm_send.SMTP_SSL')
def test_send_mime_msg_via_email_success(self, mock_smtp_ssl):
"""Test sending a MIME message via email successfully."""
msg_task = "send_vpn_profile"
profile_name = "test_profile"
msg_addr = "[email protected]"
msg_vpn_type = "wireguard"
config = {
'IMAP': {'SERVER': 'test_server', 'USERNAME': 'test_user', 'PASSWORD': 'test_password'},
'AIVPN': {'MESSAGE_SUBJECT_PREFIX': 'Test Subject', 'MESSAGE_NEW_PROFILE': 'Test Message'},
'STORAGE': {'PATH': '/path/to/storage'}
}

# Act
result = send_mime_msg_via_email(msg_task, profile_name, msg_addr, msg_vpn_type, config)

# Assert
self.assertTrue(result)
mock_smtp_ssl.return_value.login.assert_called_once_with('test_user', 'test_password')
mock_smtp_ssl.return_value.sendmail.assert_called_once()

@patch('mod_comm_send.mod_comm_send.SMTP_SSL')
def test_send_mime_msg_via_email_failure(self, mock_smtp_ssl):
"""Test failure scenario when sending a MIME message via email."""
msg_task = "send_vpn_profile"
profile_name = "test_profile"
msg_addr = "[email protected]"
msg_vpn_type = "wireguard"
config = {
'IMAP': {'SERVER': 'test_server', 'USERNAME': 'test_user', 'PASSWORD': 'test_password'},
'AIVPN': {'MESSAGE_SUBJECT_PREFIX': 'Test Subject', 'MESSAGE_NEW_PROFILE': 'Test Message'},
'STORAGE': {'PATH': '/path/to/storage'}
}
mock_smtp_ssl.side_effect = Exception("SMTP Error")

# Act
result = send_mime_msg_via_email(msg_task, profile_name, msg_addr, msg_vpn_type, config)

# Assert
self.assertFalse(result)

if __name__ == '__main__':
unittest.main()