def test_repeat_course(self): # Initially course shouldn't be authorized assert not is_bulk_email_feature_enabled(self.course.id) # Test authorizing the course, which should totally work form_data = {'course_id': str(self.course.id), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation should work assert form.is_valid() form.save() # Check that this course is authorized assert is_bulk_email_feature_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': str(self.course.id), 'email_enabled': False} form = CourseAuthorizationAdminForm(data=form_data) # Validation should not work because course_id field is unique assert not form.is_valid() assert 'Course authorization with this Course id already exists.' == form._errors[ 'course_id'][0] # pylint: disable=protected-access, line-too-long with self.assertRaisesRegex( 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) assert is_bulk_email_feature_enabled(self.course.id)
def test_authorize_mongo_course(self): # Initially course shouldn't be authorized assert not is_bulk_email_feature_enabled(self.course.id) # Test authorizing the course, which should totally work form_data = {'course_id': str(self.course.id), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation should work assert form.is_valid() form.save() # Check that this course is authorized assert is_bulk_email_feature_enabled(self.course.id)
def test_course_name_only(self): # Munge course id - common form_data = {'course_id': self.course.id.run, 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation shouldn't work assert not form.is_valid() error_msg = form._errors['course_id'][0] # pylint: disable=protected-access assert f'Entered course id was: "{self.course.id.run}".' in error_msg with self.assertRaisesRegex( ValueError, "The CourseAuthorization could not be created because the data didn't validate." ): form.save()
def test_form_invalid_key(self): form_data = {'course_id': "asd::**!@#$%^&*())//foobar!!", 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation shouldn't work self.assertFalse(form.is_valid()) msg = u'Course id invalid.' msg += u' Entered course id was: "asd::**!@#$%^&*())//foobar!!".' self.assertEqual(msg, form._errors['course_id'][0]) # pylint: disable=protected-access with self.assertRaisesRegex( ValueError, "The CourseAuthorization could not be created because the data didn't validate." ): form.save()
def test_form_typo(self): # Munge course id bad_id = CourseLocator(u'Broken{}'.format(self.course.id.org), 'hello', self.course.id.run + '_typo') form_data = {'course_id': text_type(bad_id), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation shouldn't work self.assertFalse(form.is_valid()) msg = u'Course not found.' msg += u' Entered course id was: "{0}".'.format(text_type(bad_id)) self.assertEqual(msg, form._errors['course_id'][0]) # pylint: disable=protected-access with self.assertRaisesRegex( ValueError, "The CourseAuthorization could not be created because the data didn't validate." ): form.save()
def test_form_typo(self): # Munge course id bad_id = CourseLocator(f'Broken{self.course.id.org}', 'hello', self.course.id.run + '_typo') form_data = {'course_id': str(bad_id), 'email_enabled': True} form = CourseAuthorizationAdminForm(data=form_data) # Validation shouldn't work assert not form.is_valid() msg = 'Course not found.' msg += f' Entered course id was: "{str(bad_id)}".' assert msg == form._errors['course_id'][0] # pylint: disable=protected-access with self.assertRaisesRegex( ValueError, "The CourseAuthorization could not be created because the data didn't validate." ): form.save()