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_get_categories(self):
"""
The `get_categories` method should return all categories linked to a course and
should respect publication status.
"""
# The 2 first categories are grouped in one variable name and will be linked to the
# course in the following, the third category will not be linked so we can check that
# only the categories linked to the course are retrieved (its name starts with `_`
# because it is not used and only here for unpacking purposes)
*draft_categories, _other_draft = CategoryFactory.create_batch(3)
*published_categories, _other_public = CategoryFactory.create_batch(
3, should_publish=True
)
course = CourseFactory(
fill_categories=draft_categories + published_categories, should_publish=True
)
self.assertEqual(
list(course.get_categories()), draft_categories + published_categories
)
self.assertEqual(
list(course.public_extension.get_categories()), published_categories
)
def test_templates_course_detail_cms_draft_content(self):
"""
A staff user should see a draft course including its draft elements with
an annotation
"""
user = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=user.username, password="password")
categories = CategoryFactory.create_batch(4)
organizations = OrganizationFactory.create_batch(4)
course = CourseFactory(
page_title="Very interesting course",
fill_organizations=organizations,
fill_categories=categories,
)
page = course.extended_object
now = timezone.now()
course_run1, _course_run2 = CourseRunFactory.create_batch(
2,
page_parent=course.extended_object,
start=now - timedelta(hours=1),
end=now + timedelta(hours=2),
enrollment_end=now + timedelta(hours=1),
languages=["en", "fr"],
def test_templates_course_run_detail_cms_published_content(self):
"""
Validate that the important elements are displayed on a published course run page
"""
categories = CategoryFactory.create_batch(4)
organizations = OrganizationFactory.create_batch(4)
course = CourseFactory(
page_title="Very interesting course",
fill_organizations=organizations,
fill_categories=categories,
should_publish=True,
)
course_run = CourseRunFactory(
page_title="first session",
page_parent=course.extended_object,
resource_link="https://www.example.com/enroll",
enrollment_start=datetime(2018, 10, 21, tzinfo=pytz.utc),
enrollment_end=datetime(2019, 1, 18, tzinfo=pytz.utc),
start=datetime(2018, 12, 10, tzinfo=pytz.utc),
end=datetime(2019, 2, 14, tzinfo=pytz.utc),
def test_templates_blogpost_list_related_categories(self):
"""
The top of the page should list all categories related to at least one of the blog
posts on the blog posts list page.
"""
page = PageFactory(
template="courses/cms/blogpost_list.html",
title__language="en",
should_publish=True,
)
post1, post2 = BlogPostFactory.create_batch(
2, page_parent=page, should_publish=True
)
category1, category2, category12, category_alone = CategoryFactory.create_batch(
4, should_publish=True
)
# Attach categories to post1
placeholder = post1.extended_object.get_public_object().placeholders.all()[0]
add_plugin(placeholder, "CategoryPlugin", "en", page=category1.extended_object)
add_plugin(placeholder, "CategoryPlugin", "en", page=category12.extended_object)
# Attach categories to post2
placeholder = post2.extended_object.get_public_object().placeholders.all()[0]
add_plugin(placeholder, "CategoryPlugin", "en", page=category2.extended_object)
add_plugin(placeholder, "CategoryPlugin", "en", page=category12.extended_object)
response = self.client.get(page.get_absolute_url())
self.assertEqual(response.status_code, 200)
def test_models_course_get_categories_other_placeholders(self):
"""
The `get_categories` method should return all categories linked to a course via a plugin
on whichever placeholder.
"""
category1, category2 = CategoryFactory.create_batch(2)
course = CourseFactory(should_publish=True)
placeholder1 = course.extended_object.placeholders.get(
slot="course_description"
)
placeholder2 = course.extended_object.placeholders.get(slot="course_format")
add_plugin(
language="en",
placeholder=placeholder1,
plugin_type="CategoryPlugin",
page=category1.extended_object,
)
add_plugin(
language="en",
placeholder=placeholder2,
def test_templates_course_detail_cms_published_content(self):
"""
Validate that the important elements are displayed on a published course page
"""
categories = CategoryFactory.create_batch(4)
icons = CategoryFactory.create_batch(4, fill_icon=True)
organizations = OrganizationFactory.create_batch(4)
course = CourseFactory(
page_title="Very interesting course",
fill_organizations=organizations,
fill_categories=categories,
fill_icons=icons,
)
page = course.extended_object
# Create 2 ongoing open course runs
now = timezone.now()
course_run1, _course_run2 = CourseRunFactory.create_batch(
2,
page_parent=course.extended_object,
start=now - timedelta(hours=1),
end=now + timedelta(hours=2),
def test_models_course_get_categories(self):
"""
The `get_categories` method should return all categories linked to a course and
should respect publication status.
"""
# The 2 first categories are grouped in one variable name and will be linked to the
# course in the following, the third category will not be linked so we can check that
# only the categories linked to the course are retrieved (its name starts with `_`
# because it is not used and only here for unpacking purposes)
*draft_categories, _other_draft = CategoryFactory.create_batch(3)
*published_categories, _other_public = CategoryFactory.create_batch(
3, should_publish=True
)
course = CourseFactory(
fill_categories=draft_categories + published_categories, should_publish=True
)
self.assertEqual(
list(course.get_categories()), draft_categories + published_categories
)
self.assertEqual(
list(course.public_extension.get_categories()), published_categories
)
def test_models_category_get_children_categories(self):
"""
It should be possible to retrieve the list of direct children page which
are Category extensions and not any other type.
"""
empty_category = CategoryFactory(should_publish=True)
parent_category = CategoryFactory(should_publish=True)
child_categories = CategoryFactory.create_batch(
2, page_parent=parent_category.extended_object, should_publish=True
)
with self.assertNumQueries(2):
self.assertFalse(empty_category.get_children_categories().exists())
with self.assertNumQueries(2):
self.assertEqual(
set(parent_category.get_children_categories()), set(child_categories)
)