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_person_submit_form_title_too_long(self):
"""
Trying to set a title that is too long should make the form invalid
"""
# A parent page should pre-exist
create_page(
"Persons",
"richie/single_column.html",
"en",
reverse_id=Person.PAGE["reverse_id"],
)
# Submit a title that is too long and a slug that is ok
invalid_data = {"title": "t" * 256, "slug": "s" * 200}
user = UserFactory(is_superuser=True, is_staff=True)
form = PersonWizardForm(
data=invalid_data, wizard_language="en", wizard_user=user
)
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)."],
)
page = create_page("page", "richie/single_column.html", "en")
required_permissions = ["courses.add_person", "cms.add_page", "cms.change_page"]
url = "{:s}?page={:d}".format(reverse("cms_wizard_create"), page.id)
for permission_to_be_removed in required_permissions + [None]:
if permission_to_be_removed is None:
# This is the case of sufficient permissions treated in the next test
continue
altered_permissions = required_permissions.copy()
if permission_to_be_removed:
altered_permissions.remove(permission_to_be_removed)
user = UserFactory(is_staff=True, permissions=altered_permissions)
self.client.login(username=user.username, password="password")
# Let the authorized user get the page with all wizards listed
response = self.client.get(url)
# Check that our wizard to create persons is not on this page
self.assertNotContains(response, "person", status_code=200, html=True)
def test_admin_course_run_add_view(self):
"""
The admin add view should work for course runs.
"""
user = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=user.username, password="password")
# Get the admin change view
url = reverse("admin:courses_courserun_add")
response = self.client.get(url, follow=True)
# Check that the page includes all our fields
self.assertContains(response, "id_resource_link")
self.assertContains(response, "id_start_", count=3)
self.assertContains(response, "id_end_", count=3)
self.assertContains(response, "id_enrollment_start_", count=3)
self.assertContains(response, "id_enrollment_end_", count=3)
def get_cases_for_page_change(self):
"""
Not a test, a helper to create different users for each possible level of access
and specify their expected visibility on the menu item..
pylint: disable=too-many-locals
"""
superuser = UserFactory(is_staff=True, is_superuser=True)
staff_with_permission = UserFactory(is_staff=True)
user_with_permission = UserFactory()
staff = UserFactory(is_staff=True)
user = UserFactory()
anonymous = AnonymousUser()
# Add global permission to change page for users concerned
can_change_page = Permission.objects.get(codename="change_page")
staff_with_permission.user_permissions.add(can_change_page)
user_with_permission.user_permissions.add(can_change_page)
return [
([superuser, False, False], self.check_disabled),
([superuser, True, False], self.check_active),
([superuser, False, True], self.check_disabled),
([staff_with_permission, False, False], self.check_disabled),
([staff_with_permission, True, False], self.check_active),
([staff_with_permission, False, True], self.check_disabled),
def get_cases_for_page_change(self):
"""
Not a test, a helper to create different users for each possible level of access
and specify their expected visibility on the menu item..
pylint: disable=too-many-locals
"""
superuser = UserFactory(is_staff=True, is_superuser=True)
staff_with_permission = UserFactory(is_staff=True)
user_with_permission = UserFactory()
staff = UserFactory(is_staff=True)
user = UserFactory()
anonymous = AnonymousUser()
# Add global permission to change page for users concerned
can_change_page = Permission.objects.get(codename="change_page")
staff_with_permission.user_permissions.add(can_change_page)
user_with_permission.user_permissions.add(can_change_page)
return [
([superuser, False, False], self.check_disabled),
([superuser, True, False], self.check_active),
([superuser, False, True], self.check_disabled),
([staff_with_permission, False, False], self.check_disabled),
([staff_with_permission, True, False], self.check_active),
([staff_with_permission, False, True], self.check_disabled),
([staff, False, False], self.check_missing),
"cms.add_page",
"cms.change_page",
]
url = "{:s}?page={:d}".format(reverse("cms_wizard_create"), page.id)
for permission_to_be_removed in required_permissions + [None]:
if permission_to_be_removed is None:
# This is the case of sufficient permissions treated in the next test
continue
altered_permissions = required_permissions.copy()
if permission_to_be_removed:
altered_permissions.remove(permission_to_be_removed)
user = UserFactory(is_staff=True, permissions=altered_permissions)
self.client.login(username=user.username, password="password")
# Let the authorized user get the page with all wizards listed
response = self.client.get(url)
# Check that our wizard to create organizations is not on this page
self.assertNotContains(response, "organization", status_code=200, html=True)
def test_cms_wizards_blogpost_submit_form_slug_too_long(self):
"""
Trying to set a slug that is too long should make the form invalid
"""
# A parent page should pre-exist
create_page(
"News",
"richie/single_column.html",
"en",
reverse_id=BlogPost.PAGE["reverse_id"],
)
# 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 = BlogPostWizardForm(
data=invalid_data, wizard_language="en", wizard_user=user
)
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)."],
)
def test_cms_wizards_course_submit_form_from_organization_page_no_role(self, *_):
"""
Creating a course via the wizard should not fail if the organization has no associated
page role.
"""
# A parent page should pre-exist
create_page(
"Courses",
"richie/single_column.html",
"en",
reverse_id=Course.PAGE["reverse_id"],
)
organization = OrganizationFactory()
user = UserFactory(is_staff=True, is_superuser=True)
form = CourseWizardForm(
data={"title": "My title"},
wizard_language="en",
wizard_user=user,
wizard_page=organization.extended_object,
)
self.assertTrue(form.is_valid())
page = form.save()
course = page.course
# The course and its related page should have been created as draft
Page.objects.drafts().get(id=page.id)
Course.objects.get(id=course.id, extended_object=page)
self.assertEqual(page.get_title(), "My title")
# The slug should have been automatically set
"cms.add_page",
"cms.change_page",
]
url = "{:s}?page={:d}".format(reverse("cms_wizard_create"), any_page.id)
for permission_to_be_removed in required_permissions + [None]:
if permission_to_be_removed is None:
# This is the case of sufficient permissions treated in the next test
continue
altered_permissions = required_permissions.copy()
if permission_to_be_removed:
altered_permissions.remove(permission_to_be_removed)
user = UserFactory(is_staff=True, permissions=altered_permissions)
self.client.login(username=user.username, password="password")
# Let the authorized user get the page with all wizards listed
response = self.client.get(url)
# Check that our wizard to create blogposts is not on this page
self.assertNotContains(
response, "new blog post", status_code=200, html=True
)
def test_cms_wizards_course_run_snapshot_permission_denied(self, mock_snapshot):
"""
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,
)