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_duration_default(self):
"""The duration field should default to None."""
course = Course.objects.create(extended_object=PageFactory())
self.assertIsNone(course.duration)
# distinct clause is not applied.
# This is because of the clause "extended_object__node__parent__cms_pages__..."
# which is there to exclude snapshots but also acts on the main course page and
# checks its parent (so the root page) and the duplicate comes from the fact that
# the parent has a draft and a public page... so "cms_pages" has a cardinality of 2
root_page = create_i18n_page("my title", published=True)
person = PersonFactory(should_publish=True)
course = CourseFactory(
page_parent=root_page, fill_team=[person], should_publish=True
)
CourseFactory(
page_parent=course.extended_object, fill_team=[person], should_publish=True
)
self.assertEqual(Course.objects.count(), 4)
self.assertEqual(person.get_courses().count(), 1)
self.assertEqual(person.public_extension.get_courses().count(), 1)
def test_models_category_get_courses_several_languages(self):
"""
The courses should not be duplicated if they exist in several languages.
"""
category = CategoryFactory(should_publish=True)
CourseFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_categories=[category],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 2)
self.assertEqual(category.get_courses().count(), 1)
def test_commands_create_demo_site_method(self, *_):
"""
Validate that the create_demo_site method works (with a minimum number of pages because
it takes a lot of time).
"""
create_demo_site()
self.assertEqual(models.BlogPost.objects.count(), 2)
self.assertEqual(models.Course.objects.count(), 2)
self.assertEqual(models.CourseRun.objects.count(), 2)
self.assertEqual(models.Category.objects.count(), 48)
self.assertEqual(models.Organization.objects.count(), 2)
self.assertEqual(models.Person.objects.count(), 2)
self.assertEqual(models.Licence.objects.count(), 1)
self.assertEqual(models.Program.objects.count(), 2)
self.assertEqual(Site.objects.first().domain, "richie.education:9999")
# the parent has a draft and a public page... so "cms_pages" has a cardinality of 2
root_page = create_i18n_page("my title", published=True)
organization = OrganizationFactory(should_publish=True)
course = CourseFactory(
page_parent=root_page,
fill_organizations=[organization],
should_publish=True,
)
CourseFactory(
page_parent=course.extended_object,
fill_organizations=[organization],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 4)
self.assertEqual(organization.get_courses().count(), 1)
self.assertEqual(organization.public_extension.get_courses().count(), 1)
# Add page permissions only
self.add_page_permission(
user, course.extended_object, can_change=True, can_add=True
)
# Trigger the creation of a snapshot for the course
url = "/en/admin/courses/course/{:d}/snapshot/".format(course.id)
response = self.client.post(url, follow=True)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.content,
b"You don't have sufficient permissions to snapshot this page.",
)
# No additional courses should have been created
self.assertEqual(Course.objects.count(), 2)
# which is there to exclude snapshots but also acts on the main course page and
# checks its parent (so the root page) and the duplicate comes from the fact that
# the parent has a draft and a public page... so "cms_pages" has a cardinality of 2
root_page = create_i18n_page("my title", published=True)
category = CategoryFactory(should_publish=True)
course = CourseFactory(
page_parent=root_page, fill_categories=[category], should_publish=True
)
CourseFactory(
page_parent=course.extended_object,
fill_categories=[category],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 4)
self.assertEqual(category.get_courses().count(), 1)
self.assertEqual(category.public_extension.get_courses().count(), 1)
# Add the necessary permissions (global and per page)
self.add_permission(user, "add_page")
self.add_permission(user, "change_page")
self.add_page_permission(
user, course.extended_object, can_change=True, can_add=True
)
# Trigger the creation of a snapshot for the course
url = "/en/admin/courses/course/{:d}/snapshot/".format(course.id)
with mock.patch("time.time", mock.MagicMock(return_value=1541946888)):
response = self.client.post(url, follow=True)
self.assertEqual(response.status_code, 200)
content = json.loads(response.content)
self.assertEqual(Course.objects.count(), 4)
snapshot = (
Course.objects.exclude(id=course.id)
.exclude(public_extension__isnull=True)
.get()
)
self.assertEqual(content, {"id": snapshot.id})
# The snapshot title and slug should be the timestamp at the time of snapshot
expected_titles = {
"en": "1541946888 - Snapshot of a course",
"fr": "1541946888 - Snapshot of un cours",
"de": "1541946888 - Snapshot of ein Kurs",
}
for language in ["en", "fr", "de"]:
self.assertEqual(
snapshot.extended_object.get_title(language), expected_titles[language]
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)
def test_models_organization_get_courses_several_languages(self):
"""
The courses should not be duplicated if they exist in several languages.
"""
organization = OrganizationFactory(should_publish=True)
CourseFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_organizations=[organization],
should_publish=True,
)
self.assertEqual(Course.objects.count(), 2)
self.assertEqual(organization.get_courses().count(), 1)