# coding=UTF-8 """ Tests courseware views.py """ import itertools import json import unittest from datetime import datetime, timedelta, date from HTMLParser import HTMLParser from urllib import quote, urlencode from uuid import uuid4 from crum import set_current_request from completion.test_utils import CompletionWaffleTestMixin import ddt from django.conf import settings from django.contrib.auth.models import AnonymousUser from django.urls import reverse, reverse_lazy from django.http import Http404, HttpResponseBadRequest from django.test import TestCase from django.test.client import Client, RequestFactory from django.test.utils import override_settings from freezegun import freeze_time from milestones.tests.utils import MilestonesTestCaseMixin from mock import MagicMock, PropertyMock, create_autospec, patch from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from pytz import UTC from six import text_type from web_fragments.fragment import Fragment from xblock.core import XBlock from xblock.fields import Scope, String import courseware.views.views as views import shoppingcart from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory from course_modes.models import CourseMode from course_modes.tests.factories import CourseModeFactory 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 from lms.djangoapps.certificates import api as certs_api from lms.djangoapps.certificates.models import ( CertificateGenerationConfiguration, CertificateStatuses, CertificateWhitelist, ) from lms.djangoapps.certificates.tests.factories import CertificateInvalidationFactory, GeneratedCertificateFactory from lms.djangoapps.commerce.models import CommerceConfiguration from lms.djangoapps.commerce.utils import EcommerceService from lms.djangoapps.grades.config.waffle import waffle as grades_waffle from lms.djangoapps.grades.config.waffle import ASSUME_ZERO_GRADE_IF_ABSENT from openedx.core.djangoapps.catalog.tests.factories import CourseFactory as CatalogCourseFactory from openedx.core.djangoapps.catalog.tests.factories import CourseRunFactory, ProgramFactory from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.djangoapps.crawlers.models import CrawlersConfig from openedx.core.djangoapps.credit.api import set_credit_requirements from openedx.core.djangoapps.credit.models import CreditCourse, CreditProvider from openedx.core.djangoapps.waffle_utils import CourseWaffleFlag, WaffleFlagNamespace from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES, override_waffle_flag from openedx.core.djangolib.testing.utils import get_mock_request from openedx.core.lib.gating import api as gating_api from openedx.core.lib.tests import attr from openedx.core.lib.url_utils import quote_slashes from openedx.features.content_type_gating.models import ContentTypeGatingConfig from openedx.features.course_duration_limits.models import CourseDurationLimitConfig from openedx.features.course_experience.tests.views.helpers import add_course_mode from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseTestConsentRequired from openedx.features.course_experience import ( COURSE_OUTLINE_PAGE_FLAG, UNIFIED_COURSE_TAB_FLAG, ) from student.models import CourseEnrollment from student.tests.factories import TEST_PASSWORD, AdminFactory, CourseEnrollmentFactory, UserFactory from util.tests.test_date_utils import fake_pgettext, fake_ugettext from util.url import reload_django_url_config from util.views import ensure_valid_course_key from xmodule.graders import ShowCorrectness from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ( TEST_DATA_MIXED_MODULESTORE, ModuleStoreTestCase, SharedModuleStoreTestCase ) from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory, check_mongo_calls QUERY_COUNT_TABLE_BLACKLIST = WAFFLE_TABLES @attr(shard=5) class TestJumpTo(ModuleStoreTestCase): """ Check the jumpto link for a course. """ MODULESTORE = TEST_DATA_MIXED_MODULESTORE def setUp(self): super(TestJumpTo, self).setUp() # Use toy course from XML self.course_key = CourseKey.from_string('edX/toy/2012_Fall') def test_jumpto_invalid_location(self): location = self.course_key.make_usage_key(None, 'NoSuchPlace') # This is fragile, but unfortunately the problem is that within the LMS we # can't use the reverse calls from the CMS jumpto_url = '{0}/{1}/jump_to/{2}'.format('/courses', unicode(self.course_key), unicode(location)) response = self.client.get(jumpto_url) self.assertEqual(response.status_code, 404) def test_jumpto_from_section(self): course = CourseFactory.create() chapter = ItemFactory.create(category='chapter', parent_location=course.location) section = ItemFactory.create(category='sequential', parent_location=chapter.location) expected = '/courses/{course_id}/courseware/{chapter_id}/{section_id}/?{activate_block_id}'.format( course_id=unicode(course.id), chapter_id=chapter.url_name, section_id=section.url_name, activate_block_id=urlencode({'activate_block_id': unicode(section.location)}) ) jumpto_url = '{0}/{1}/jump_to/{2}'.format( '/courses', unicode(course.id), unicode(section.location), ) response = self.client.get(jumpto_url) self.assertRedirects(response, expected, status_code=302, target_status_code=302) def test_jumpto_from_module(self): course = CourseFactory.create() chapter = ItemFactory.create(category='chapter', parent_location=course.location) section = ItemFactory.create(category='sequential', parent_location=chapter.location) vertical1 = ItemFactory.create(category='vertical', parent_location=section.location) vertical2 = ItemFactory.create(category='vertical', parent_location=section.location) module1 = ItemFactory.create(category='html', parent_location=vertical1.location) module2 = ItemFactory.create(category='html', parent_location=vertical2.location) expected = '/courses/{course_id}/courseware/{chapter_id}/{section_id}/1?{activate_block_id}'.format( course_id=unicode(course.id), chapter_id=chapter.url_name, section_id=section.url_name, activate_block_id=urlencode({'activate_block_id': unicode(module1.location)}) ) jumpto_url = '{0}/{1}/jump_to/{2}'.format( '/courses', unicode(course.id), unicode(module1.location), ) response = self.client.get(jumpto_url) self.assertRedirects(response, expected, status_code=302, target_status_code=302) expected = '/courses/{course_id}/courseware/{chapter_id}/{section_id}/2?{activate_block_id}'.format( course_id=unicode(course.id), chapter_id=chapter.url_name, section_id=section.url_name, activate_block_id=urlencode({'activate_block_id': unicode(module2.location)}) ) jumpto_url = '{0}/{1}/jump_to/{2}'.format( '/courses', unicode(course.id), unicode(module2.location), ) response = self.client.get(jumpto_url) self.assertRedirects(response, expected, status_code=302, target_status_code=302) def test_jumpto_from_nested_module(self): course = CourseFactory.create() chapter = ItemFactory.create(category='chapter', parent_location=course.location) section = ItemFactory.create(category='sequential', parent_location=chapter.location) vertical = ItemFactory.create(category='vertical', parent_location=section.location) nested_section = ItemFactory.create(category='sequential', parent_location=vertical.location) nested_vertical1 = ItemFactory.create(category='vertical', parent_location=nested_section.location) # put a module into nested_vertical1 for completeness ItemFactory.create(category='html', parent_location=nested_vertical1.location) nested_vertical2 = ItemFactory.create(category='vertical', parent_location=nested_section.location) module2 = ItemFactory.create(category='html', parent_location=nested_vertical2.location) # internal position of module2 will be 1_2 (2nd item withing 1st item) expected = '/courses/{course_id}/courseware/{chapter_id}/{section_id}/1?{activate_block_id}'.format( course_id=unicode(course.id), chapter_id=chapter.url_name, section_id=section.url_name, activate_block_id=urlencode({'activate_block_id': unicode(module2.location)}) ) jumpto_url = '{0}/{1}/jump_to/{2}'.format( '/courses', unicode(course.id), unicode(module2.location), ) response = self.client.get(jumpto_url) self.assertRedirects(response, expected, status_code=302, target_status_code=302) def test_jumpto_id_invalid_location(self): location = BlockUsageLocator(CourseLocator('edX', 'toy', 'NoSuchPlace', deprecated=True), None, None, deprecated=True) jumpto_url = '{0}/{1}/jump_to_id/{2}'.format('/courses', unicode(self.course_key), unicode(location)) response = self.client.get(jumpto_url) self.assertEqual(response.status_code, 404) @attr(shard=5) @ddt.ddt class IndexQueryTestCase(ModuleStoreTestCase): """ Tests for query count. """ CREATE_USER = False NUM_PROBLEMS = 20 @ddt.data( (ModuleStoreEnum.Type.mongo, 10, 162), (ModuleStoreEnum.Type.split, 4, 160), ) @ddt.unpack def test_index_query_counts(self, store_type, expected_mongo_query_count, expected_mysql_query_count): ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) with self.store.default_store(store_type): course = CourseFactory.create() with self.store.bulk_operations(course.id): chapter = ItemFactory.create(category='chapter', parent_location=course.location) section = ItemFactory.create(category='sequential', parent_location=chapter.location) vertical = ItemFactory.create(category='vertical', parent_location=section.location) for _ in range(self.NUM_PROBLEMS): ItemFactory.create(category='problem', parent_location=vertical.location) self.user = UserFactory() self.client.login(username=self.user.username, password=TEST_PASSWORD) CourseEnrollment.enroll(self.user, course.id) with self.assertNumQueries(expected_mysql_query_count, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST): with check_mongo_calls(expected_mongo_query_count): url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(course.id), 'chapter': unicode(chapter.location.block_id), 'section': unicode(section.location.block_id), } ) response = self.client.get(url) self.assertEqual(response.status_code, 200) @attr(shard=5) @ddt.ddt class ViewsTestCase(ModuleStoreTestCase): """ Tests for views.py methods. """ YESTERDAY = 'yesterday' DATES = { YESTERDAY: datetime.now(UTC) - timedelta(days=1), None: None, } def setUp(self): super(ViewsTestCase, self).setUp() self.course = CourseFactory.create(display_name=u'teꜱᴛ course', run="Testing_course") with self.store.bulk_operations(self.course.id): self.chapter = ItemFactory.create( category='chapter', parent_location=self.course.location, display_name="Chapter 1", ) self.section = ItemFactory.create( category='sequential', parent_location=self.chapter.location, due=datetime(2013, 9, 18, 11, 30, 00), display_name='Sequential 1', format='Homework' ) self.vertical = ItemFactory.create( category='vertical', parent_location=self.section.location, display_name='Vertical 1', ) self.problem = ItemFactory.create( category='problem', parent_location=self.vertical.location, display_name='Problem 1', ) self.section2 = ItemFactory.create( category='sequential', parent_location=self.chapter.location, display_name='Sequential 2', ) self.vertical2 = ItemFactory.create( category='vertical', parent_location=self.section2.location, display_name='Vertical 2', ) self.problem2 = ItemFactory.create( category='problem', parent_location=self.vertical2.location, display_name='Problem 2', ) self.course_key = self.course.id # Set profile country to Åland Islands to check Unicode characters does not raise error self.user = UserFactory(username='dummy', profile__country='AX') self.date = datetime(2013, 1, 22, tzinfo=UTC) self.enrollment = CourseEnrollment.enroll(self.user, self.course_key) self.enrollment.created = self.date self.enrollment.save() chapter = 'Overview' self.chapter_url = '%s/%s/%s' % ('/courses', self.course_key, chapter) self.org = u"ꜱᴛᴀʀᴋ ɪɴᴅᴜꜱᴛʀɪᴇꜱ" self.org_html = "

'+Stark/Industries+'

" self.assertTrue(self.client.login(username=self.user.username, password=TEST_PASSWORD)) # refresh the course from the modulestore so that it has children self.course = modulestore().get_course(self.course.id) def test_index_success(self): response = self._verify_index_response() self.assertIn(unicode(self.problem2.location), response.content.decode("utf-8")) # re-access to the main course page redirects to last accessed view. url = reverse('courseware', kwargs={'course_id': unicode(self.course_key)}) response = self.client.get(url) self.assertEqual(response.status_code, 302) response = self.client.get(response.url) self.assertNotIn(unicode(self.problem.location), response.content.decode("utf-8")) self.assertIn(unicode(self.problem2.location), response.content.decode("utf-8")) def test_index_nonexistent_chapter(self): self._verify_index_response(expected_response_code=404, chapter_name='non-existent') def test_index_nonexistent_chapter_masquerade(self): with patch('courseware.views.index.setup_masquerade') as patch_masquerade: masquerade = MagicMock(role='student') patch_masquerade.return_value = (masquerade, self.user) self._verify_index_response(expected_response_code=302, chapter_name='non-existent') def test_index_nonexistent_section(self): self._verify_index_response(expected_response_code=404, section_name='non-existent') def test_index_nonexistent_section_masquerade(self): with patch('courseware.views.index.setup_masquerade') as patch_masquerade: masquerade = MagicMock(role='student') patch_masquerade.return_value = (masquerade, self.user) self._verify_index_response(expected_response_code=302, section_name='non-existent') def _verify_index_response(self, expected_response_code=200, chapter_name=None, section_name=None): """ Verifies the response when the courseware index page is accessed with the given chapter and section names. """ url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(self.course_key), 'chapter': unicode(self.chapter.location.block_id) if chapter_name is None else chapter_name, 'section': unicode(self.section2.location.block_id) if section_name is None else section_name, } ) response = self.client.get(url) self.assertEqual(response.status_code, expected_response_code) return response def test_index_no_visible_section_in_chapter(self): # reload the chapter from the store so its children information is updated self.chapter = self.store.get_item(self.chapter.location) # disable the visibility of the sections in the chapter for section in self.chapter.get_children(): section.visible_to_staff_only = True self.store.update_item(section, ModuleStoreEnum.UserID.test) url = reverse( 'courseware_chapter', kwargs={'course_id': unicode(self.course.id), 'chapter': unicode(self.chapter.location.block_id)}, ) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertNotIn('Problem 1', response.content) self.assertNotIn('Problem 2', response.content) def _create_global_staff_user(self): """ Create global staff user and log them in """ self.global_staff = GlobalStaffFactory.create() # pylint: disable=attribute-defined-outside-init self.assertTrue(self.client.login(username=self.global_staff.username, password=TEST_PASSWORD)) def _create_url_for_enroll_staff(self): """ creates the courseware url and enroll staff url """ # create the _next parameter courseware_url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(self.course_key), 'chapter': unicode(self.chapter.location.block_id), 'section': unicode(self.section.location.block_id), } ) # create the url for enroll_staff view enroll_url = "{enroll_url}?next={courseware_url}".format( enroll_url=reverse('enroll_staff', kwargs={'course_id': unicode(self.course.id)}), courseware_url=courseware_url ) return courseware_url, enroll_url @ddt.data( ({'enroll': "Enroll"}, True), ({'dont_enroll': "Don't enroll"}, False)) @ddt.unpack def test_enroll_staff_redirection(self, data, enrollment): """ Verify unenrolled staff is redirected to correct url. """ self._create_global_staff_user() courseware_url, enroll_url = self._create_url_for_enroll_staff() response = self.client.post(enroll_url, data=data, follow=True) self.assertEqual(response.status_code, 200) # we were redirected to our current location self.assertIn(302, response.redirect_chain[0]) self.assertEqual(len(response.redirect_chain), 1) if enrollment: self.assertRedirects(response, courseware_url) else: self.assertRedirects(response, '/courses/{}/about'.format(unicode(self.course_key))) def test_enroll_staff_with_invalid_data(self): """ If we try to post with an invalid data pattern, then we'll redirected to course about page. """ self._create_global_staff_user() __, enroll_url = self._create_url_for_enroll_staff() response = self.client.post(enroll_url, data={'test': "test"}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/courses/{}/about'.format(unicode(self.course_key))) @unittest.skipUnless(settings.FEATURES.get('ENABLE_SHOPPING_CART'), "Shopping Cart not enabled in settings") @patch.dict(settings.FEATURES, {'ENABLE_PAID_COURSE_REGISTRATION': True}) def test_course_about_in_cart(self): in_cart_span = '' # don't mock this course due to shopping cart existence checking course = CourseFactory.create(org="new", number="unenrolled", display_name="course") self.client.logout() response = self.client.get(reverse('about_course', args=[unicode(course.id)])) self.assertEqual(response.status_code, 200) self.assertNotIn(in_cart_span, response.content) # authenticated user with nothing in cart self.assertTrue(self.client.login(username=self.user.username, password=TEST_PASSWORD)) response = self.client.get(reverse('about_course', args=[unicode(course.id)])) self.assertEqual(response.status_code, 200) self.assertNotIn(in_cart_span, response.content) # now add the course to the cart cart = shoppingcart.models.Order.get_cart_for_user(self.user) shoppingcart.models.PaidCourseRegistration.add_to_order(cart, course.id) response = self.client.get(reverse('about_course', args=[unicode(course.id)])) self.assertEqual(response.status_code, 200) self.assertIn(in_cart_span, response.content) def assert_enrollment_link_present(self, is_anonymous): """ Prepare ecommerce checkout data and assert if the ecommerce link is contained in the response. Arguments: is_anonymous(bool): Tell the method to use an anonymous user or the logged in one. _id(bool): Tell the method to either expect an id in the href or not. """ sku = 'TEST123' configuration = CommerceConfiguration.objects.create(checkout_on_ecommerce_service=True) course = CourseFactory.create() CourseModeFactory(mode_slug=CourseMode.PROFESSIONAL, course_id=course.id, sku=sku, min_price=1) if is_anonymous: self.client.logout() else: self.assertTrue(self.client.login(username=self.user.username, password=TEST_PASSWORD)) # Construct the link according the following scenarios and verify its presence in the response: # (1) shopping cart is enabled and the user is not logged in # (2) shopping cart is enabled and the user is logged in href = ''.format( uri_stem=configuration.basket_checkout_page, sku=sku, ) # Generate the course about page content response = self.client.get(reverse('about_course', args=[unicode(course.id)])) self.assertEqual(response.status_code, 200) self.assertIn(href, response.content) @ddt.data(True, False) def test_ecommerce_checkout(self, is_anonymous): if not is_anonymous: self.assert_enrollment_link_present(is_anonymous=is_anonymous) else: self.assertEqual(EcommerceService().is_enabled(AnonymousUser()), False) @ddt.data(True, False) @unittest.skipUnless(settings.FEATURES.get('ENABLE_SHOPPING_CART'), 'Shopping Cart not enabled in settings') @patch.dict(settings.FEATURES, {'ENABLE_PAID_COURSE_REGISTRATION': True}) def test_ecommerce_checkout_shopping_cart_enabled(self, is_anonymous): """ Two scenarios are being validated here -- authenticated/known user and unauthenticated/anonymous user For a known user we expect the checkout link to point to Otto in a scenario where the CommerceConfiguration is active and the course mode is PROFESSIONAL. """ if not is_anonymous: self.assert_enrollment_link_present(is_anonymous=is_anonymous) else: self.assertEqual(EcommerceService().is_enabled(AnonymousUser()), False) def test_user_groups(self): # deprecated function mock_user = MagicMock() type(mock_user).is_authenticated = PropertyMock(return_value=False) self.assertEqual(views.user_groups(mock_user), []) def test_get_redirect_url(self): # test the course location self.assertEqual( u'/courses/{course_key}/courseware?{activate_block_id}'.format( course_key=text_type(self.course_key), activate_block_id=urlencode({'activate_block_id': text_type(self.course.location)}) ), get_redirect_url(self.course_key, self.course.location), ) # test a section location self.assertEqual( u'/courses/{course_key}/courseware/Chapter_1/Sequential_1/?{activate_block_id}'.format( course_key=text_type(self.course_key), activate_block_id=urlencode({'activate_block_id': text_type(self.section.location)}) ), get_redirect_url(self.course_key, self.section.location), ) def test_invalid_course_id(self): response = self.client.get('/courses/MITx/3.091X/') self.assertEqual(response.status_code, 404) def test_incomplete_course_id(self): response = self.client.get('/courses/MITx/') self.assertEqual(response.status_code, 404) def test_index_invalid_position(self): request_url = '/'.join([ '/courses', unicode(self.course.id), 'courseware', self.chapter.location.block_id, self.section.location.block_id, 'f' ]) self.assertTrue(self.client.login(username=self.user.username, password=TEST_PASSWORD)) response = self.client.get(request_url) self.assertEqual(response.status_code, 404) def test_unicode_handling_in_url(self): url_parts = [ '/courses', unicode(self.course.id), 'courseware', self.chapter.location.block_id, self.section.location.block_id, '1' ] self.assertTrue(self.client.login(username=self.user.username, password=TEST_PASSWORD)) for idx, val in enumerate(url_parts): url_parts_copy = url_parts[:] url_parts_copy[idx] = val + u'χ' request_url = '/'.join(url_parts_copy) response = self.client.get(request_url) self.assertEqual(response.status_code, 404) def test_jump_to_invalid(self): # TODO add a test for invalid location # TODO add a test for no data * response = self.client.get(reverse('jump_to', args=['foo/bar/baz', 'baz'])) self.assertEquals(response.status_code, 404) def verify_end_date(self, course_id, expected_end_text=None): """ Visits the about page for `course_id` and tests that both the text "Classes End", as well as the specified `expected_end_text`, is present on the page. If `expected_end_text` is None, verifies that the about page *does not* contain the text "Classes End". """ result = self.client.get(reverse('about_course', args=[unicode(course_id)])) if expected_end_text is not None: self.assertContains(result, "Classes End") self.assertContains(result, expected_end_text) else: self.assertNotContains(result, "Classes End") def test_submission_history_accepts_valid_ids(self): # log into a staff account admin = AdminFactory() self.assertTrue(self.client.login(username=admin.username, password='test')) url = reverse('submission_history', kwargs={ 'course_id': unicode(self.course_key), 'student_username': 'dummy', 'location': unicode(self.problem.location), }) response = self.client.get(url) # Tests that we do not get an "Invalid x" response when passing correct arguments to view self.assertNotIn('Invalid', response.content) def test_submission_history_xss(self): # log into a staff account admin = AdminFactory() self.assertTrue(self.client.login(username=admin.username, password='test')) # try it with an existing user and a malicious location url = reverse('submission_history', kwargs={ 'course_id': unicode(self.course_key), 'student_username': 'dummy', 'location': '' }) response = self.client.get(url) self.assertNotIn('', 'location': 'dummy' }) response = self.client.get(url) self.assertNotIn('', '', '') def test_progress_page_xss_prevent(self, malicious_code): """ Test that XSS attack is prevented """ resp = self._get_student_progress_page() # Test that malicious code does not appear in html self.assertNotIn(malicious_code, resp.content) def test_pure_ungraded_xblock(self): ItemFactory.create(category='acid', parent_location=self.vertical.location) self._get_progress_page() @ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split) def test_student_progress_with_valid_and_invalid_id(self, default_store): """ Check that invalid 'student_id' raises Http404 for both old mongo and split mongo courses. """ # Create new course with respect to 'default_store' # Enroll student into course self.course = CourseFactory.create(default_store=default_store) CourseEnrollmentFactory(user=self.user, course_id=self.course.id, mode=CourseMode.HONOR) # Invalid Student Ids (Integer and Non-int) invalid_student_ids = [ 991021, 'azU3N_8$', ] for invalid_id in invalid_student_ids: resp = self.client.get( reverse('student_progress', args=[unicode(self.course.id), invalid_id]) ) self.assertEquals(resp.status_code, 404) # Assert that valid 'student_id' returns 200 status self._get_student_progress_page() @ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split) def test_unenrolled_student_progress_for_credit_course(self, default_store): """ Test that student progress page does not break while checking for an unenrolled student. Scenario: When instructor checks the progress of a student who is not enrolled in credit course. It should return 200 response. """ # Create a new course, a user which will not be enrolled in course, admin user for staff access course = CourseFactory.create(default_store=default_store) not_enrolled_user = UserFactory.create() admin = AdminFactory.create() self.assertTrue(self.client.login(username=admin.username, password='test')) # Create and enable Credit course CreditCourse.objects.create(course_key=course.id, enabled=True) # Configure a credit provider for the course CreditProvider.objects.create( provider_id="ASU", enable_integration=True, provider_url="https://credit.example.com/request" ) requirements = [{ "namespace": "grade", "name": "grade", "display_name": "Grade", "criteria": {"min_grade": 0.52}, }] # Add a single credit requirement (final grade) set_credit_requirements(course.id, requirements) self._get_student_progress_page() def test_non_ascii_grade_cutoffs(self): self._get_progress_page() def test_generate_cert_config(self): resp = self._get_progress_page() self.assertNotContains(resp, 'Request Certificate') # Enable the feature, but do not enable it for this course CertificateGenerationConfiguration(enabled=True).save() resp = self._get_progress_page() self.assertNotContains(resp, 'Request Certificate') # Enable certificate generation for this course certs_api.set_cert_generation_enabled(self.course.id, True) resp = self._get_progress_page() self.assertNotContains(resp, 'Request Certificate') @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) @patch.dict('django.conf.settings.FEATURES', {'CERTIFICATES_HTML_VIEW': True}) def test_view_certificate_link(self): """ If certificate web view is enabled then certificate web view button should appear for user who certificate is available/generated """ certificate = GeneratedCertificateFactory.create( user=self.user, course_id=self.course.id, status=CertificateStatuses.downloadable, download_url="http://www.example.com/certificate.pdf", mode='verified' ) # Enable the feature, but do not enable it for this course CertificateGenerationConfiguration(enabled=True).save() # Enable certificate generation for this course certs_api.set_cert_generation_enabled(self.course.id, True) # Course certificate configurations certificates = [ { 'id': 1, 'name': 'Name 1', 'description': 'Description 1', 'course_title': 'course_title_1', 'signatories': [], 'version': 1, 'is_active': True } ] self.course.certificates = {'certificates': certificates} self.course.cert_html_view_enabled = True self.course.save() self.store.update_item(self.course, self.user.id) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {}} resp = self._get_progress_page() self.assertContains(resp, u"View Certificate") self.assertContains(resp, u"earned a certificate for this course") cert_url = certs_api.get_certificate_url(course_id=self.course.id, uuid=certificate.verify_uuid) self.assertContains(resp, cert_url) # when course certificate is not active certificates[0]['is_active'] = False self.store.update_item(self.course, self.user.id) resp = self._get_progress_page() self.assertNotContains(resp, u"View Your Certificate") self.assertNotContains(resp, u"You can now view your certificate") self.assertContains(resp, "Your certificate is available") self.assertContains(resp, "earned a certificate for this course.") @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) @patch.dict('django.conf.settings.FEATURES', {'CERTIFICATES_HTML_VIEW': False}) def test_view_certificate_link_hidden(self): """ If certificate web view is disabled then certificate web view button should not appear for user who certificate is available/generated """ GeneratedCertificateFactory.create( user=self.user, course_id=self.course.id, status=CertificateStatuses.downloadable, download_url="http://www.example.com/certificate.pdf", mode='verified' ) # Enable the feature, but do not enable it for this course CertificateGenerationConfiguration(enabled=True).save() # Enable certificate generation for this course certs_api.set_cert_generation_enabled(self.course.id, True) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {}} resp = self._get_progress_page() self.assertContains(resp, u"Download Your Certificate") @ddt.data( (True, 46), (False, 45) ) @ddt.unpack def test_progress_queries_paced_courses(self, self_paced, query_count): """Test that query counts remain the same for self-paced and instructor-paced courses.""" ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) self.setup_course(self_paced=self_paced) with self.assertNumQueries(query_count, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST), check_mongo_calls(1): self._get_progress_page() @patch.dict(settings.FEATURES, {'ASSUME_ZERO_GRADE_IF_ABSENT_FOR_ALL_TESTS': False}) @ddt.data( (False, 53, 33), (True, 45, 29) ) @ddt.unpack def test_progress_queries(self, enable_waffle, initial, subsequent): ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) self.setup_course() with grades_waffle().override(ASSUME_ZERO_GRADE_IF_ABSENT, active=enable_waffle): with self.assertNumQueries( initial, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST ), check_mongo_calls(1): self._get_progress_page() # subsequent accesses to the progress page require fewer queries. for _ in range(2): with self.assertNumQueries( subsequent, table_blacklist=QUERY_COUNT_TABLE_BLACKLIST ), check_mongo_calls(1): self._get_progress_page() @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) @ddt.data( *itertools.product( ( CourseMode.AUDIT, CourseMode.HONOR, CourseMode.VERIFIED, CourseMode.PROFESSIONAL, CourseMode.NO_ID_PROFESSIONAL_MODE, CourseMode.CREDIT_MODE ), (True, False) ) ) @ddt.unpack def test_show_certificate_request_button(self, course_mode, user_verified): """Verify that the Request Certificate is not displayed in audit mode.""" CertificateGenerationConfiguration(enabled=True).save() certs_api.set_cert_generation_enabled(self.course.id, True) CourseEnrollment.enroll(self.user, self.course.id, mode=course_mode) with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = user_verified with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = { 'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {} } resp = self._get_progress_page() cert_button_hidden = course_mode in (CourseMode.AUDIT, CourseMode.HONOR) or \ course_mode in CourseMode.VERIFIED_MODES and not user_verified self.assertEqual( cert_button_hidden, 'Request Certificate' not in resp.content ) @patch.dict('django.conf.settings.FEATURES', {'CERTIFICATES_HTML_VIEW': True}) def test_page_with_invalidated_certificate_with_html_view(self): """ Verify that for html certs if certificate is marked as invalidated than re-generate button should not appear on progress page. """ generated_certificate = self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) # Course certificate configurations certificates = [ { 'id': 1, 'name': 'dummy', 'description': 'dummy description', 'course_title': 'dummy title', 'signatories': [], 'version': 1, 'is_active': True } ] self.course.certificates = {'certificates': certificates} self.course.cert_html_view_enabled = True self.course.save() self.store.update_item(self.course, self.user.id) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = { 'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {} } resp = self._get_progress_page() self.assertContains(resp, u"View Certificate") self.assert_invalidate_certificate(generated_certificate) @patch.dict('django.conf.settings.FEATURES', {'CERTIFICATES_HTML_VIEW': True}) def test_page_with_whitelisted_certificate_with_html_view(self): """ Verify that for white listed user the view certificate is appearing on dashboard """ generated_certificate = self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) # Course certificate configurations certificates = [ { 'id': 1, 'name': 'dummy', 'description': 'dummy description', 'course_title': 'dummy title', 'signatories': [], 'version': 1, 'is_active': True } ] self.course.certificates = {'certificates': certificates} self.course.cert_html_view_enabled = True self.course.save() self.store.update_item(self.course, self.user.id) CertificateWhitelist.objects.create( user=self.user, course_id=self.course.id, whitelist=True ) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = False course_grade.summary = { 'grade': 'Fail', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {} } resp = self._get_progress_page() self.assertContains(resp, u"View Certificate") self.assert_invalidate_certificate(generated_certificate) @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) def test_page_with_invalidated_certificate_with_pdf(self): """ Verify that for pdf certs if certificate is marked as invalidated than re-generate button should not appear on progress page. """ generated_certificate = self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {}} resp = self._get_progress_page() self.assertContains(resp, u'Download Your Certificate') self.assert_invalidate_certificate(generated_certificate) @patch('courseware.views.views.is_course_passed', PropertyMock(return_value=True)) @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) def test_message_for_audit_mode(self): """ Verify that message appears on progress page, if learner is enrolled in audit mode. """ user = UserFactory.create() self.assertTrue(self.client.login(username=user.username, password='test')) CourseEnrollmentFactory(user=user, course_id=self.course.id, mode=CourseMode.AUDIT) with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {}} response = self._get_progress_page() self.assertContains( response, u'You are enrolled in the audit track for this course. The audit track does not include a certificate.' ) @ddt.data( *itertools.product( ( CourseMode.AUDIT, CourseMode.HONOR, CourseMode.VERIFIED, CourseMode.PROFESSIONAL, CourseMode.NO_ID_PROFESSIONAL_MODE, CourseMode.CREDIT_MODE ) ) ) @ddt.unpack def test_progress_with_course_duration_limits(self, course_mode): """ Verify that expired banner message appears on progress page, if learner is enrolled in audit mode. """ CourseDurationLimitConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) user = UserFactory.create() self.assertTrue(self.client.login(username=user.username, password='test')) add_course_mode(self.course, upgrade_deadline_expired=False) CourseEnrollmentFactory(user=user, course_id=self.course.id, mode=course_mode) response = self._get_progress_page() bannerText = get_expiration_banner_text(user, self.course) if course_mode == CourseMode.AUDIT: self.assertContains(response, bannerText, html=True) else: self.assertNotContains(response, bannerText, html=True) @ddt.data( *itertools.product( ( CourseMode.AUDIT, CourseMode.HONOR, CourseMode.VERIFIED, CourseMode.PROFESSIONAL, CourseMode.NO_ID_PROFESSIONAL_MODE, CourseMode.CREDIT_MODE ) ) ) @ddt.unpack def test_progress_without_course_duration_limits(self, course_mode): """ Verify that expired banner message never appears on progress page, regardless of course_mode """ CourseDurationLimitConfig.objects.create(enabled=False) user = UserFactory.create() self.assertTrue(self.client.login(username=user.username, password='test')) CourseEnrollmentFactory(user=user, course_id=self.course.id, mode=course_mode) response = self._get_progress_page() bannerText = get_expiration_banner_text(user, self.course) self.assertNotContains(response, bannerText, html=True) @patch('courseware.views.views.is_course_passed', PropertyMock(return_value=True)) @patch('lms.djangoapps.certificates.api.get_active_web_certificate', PropertyMock(return_value=True)) def test_message_for_honor_mode(self): """ Verify that message appears on progress page, if learner is enrolled in honor mode. """ user = UserFactory.create() self.assertTrue(self.client.login(username=user.username, password='test')) CourseEnrollmentFactory(user=user, course_id=self.course.id, mode=CourseMode.HONOR) with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75, 'section_breakdown': [], 'grade_breakdown': {}} response = self._get_progress_page() self.assertContains( response, u'You are enrolled in the honor track for this course. The honor track does not include a certificate.' ) def test_invalidated_cert_data(self): """ Verify that invalidated cert data is returned if cert is invalidated. """ generated_certificate = self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) CertificateInvalidationFactory.create( generated_certificate=generated_certificate, invalidated_by=self.user ) # Invalidate user certificate generated_certificate.invalidate() response = views._get_cert_data(self.user, self.course, CourseMode.VERIFIED, MagicMock(passed=True)) self.assertEqual(response.cert_status, 'invalidated') self.assertEqual(response.title, 'Your certificate has been invalidated') def test_downloadable_get_cert_data(self): """ Verify that downloadable cert data is returned if cert is downloadable. """ self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.certificates.api.certificate_downloadable_status', return_value=self.mock_certificate_downloadable_status(is_downloadable=True)): response = views._get_cert_data(self.user, self.course, CourseMode.VERIFIED, MagicMock(passed=True)) self.assertEqual(response.cert_status, 'downloadable') self.assertEqual(response.title, 'Your certificate is available') def test_generating_get_cert_data(self): """ Verify that generating cert data is returned if cert is generating. """ self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) with patch('lms.djangoapps.certificates.api.certificate_downloadable_status', return_value=self.mock_certificate_downloadable_status(is_generating=True)): response = views._get_cert_data(self.user, self.course, CourseMode.VERIFIED, MagicMock(passed=True)) self.assertEqual(response.cert_status, 'generating') self.assertEqual(response.title, "We're working on it...") def test_unverified_get_cert_data(self): """ Verify that unverified cert data is returned if cert is unverified. """ self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) with patch('lms.djangoapps.certificates.api.certificate_downloadable_status', return_value=self.mock_certificate_downloadable_status(is_unverified=True)): response = views._get_cert_data(self.user, self.course, CourseMode.VERIFIED, MagicMock(passed=True)) self.assertEqual(response.cert_status, 'unverified') self.assertEqual(response.title, "Certificate unavailable") def test_request_get_cert_data(self): """ Verify that requested cert data is returned if cert is to be requested. """ self.generate_certificate( "http://www.example.com/certificate.pdf", "verified" ) CourseEnrollment.enroll(self.user, self.course.id, mode="verified") with patch( 'lms.djangoapps.verify_student.services.IDVerificationService.user_is_verified' ) as user_verify: user_verify.return_value = True with patch('lms.djangoapps.certificates.api.certificate_downloadable_status', return_value=self.mock_certificate_downloadable_status()): response = views._get_cert_data(self.user, self.course, CourseMode.VERIFIED, MagicMock(passed=True)) self.assertEqual(response.cert_status, 'requesting') self.assertEqual(response.title, "Congratulations, you qualified for a certificate!") def assert_invalidate_certificate(self, certificate): """ Dry method to mark certificate as invalid. And assert the response. """ CertificateInvalidationFactory.create( generated_certificate=certificate, invalidated_by=self.user ) # Invalidate user certificate certificate.invalidate() resp = self._get_progress_page() self.assertNotContains(resp, u'Request Certificate') self.assertContains(resp, u'Your certificate has been invalidated') self.assertContains(resp, u'Please contact your course team if you have any questions.') self.assertNotContains(resp, u'View Your Certificate') self.assertNotContains(resp, u'Download Your Certificate') def generate_certificate(self, url, mode): """ Dry method to generate certificate. """ generated_certificate = GeneratedCertificateFactory.create( user=self.user, course_id=self.course.id, status=CertificateStatuses.downloadable, download_url=url, mode=mode ) CertificateGenerationConfiguration(enabled=True).save() certs_api.set_cert_generation_enabled(self.course.id, True) return generated_certificate def mock_certificate_downloadable_status( self, is_downloadable=False, is_generating=False, is_unverified=False, uuid=None, download_url=None ): """Dry method to mock certificate downloadable status response.""" return { 'is_downloadable': is_downloadable, 'is_generating': is_generating, 'is_unverified': is_unverified, 'download_url': uuid, 'uuid': download_url, } @attr(shard=5) @ddt.ddt class ProgressPageShowCorrectnessTests(ProgressPageBaseTests): """ Tests that verify that the progress page works correctly when displaying subsections where correctness is hidden. """ # Constants used in the test data NOW = datetime.now(UTC) DAY_DELTA = timedelta(days=1) YESTERDAY = 'yesterday' TODAY = 'today' TOMORROW = 'tomorrow' GRADER_TYPE = 'Homework' DATES = { YESTERDAY: NOW - DAY_DELTA, TODAY: NOW, TOMORROW: NOW + DAY_DELTA, None: None, } def setUp(self): super(ProgressPageShowCorrectnessTests, self).setUp() self.staff_user = UserFactory.create(is_staff=True) def setup_course(self, show_correctness='', due_date=None, graded=False, **course_options): """ Set up course with a subsection with the given show_correctness, due_date, and graded settings. """ # Use a simple grading policy course_options['grading_policy'] = { "GRADER": [{ "type": self.GRADER_TYPE, "min_count": 2, "drop_count": 0, "short_label": "HW", "weight": 1.0 }], "GRADE_CUTOFFS": { 'A': .9, 'B': .33 } } self.create_course(**course_options) metadata = dict( show_correctness=show_correctness, ) if due_date is not None: metadata['due'] = due_date if graded: metadata['graded'] = True metadata['format'] = self.GRADER_TYPE with self.store.bulk_operations(self.course.id): self.chapter = ItemFactory.create(category='chapter', parent_location=self.course.location, display_name="Section 1") self.section = ItemFactory.create(category='sequential', parent_location=self.chapter.location, display_name="Subsection 1", metadata=metadata) self.vertical = ItemFactory.create(category='vertical', parent_location=self.section.location) CourseEnrollmentFactory(user=self.user, course_id=self.course.id, mode=CourseMode.HONOR) def add_problem(self): """ Add a problem to the subsection """ problem_xml = MultipleChoiceResponseXMLFactory().build_xml( question_text='The correct answer is Choice 1', choices=[True, False], choice_names=['choice_0', 'choice_1'] ) self.problem = ItemFactory.create(category='problem', parent_location=self.vertical.location, data=problem_xml, display_name='Problem 1') # Re-fetch the course from the database self.course = self.store.get_course(self.course.id) def answer_problem(self, value=1, max_value=1): """ Submit the given score to the problem on behalf of the user """ # Get the module for the problem, as viewed by the user field_data_cache = FieldDataCache.cache_for_descriptor_descendents( self.course.id, self.user, self.course, depth=2 ) self.addCleanup(set_current_request, None) # pylint: disable=protected-access module = get_module( self.user, get_mock_request(self.user), self.problem.scope_ids.usage_id, field_data_cache, )._xmodule # Submit the given score/max_score to the problem xmodule grade_dict = {'value': value, 'max_value': max_value, 'user_id': self.user.id} module.system.publish(self.problem, 'grade', grade_dict) def assert_progress_page_show_grades(self, response, show_correctness, due_date, graded, show_grades, score, max_score, avg): """ Ensures that grades and scores are shown or not shown on the progress page as required. """ expected_score = "
{score}/{max_score}
".format(score=score, max_score=max_score) percent = score / float(max_score) if show_grades: # If grades are shown, we should be able to see the current problem scores. self.assertIn(expected_score, response.content) if graded: expected_summary_text = "Problem Scores:" else: expected_summary_text = "Practice Scores:" else: # If grades are hidden, we should not be able to see the current problem scores. self.assertNotIn(expected_score, response.content) if graded: expected_summary_text = "Problem scores are hidden" else: expected_summary_text = "Practice scores are hidden" if show_correctness == ShowCorrectness.PAST_DUE and due_date: expected_summary_text += ' until the due date.' else: expected_summary_text += '.' # Ensure that expected text is present self.assertIn(expected_summary_text, response.content) @ddt.data( ('', None, False), ('', None, True), (ShowCorrectness.ALWAYS, None, False), (ShowCorrectness.ALWAYS, None, True), (ShowCorrectness.ALWAYS, YESTERDAY, False), (ShowCorrectness.ALWAYS, YESTERDAY, True), (ShowCorrectness.ALWAYS, TODAY, False), (ShowCorrectness.ALWAYS, TODAY, True), (ShowCorrectness.ALWAYS, TOMORROW, False), (ShowCorrectness.ALWAYS, TOMORROW, True), (ShowCorrectness.NEVER, None, False), (ShowCorrectness.NEVER, None, True), (ShowCorrectness.NEVER, YESTERDAY, False), (ShowCorrectness.NEVER, YESTERDAY, True), (ShowCorrectness.NEVER, TODAY, False), (ShowCorrectness.NEVER, TODAY, True), (ShowCorrectness.NEVER, TOMORROW, False), (ShowCorrectness.NEVER, TOMORROW, True), (ShowCorrectness.PAST_DUE, None, False), (ShowCorrectness.PAST_DUE, None, True), (ShowCorrectness.PAST_DUE, YESTERDAY, False), (ShowCorrectness.PAST_DUE, YESTERDAY, True), (ShowCorrectness.PAST_DUE, TODAY, False), (ShowCorrectness.PAST_DUE, TODAY, True), (ShowCorrectness.PAST_DUE, TOMORROW, False), (ShowCorrectness.PAST_DUE, TOMORROW, True), ) @ddt.unpack def test_progress_page_no_problem_scores(self, show_correctness, due_date_name, graded): """ Test that "no problem scores are present" for a course with no problems, regardless of the various show correctness settings. """ self.setup_course(show_correctness=show_correctness, due_date=self.DATES[due_date_name], graded=graded) resp = self._get_progress_page() # Test that no problem scores are present self.assertIn('No problem scores in this section', resp.content) @ddt.data( ('', None, False, True), ('', None, True, True), (ShowCorrectness.ALWAYS, None, False, True), (ShowCorrectness.ALWAYS, None, True, True), (ShowCorrectness.ALWAYS, YESTERDAY, False, True), (ShowCorrectness.ALWAYS, YESTERDAY, True, True), (ShowCorrectness.ALWAYS, TODAY, False, True), (ShowCorrectness.ALWAYS, TODAY, True, True), (ShowCorrectness.ALWAYS, TOMORROW, False, True), (ShowCorrectness.ALWAYS, TOMORROW, True, True), (ShowCorrectness.NEVER, None, False, False), (ShowCorrectness.NEVER, None, True, False), (ShowCorrectness.NEVER, YESTERDAY, False, False), (ShowCorrectness.NEVER, YESTERDAY, True, False), (ShowCorrectness.NEVER, TODAY, False, False), (ShowCorrectness.NEVER, TODAY, True, False), (ShowCorrectness.NEVER, TOMORROW, False, False), (ShowCorrectness.NEVER, TOMORROW, True, False), (ShowCorrectness.PAST_DUE, None, False, True), (ShowCorrectness.PAST_DUE, None, True, True), (ShowCorrectness.PAST_DUE, YESTERDAY, False, True), (ShowCorrectness.PAST_DUE, YESTERDAY, True, True), (ShowCorrectness.PAST_DUE, TODAY, False, True), (ShowCorrectness.PAST_DUE, TODAY, True, True), (ShowCorrectness.PAST_DUE, TOMORROW, False, False), (ShowCorrectness.PAST_DUE, TOMORROW, True, False), ) @ddt.unpack def test_progress_page_hide_scores_from_learner(self, show_correctness, due_date_name, graded, show_grades): """ Test that problem scores are hidden on progress page when correctness is not available to the learner, and that they are visible when it is. """ due_date = self.DATES[due_date_name] self.setup_course(show_correctness=show_correctness, due_date=due_date, graded=graded) self.add_problem() self.client.login(username=self.user.username, password='test') resp = self._get_progress_page() # Ensure that expected text is present self.assert_progress_page_show_grades(resp, show_correctness, due_date, graded, show_grades, 0, 1, 0) # Submit answers to the problem, and re-fetch the progress page self.answer_problem() resp = self._get_progress_page() # Test that the expected text is still present. self.assert_progress_page_show_grades(resp, show_correctness, due_date, graded, show_grades, 1, 1, .5) @ddt.data( ('', None, False, True), ('', None, True, True), (ShowCorrectness.ALWAYS, None, False, True), (ShowCorrectness.ALWAYS, None, True, True), (ShowCorrectness.ALWAYS, YESTERDAY, False, True), (ShowCorrectness.ALWAYS, YESTERDAY, True, True), (ShowCorrectness.ALWAYS, TODAY, False, True), (ShowCorrectness.ALWAYS, TODAY, True, True), (ShowCorrectness.ALWAYS, TOMORROW, False, True), (ShowCorrectness.ALWAYS, TOMORROW, True, True), (ShowCorrectness.NEVER, None, False, False), (ShowCorrectness.NEVER, None, True, False), (ShowCorrectness.NEVER, YESTERDAY, False, False), (ShowCorrectness.NEVER, YESTERDAY, True, False), (ShowCorrectness.NEVER, TODAY, False, False), (ShowCorrectness.NEVER, TODAY, True, False), (ShowCorrectness.NEVER, TOMORROW, False, False), (ShowCorrectness.NEVER, TOMORROW, True, False), (ShowCorrectness.PAST_DUE, None, False, True), (ShowCorrectness.PAST_DUE, None, True, True), (ShowCorrectness.PAST_DUE, YESTERDAY, False, True), (ShowCorrectness.PAST_DUE, YESTERDAY, True, True), (ShowCorrectness.PAST_DUE, TODAY, False, True), (ShowCorrectness.PAST_DUE, TODAY, True, True), (ShowCorrectness.PAST_DUE, TOMORROW, False, True), (ShowCorrectness.PAST_DUE, TOMORROW, True, True), ) @ddt.unpack def test_progress_page_hide_scores_from_staff(self, show_correctness, due_date_name, graded, show_grades): """ Test that problem scores are hidden from staff viewing a learner's progress page only if show_correctness=never. """ due_date = self.DATES[due_date_name] self.setup_course(show_correctness=show_correctness, due_date=due_date, graded=graded) self.add_problem() # Login as a course staff user to view the student progress page. self.client.login(username=self.staff_user.username, password='test') resp = self._get_student_progress_page() # Ensure that expected text is present self.assert_progress_page_show_grades(resp, show_correctness, due_date, graded, show_grades, 0, 1, 0) # Submit answers to the problem, and re-fetch the progress page self.answer_problem() resp = self._get_student_progress_page() # Test that the expected text is still present. self.assert_progress_page_show_grades(resp, show_correctness, due_date, graded, show_grades, 1, 1, .5) @attr(shard=5) class VerifyCourseKeyDecoratorTests(TestCase): """ Tests for the ensure_valid_course_key decorator. """ def setUp(self): super(VerifyCourseKeyDecoratorTests, self).setUp() self.request = RequestFactory().get("foo") self.valid_course_id = "edX/test/1" self.invalid_course_id = "edX/" def test_decorator_with_valid_course_id(self): mocked_view = create_autospec(views.course_about) view_function = ensure_valid_course_key(mocked_view) view_function(self.request, course_id=self.valid_course_id) self.assertTrue(mocked_view.called) def test_decorator_with_invalid_course_id(self): mocked_view = create_autospec(views.course_about) view_function = ensure_valid_course_key(mocked_view) self.assertRaises(Http404, view_function, self.request, course_id=self.invalid_course_id) self.assertFalse(mocked_view.called) @attr(shard=5) class GenerateUserCertTests(ModuleStoreTestCase): """ Tests for the view function Generated User Certs """ def setUp(self): super(GenerateUserCertTests, self).setUp() self.student = UserFactory() self.course = CourseFactory.create( org='edx', number='verified', end=datetime.now(), display_name='Verified Course', grade_cutoffs={'cutoff': 0.75, 'Pass': 0.5}, self_paced=True ) self.enrollment = CourseEnrollment.enroll(self.student, self.course.id, mode='honor') self.assertTrue(self.client.login(username=self.student, password=TEST_PASSWORD)) self.url = reverse('generate_user_cert', kwargs={'course_id': unicode(self.course.id)}) def test_user_with_out_passing_grades(self): # If user has no grading then json will return failed message and badrequest code resp = self.client.post(self.url) self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertIn("Your certificate will be available when you pass the course.", resp.content) @patch('courseware.views.views.is_course_passed', return_value=True) @override_settings(CERT_QUEUE='certificates', LMS_SEGMENT_KEY="foobar") def test_user_with_passing_grade(self, mock_is_course_passed): # If user has above passing grading then json will return cert generating message and # status valid code # mocking xqueue and Segment analytics analytics_patcher = patch('courseware.views.views.segment') mock_tracker = analytics_patcher.start() self.addCleanup(analytics_patcher.stop) with patch('capa.xqueue_interface.XQueueInterface.send_to_queue') as mock_send_to_queue: mock_send_to_queue.return_value = (0, "Successfully queued") resp = self.client.post(self.url) self.assertEqual(resp.status_code, 200) # Verify Google Analytics event fired after generating certificate mock_tracker.track.assert_called_once_with( self.student.id, 'edx.bi.user.certificate.generate', { 'category': 'certificates', 'label': unicode(self.course.id) }, ) mock_tracker.reset_mock() def test_user_with_passing_existing_generating_cert(self): # If user has passing grade but also has existing generating cert # then json will return cert generating message with bad request code GeneratedCertificateFactory.create( user=self.student, course_id=self.course.id, status=CertificateStatuses.generating, mode='verified' ) with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summary = {'grade': 'Pass', 'percent': 0.75} resp = self.client.post(self.url) self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertIn("Certificate is being created.", resp.content) @override_settings(CERT_QUEUE='certificates', LMS_SEGMENT_KEY="foobar") def test_user_with_passing_existing_downloadable_cert(self): # If user has already downloadable certificate # then json will return cert generating message with bad request code GeneratedCertificateFactory.create( user=self.student, course_id=self.course.id, status=CertificateStatuses.downloadable, mode='verified' ) with patch('lms.djangoapps.grades.course_grade_factory.CourseGradeFactory.read') as mock_create: course_grade = mock_create.return_value course_grade.passed = True course_grade.summay = {'grade': 'Pass', 'percent': 0.75} resp = self.client.post(self.url) self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertIn("Certificate has already been created.", resp.content) def test_user_with_non_existing_course(self): # If try to access a course with valid key pattern then it will return # bad request code with course is not valid message resp = self.client.post('/courses/def/abc/in_valid/generate_user_cert') self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertIn("Course is not valid", resp.content) def test_user_with_invalid_course_id(self): # If try to access a course with invalid key pattern then 404 will return resp = self.client.post('/courses/def/generate_user_cert') self.assertEqual(resp.status_code, 404) def test_user_without_login_return_error(self): # If user try to access without login should see a bad request status code with message self.client.logout() resp = self.client.post(self.url) self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) self.assertIn(u"You must be signed in to {platform_name} to create a certificate.".format( platform_name=settings.PLATFORM_NAME ), resp.content.decode('utf-8')) class ActivateIDCheckerBlock(XBlock): """ XBlock for checking for an activate_block_id entry in the render context. """ # We don't need actual children to test this. has_children = False def student_view(self, context): """ A student view that displays the activate_block_id context variable. """ result = Fragment() if 'activate_block_id' in context: result.add_content(u"Activate Block ID: {block_id}

".format(block_id=context['activate_block_id'])) return result class ViewCheckerBlock(XBlock): """ XBlock for testing user state in views. """ has_children = True state = String(scope=Scope.user_state) position = 0 def student_view(self, context): # pylint: disable=unused-argument """ A student_view that asserts that the ``state`` field for this block matches the block's usage_id. """ msg = "{} != {}".format(self.state, self.scope_ids.usage_id) assert self.state == unicode(self.scope_ids.usage_id), msg fragments = self.runtime.render_children(self) result = Fragment( content=u"

ViewCheckerPassed: {}

\n{}".format( unicode(self.scope_ids.usage_id), "\n".join(fragment.content for fragment in fragments), ) ) return result @attr(shard=5) @ddt.ddt class TestIndexView(ModuleStoreTestCase): """ Tests of the courseware.views.index view. """ SEO_WAFFLE_FLAG = CourseWaffleFlag(WaffleFlagNamespace(name='seo'), 'enable_anonymous_courseware_access') @XBlock.register_temp_plugin(ViewCheckerBlock, 'view_checker') @ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split) def test_student_state(self, default_store): """ Verify that saved student state is loaded for xblocks rendered in the index view. """ user = UserFactory() with modulestore().default_store(default_store): course = CourseFactory.create() chapter = ItemFactory.create(parent=course, category='chapter') section = ItemFactory.create(parent=chapter, category='view_checker', display_name="Sequence Checker") vertical = ItemFactory.create(parent=section, category='view_checker', display_name="Vertical Checker") block = ItemFactory.create(parent=vertical, category='view_checker', display_name="Block Checker") for item in (section, vertical, block): StudentModuleFactory.create( student=user, course_id=course.id, module_state_key=item.scope_ids.usage_id, state=json.dumps({'state': unicode(item.scope_ids.usage_id)}) ) CourseOverview.load_from_module_store(course.id) CourseEnrollmentFactory(user=user, course_id=course.id) self.assertTrue(self.client.login(username=user.username, password='test')) response = self.client.get( reverse( 'courseware_section', kwargs={ 'course_id': unicode(course.id), 'chapter': chapter.url_name, 'section': section.url_name, } ) ) # Trigger the assertions embedded in the ViewCheckerBlocks self.assertEquals(response.content.count("ViewCheckerPassed"), 3) @XBlock.register_temp_plugin(ActivateIDCheckerBlock, 'id_checker') def test_activate_block_id(self): user = UserFactory() course = CourseFactory.create() with self.store.bulk_operations(course.id): chapter = ItemFactory.create(parent=course, category='chapter') section = ItemFactory.create(parent=chapter, category='sequential', display_name="Sequence") vertical = ItemFactory.create(parent=section, category='vertical', display_name="Vertical") ItemFactory.create(parent=vertical, category='id_checker', display_name="ID Checker") CourseOverview.load_from_module_store(course.id) CourseEnrollmentFactory(user=user, course_id=course.id) self.assertTrue(self.client.login(username=user.username, password='test')) response = self.client.get( reverse( 'courseware_section', kwargs={ 'course_id': unicode(course.id), 'chapter': chapter.url_name, 'section': section.url_name, } ) + '?activate_block_id=test_block_id' ) self.assertIn("Activate Block ID: test_block_id", response.content) def test_anonymous_access(self): course = CourseFactory() with self.store.bulk_operations(course.id): chapter = ItemFactory(parent=course, category='chapter') section = ItemFactory(parent=chapter, category='sequential') ItemFactory.create(parent=section, category='vertical', display_name="Vertical") url = reverse( 'courseware_section', kwargs={ 'course_id': str(course.id), 'chapter': chapter.url_name, 'section': section.url_name, } ) response = self.client.get(url, follow=False) assert response.status_code == 302 waffle_flag = CourseWaffleFlag(WaffleFlagNamespace(name='seo'), 'enable_anonymous_courseware_access') with override_waffle_flag(waffle_flag, active=True): response = self.client.get(url, follow=False) assert response.status_code == 200 self.assertIn('data-save-position="false"', response.content) self.assertIn('data-show-completion="false"', response.content) user = UserFactory() CourseEnrollmentFactory(user=user, course_id=course.id) self.assertTrue(self.client.login(username=user.username, password='test')) response = self.client.get(url, follow=False) assert response.status_code == 200 self.assertIn('data-save-position="true"', response.content) self.assertIn('data-show-completion="true"', response.content) @attr(shard=5) @ddt.ddt class TestIndexViewCompleteOnView(ModuleStoreTestCase, CompletionWaffleTestMixin): """ Tests CompleteOnView is set up correctly in CoursewareIndex. """ def setup_course(self, default_store): """ Set up course content for modulestore. """ # pylint:disable=attribute-defined-outside-init self.request_factory = RequestFactory() self.user = UserFactory() with modulestore().default_store(default_store): self.course = CourseFactory.create() with self.store.bulk_operations(self.course.id): self.chapter = ItemFactory.create( parent_location=self.course.location, category='chapter', display_name='Week 1' ) self.section_1 = ItemFactory.create( parent_location=self.chapter.location, category='sequential', display_name='Lesson 1' ) self.vertical_1 = ItemFactory.create( parent_location=self.section_1.location, category='vertical', display_name='Subsection 1' ) self.html_1_1 = ItemFactory.create( parent_location=self.vertical_1.location, category='html', display_name="HTML 1_1" ) self.problem_1 = ItemFactory.create( parent_location=self.vertical_1.location, category='problem', display_name="Problem 1" ) self.html_1_2 = ItemFactory.create( parent_location=self.vertical_1.location, category='html', display_name="HTML 1_2" ) self.section_2 = ItemFactory.create( parent_location=self.chapter.location, category='sequential', display_name='Lesson 2' ) self.vertical_2 = ItemFactory.create( parent_location=self.section_2.location, category='vertical', display_name='Subsection 2' ) self.video_2 = ItemFactory.create( parent_location=self.vertical_2.location, category='video', display_name="Video 2" ) self.problem_2 = ItemFactory.create( parent_location=self.vertical_2.location, category='problem', display_name="Problem 2" ) self.section_1_url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(self.course.id), 'chapter': self.chapter.url_name, 'section': self.section_1.url_name, } ) self.section_2_url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(self.course.id), 'chapter': self.chapter.url_name, 'section': self.section_2.url_name, } ) CourseOverview.load_from_module_store(self.course.id) CourseEnrollmentFactory(user=self.user, course_id=self.course.id) @ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split) def test_completion_service_disabled(self, default_store): self.setup_course(default_store) self.assertTrue(self.client.login(username=self.user.username, password='test')) response = self.client.get(self.section_1_url) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) response = self.client.get(self.section_2_url) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) @ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split) def test_completion_service_enabled(self, default_store): self.override_waffle_switch(True) self.setup_course(default_store) self.assertTrue(self.client.login(username=self.user.username, password='test')) response = self.client.get(self.section_1_url) self.assertIn('data-mark-completed-on-view-after-delay', response.content) self.assertEquals(response.content.count("data-mark-completed-on-view-after-delay"), 2) request = self.request_factory.post( '/', data=json.dumps({"completion": 1}), content_type='application/json', ) request.user = self.user response = handle_xblock_callback( request, unicode(self.course.id), quote_slashes(unicode(self.html_1_1.scope_ids.usage_id)), 'publish_completion', ) self.assertEqual(json.loads(response.content), {'result': "ok"}) response = self.client.get(self.section_1_url) self.assertIn('data-mark-completed-on-view-after-delay', response.content) self.assertEquals(response.content.count("data-mark-completed-on-view-after-delay"), 1) request = self.request_factory.post( '/', data=json.dumps({"completion": 1}), content_type='application/json', ) request.user = self.user response = handle_xblock_callback( request, unicode(self.course.id), quote_slashes(unicode(self.html_1_2.scope_ids.usage_id)), 'publish_completion', ) self.assertEqual(json.loads(response.content), {'result': "ok"}) response = self.client.get(self.section_1_url) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) response = self.client.get(self.section_2_url) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) @attr(shard=5) @ddt.ddt class TestIndexViewWithVerticalPositions(ModuleStoreTestCase): """ Test the index view to handle vertical positions. Confirms that first position is loaded if input position is non-positive or greater than number of positions available. """ def setUp(self): """ Set up initial test data """ super(TestIndexViewWithVerticalPositions, self).setUp() self.user = UserFactory() # create course with 3 positions self.course = CourseFactory.create() with self.store.bulk_operations(self.course.id): self.chapter = ItemFactory.create(parent=self.course, category='chapter') self.section = ItemFactory.create(parent=self.chapter, category='sequential', display_name="Sequence") ItemFactory.create(parent=self.section, category='vertical', display_name="Vertical1") ItemFactory.create(parent=self.section, category='vertical', display_name="Vertical2") ItemFactory.create(parent=self.section, category='vertical', display_name="Vertical3") CourseOverview.load_from_module_store(self.course.id) self.client.login(username=self.user, password='test') CourseEnrollmentFactory(user=self.user, course_id=self.course.id) def _get_course_vertical_by_position(self, input_position): """ Returns client response to input position. """ return self.client.get( reverse( 'courseware_position', kwargs={ 'course_id': unicode(self.course.id), 'chapter': self.chapter.url_name, 'section': self.section.url_name, 'position': input_position, } ) ) def _assert_correct_position(self, response, expected_position): """ Asserts that the expected position and the position in the response are the same """ self.assertIn('data-position="{}"'.format(expected_position), response.content) @ddt.data(("-1", 1), ("0", 1), ("-0", 1), ("2", 2), ("5", 1)) @ddt.unpack def test_vertical_positions(self, input_position, expected_position): """ Tests the following cases: * Load first position when negative position inputted. * Load first position when 0/-0 position inputted. * Load given position when 0 < input_position <= num_positions_available. * Load first position when positive position > num_positions_available. """ resp = self._get_course_vertical_by_position(input_position) self._assert_correct_position(resp, expected_position) @attr(shard=5) class TestIndexViewWithGating(ModuleStoreTestCase, MilestonesTestCaseMixin): """ Test the index view for a course with gated content """ def setUp(self): """ Set up the initial test data """ super(TestIndexViewWithGating, self).setUp() self.user = UserFactory() self.course = CourseFactory.create() with self.store.bulk_operations(self.course.id): self.course.enable_subsection_gating = True self.course.save() self.store.update_item(self.course, 0) self.chapter = ItemFactory.create(parent=self.course, category="chapter", display_name="Chapter") self.open_seq = ItemFactory.create( parent=self.chapter, category='sequential', display_name="Open Sequential" ) ItemFactory.create(parent=self.open_seq, category='problem', display_name="Problem 1") self.gated_seq = ItemFactory.create( parent=self.chapter, category='sequential', display_name="Gated Sequential" ) ItemFactory.create(parent=self.gated_seq, category='problem', display_name="Problem 2") gating_api.add_prerequisite(self.course.id, self.open_seq.location) gating_api.set_required_content(self.course.id, self.gated_seq.location, self.open_seq.location, 100) CourseEnrollmentFactory(user=self.user, course_id=self.course.id) def test_index_with_gated_sequential(self): """ Test index view with a gated sequential raises Http404 """ 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.gated_seq.url_name, } ) ) 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) def test_index_with_course_duration_limits(self): """ Test that the courseware contains the course expiration banner when course_duration_limits are enabled. """ CourseDurationLimitConfig.objects.create(enabled=True, enabled_as_of=date(2018, 1, 1)) self.assertTrue(self.client.login(username=self.user.username, password='test')) add_course_mode(self.course, upgrade_deadline_expired=False) 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) 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. """ CourseDurationLimitConfig.objects.create(enabled=False) self.assertTrue(self.client.login(username=self.user.username, password='test')) add_course_mode(self.course, upgrade_deadline_expired=False) 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): """ Tests for the courseware.render_xblock endpoint. This class overrides the get_response method, which is used by the tests defined in RenderXBlockTestMixin. """ def setUp(self): reload_django_url_config() super(TestRenderXBlock, self).setUp() def test_render_xblock_with_invalid_usage_key(self): """ Test XBlockRendering with invalid usage key """ response = self.get_response(usage_key='some_invalid_usage_key') self.assertEqual(response.status_code, 404) self.assertIn('Page not found', response.content) def get_response(self, usage_key, url_encoded_params=None): """ Overridable method to get the response from the endpoint that is being tested. """ url = reverse('render_xblock', kwargs={'usage_key_string': unicode(usage_key)}) if url_encoded_params: url += '?' + url_encoded_params return self.client.get(url) def test_render_xblock_with_completion_service_disabled(self): """ Test that render_xblock does not set up the CompletionOnViewService. """ self.setup_course(ModuleStoreEnum.Type.split) self.setup_user(admin=True, enroll=True, login=True) response = self.get_response(usage_key=self.html_block.location) self.assertEqual(response.status_code, 200) self.assertIn('data-enable-completion-on-view-service="false"', response.content) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) def test_render_xblock_with_completion_service_enabled(self): """ Test that render_xblock sets up the CompletionOnViewService for relevant xblocks. """ self.override_waffle_switch(True) self.setup_course(ModuleStoreEnum.Type.split) self.setup_user(admin=False, enroll=True, login=True) response = self.get_response(usage_key=self.html_block.location) self.assertEqual(response.status_code, 200) self.assertIn('data-enable-completion-on-view-service="true"', response.content) self.assertIn('data-mark-completed-on-view-after-delay', response.content) request = RequestFactory().post( '/', data=json.dumps({"completion": 1}), content_type='application/json', ) request.user = self.user response = handle_xblock_callback( request, unicode(self.course.id), quote_slashes(unicode(self.html_block.location)), 'publish_completion', ) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content), {'result': "ok"}) response = self.get_response(usage_key=self.html_block.location) self.assertEqual(response.status_code, 200) self.assertIn('data-enable-completion-on-view-service="false"', response.content) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) response = self.get_response(usage_key=self.problem_block.location) self.assertEqual(response.status_code, 200) self.assertIn('data-enable-completion-on-view-service="false"', response.content) self.assertNotIn('data-mark-completed-on-view-after-delay', response.content) class TestRenderXBlockSelfPaced(TestRenderXBlock): """ Test rendering XBlocks for a self-paced course. Relies on the query count assertions in the tests defined by RenderXBlockMixin. """ def setUp(self): super(TestRenderXBlockSelfPaced, self).setUp() def course_options(self): options = super(TestRenderXBlockSelfPaced, self).course_options() options['self_paced'] = True return options @attr(shard=5) class TestIndexViewCrawlerStudentStateWrites(SharedModuleStoreTestCase): """ Ensure that courseware index requests do not trigger student state writes. This is to prevent locking issues that have caused latency spikes in the courseware_studentmodule table when concurrent requests each try to update the same rows for sequence, section, and course positions. """ @classmethod def setUpClass(cls): """Set up the simplest course possible.""" # setUpClassAndTestData() already calls setUpClass on SharedModuleStoreTestCase # pylint: disable=super-method-not-called with super(TestIndexViewCrawlerStudentStateWrites, cls).setUpClassAndTestData(): cls.course = CourseFactory.create() with cls.store.bulk_operations(cls.course.id): cls.chapter = ItemFactory.create(category='chapter', parent_location=cls.course.location) cls.section = ItemFactory.create(category='sequential', parent_location=cls.chapter.location) cls.vertical = ItemFactory.create(category='vertical', parent_location=cls.section.location) @classmethod def setUpTestData(cls): """Set up and enroll our fake user in the course.""" cls.user = UserFactory() CourseEnrollment.enroll(cls.user, cls.course.id) def setUp(self): """Do the client login.""" super(TestIndexViewCrawlerStudentStateWrites, self).setUp() self.client.login(username=self.user.username, password=TEST_PASSWORD) def test_write_by_default(self): """By default, always write student state, regardless of user agent.""" with patch('courseware.model_data.UserStateCache.set_many') as patched_state_client_set_many: # Simulate someone using Chrome self._load_courseware('Mozilla/5.0 AppleWebKit/537.36') self.assertTrue(patched_state_client_set_many.called) patched_state_client_set_many.reset_mock() # Common crawler user agent self._load_courseware('edX-downloader/0.1') self.assertTrue(patched_state_client_set_many.called) def test_writes_with_config(self): """Test state writes (or lack thereof) based on config values.""" CrawlersConfig.objects.create(known_user_agents='edX-downloader,crawler_foo', enabled=True) with patch('courseware.model_data.UserStateCache.set_many') as patched_state_client_set_many: # Exact matching of crawler user agent self._load_courseware('crawler_foo') self.assertFalse(patched_state_client_set_many.called) # Partial matching of crawler user agent self._load_courseware('edX-downloader/0.1') self.assertFalse(patched_state_client_set_many.called) # Simulate an actual browser hitting it (we should write) self._load_courseware('Mozilla/5.0 AppleWebKit/537.36') self.assertTrue(patched_state_client_set_many.called) # Disabling the crawlers config should revert us to default behavior CrawlersConfig.objects.create(enabled=False) self.test_write_by_default() def _load_courseware(self, user_agent): """Helper to load the actual courseware page.""" url = reverse( 'courseware_section', kwargs={ 'course_id': unicode(self.course.id), 'chapter': unicode(self.chapter.location.block_id), 'section': unicode(self.section.location.block_id), } ) response = self.client.get(url, HTTP_USER_AGENT=user_agent) # Make sure we get back an actual 200, and aren't redirected because we # messed up the setup somehow (e.g. didn't enroll properly) self.assertEqual(response.status_code, 200) @attr(shard=5) class EnterpriseConsentTestCase(EnterpriseTestConsentRequired, ModuleStoreTestCase): """ Ensure that the Enterprise Data Consent redirects are in place only when consent is required. """ def setUp(self): super(EnterpriseConsentTestCase, self).setUp() self.user = UserFactory.create() self.assertTrue(self.client.login(username=self.user.username, password='test')) self.course = CourseFactory.create() CourseOverview.load_from_module_store(self.course.id) CourseEnrollmentFactory(user=self.user, course_id=self.course.id) @patch('openedx.features.enterprise_support.api.enterprise_customer_for_request') def test_consent_required(self, mock_enterprise_customer_for_request): """ Test that enterprise data sharing consent is required when enabled for the various courseware views. """ # ENT-924: Temporary solution to replace sensitive SSO usernames. mock_enterprise_customer_for_request.return_value = None course_id = unicode(self.course.id) for url in ( reverse("courseware", kwargs=dict(course_id=course_id)), reverse("progress", kwargs=dict(course_id=course_id)), reverse("student_progress", kwargs=dict(course_id=course_id, student_id=str(self.user.id))), ): self.verify_consent_required(self.client, url) @attr(shard=5) @ddt.ddt class AccessUtilsTestCase(ModuleStoreTestCase): """ Test access utilities """ @ddt.data( (1, False), (-1, True) ) @ddt.unpack @patch.dict('django.conf.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_is_course_open_for_learner(self, start_date_modifier, expected_value): staff_user = AdminFactory() start_date = datetime.now(UTC) + timedelta(days=start_date_modifier) course = CourseFactory.create(start=start_date) self.assertEqual(bool(check_course_open_for_learner(staff_user, course)), expected_value)