From 80a260abc777dc8244c4b14588960b3569acee04 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Thu, 14 Sep 2023 17:16:31 -0500 Subject: [PATCH] feat: move mako logic to init method --- .../frontend/tests/test_views.py | 25 +-------- eox_nelp/course_experience/frontend/views.py | 4 +- eox_nelp/init_pipeline.py | 24 +++++++++ eox_nelp/stats/views.py | 4 +- eox_nelp/templates_config.py | 21 -------- eox_nelp/tests/test_init_pipeline.py | 51 +++++++++++++++++++ 6 files changed, 80 insertions(+), 49 deletions(-) delete mode 100644 eox_nelp/templates_config.py create mode 100644 eox_nelp/tests/test_init_pipeline.py diff --git a/eox_nelp/course_experience/frontend/tests/test_views.py b/eox_nelp/course_experience/frontend/tests/test_views.py index 0f2be030..bae296da 100644 --- a/eox_nelp/course_experience/frontend/tests/test_views.py +++ b/eox_nelp/course_experience/frontend/tests/test_views.py @@ -2,12 +2,8 @@ Classes: CourseExperienceFrontendTestCase: Test CourseExperienceFrontendView template. """ -from django.apps import apps -from django.http import HttpResponse -from django.shortcuts import render from django.test import TestCase from django.urls import reverse -from mock import patch from rest_framework.test import APIClient @@ -21,24 +17,7 @@ def setUp(self): # pylint: disable=invalid-name self.client = APIClient() self.url_endpoint = reverse("course-experience-frontend:feedback-courses") - @patch("eox_nelp.templates_config.edxmako") - def test_edxmako_render_correct_call(self, edxmako_mock): - """ Test edxmako functions from edxapp_wrapper are called with the right values. - Expected behavior: - - `edxmako_mock.paths.add_lookup` is called with course_experience_template_path. - (The path of course xp templates is used) - - The get request to the url_endpoint is using the template `feedback_courses.html`. - """ - course_experience_template_path = apps.get_app_config('eox_nelp').path + "/course_experience/frontend/templates" - edxmako_mock.shortcuts.render_to_response.return_value = HttpResponse(content='Template mock') - - self.client.get(self.url_endpoint) - - edxmako_mock.paths.add_lookup.assert_called_with('main', course_experience_template_path) - edxmako_mock.shortcuts.render_to_response.assert_called_with("feedback_courses.html", {}, 'main', None) - - @patch("eox_nelp.course_experience.frontend.views.render_to_response") - def test_feedback_course_template_behaviour(self, render_to_response_mock): + def test_feedback_course_template_behaviour(self): """ The correct rendering of the feedback courses template using the url_endpoint for frontend feedback courses. Expected behavior: @@ -48,8 +27,6 @@ def test_feedback_course_template_behaviour(self, render_to_response_mock): - Response has the correct path to load styles with feedback carrousel css. - Response has the correct path to load script with feedback carrousel js. """ - render_to_response_mock.return_value = render(None, "feedback_courses.html") - response = self.client.get(self.url_endpoint) self.assertContains(response, 'Feedback courses general', status_code=200) diff --git a/eox_nelp/course_experience/frontend/views.py b/eox_nelp/course_experience/frontend/views.py index 7834fbb1..839f9493 100644 --- a/eox_nelp/course_experience/frontend/views.py +++ b/eox_nelp/course_experience/frontend/views.py @@ -8,7 +8,7 @@ """ from django.views import View -from eox_nelp.templates_config import render_to_response +from eox_nelp.edxapp_wrapper.edxmako import edxmako class FeedbackCoursesTemplate(View): @@ -19,4 +19,4 @@ class FeedbackCoursesTemplate(View): def get(self, request): # pylint: disable=unused-argument """Render start html""" - return render_to_response("feedback_courses.html", {}) + return edxmako.shortcuts.render_to_response("feedback_courses.html", {}, "main", request) diff --git a/eox_nelp/init_pipeline.py b/eox_nelp/init_pipeline.py index 4c73b96c..d351d648 100644 --- a/eox_nelp/init_pipeline.py +++ b/eox_nelp/init_pipeline.py @@ -7,6 +7,8 @@ patch_user_gender_choices: Change the current openedx gender options (Male, Female, Other) """ +import os + from django.utils.translation import gettext_noop @@ -15,6 +17,7 @@ def run_init_pipeline(): Executes multiple processes that must run before starting the django application. """ patch_user_gender_choices() + set_mako_templates() def patch_user_gender_choices(): @@ -30,3 +33,24 @@ def patch_user_gender_choices(): ('m', gettext_noop('Male')), ('f', gettext_noop('Female')), ) + + +def set_mako_templates(): + """This method adds the plugin templates to mako configuration.""" + # pylint: disable=import-error, import-outside-toplevel + # This cannot be at the top of the file since this file is imported the plugin initialization + # and therefore the settings has not been set yet + from eox_nelp.course_experience.frontend import templates as course_experience_templates + from eox_nelp.edxapp_wrapper.edxmako import edxmako + from eox_nelp.stats import templates as stats_templates + + templates_modules_to_include = [ + stats_templates, + course_experience_templates, + ] + + for module in templates_modules_to_include: + path_to_templates = os.path.dirname(module.__file__) + + if path_to_templates not in edxmako.LOOKUP['main'].directories: + edxmako.paths.add_lookup('main', path_to_templates) diff --git a/eox_nelp/stats/views.py b/eox_nelp/stats/views.py index 7e53b5b0..bcc7ca6e 100644 --- a/eox_nelp/stats/views.py +++ b/eox_nelp/stats/views.py @@ -5,7 +5,7 @@ classes: get_tenant_stats: function based view. """ -from eox_nelp.templates_config import render_to_response +from eox_nelp.edxapp_wrapper.edxmako import edxmako STATS_QUERY_PARAMS = [ "show_courses", @@ -47,4 +47,4 @@ def get_tenant_stats(request): context = {query_param: "true" for query_param in STATS_QUERY_PARAMS} context.update(request.GET.dict()) - return render_to_response("tenant_stats.html", context) + return edxmako.shortcuts.render_to_response("tenant_stats.html", context, "main", request) diff --git a/eox_nelp/templates_config.py b/eox_nelp/templates_config.py deleted file mode 100644 index 619cd5e5..00000000 --- a/eox_nelp/templates_config.py +++ /dev/null @@ -1,21 +0,0 @@ -""" Module to configure the rendering for custom templates to eox-nelp.""" -import os - -import eox_nelp.course_experience.frontend.templates as course_experience_templates -from eox_nelp.edxapp_wrapper.edxmako import edxmako -from eox_nelp.stats import templates as stats_templates - -module_templates_to_include = [ - stats_templates, - course_experience_templates, -] - - -def render_to_response(template_name, dictionary=None, namespace='main', request=None, **kwargs): - """ Custom render_to_response implementation using configurable backend and adding template dir """ - - for module in module_templates_to_include: - path_to_templates = os.path.dirname(module.__file__) - if path_to_templates not in edxmako.LOOKUP['main'].directories: - edxmako.paths.add_lookup('main', path_to_templates) - return edxmako.shortcuts.render_to_response(template_name, dictionary, namespace, request, **kwargs) diff --git a/eox_nelp/tests/test_init_pipeline.py b/eox_nelp/tests/test_init_pipeline.py new file mode 100644 index 00000000..606c660a --- /dev/null +++ b/eox_nelp/tests/test_init_pipeline.py @@ -0,0 +1,51 @@ +"""This file contains all the test for the init_pipeline.py file. + +Classes: + RunInitPipelineTestCase: Tests cases for run_init_pipeline method. + SetMakoTemplatesTestCase: Tests cases for set_mako_templates method. +""" +import os +from unittest.mock import call + +from django.test import TestCase +from mock import patch + +from eox_nelp.course_experience.frontend import templates as course_experience_templates +from eox_nelp.edxapp_wrapper.edxmako import edxmako +from eox_nelp.init_pipeline import run_init_pipeline, set_mako_templates +from eox_nelp.stats import templates as stats_templates + + +class RunInitPipelineTestCase(TestCase): + """Test class for run_init_pipeline method.""" + + @patch("eox_nelp.init_pipeline.patch_user_gender_choices") + @patch("eox_nelp.init_pipeline.set_mako_templates") + def test_pipeline_execute_expected_methods(self, set_mako_templates_mock, patch_user_gender_choices_mock): + """ Test that method calls the expected methods during the pipeline execution. + + Expected behavior: + - set_mako_templates_mock is called once. + - patch_user_gender_choices_mock is called once. + """ + run_init_pipeline() + + set_mako_templates_mock.assert_called_once() + patch_user_gender_choices_mock.assert_called_once() + + +class SetMakoTemplatesTestCase(TestCase): + """Test class for set_mako_templates method.""" + + def test_edxmako_adds_expected_paths(self): + """ Test that method adds the expected template paths. + + Expected behavior: + - `edxmako.paths.add_lookup` is called with the expected paths. + """ + set_mako_templates() + + edxmako.paths.add_lookup.assert_has_calls([ + call('main', os.path.dirname(stats_templates.__file__)), + call('main', os.path.dirname(course_experience_templates.__file__)), + ])