From a8b98ea18dd6140668289a4a1d9ee771584985ef Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Sat, 23 Mar 2024 21:20:23 +0530 Subject: [PATCH 1/6] adding a unit test --- module_testing.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 module_testing.py diff --git a/module_testing.py b/module_testing.py new file mode 100644 index 0000000..80f9545 --- /dev/null +++ b/module_testing.py @@ -0,0 +1,49 @@ +import unittest +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.SMTP_SSL') + def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): + # Arrange + msg_task = "send_vpn_profile" + profile_name = "test_profile" + msg_addr = "test@example.com" + 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.SMTP_SSL') + def test_send_mime_msg_via_email_failure(self, mock_smtp_ssl): + # Arrange + msg_task = "send_vpn_profile" + profile_name = "test_profile" + msg_addr = "test@example.com" + 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() From d95a9fd25116fd380bf92183544d5436f42607b7 Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 27 Mar 2024 22:37:39 +0530 Subject: [PATCH 2/6] Changing the folder structure to follow the naming convention --- tests/test_mod_comm_send.py | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test_mod_comm_send.py diff --git a/tests/test_mod_comm_send.py b/tests/test_mod_comm_send.py new file mode 100644 index 0000000..80f9545 --- /dev/null +++ b/tests/test_mod_comm_send.py @@ -0,0 +1,49 @@ +import unittest +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.SMTP_SSL') + def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): + # Arrange + msg_task = "send_vpn_profile" + profile_name = "test_profile" + msg_addr = "test@example.com" + 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.SMTP_SSL') + def test_send_mime_msg_via_email_failure(self, mock_smtp_ssl): + # Arrange + msg_task = "send_vpn_profile" + profile_name = "test_profile" + msg_addr = "test@example.com" + 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() From 5e5d67285135ef2edd4e6b19196ebc8a4d30b62f Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 27 Mar 2024 22:38:34 +0530 Subject: [PATCH 3/6] deleting the redundant file --- module_testing.py | 49 ----------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 module_testing.py diff --git a/module_testing.py b/module_testing.py deleted file mode 100644 index 80f9545..0000000 --- a/module_testing.py +++ /dev/null @@ -1,49 +0,0 @@ -import unittest -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.SMTP_SSL') - def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): - # Arrange - msg_task = "send_vpn_profile" - profile_name = "test_profile" - msg_addr = "test@example.com" - 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.SMTP_SSL') - def test_send_mime_msg_via_email_failure(self, mock_smtp_ssl): - # Arrange - msg_task = "send_vpn_profile" - profile_name = "test_profile" - msg_addr = "test@example.com" - 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() From 1de1b3372cd8264cba9fead108db8c46a02070cd Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 27 Mar 2024 22:46:27 +0530 Subject: [PATCH 4/6] Added the proper comments for each test case --- tests/test_mod_comm_send.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_mod_comm_send.py b/tests/test_mod_comm_send.py index 80f9545..23ba7f2 100644 --- a/tests/test_mod_comm_send.py +++ b/tests/test_mod_comm_send.py @@ -6,7 +6,7 @@ class TestSendMimeMsgViaEmail(unittest.TestCase): @patch('mod_comm_send.SMTP_SSL') def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): - # Arrange + """Test sending a MIME message via email successfully.""" msg_task = "send_vpn_profile" profile_name = "test_profile" msg_addr = "test@example.com" @@ -27,7 +27,7 @@ def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): @patch('mod_comm_send.SMTP_SSL') def test_send_mime_msg_via_email_failure(self, mock_smtp_ssl): - # Arrange + """Test failure scenario when sending a MIME message via email.""" msg_task = "send_vpn_profile" profile_name = "test_profile" msg_addr = "test@example.com" From 955bb40b2a867e9f55aedad7f9b36b16ebaff7ba Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Thu, 28 Mar 2024 09:22:15 +0530 Subject: [PATCH 5/6] Path handling of the module and a change in import of mod_comm_send.py file --- mod_comm_send/mod_comm_send.py | 2 +- tests/test_mod_comm_send.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mod_comm_send/mod_comm_send.py b/mod_comm_send/mod_comm_send.py index f12749b..e7af552 100644 --- a/mod_comm_send/mod_comm_send.py +++ b/mod_comm_send/mod_comm_send.py @@ -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): diff --git a/tests/test_mod_comm_send.py b/tests/test_mod_comm_send.py index 23ba7f2..020f5cf 100644 --- a/tests/test_mod_comm_send.py +++ b/tests/test_mod_comm_send.py @@ -1,9 +1,12 @@ 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): +class TestSendMimeMsgViaEmail(unittest.TestCase): @patch('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.""" From 5fddf0f6f7c6021fad9bea3f4cef5907028a29a2 Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Thu, 28 Mar 2024 09:25:58 +0530 Subject: [PATCH 6/6] Handled the attribute error --- tests/test_mod_comm_send.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_mod_comm_send.py b/tests/test_mod_comm_send.py index 020f5cf..14116ba 100644 --- a/tests/test_mod_comm_send.py +++ b/tests/test_mod_comm_send.py @@ -7,7 +7,7 @@ class TestSendMimeMsgViaEmail(unittest.TestCase): - @patch('mod_comm_send.SMTP_SSL') + @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" @@ -28,7 +28,7 @@ def test_send_mime_msg_via_email_success(self, mock_smtp_ssl): 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.SMTP_SSL') + @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"