Skip to content
Merged
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
77 changes: 75 additions & 2 deletions common/djangoapps/student/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
from django.test import override_settings
from django.urls import reverse
from openedx_filters import PipelineStep
from openedx_filters.learning.filters import DashboardRenderStarted, CourseEnrollmentStarted, CourseUnenrollmentStarted
from openedx_filters.learning.filters import CourseEnrollmentStarted, CourseUnenrollmentStarted, DashboardRenderStarted
from rest_framework import status
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

from common.djangoapps.student.models import CourseEnrollment, EnrollmentNotAllowed, UnenrollmentNotAllowed
from common.djangoapps.student.models import (
CourseEnrollment,
EnrollmentNotAllowed,
Registration,
UnenrollmentNotAllowed,
)
from common.djangoapps.student.tests.factories import UserFactory, UserProfileFactory
from common.djangoapps.student.views.management import compose_activation_email
from openedx.core.djangolib.testing.utils import skip_unless_lms


Expand Down Expand Up @@ -111,6 +117,18 @@ def run_filter(self, context, template_name): # pylint: disable=arguments-diffe
)


class ActivationEmailWaldoEnricher(PipelineStep):
"""
Test pipeline step for the AccountActivationEmailContextGenerated filter
which adds a `show_waldo` context key.
"""

def run_filter(self, user, message_context): # pylint: disable=arguments-differ
"""Pipeline step that stamps a fake show_waldo flag onto the activation email context."""
message_context["show_waldo"] = True
return {"user": user, "message_context": message_context}


@skip_unless_lms
class EnrollmentFiltersTest(ModuleStoreTestCase):
"""
Expand Down Expand Up @@ -464,3 +482,58 @@ def test_dashboard_render_without_filter_config(self):

self.assertContains(response, self.first_course.id)
self.assertContains(response, self.second_course.id)


@skip_unless_lms
class AccountActivationEmailFiltersTest(ModuleStoreTestCase):
"""
Tests for the Open edX Filters associated with the account activation email context.

This class guarantees that the following filter is triggered when the activation email
context is generated:
- AccountActivationEmailContextGenerated
"""

def setUp(self): # pylint: disable=arguments-differ
super().setUp()
self.user = UserFactory()
self.registration = Registration()
self.registration.register(self.user)
self.registration.save()

@override_settings(
OPEN_EDX_FILTERS_CONFIG={
"org.openedx.authentication.account_activation.email.context.generated.v1": {
"pipeline": [
"common.djangoapps.student.tests.test_filters.ActivationEmailWaldoEnricher",
],
"fail_silently": False,
},
},
)
def test_activation_email_context_generated_filter_executed(self):
"""
Test whether the activation email context filter is triggered before the
activation email message context is finalized.

Expected result:
- AccountActivationEmailContextGenerated is triggered and executes
ActivationEmailWaldoEnricher.
- The composed message's context contains the pipeline step's modification.
"""
message = compose_activation_email(self.user, self.registration)

assert message.context["show_waldo"] is True

@override_settings(OPEN_EDX_FILTERS_CONFIG={})
def test_activation_email_context_generated_without_filter_config(self):
"""
Test that compose_activation_email succeeds with no pipeline steps configured.

Expected result:
- AccountActivationEmailContextGenerated executes a noop (empty pipeline).
- No 'show_waldo' key is injected into the message context.
"""
message = compose_activation_email(self.user, self.registration)

assert "show_waldo" not in message.context
8 changes: 6 additions & 2 deletions common/djangoapps/student/views/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# Note that this lives in LMS, so this dependency should be refactored.
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from openedx_filters.authentication.filters import AccountActivationEmailContextGenerated
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated

Expand Down Expand Up @@ -98,7 +99,6 @@
from openedx.core.lib.api.authentication import BearerAuthenticationAllowInactiveUser
from openedx.features.course_experience.url_helpers import make_learning_mfe_courseware_url
from openedx.features.discounts.applicability import FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG
from openedx.features.enterprise_support.utils import is_enterprise_learner
from common.djangoapps.util.db import outer_atomic
from common.djangoapps.util.json_request import JsonResponse
from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order
Expand Down Expand Up @@ -222,7 +222,6 @@ def compose_activation_email(
message_context = generate_activation_email_context(user, user_registration)
message_context.update({
'confirm_activation_link': _get_activation_confirmation_link(message_context['key'], redirect_url),
'is_enterprise_learner': is_enterprise_learner(user),
'is_first_purchase_discount_overridden': FIRST_PURCHASE_DISCOUNT_OVERRIDE_FLAG.is_enabled(),
'route_enabled': route_enabled,
'routed_user': user.username,
Expand All @@ -231,6 +230,11 @@ def compose_activation_email(
'registration_flow': registration_flow,
'show_auto_generated_username': show_auto_generated_username(user.username),
})
# .. filter_implemented_name: AccountActivationEmailContextGenerated
# .. filter_type: org.openedx.authentication.account_activation.email.context.generated.v1
__, message_context = AccountActivationEmailContextGenerated.run_filter(
user=user, message_context=message_context,
)

if route_enabled:
dest_addr = settings.FEATURES['REROUTE_ACTIVATION_EMAIL']
Expand Down
1 change: 0 additions & 1 deletion openedx/core/djangoapps/user_authn/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def test_ComposeEmail(self):
assert self.msg.context['routed_user_email'] == self.student.email
assert self.msg.context['routed_profile_name'] == ''
assert self.msg.context['registration_flow'] is False
assert self.msg.context['is_enterprise_learner'] is False
assert self.msg.context['is_first_purchase_discount_overridden'] is False

@mock.patch('time.sleep', mock.Mock(return_value=None))
Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ django-stubs<6
# The team that owns this package will manually bump this package rather than having it pulled in automatically.
# This is to allow them to better control its deployment and to do it in a process that works better
# for them.
edx-enterprise==8.11.0
edx-enterprise==8.14.0

# Date: 2023-07-26
# Our legacy Sass code is incompatible with anything except this ancient libsass version.
Expand Down
4 changes: 2 additions & 2 deletions requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ edx-drf-extensions==10.6.0
# edxval
# enterprise-integrated-channels
# openedx-learning
edx-enterprise==8.11.0
edx-enterprise==8.14.0
# via
# -c requirements/constraints.txt
# -r requirements/edx/kernel.in
Expand Down Expand Up @@ -829,7 +829,7 @@ openedx-events==10.5.0
# edx-name-affirmation
# event-tracking
# ora2
openedx-filters==3.11.0
openedx-filters==3.14.0
# via
# -r requirements/edx/kernel.in
# edx-enterprise
Expand Down
4 changes: 2 additions & 2 deletions requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ edx-drf-extensions==10.6.0
# edxval
# enterprise-integrated-channels
# openedx-learning
edx-enterprise==8.11.0
edx-enterprise==8.14.0
# via
# -c requirements/constraints.txt
# -r requirements/edx/doc.txt
Expand Down Expand Up @@ -1375,7 +1375,7 @@ openedx-events==10.5.0
# edx-name-affirmation
# event-tracking
# ora2
openedx-filters==3.11.0
openedx-filters==3.14.0
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
Expand Down
4 changes: 2 additions & 2 deletions requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ edx-drf-extensions==10.6.0
# edxval
# enterprise-integrated-channels
# openedx-learning
edx-enterprise==8.11.0
edx-enterprise==8.14.0
# via
# -c requirements/constraints.txt
# -r requirements/edx/base.txt
Expand Down Expand Up @@ -1001,7 +1001,7 @@ openedx-events==10.5.0
# edx-name-affirmation
# event-tracking
# ora2
openedx-filters==3.11.0
openedx-filters==3.14.0
# via
# -r requirements/edx/base.txt
# edx-enterprise
Expand Down
4 changes: 2 additions & 2 deletions requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ edx-drf-extensions==10.6.0
# edxval
# enterprise-integrated-channels
# openedx-learning
edx-enterprise==8.11.0
edx-enterprise==8.14.0
# via
# -c requirements/constraints.txt
# -r requirements/edx/base.txt
Expand Down Expand Up @@ -1046,7 +1046,7 @@ openedx-events==10.5.0
# edx-name-affirmation
# event-tracking
# ora2
openedx-filters==3.11.0
openedx-filters==3.14.0
# via
# -r requirements/edx/base.txt
# edx-enterprise
Expand Down
Loading