fix: add unit test

Pulled from @julianajlk's WIP, thank you!
This commit is contained in:
Phillip Shiu
2021-09-15 10:14:58 -04:00
parent 9185b3335b
commit 09809f322e

View File

@@ -35,6 +35,11 @@ from xmodule.modulestore.tests.factories import CourseFactory
from ..views import VALUE_PROP_TRACK_SELECTION_FLAG
# Name of the class to mock for Content Type Gating.
GATING_CLASS_NAME = 'openedx.features.content_type_gating.models.ContentTypeGatingConfig.enabled_for_enrollment'
# Name of the class to mock for Course Duration Limits.
CDL_CLASS_NAME = 'openedx.features.course_duration_limits.models.CourseDurationLimitConfig.enabled_for_enrollment'
@ddt.ddt
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
@@ -511,65 +516,118 @@ class CourseModeViewTest(CatalogIntegrationMixin, UrlResetMixin, ModuleStoreTest
redirect_url = reverse('dashboard') + '?course_closed=1%2F1%2F15%2C+12%3A00+AM'
self.assertRedirects(response, redirect_url)
# Value Prop TODO (REV-2378): remove waffle flag from tests once the new Track Selection template is rolled out.
# Other tests may need to be updated/removed to reflect the new page.
# The below test can be separated into multiple tests once un-happy path is implemented.
def test_new_track_selection(self):
# For the new track selection template to render, FBE must be fully on (gated_content and audit_access_deadline)
# and happy path conditions must be met:
# User can upgrade, FBE is fully on, and user is not an enterprise user.
def _assert_fbe_page(self, response, min_price=None, **_):
"""
Assert fbe.html was transcluded.
"""
self.assertContains(response, "Choose a path for your course in")
# Create the course modes and enroll the user
# Check if it displays the upgrade price for verified track and "Free" for audit track
self.assertContains(response, min_price)
self.assertContains(response, "Free")
# Check for specific HTML elements
self.assertContains(response, '<span class="award-icon">')
self.assertContains(response, '<span class="popover-icon">')
self.assertContains(response, '<span class="note-icon">')
# Check for upgrade button ID
self.assertContains(response, 'track_selection_upgrade')
# Check for audit button ID
self.assertContains(response, 'track_selection_audit')
# Check for happy path messaging - verified
self.assertContains(response, '<li class="collapsible-item">')
self.assertContains(response, 'access to all course activities')
self.assertContains(response, 'Full access')
# Check for happy path messaging - audit
self.assertContains(response, "discussion forums and non-graded assignments")
self.assertContains(response, "Get temporary access")
self.assertContains(response, "Access expires and all progress will be lost")
def _assert_unfbe_page(self, response, min_price=None, **_):
"""
Assert track_selection.html and unfbe.html were transcluded.
"""
# Check for string unique to track_selection.html.
self.assertContains(response, "| Upgrade Now")
# This string only occurs in lms/templates/course_modes/track_selection.html
# and related theme and translation files.
# Check for string unique to unfbe.html.
self.assertContains(response, "Some graded content may be lost")
# This string only occurs in lms/templates/course_modes/unfbe.html
# and related theme and translation files.
# Check min_price was correctly passed in.
self.assertContains(response, min_price)
def _assert_legacy_page(self, response, **_):
"""
Assert choose.html was transcluded.
"""
# Check for string unique to the legacy choose.html.
self.assertContains(response, "Choose Your Track")
# This string only occurs in lms/templates/course_modes/choose.html
# and related theme and translation files.
@ddt.data(
# gated_content_on, course_duration_limits_on, waffle_flag_on, expected_page_assertion_function
(True, True, True, _assert_fbe_page),
(True, False, True, _assert_unfbe_page),
(False, True, True, _assert_unfbe_page),
(False, False, True, _assert_unfbe_page),
(True, True, False, _assert_legacy_page),
(True, False, False, _assert_legacy_page),
(False, True, False, _assert_legacy_page),
(False, False, False, _assert_legacy_page),
)
@ddt.unpack
def test_track_selection_types(
self,
gated_content_on,
course_duration_limits_on,
waffle_flag_on,
expected_page_assertion_function
):
"""
Feature-based enrollment (FBE) is when gated content and course duration
limits are enabled when a user is auditing a course.
When prompted to perform track selection (choosing between the audit and
verified course modes), the learner may view 3 different pages:
1. fbe.html - full FBE
2. unfbe.html - partial or no FBE
3. choose.html - legacy track selection page
This test checks that the right page is shown.
"""
# The active course mode already exists. Create verified course mode:
verified_mode = CourseModeFactory.create(
mode_slug='verified',
course_id=self.course_that_started.id,
min_price=149,
)
# Enroll the test user in the audit mode:
CourseEnrollmentFactory(
is_active=True,
course_id=self.course_that_started.id,
user=self.user
)
# Value Prop TODO (REV-2378): remove waffle flag from tests once the new Track Selection template is rolled out.
# Check whether new track selection template is rendered.
# This should *only* be shown when the waffle flag is on.
with override_waffle_flag(VALUE_PROP_TRACK_SELECTION_FLAG, active=True):
with mock.patch(
'openedx.features.content_type_gating.models.ContentTypeGatingConfig.enabled_for_enrollment',
return_value=True
):
with mock.patch(
'openedx.features.course_duration_limits.models.CourseDurationLimitConfig.enabled_for_enrollment',
return_value=True
):
with override_waffle_flag(VALUE_PROP_TRACK_SELECTION_FLAG, active=waffle_flag_on):
with patch(GATING_CLASS_NAME, return_value=gated_content_on):
with patch(CDL_CLASS_NAME, return_value=course_duration_limits_on):
url = reverse('course_modes_choose', args=[str(self.course_that_started.id)])
response = self.client.get(url)
self.assertContains(response, "Choose a path for your course in")
# Check if it displays the upgrade price for verified track and "Free" for audit track
self.assertContains(response, verified_mode.min_price)
self.assertContains(response, "Free")
# Check for specific HTML elements
self.assertContains(response, '<span class="award-icon">')
self.assertContains(response, '<span class="popover-icon">')
self.assertContains(response, '<span class="note-icon">')
# Check for upgrade button ID
self.assertContains(response, 'track_selection_upgrade')
# Check for audit button ID
self.assertContains(response, 'track_selection_audit')
# Check for happy path messaging - verified
self.assertContains(response, '<li class="collapsible-item">')
self.assertContains(response, 'access to all course activities')
self.assertContains(response, 'Full access')
# Check for happy path messaging - audit
self.assertContains(response, "discussion forums and non-graded assignments")
self.assertContains(response, "Get temporary access")
self.assertContains(response, "Access expires and all progress will be lost")
expected_page_assertion_function(self, response, min_price=verified_mode.min_price)
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class TrackSelectionEmbargoTest(UrlResetMixin, ModuleStoreTestCase):