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_organizations(self):
"""
The `get_organizations` method should return all organizations linked to a course and
should respect publication status.
"""
# The 2 first organizations 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 organizations linked to the course are retrieved (its name starts with `_`
# because it is not used and only here for unpacking purposes)
*draft_organizations, _other_draft = OrganizationFactory.create_batch(3)
*published_organizations, _other_public = OrganizationFactory.create_batch(
3, should_publish=True
)
course = CourseFactory(
fill_organizations=draft_organizations + published_organizations,
should_publish=True,
)
self.assertEqual(
list(course.get_organizations()),
draft_organizations + published_organizations,
)
self.assertEqual(
list(course.public_extension.get_organizations()), published_organizations
)
def test_templates_course_run_detail_cms_draft_content(self):
"""
A staff user should see a draft course run 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,
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),
languages=["en", "fr"],
)
organizations_child = OrganizationFactory.create_batch(
2, fill_categories=[child_category], should_publish=True
)
grand_child_category_page = create_page(
"Literature",
"courses/cms/category_detail.html",
"en",
parent=child_category_page,
published=True,
)
grand_child_category = CategoryFactory(
extended_object=grand_child_category_page, should_publish=True
)
organizations_grand_child = OrganizationFactory.create_batch(
2, fill_categories=[grand_child_category], should_publish=True
)
# Check that each category gets organizations from its descendants
# ...unless we pass an argument to deactivate it
self.assertEqual(
list(category.get_organizations()),
organizations + organizations_child + organizations_grand_child,
)
self.assertEqual(
list(category.get_organizations(include_descendants=False)), organizations
)
self.assertEqual(
list(child_category.get_organizations()),
organizations_child + organizations_grand_child,
def test_models_course_get_main_organization(self):
"""
The `get_main_organization` method should return the first organization linked to a
course via plugins, respecting publication status.
"""
# The 2 first organizations 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 organizations linked to the course are retrieved (its name starts with `_`
# because it is not used and only here for unpacking purposes)
*draft_organizations, _other_draft = OrganizationFactory.create_batch(3)
*published_organizations, _other_public = OrganizationFactory.create_batch(
3, should_publish=True
)
# Shuffle all organizations to make sure their order in the placeholder is what
# determines which one is the main organization
all_organizations = draft_organizations + published_organizations
random.shuffle(all_organizations)
course = CourseFactory(
fill_organizations=all_organizations, should_publish=True
)
self.assertEqual(course.get_main_organization(), all_organizations[0])
self.assertEqual(
course.public_extension.get_main_organization(),
# Find the first published organization in this list of organizations
def test_models_category_get_organizations(self):
"""
It should be possible to retrieve the list of related organizations on the category
instance. The number of queries should be minimal.
"""
category = CategoryFactory(should_publish=True)
organizations = OrganizationFactory.create_batch(
2, page_title="my title", fill_categories=[category], should_publish=True
)
retrieved_organizations = category.get_organizations()
with self.assertNumQueries(2):
self.assertEqual(set(retrieved_organizations), set(organizations))
with self.assertNumQueries(0):
for organization in retrieved_organizations:
self.assertEqual(
organization.extended_object.prefetched_titles[0].title, "my title"
)
def test_models_course_get_organizations_other_placeholders(self):
"""
The `get_organizations` method should return all organizations linked to a course via a
plugin on whichever placeholder.
"""
organization1, organization2 = OrganizationFactory.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="OrganizationPlugin",
page=organization1.extended_object,
)
add_plugin(
language="en",
placeholder=placeholder2,
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),
languages=["en", "fr"],
def test_models_category_get_organizations_descendants(self):
"""
Related organizations should include the organizations linked to the category's
descendants, unless specifically deactivated by the "include_descendants" argument.
"""
category_page = create_page(
"Subjects", "courses/cms/category_detail.html", "en", published=True
)
category = CategoryFactory(extended_object=category_page, should_publish=True)
organizations = OrganizationFactory.create_batch(
2, fill_categories=[category], should_publish=True
)
child_category_page = create_page(
"Literature",
"courses/cms/category_detail.html",
"en",
parent=category_page,
published=True,
)
child_category = CategoryFactory(
extended_object=child_category_page, should_publish=True
)
organizations_child = OrganizationFactory.create_batch(
2, fill_categories=[child_category], should_publish=True
)