Skip to content

Commit

Permalink
build: print log messages during test run
Browse files Browse the repository at this point in the history
  • Loading branch information
igobranco committed Sep 26, 2024
1 parent c3cc1ae commit b7ff8dd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
31 changes: 22 additions & 9 deletions nau_openedx_extensions/certificates/context_overrides.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This file defines overrides of the context render of course certificates using an Open edX Filters pipeline step.
"""

import logging

from openedx_filters import PipelineStep
Expand All @@ -9,6 +10,7 @@

log = logging.getLogger(__name__)


class CertificatesContextCohortOverride(PipelineStep):
"""
Override the certificates render template context with information from the student cohort.
Expand Down Expand Up @@ -37,23 +39,34 @@ class CertificatesContextCohortOverride(PipelineStep):
}
"""

def run_filter(self, context, custom_template): # pylint: disable=unused-argument, arguments-differ
def run_filter(self, context, custom_template): # pylint: disable=arguments-differ
"""
The filter logic.
"""
username = context['username']
course_key = context['course_id']
if 'cohort_overrides' in context:
username = context["username"]
course_key = context["course_id"]
if "cohort_overrides" in context:
cohort = get_cohort(username, course_key)
if cohort:
if cohort.name in context['cohort_overrides']:
cohort_override_dict = context['cohort_overrides'][cohort.name]
if cohort.name in context["cohort_overrides"]:
cohort_override_dict = context["cohort_overrides"][cohort.name]
context.update(cohort_override_dict)
else:
log.info("The user '%s' enrollment on course '%s' doesn't have a cohort certificate context overrides configured for the cohort '%s'.", username, course_key, cohort.name)
log.info(
"The user '%s' enrollment on course '%s' doesn't have a cohort "
"certificate context overrides configured for the cohort '%s'.",
username,
course_key,
cohort.name,
)
else:
log.info("User '%s' not in a cohort on course '%s'", username, course_key)
log.info(
"User '%s' not in a cohort on course '%s'", username, course_key
)
else:
log.info("No Certificates context cohort_overrides defined on course '%s'", course_key)
log.info(
"No Certificates context cohort_overrides defined on course '%s'",
course_key,
)

return {"context": context, "custom_template": custom_template}
5 changes: 3 additions & 2 deletions nau_openedx_extensions/edxapp_wrapper/backends/cohort_v1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Cohort abstraction backend
"""
from common.djangoapps.student.models import get_user_by_username_or_email
from openedx.core.djangoapps.course_groups.cohorts import get_cohort as edxapp_get_cohort # pylint: disable=import-error
from common.djangoapps.student.models import get_user_by_username_or_email # pylint: disable=import-error
from openedx.core.djangoapps.course_groups.cohorts import \
get_cohort as edxapp_get_cohort # pylint: disable=import-error


def get_cohort(username, course_key):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


def get_cohort(username, course_key):
def get_cohort(username, course_key): # pylint: disable=unused-argument
"""
For tests.
"""
Expand Down
1 change: 0 additions & 1 deletion nau_openedx_extensions/edxapp_wrapper/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ def get_cohort(*args, **kwargs):
backend = import_module(backend_module)

return backend.get_cohort(*args, **kwargs)

24 changes: 13 additions & 11 deletions nau_openedx_extensions/tests/test_certificates_context_overrides.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
Tests for certificates context overrides.
"""
from unittest.mock import MagicMock, Mock, PropertyMock, patch
from unittest.mock import MagicMock, PropertyMock, patch

from django.test import TestCase

from nau_openedx_extensions.certificates.context_overrides import CertificatesContextCohortOverride


Expand All @@ -27,7 +28,7 @@ def test_certificates_context_overrides_no_cohort_and_no_override(self, get_coho
}
result = CertificatesContextCohortOverride.run_filter(None, context, "some_template")
get_cohort_mock.assert_not_called()

self.assertDictEqual(result['context'], {
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
Expand Down Expand Up @@ -61,7 +62,7 @@ def test_certificates_context_overrides_with_no_cohort_and_with_override(self, g
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/base_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
Expand All @@ -74,18 +75,18 @@ def test_certificates_context_overrides_with_no_cohort_and_with_override(self, g
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/base_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
},
})


@patch('nau_openedx_extensions.certificates.context_overrides.get_cohort')
def test_certificates_context_overrides_with_cohort_and_override_dont_match(self, get_cohort_mock):
"""
Check that the override isn't being applied when the learner belongs to a cohort that isn't configured on the override.
Check that the override isn't being applied when the learner belongs to a cohort that isn't configured on the
override.
"""
mocked_cohort = MagicMock()
cohort_name_property = PropertyMock(return_value="some_group_not_configured_on_override")
Expand All @@ -96,7 +97,7 @@ def test_certificates_context_overrides_with_cohort_and_override_dont_match(self
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/base_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
Expand All @@ -109,7 +110,7 @@ def test_certificates_context_overrides_with_cohort_and_override_dont_match(self
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/base_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
Expand All @@ -119,7 +120,8 @@ def test_certificates_context_overrides_with_cohort_and_override_dont_match(self
@patch('nau_openedx_extensions.certificates.context_overrides.get_cohort')
def test_certificates_context_overrides_with_cohort_and_override(self, get_cohort_mock):
"""
Check that the override is being applied if the learner belongs to a cohort and that cohort is configured has override.
Check that the override is being applied if the learner belongs to a cohort and that cohort
is configured has override.
"""
mocked_cohort = MagicMock()
cohort_name_property = PropertyMock(return_value="SomeGroup")
Expand All @@ -130,7 +132,7 @@ def test_certificates_context_overrides_with_cohort_and_override(self, get_cohor
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/base_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
Expand All @@ -143,7 +145,7 @@ def test_certificates_context_overrides_with_cohort_and_override(self, get_cohor
"username": "[email protected]",
"course_id": "course-v1:Demo+DemoX+Demo_Course",
"footer_additional_logo": "http://lms.example.com/override_logo.png",
"cohort_overrides" : {
"cohort_overrides": {
"SomeGroup": {
"footer_additional_logo": "http://lms.example.com/override_logo.png",
},
Expand Down
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool:pytest]
DJANGO_SETTINGS_MODULE = nau_openedx_extensions.settings.test

[pytest]
log_cli = 1
log_cli_level = INFO

0 comments on commit b7ff8dd

Please sign in to comment.