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_models_course_field_effort_invalid_unit(self):
"""The second value should be a valid time unit choice."""
with self.assertRaises(ValidationError) as context:
CourseFactory(effort=[1, "invalid", "month"])
self.assertEqual(
context.exception.messages[0],
"invalid is not a valid choice for a time unit.",
)
def test_cms_toolbars_course_has_page_extension_settings_item(self):
"""
Validate that a new item to edit the course is available only when visiting the page
in edit mode and for users with permission to edit the page.
"""
course = CourseFactory()
url = "/en/admin/courses/course/{id:d}/change/".format(id=course.id)
for args, method in self.get_cases_for_page_change():
toolbar = self.get_toolbar_for_page(course.extended_object, *args)
item = method(toolbar, "Course settings...")
if item:
self.assertEqual(item.url, url)
def test_models_course_run_get_course_child_of_snapshot(self):
"""
We should be able to retrieve the course from a course run that is a child of one of
its snapshots.
"""
course = CourseFactory(should_publish=True)
snapshot = CourseFactory(
page_parent=course.extended_object, should_publish=True
)
course_run = CourseRunFactory(
page_parent=snapshot.extended_object, should_publish=True
)
# Add a sibling course to make sure it is not returned
CourseFactory(should_publish=True)
self.assertEqual(course_run.get_course(), course)
self.assertEqual(
course_run.public_extension.get_course(), course.public_extension
)
def test_templates_course_run_detail_no_index(self):
"""
A course run page should not be indexable by search engine robots.
"""
course = CourseFactory(should_publish=True)
course_run = CourseRunFactory(
page_parent=course.extended_object, should_publish=True
)
url = course_run.extended_object.get_absolute_url()
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, '')
def test_templates_course_run_detail_breadcrumb_below_course(self):
"""
Validate the format of the breadcrumb on a course run directly placed below the course.
"""
home_page = PageFactory(
title__title="home", title__language="en", should_publish=True
)
search_page = PageFactory(
title__title="courses",
title__language="en",
parent=home_page,
should_publish=True,
)
course = CourseFactory(
page_title="course name",
page_parent=search_page,
page_in_navigation=True,
should_publish=True,
)
course_run = CourseRunFactory(
page_title="session 42",
page_parent=course.extended_object,
should_publish=True,
)
response = self.client.get(course_run.extended_object.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
(
def test_templates_person_detail_related_courses(self):
"""
The courses to which a person has participated should appear on this person's detail page.
"""
user = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=user.username, password="password")
person = PersonFactory()
course = CourseFactory(fill_team=[person])
url = person.extended_object.get_absolute_url()
response = self.client.get(url)
# The course should be present on the page
self.assertContains(
response,
'<p class="course-glimpse__title">{:s}</p>'.format(
course.extended_object.get_title()
),
html=True,
)
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)
def test_indexers_courses_get_es_documents_snapshots(self):
"""
Course snapshots should not get indexed.
"""
course = CourseFactory(should_publish=True)
CourseFactory(page_parent=course.extended_object, should_publish=True)
indexed_courses = list(
CoursesIndexer.get_es_documents(index="some_index", action="some_action")
)
self.assertEqual(len(indexed_courses), 1)
self.assertEqual(
indexed_courses[0]["_id"], str(course.public_extension.extended_object_id)
)
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"],
def test_models_course_create_page_role_public_page(self, *_):
"""
A page role should not be created for the public version of a course.
"""
course = CourseFactory(should_publish=True).public_extension
self.assertIsNone(course.create_page_role())
self.assertFalse(course.extended_object.roles.exists())