Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cms_wizards_course_run_submit_form_slug_too_long(self, mock_snapshot):
"""
Trying to set a slug that is too long should make the form invalid
"""
# A course should pre-exist
course = CourseFactory()
# Submit a slug that is too long and a title that is ok
invalid_data = {"title": "t" * 255, "slug": "s" * 201}
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data=invalid_data,
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertFalse(form.is_valid())
# Check that the slug being too long is a cause for the invalid form
self.assertEqual(
form.errors["slug"],
["Ensure this value has at most 200 characters (it has 201)."],
)
# Snapshot was not request and should not have been triggered
self.assertFalse(mock_snapshot.called)
def test_cms_wizards_course_run_submit_form_invalid_slug(self, mock_snapshot):
"""Trying to submit a slug that is not valid should raise a 400 exception."""
# A course should pre-exist
course = CourseFactory()
# Submit an invalid slug
data = {"title": "my title", "slug": "invalid slug"}
user = UserFactory(is_superuser=True, is_staff=True)
form = CourseRunWizardForm(data=data, wizard_language="en", wizard_user=user)
form.page = course.extended_object
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors["slug"][0],
"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.",
)
# Snapshot was not request and should not have been triggered
self.assertFalse(mock_snapshot.called)
def test_cms_wizards_course_run_submit_form_success(self, mock_snapshot):
"""
A user with the required permissions submitting a valid CourseRunWizardForm should be able
to create a course run and its related page.
"""
course = CourseFactory()
# Create a user with just the required permissions
user = UserFactory(
is_staff=True,
permissions=["courses.add_courserun", "cms.add_page", "cms.change_page"],
)
# Submit a valid form
form = CourseRunWizardForm(
data={"title": "My title"},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertTrue(form.is_valid())
page = form.save()
course_run = page.courserun
# The course run and its related page should have been created as draft
Page.objects.drafts().get(id=page.id)
CourseRun.objects.get(id=course_run.id, extended_object=page)
self.assertEqual(page.get_title(), "My title")
# The slug should have been automatically set
"""
Requesting a snapshot when the permission is denied should call the helper function
only once for validation and return the error message.
"""
course = CourseFactory()
user = UserFactory()
# Generate a permission error on form submission
def raise_permission_denied(*args, **kwargs):
raise PermissionDenied("can't do that")
mock_snapshot.side_effect = raise_permission_denied
# Submit a valid form with the snapshot flag enabled
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data={"title": "My title", "should_snapshot_course": True},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors, {"__all__": ["can't do that"]})
mock_snapshot.assert_called_once_with(
course.extended_object, user, simulate_only=True
)
def test_cms_wizards_course_run_submit_form_title_too_long(self, mock_snapshot):
"""
Trying to set a title that is too long should make the form invalid
"""
# A course should pre-exist
course = CourseFactory()
# Submit a title that is too long
invalid_data = {"title": "t" * 256}
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data=invalid_data,
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertFalse(form.is_valid())
# Check that the title being too long is a cause for the invalid form
self.assertEqual(
form.errors["title"],
["Ensure this value has at most 255 characters (it has 256)."],
)
# Snapshot was not request and should not have been triggered
self.assertFalse(mock_snapshot.called)
Check that the form correctly raises an error when the slug is too long. The path built
by combining the slug of the page with the slug of its parent page, should not exceed
255 characters in length.
"""
# A parent page with a very long slug
root_page = create_page(
"p" * 100,
"richie/single_column.html",
"en",
reverse_id=Course.PAGE["reverse_id"],
)
course = CourseFactory(page_title="c" * 100, page_parent=root_page)
# A course run with a slug at the limit length should work
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data={"title": "t" * 53},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertTrue(form.is_valid())
form.save()
# A course run with a slug too long with regards to the parent's one should raise an error
form = CourseRunWizardForm(
data={"title": "t" * 54},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
)
course = CourseFactory(page_title="c" * 100, page_parent=root_page)
# A course run with a slug at the limit length should work
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data={"title": "t" * 53},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertTrue(form.is_valid())
form.save()
# A course run with a slug too long with regards to the parent's one should raise an error
form = CourseRunWizardForm(
data={"title": "t" * 54},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors["slug"][0],
(
"This slug is too long. The length of the path built by prepending the slug of "
"the parent page would be 256 characters long and it should be less than 255"
),
)
# Snapshot was not request and should not have been triggered
def test_cms_wizards_course_run_submit_form_slugify_long_title(self, mock_snapshot):
"""
When generating the slug from the title, we should respect the slug's "max_length"
"""
# A course should pre-exist
course = CourseFactory()
# Submit a title at max length
data = {"title": "t" * 255}
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data=data,
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertTrue(form.is_valid())
page = form.save()
# Check that the slug has been truncated
self.assertEqual(page.get_slug(), "t" * 200)
# Snapshot was not request and should not have been triggered
self.assertFalse(mock_snapshot.called)
def test_cms_wizards_course_run_submit_form_slug_duplicate(self, mock_snapshot):
"""
Trying to create a course run with a slug that would lead to a duplicate path should
raise a validation error.
"""
# A course should pre-exist
course = CourseFactory()
# Create an existing page with a known slug
CourseRunFactory(page_parent=course.extended_object, page_title="My title")
# Submit a title that will lead to the same slug
data = {"title": "my title"}
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data=data,
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors, {"slug": ["This slug is already in use"]})
# Snapshot was not request and should not have been triggered
self.assertFalse(mock_snapshot.called)
def test_cms_wizards_course_run_language_active_not_in_all_languages(self, *_):
"""
If the ALL_LANGUAGES setting does not include the full active language, it should match
on the simple language prefix.
"""
course = CourseFactory(page_title={"fr-ca": "my title"})
# Submit a valid form
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseRunWizardForm(
data={"title": "My title"},
wizard_language="en",
wizard_user=user,
wizard_page=course.extended_object,
)
self.assertTrue(form.is_valid())
with translation.override("fr-ca"):
page = form.save()
# The language field should have been set to the active language
self.assertEqual(page.courserun.languages, ["fr"])