From 27f84f4bbeabd9113f359777b1cf587fce135c04 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Mon, 17 Apr 2023 13:00:36 -0500 Subject: [PATCH] test: add basic tests for notify ^ plugin settings --- .../check_course_due_date_emails_delivery.py | 3 - eox_nelp/notifications/tasks.py | 6 +- eox_nelp/notifications/tests/__init__.py | 0 eox_nelp/notifications/tests/test_tasks.py | 29 ++++++ eox_nelp/settings/common.py | 3 +- eox_nelp/settings/tests/__init__.py | 0 .../tests/test_common_plugin_settings.py | 95 +++++++++++++++++++ 7 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 eox_nelp/notifications/tests/__init__.py create mode 100644 eox_nelp/notifications/tests/test_tasks.py create mode 100644 eox_nelp/settings/tests/__init__.py create mode 100644 eox_nelp/settings/tests/test_common_plugin_settings.py diff --git a/eox_nelp/management/commands/check_course_due_date_emails_delivery.py b/eox_nelp/management/commands/check_course_due_date_emails_delivery.py index 488b24e4..f6a9d11b 100644 --- a/eox_nelp/management/commands/check_course_due_date_emails_delivery.py +++ b/eox_nelp/management/commands/check_course_due_date_emails_delivery.py @@ -5,9 +5,6 @@ from django.core.management import BaseCommand -from eox_nelp.notifications.tasks import send_course_due_date_emails - - logger = logging.getLogger(__name__) diff --git a/eox_nelp/notifications/tasks.py b/eox_nelp/notifications/tasks.py index 849fd542..4c67c69b 100644 --- a/eox_nelp/notifications/tasks.py +++ b/eox_nelp/notifications/tasks.py @@ -1,4 +1,6 @@ -# tasks.py +""" +Celery task related the notifications eox-nelp module. +""" import logging @@ -6,10 +8,10 @@ logger = logging.getLogger(__name__) + @shared_task def send_course_due_date_emails(): """ Task to send upcoming course due date emails. """ logger.info("------Sending upcoming course due date emails.-------") - logger.info("This is a log message. yeah yeah") diff --git a/eox_nelp/notifications/tests/__init__.py b/eox_nelp/notifications/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eox_nelp/notifications/tests/test_tasks.py b/eox_nelp/notifications/tests/test_tasks.py new file mode 100644 index 00000000..bf314a5d --- /dev/null +++ b/eox_nelp/notifications/tests/test_tasks.py @@ -0,0 +1,29 @@ +"""This file contains all the test for tasks.py file. + +Classes: + SendCourseDueDateEmailtTestCase: Test `send_course_due_date_emails` method. + +""" +import unittest + +from eox_nelp.notifications import tasks +from eox_nelp.notifications.tasks import send_course_due_date_emails + + +class SendCourseDueDateEmailtTestCase(unittest.TestCase): + """Test class for function `send_course_due_date_emails`""" + + def test_send_course_due_date_emails(self): + """Test when `send_course_due_date_emails` is called + with the required parameters. Check the functions inside are called with + their desired values. + + Expected behavior: + - Log error message. + """ + log_sent_email = "------Sending upcoming course due date emails.-------" + + with self.assertLogs(tasks.__name__, level="INFO") as logs: + send_course_due_date_emails() + + self.assertEqual(logs.output, [f"INFO:{tasks.__name__}:{log_sent_email}"]) diff --git a/eox_nelp/settings/common.py b/eox_nelp/settings/common.py index 69909f45..149af478 100644 --- a/eox_nelp/settings/common.py +++ b/eox_nelp/settings/common.py @@ -18,6 +18,7 @@ "eox_nelp.notifications.tasks", ) + def plugin_settings(settings): """ Defines eox-nelp settings when app is used as a plugin to edx-platform. @@ -35,7 +36,7 @@ def plugin_settings(settings): settings.FUTUREX_API_CLIENT_ID = 'my-test-client-id' settings.FUTUREX_API_CLIENT_SECRET = 'my-test-client-secret' - if COURSE_CREATOR_APP not in settings.INSTALLED_APPS: + if hasattr(settings, "INSTALLED_APPS") and COURSE_CREATOR_APP not in settings.INSTALLED_APPS: settings.INSTALLED_APPS.append(COURSE_CREATOR_APP) if getattr(settings, "CELERY_IMPORTS", None): diff --git a/eox_nelp/settings/tests/__init__.py b/eox_nelp/settings/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eox_nelp/settings/tests/test_common_plugin_settings.py b/eox_nelp/settings/tests/test_common_plugin_settings.py new file mode 100644 index 00000000..73617912 --- /dev/null +++ b/eox_nelp/settings/tests/test_common_plugin_settings.py @@ -0,0 +1,95 @@ +"""This file contains test for the `plugin_settings` method of +common setting of eox-nelp. + +Classes: + CommonPluginSettingsTestCase: Test `plugin_settings` method. + +""" +import unittest + +from eox_nelp.settings.common import COURSE_CREATOR_APP, EOX_NELP_CELERY_TASKS, plugin_settings + + +class SettingsClass: + """ dummy settings class """ + + +class CommonPluginSettingsTestCase(unittest.TestCase): + """Test class for function `plugin_settings`""" + + def test_base_common_plugin_settings(self): + """Test when `plugin_settings` is called + with the required parameters. Check the functions inside are called with + their desired values. + + Expected behavior: + - Eox-nelp setting items are presented. + """ + common_settings = SettingsClass() + eox_nelp_config = { + "EOX_NELP_COURSE_CREATORS_BACKEND": "eox_nelp.edxapp_wrapper.backends.course_creators_k_v1", + "EOX_NELP_COURSE_OVERVIEWS_BACKEND": "eox_nelp.edxapp_wrapper.backends.course_overviews_m_v1", + "EOX_NELP_SITE_CONFIGURATION": "eox_nelp.edxapp_wrapper.backends.site_configuration_m_v1", + "EOX_NELP_USER_API": "eox_nelp.edxapp_wrapper.backends.user_api_m_v1", + "EOX_NELP_USER_AUTHN": "eox_nelp.edxapp_wrapper.backends.user_authn_m_v1", + "EOX_NELP_MFE_CONFIG_VIEW": "eox_nelp.edxapp_wrapper.backends.mfe_config_view_m_v1", + "EOX_NELP_COURSE_API": "eox_nelp.edxapp_wrapper.backends.course_api_m_v1", + "FUTUREX_API_URL": "https://testing-site.com", + "FUTUREX_API_CLIENT_ID": "my-test-client-id", + "FUTUREX_API_CLIENT_SECRET": "my-test-client-secret", + } + + plugin_settings(common_settings) + + assert eox_nelp_config.items() <= common_settings.__dict__.items() + + def test_append_course_creator_app(self): + """Test when `plugin_settings` is called + append the course_creator apps in INSTALLED APPS. + + Expected behavior: + - Course creator app is presentend in INSTALLED_APPS. + """ + common_settings = SettingsClass() + setattr(common_settings, "INSTALLED_APPS", []) + + plugin_settings(common_settings) + + self.assertIn(COURSE_CREATOR_APP, getattr(common_settings, "INSTALLED_APPS")) + + def test_append_eox_nelp_celery_imports(self): + """Test when `plugin_settings` is called + that the eox_nelp_celery_import are added to the previous + CELERY_IMPORTS + + Expected behavior: + - EOX_NELP_CELERY_TASKS are presented in CELERY_IMPORTS. + """ + common_settings = SettingsClass() + previous_celery_imports = ( + 'example.import.tasks', + 'example.import2.tasks', + 'example.import3.tasks', + ) + setattr(common_settings, "CELERY_IMPORTS", previous_celery_imports) + + plugin_settings(common_settings) + + self.assertEqual( + getattr(common_settings, "CELERY_IMPORTS"), + previous_celery_imports + EOX_NELP_CELERY_TASKS, + ) + + def test_create_eox_nelp_celery_imports(self): + """Test when `plugin_settings` is called + that the eox_nelp_celery_import are created if there are not previous + CELERY_IMPORTS + + Expected behavior: + - EOX_NELP_CELERY_TASKS are presented in CELERY_IMPORTS. + """ + common_settings = SettingsClass() + + plugin_settings(common_settings) + + self.assertEqual(getattr(common_settings, "CELERY_IMPORTS"), EOX_NELP_CELERY_TASKS)