def test_repeat_course(self): # Initially course shouldn't be authorized self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id)) # Test authorizing the course, which should totally work form_data = {'course_id': self.course.id.to_deprecated_string(), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation should work self.assertTrue(form.is_valid()) form.save() # Check that this course is authorized self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Now make a new course authorization with the same course id that tries to turn email off form_data = {'course_id': self.course.id.to_deprecated_string(), 'email_enabled': False} form = CourseAuthorizationAdminForm(data=form_data) # Validation should not work because course_id field is unique self.assertFalse(form.is_valid()) self.assertEquals( "Course authorization with this Course id already exists.", form._errors['course_id'][0] # pylint: disable=protected-access ) with self.assertRaisesRegexp(ValueError, "The CourseAuthorization could not be created because the data didn't validate."): form.save() # Course should still be authorized (invalid attempt had no effect) self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
def test_email_authorized(self): # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email is enabled for this course self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view is not in the response # if this course isn't authorized response = self.client.get(self.url) self.assertTrue(self.email_modal_link in response.content)
def test_course_authorized_feature_off(self): # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email IS enabled for this course self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view IS NOT in the response response = self.client.get(self.url) self.assertFalse(self.email_link in response.content)
def test_authorize_mongo_course(self): # Initially course shouldn't be authorized self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id)) # Test authorizing the course, which should totally work form_data = {'course_id': self.course.id.to_deprecated_string(), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation should work self.assertTrue(form.is_valid()) form.save() # Check that this course is authorized self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id))
def test_email_authorized(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email is enabled for this course self.assertTrue(BulkEmailFlag.feature_enabled(self.course.id)) # Assert that the URL for the email view is not in the response # if this course isn't authorized response = self.client.get(self.url) self.assertIn(self.email_modal_link, response.content)
def test_course_authorized_feature_off(self): BulkEmailFlag.objects.create(enabled=False, require_course_email_auth=True) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that this course is authorized for instructor email, but the feature is not enabled self.assertFalse(BulkEmailFlag.feature_enabled(self.course.id)) self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view IS NOT in the response response = self.client.get(self.url) self.assertFalse(self.email_link in response.content)
def test_creation_auth_off(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=False) course_id = CourseKey.from_string('blahx/blah101/ehhhhhhh') # Test that course is authorized by default, since auth is turned off self.assertTrue(BulkEmailFlag.feature_enabled(course_id)) # Use the admin interface to unauthorize the course cauth = CourseAuthorization(course_id=course_id, email_enabled=False) cauth.save() # Now, course should STILL be authorized! self.assertTrue(BulkEmailFlag.feature_enabled(course_id))
def test_authorize_mongo_course(self): # Initially course shouldn't be authorized self.assertFalse( CourseAuthorization.instructor_email_enabled(self.course.id)) # Test authorizing the course, which should totally work form_data = {'course_id': self.course.id, 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation should work self.assertTrue(form.is_valid()) form.save() # Check that this course is authorized self.assertTrue( CourseAuthorization.instructor_email_enabled(self.course.id))
def test_course_authorized_feature_off(self): BulkEmailFlag.objects.create(enabled=False, require_course_email_auth=True) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that this course is authorized for instructor email, but the feature is not enabled self.assertFalse(is_bulk_email_feature_enabled(self.course.id)) self.assertTrue(is_bulk_email_enabled_for_course(self.course.id)) # Assert that the URL for the email view IS NOT in the response response = self.client.get(self.url) self.assertNotIn(self.email_link, response.content)
def test_creation_auth_on(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) course_id = CourseKey.from_string('abc/123/doremi') # Test that course is not authorized by default self.assertFalse(BulkEmailFlag.feature_enabled(course_id)) # Authorize cauth = CourseAuthorization(course_id=course_id, email_enabled=True) cauth.save() # Now, course should be authorized self.assertTrue(BulkEmailFlag.feature_enabled(course_id)) self.assertEqual( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Enabled" ) # Unauthorize by explicitly setting email_enabled to False cauth.email_enabled = False cauth.save() # Test that course is now unauthorized self.assertFalse(BulkEmailFlag.feature_enabled(course_id)) self.assertEqual( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Not Enabled" )
def test_email_unauthorized(self): # Assert that instructor email is not enabled for this course self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view is not in the response # if this course isn't authorized response = self.client.get(self.url) self.assertFalse(self.email_modal_link in response.content)
def test_email_flag_authorized(self): # Assert that the URL for the email view is in the response # email is enabled, and this course is authorized to send email # Assert that instructor email is not enabled for this course self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id)) response = self.client.get(self.url) self.assertFalse(self.email_link in response.content) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email is enabled for this course self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) response = self.client.get(self.url) self.assertTrue(self.email_link in response.content)
def test_course_authorized(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) # Assert that instructor email is not enabled for this course self.assertFalse(is_bulk_email_feature_enabled(self.course.id)) # Assert that the URL for the email view is not in the response response = self.client.get(self.url) self.assertNotContains(response, self.email_link) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email is enabled for this course self.assertTrue(is_bulk_email_feature_enabled(self.course.id)) # Assert that the URL for the email view is in the response response = self.client.get(self.url) self.assertContains(response, self.email_link)
def test_creation_auth_on(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) course_id = CourseKey.from_string('abc/123/doremi') # Test that course is not authorized by default self.assertFalse(is_bulk_email_feature_enabled(course_id)) # Authorize cauth = CourseAuthorization(course_id=course_id, email_enabled=True) cauth.save() # Now, course should be authorized self.assertTrue(is_bulk_email_feature_enabled(course_id)) self.assertEqual( str(cauth), "Course 'abc/123/doremi': Instructor Email Enabled" ) # Unauthorize by explicitly setting email_enabled to False cauth.email_enabled = False cauth.save() # Test that course is now unauthorized self.assertFalse(is_bulk_email_feature_enabled(course_id)) self.assertEqual( str(cauth), "Course 'abc/123/doremi': Instructor Email Not Enabled" )
def instructor_dashboard_2(request, course_id): """ Display the instructor dashboard for a course. """ course = get_course_by_id(course_id, depth=None) is_studio_course = (modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE) access = { 'admin': request.user.is_staff, 'instructor': has_access(request.user, course, 'instructor'), 'staff': has_access(request.user, course, 'staff'), 'forum_admin': has_forum_access( request.user, course_id, FORUM_ROLE_ADMINISTRATOR ), } if not access['staff']: raise Http404() sections = [ _section_course_info(course_id, access), _section_membership(course_id, access), _section_student_admin(course_id, access), _section_data_download(course_id, access), _section_analytics(course_id, access), ] if (settings.FEATURES.get('INDIVIDUAL_DUE_DATES') and access['instructor']): sections.insert(3, _section_extensions(course)) # Gate access to course email by feature flag & by course-specific authorization if settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] and \ is_studio_course and CourseAuthorization.instructor_email_enabled(course_id): sections.append(_section_send_email(course_id, access, course)) # Gate access to Metrics tab by featue flag and staff authorization if settings.FEATURES['CLASS_DASHBOARD'] and access['staff']: sections.append(_section_metrics(course_id, access)) studio_url = None if is_studio_course: studio_url = get_cms_course_link(course) enrollment_count = sections[0]['enrollment_count'] disable_buttons = False max_enrollment_for_buttons = settings.FEATURES.get("MAX_ENROLLMENT_INSTR_BUTTONS") if max_enrollment_for_buttons is not None: disable_buttons = enrollment_count > max_enrollment_for_buttons context = { 'course': course, 'old_dashboard_url': reverse('instructor_dashboard', kwargs={'course_id': course_id}), 'studio_url': studio_url, 'sections': sections, 'disable_buttons': disable_buttons, } return render_to_response('instructor/instructor_dashboard_2/instructor_dashboard_2.html', context)
def test_send_mail_authorized(self): """ Test 'Send email' action when course is authorized to send email. """ course_authorization = CourseAuthorization(course_id=self.course.id, email_enabled=True) course_authorization.save() session = self.client.session session[u'idash_mode:{0}'.format(self.course.location.course_id)] = 'Email' session.save() response = self.client.post( self.url, { 'action': 'Send email', 'to_option': 'all', 'subject': 'Welcome to the course!', 'message': 'Lets start with an introduction!', } ) self.assertContains(response, "Your email was successfully queued for sending.")
def test_send_mail_authorized(self): """ Test 'Send email' action when course is authorized to send email. """ course_authorization = CourseAuthorization(course_id=self.course.id, email_enabled=True) course_authorization.save() session = self.client.session session[u'idash_mode:{0}'.format(self.course.location.course_key.to_deprecated_string())] = 'Email' session.save() response = self.client.post( self.url, { 'action': 'Send email', 'to_option': 'all', 'subject': 'Welcome to the course!', 'message': 'Lets start with an introduction!', } ) self.assertContains(response, "Your email was successfully queued for sending.")
def is_bulk_email_enabled_for_course(course_id): """ Arguments: course_id: the course id of the course Returns: bool: True if the Bulk Email feature is enabled for the course associated with the course_id; False otherwise """ return CourseAuthorization.instructor_email_enabled(course_id)
def instructor_dashboard_2(request, course_id): """Display the instructor dashboard for a course.""" course = get_course_by_id(course_id, depth=None) is_studio_course = modulestore().get_modulestore_type(course_id) == MONGO_MODULESTORE_TYPE access = { "admin": request.user.is_staff, "instructor": has_access(request.user, course, "instructor"), "staff": has_access(request.user, course, "staff"), "forum_admin": has_forum_access(request.user, course_id, FORUM_ROLE_ADMINISTRATOR), } if not access["staff"]: raise Http404() sections = [ _section_course_info(course_id, access), _section_membership(course_id, access), _section_student_admin(course_id, access), _section_data_download(course_id, access), _section_analytics(course_id, access), ] if settings.FEATURES.get("INDIVIDUAL_DUE_DATES") and access["instructor"]: sections.insert(3, _section_extensions(course)) # Gate access to course email by feature flag & by course-specific authorization if ( settings.FEATURES["ENABLE_INSTRUCTOR_EMAIL"] and is_studio_course and CourseAuthorization.instructor_email_enabled(course_id) ): sections.append(_section_send_email(course_id, access, course)) studio_url = None if is_studio_course: studio_url = get_cms_course_link(course) enrollment_count = sections[0]["enrollment_count"] disable_buttons = False max_enrollment_for_buttons = settings.FEATURES.get("MAX_ENROLLMENT_INSTR_BUTTONS") if max_enrollment_for_buttons is not None: disable_buttons = enrollment_count > max_enrollment_for_buttons context = { "course": course, "old_dashboard_url": reverse("instructor_dashboard", kwargs={"course_id": course_id}), "studio_url": studio_url, "sections": sections, "disable_buttons": disable_buttons, } return render_to_response("instructor/instructor_dashboard_2/instructor_dashboard_2.html", context)
def test_email_flag_true_mongo_true(self): # Assert that instructor email is enabled for this course - since REQUIRE_COURSE_EMAIL_AUTH is False, # all courses should be authorized to use email. self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view is in the response response = self.client.get(self.url) self.assertIn(self.email_link, response.content) send_to_label = '<label for="id_to">Send to:</label>' self.assertTrue(send_to_label in response.content) self.assertEqual(response.status_code, 200)
def test_repeat_course(self): # Initially course shouldn't be authorized self.assertFalse( CourseAuthorization.instructor_email_enabled(self.course.id)) # Test authorizing the course, which should totally work form_data = { 'course_id': self.course.id.to_deprecated_string(), 'email_enabled': True } form = CourseAuthorizationAdminForm(data=form_data) # Validation should work self.assertTrue(form.is_valid()) form.save() # Check that this course is authorized self.assertTrue( CourseAuthorization.instructor_email_enabled(self.course.id)) # Now make a new course authorization with the same course id that tries to turn email off form_data = { 'course_id': self.course.id.to_deprecated_string(), 'email_enabled': False } form = CourseAuthorizationAdminForm(data=form_data) # Validation should not work because course_id field is unique self.assertFalse(form.is_valid()) self.assertEquals( "Course authorization with this Course id already exists.", form._errors['course_id'][0] # pylint: disable=protected-access ) with self.assertRaisesRegexp( ValueError, "The CourseAuthorization could not be created because the data didn't validate." ): form.save() # Course should still be authorized (invalid attempt had no effect) self.assertTrue( CourseAuthorization.instructor_email_enabled(self.course.id))
def bulk_email_is_enabled_for_course(course_id): """ Staff can only send bulk email for a course if all the following conditions are true: 1. Bulk email feature flag is on. 2. It is a studio course. 3. Bulk email is enabled for the course. """ bulk_email_enabled_globally = (settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] is True) bulk_email_enabled_for_course = CourseAuthorization.instructor_email_enabled(course_id) if bulk_email_enabled_globally and bulk_email_enabled_for_course: return True return False
def bulk_email_is_enabled_for_course(course_id): """ Staff can only send bulk email for a course if all the following conditions are true: 1. Bulk email feature flag is on. 2. It is a studio course. 3. Bulk email is enabled for the course. """ bulk_email_enabled_globally = (settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] == True) is_studio_course = (modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE) bulk_email_enabled_for_course = CourseAuthorization.instructor_email_enabled(course_id) if bulk_email_enabled_globally and is_studio_course and bulk_email_enabled_for_course: return True return False
def test_creation_auth_off(self): course_id = SlashSeparatedCourseKey('blahx', 'blah101', 'ehhhhhhh') # Test that course is authorized by default, since auth is turned off self.assertTrue(CourseAuthorization.instructor_email_enabled(course_id)) # Use the admin interface to unauthorize the course cauth = CourseAuthorization(course_id=course_id, email_enabled=False) cauth.save() # Now, course should STILL be authorized! self.assertTrue(CourseAuthorization.instructor_email_enabled(course_id))
def test_creation_auth_on(self): BulkEmailFlag.objects.create(enabled=True, require_course_email_auth=True) course_id = SlashSeparatedCourseKey('abc', '123', 'doremi') # Test that course is not authorized by default self.assertFalse(BulkEmailFlag.feature_enabled(course_id)) # Authorize cauth = CourseAuthorization(course_id=course_id, email_enabled=True) cauth.save() # Now, course should be authorized self.assertTrue(BulkEmailFlag.feature_enabled(course_id)) self.assertEquals(cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Enabled") # Unauthorize by explicitly setting email_enabled to False cauth.email_enabled = False cauth.save() # Test that course is now unauthorized self.assertFalse(BulkEmailFlag.feature_enabled(course_id)) self.assertEquals( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Not Enabled")
def bulk_email_is_enabled_for_course(course_id): """ Staff can only send bulk email for a course if all the following conditions are true: 1. Bulk email feature flag is on. 2. It is a studio course. 3. Bulk email is enabled for the course. """ bulk_email_enabled_globally = ( settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] == True) is_studio_course = (modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE) bulk_email_enabled_for_course = CourseAuthorization.instructor_email_enabled( course_id) if bulk_email_enabled_globally and is_studio_course and bulk_email_enabled_for_course: return True return False
def test_course_authorized(self): # Assert that instructor email is not enabled for this course self.assertFalse(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view is not in the response response = self.client.get(self.url) self.assertFalse(self.email_link in response.content) # Authorize the course to use email cauth = CourseAuthorization(course_id=self.course.id, email_enabled=True) cauth.save() # Assert that instructor email is enabled for this course self.assertTrue(CourseAuthorization.instructor_email_enabled(self.course.id)) # Assert that the URL for the email view is in the response response = self.client.get(self.url) self.assertTrue(self.email_link in response.content)
def test_creation_auth_on(self): course_id = SlashSeparatedCourseKey('abc', '123', 'doremi') # Test that course is not authorized by default self.assertFalse(CourseAuthorization.instructor_email_enabled(course_id)) # Authorize cauth = CourseAuthorization(course_id=course_id, email_enabled=True) cauth.save() # Now, course should be authorized self.assertTrue(CourseAuthorization.instructor_email_enabled(course_id)) self.assertEquals( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Enabled" ) # Unauthorize by explicitly setting email_enabled to False cauth.email_enabled = False cauth.save() # Test that course is now unauthorized self.assertFalse(CourseAuthorization.instructor_email_enabled(course_id)) self.assertEquals( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Not Enabled" )
def test_creation_auth_on(self): course_id = 'abc/123/doremi' # Test that course is not authorized by default self.assertFalse( CourseAuthorization.instructor_email_enabled(course_id)) # Authorize cauth = CourseAuthorization(course_id=course_id, email_enabled=True) cauth.save() # Now, course should be authorized self.assertTrue( CourseAuthorization.instructor_email_enabled(course_id)) self.assertEquals(cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Enabled") # Unauthorize by explicitly setting email_enabled to False cauth.email_enabled = False cauth.save() # Test that course is now unauthorized self.assertFalse( CourseAuthorization.instructor_email_enabled(course_id)) self.assertEquals( cauth.__unicode__(), "Course 'abc/123/doremi': Instructor Email Not Enabled")