Merge pull request #19250 from edx/REV-503/add-banner-to-courseware

add expiration banner
This commit is contained in:
Matthew Piatetsky
2018-11-08 18:17:43 -05:00
committed by GitHub
7 changed files with 130 additions and 8 deletions

View File

@@ -1,18 +1,21 @@
"""
Helpers for courseware tests.
"""
from datetime import timedelta
import json
from django.contrib import messages
from django.contrib.auth.models import User
from django.urls import reverse
from django.test import TestCase
from django.test.client import Client, RequestFactory
from django.urls import reverse
from django.utils.timezone import now
from six import text_type
from courseware.access import has_access
from courseware.masquerade import handle_ajax, setup_masquerade
from edxmako.shortcuts import render_to_string
from lms.djangoapps.courseware.date_summary import verified_upgrade_deadline_link
from lms.djangoapps.lms_xblock.field_data import LmsFieldData
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.lib.url_utils import quote_slashes
@@ -348,3 +351,18 @@ def _create_mock_json_request(user, data, method='POST'):
request.user = user
request.session = {}
return request
def get_expiration_banner_text(user, course):
"""
Get text for banner that messages user course expiration date
for different tests that depend on it.
"""
expiration_date = (now() + timedelta(weeks=4)).strftime('%b %-d')
upgrade_link = verified_upgrade_deadline_link(user=user, course=course)
bannerText = 'Your access to this course expires on {expiration_date}. \
<a href="{upgrade_link}">Upgrade now</a> for unlimited access.'.format(
expiration_date=expiration_date,
upgrade_link=upgrade_link
)
return bannerText

View File

@@ -40,6 +40,7 @@ from courseware.access_utils import check_course_open_for_learner
from courseware.model_data import FieldDataCache, set_score
from courseware.module_render import get_module, handle_xblock_callback
from courseware.tests.factories import GlobalStaffFactory, StudentModuleFactory
from courseware.tests.helpers import get_expiration_banner_text
from courseware.testutils import RenderXBlockTestMixin
from courseware.url_helpers import get_redirect_url
from courseware.user_state_client import DjangoXBlockUserStateClient
@@ -211,8 +212,8 @@ class IndexQueryTestCase(ModuleStoreTestCase):
@override_waffle_flag(CONTENT_TYPE_GATING_FLAG, True)
@ddt.data(
(ModuleStoreEnum.Type.mongo, 10, 157),
(ModuleStoreEnum.Type.split, 4, 153),
(ModuleStoreEnum.Type.mongo, 10, 160),
(ModuleStoreEnum.Type.split, 4, 156),
)
@ddt.unpack
def test_index_query_counts(self, store_type, expected_mongo_query_count, expected_mysql_query_count):
@@ -2645,11 +2646,71 @@ class TestIndexViewWithGating(ModuleStoreTestCase, MilestonesTestCaseMixin):
}
)
)
self.assertEquals(response.status_code, 200)
self.assertIn("Content Locked", response.content)
@attr(shard=5)
class TestIndexViewWithCourseDurationLimits(ModuleStoreTestCase):
"""
Test the index view for a course with course duration limits enabled.
"""
def setUp(self):
"""
Set up the initial test data.
"""
super(TestIndexViewWithCourseDurationLimits, self).setUp()
self.user = UserFactory()
self.course = CourseFactory.create(start=datetime.now() - timedelta(weeks=1))
with self.store.bulk_operations(self.course.id):
self.chapter = ItemFactory.create(parent=self.course, category="chapter")
self.sequential = ItemFactory.create(parent=self.chapter, category='sequential')
CourseEnrollmentFactory(user=self.user, course_id=self.course.id)
@override_waffle_flag(CONTENT_TYPE_GATING_FLAG, True)
def test_index_with_course_duration_limits(self):
"""
Test that the courseware contains the course expiration banner
when course_duration_limits are enabled.
"""
self.assertTrue(self.client.login(username=self.user.username, password='test'))
response = self.client.get(
reverse(
'courseware_section',
kwargs={
'course_id': unicode(self.course.id),
'chapter': self.chapter.url_name,
'section': self.sequential.url_name,
}
)
)
bannerText = get_expiration_banner_text(self.user, self.course)
self.assertContains(response, bannerText, html=True)
@override_waffle_flag(CONTENT_TYPE_GATING_FLAG, False)
def test_index_without_course_duration_limits(self):
"""
Test that the courseware does not contain the course expiration banner
when course_duration_limits are disabled.
"""
self.assertTrue(self.client.login(username=self.user.username, password='test'))
response = self.client.get(
reverse(
'courseware_section',
kwargs={
'course_id': unicode(self.course.id),
'chapter': self.chapter.url_name,
'section': self.sequential.url_name,
}
)
)
bannerText = get_expiration_banner_text(self.user, self.course)
self.assertNotContains(response, bannerText, html=True)
@attr(shard=5)
class TestRenderXBlock(RenderXBlockTestMixin, ModuleStoreTestCase, CompletionWaffleTestMixin):
"""

View File

@@ -36,6 +36,7 @@ from openedx.core.djangoapps.user_api.preferences.api import get_user_preference
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace
from openedx.core.djangolib.markup import HTML, Text
from openedx.features.course_duration_limits.access import register_course_expired_message
from openedx.features.course_experience import (
COURSE_OUTLINE_PAGE_FLAG, default_course_url_name, COURSE_ENABLE_UNENROLLED_ACCESS_FLAG
)
@@ -132,6 +133,8 @@ class CoursewareIndex(View):
self.is_staff = has_access(request.user, 'staff', self.course)
self._setup_masquerade_for_effective_user()
register_course_expired_message(request, self.course)
return self.render(request)
except Exception as exception: # pylint: disable=broad-except
return CourseTabView.handle_exceptions(request, self.course, exception)

View File

@@ -88,6 +88,7 @@ from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
from openedx.core.djangolib.markup import HTML, Text
from openedx.features.course_duration_limits.access import register_course_expired_message
from openedx.features.course_experience import UNIFIED_COURSE_TAB_FLAG, course_home_url_name
from openedx.features.course_experience.course_tools import CourseToolsPluginManager
from openedx.features.course_experience.views.course_dates import CourseDatesFragmentView
@@ -504,6 +505,7 @@ class CourseTabView(EdxFragmentView):
# Show warnings if the user has limited access
# Must come after masquerading on creation of page context
self.register_user_access_warning_messages(request, course_key)
register_course_expired_message(request, course)
set_custom_metrics_for_course_key(course_key)
return super(CourseTabView, self).get(request, course=course, page_context=page_context, **kwargs)