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_plugins_blogpost_form_page_choices(self):
"""
The form to create a blogpost plugin should only list blogpost pages
in the select box.
"""
class BlogPostPluginModelForm(forms.ModelForm):
"""A form for testing the choices in the select box"""
class Meta:
model = BlogPostPluginModel
fields = ["page"]
blog_page = create_i18n_page("my title", published=True)
blogpost = BlogPostFactory(page_parent=blog_page)
other_page_title = "other page"
create_page(
other_page_title, "richie/single_column.html", settings.LANGUAGE_CODE
)
plugin_form = BlogPostPluginModelForm()
rendered_form = plugin_form.as_table()
self.assertEqual(rendered_form.count(blogpost.extended_object.get_title()), 1)
self.assertNotIn(other_page_title, plugin_form.as_table())
def test_models_category_get_blogposts_public_category_page(self):
"""
When a category is added on a draft blog post, the blog post should not be visible on
the public category page until the blog post is published.
"""
category = CategoryFactory(should_publish=True)
category_page = category.extended_object
blog_post = BlogPostFactory(page_title="my title", should_publish=True)
blog_post_page = blog_post.extended_object
# Add a category to the blog post but don't publish the modification
placeholder = blog_post_page.placeholders.get(slot="categories")
add_plugin(placeholder, CategoryPlugin, "en", page=category_page)
self.assertEqual(list(category.get_blogposts()), [blog_post])
self.assertEqual(list(category.public_extension.get_blogposts()), [])
# Now publish the modification and check that the blog post is displayed
# on the public category page
blog_post.extended_object.publish("en")
self.assertEqual(
list(category.public_extension.get_blogposts()),
[blog_post.public_extension],
)
def test_cms_plugins_blogpost_default_variant(self):
"""
If the variant is not specified on the blogpost pluging, it should render
according to variant variable eventually present in the context of its
container.
"""
# Create an blogpost
blogpost = BlogPostFactory(page_title="public title")
blogpost_page = blogpost.extended_object
# Create a page to add the plugin to
page = create_i18n_page("A page")
placeholder = page.placeholders.get(slot="maincontent")
# Add blogpost plugin with default template
model_instance = add_plugin(
placeholder, BlogPostPlugin, "en", page=blogpost_page, variant="small"
)
# Get generated html
request = RequestFactory()
request.current_page = page
request.user = AnonymousUser()
context = {"current_page": page, "blogpost_variant": "xxl", "request": request}
def test_templates_blogpost_detail_cms_published_content(self):
"""
Validate that the important elements are displayed on a published blogpost page
"""
author = PersonFactory(
page_title={"en": "Comte de Saint-Germain"}, should_publish=True
)
blogpost = BlogPostFactory(
page_title="Preums", fill_cover=True, fill_body=True, fill_author=[author]
)
page = blogpost.extended_object
# The page should not be visible before it is published
url = page.get_absolute_url()
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
# Publish the blogpost with a past date and ensure the content is correct
with mock.patch(
"cms.models.pagemodel.now",
return_value=datetime(2019, 11, 27, tzinfo=timezone.utc),
):
page.publish("en")
def test_models_category_get_blogposts_several_languages(self):
"""
The blogposts should not be duplicated if they exist in several languages.
"""
category = CategoryFactory(should_publish=True)
BlogPostFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_categories=[category],
should_publish=True,
)
self.assertEqual(BlogPost.objects.count(), 2)
self.assertEqual(category.get_blogposts().count(), 1)
def test_models_person_get_blogposts_several_languages(self):
"""
The blogposts should not be duplicated if they exist in several languages.
"""
person = PersonFactory(should_publish=True)
BlogPostFactory(
page_title={"en": "my title", "fr": "mon titre"},
fill_author=[person],
should_publish=True,
)
self.assertEqual(BlogPost.objects.count(), 2)
self.assertEqual(person.get_blogposts().count(), 1)
def test_cms_plugins_blogpost_render_on_public_page(self):
"""
The blogpost plugin should render as expected on a public page.
"""
# Create an blogpost
blogpost = BlogPostFactory(
page_title={"en": "public title", "fr": "titre public"},
fill_cover={
"original_filename": "cover.jpg",
"default_alt_text": "my cover",
},
)
blogpost_page = blogpost.extended_object
# Create a page to add the plugin to
page = create_i18n_page({"en": "A page", "fr": "Une page"})
placeholder = page.placeholders.get(slot="maincontent")
add_plugin(placeholder, BlogPostPlugin, "en", **{"page": blogpost_page})
add_plugin(placeholder, BlogPostPlugin, "fr", **{"page": blogpost_page})
with mock.patch(
"cms.models.pagemodel.now",
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)
def test_models_person_get_blogposts(self):
"""
It should be possible to retrieve the list of related blogposts on the person
instance. The number of queries should be minimal.
"""
person = PersonFactory(should_publish=True)
blogposts = BlogPostFactory.create_batch(
2, page_title="my title", fill_author=[person], should_publish=True
)
retrieved_blogposts = person.get_blogposts()
with self.assertNumQueries(2):
self.assertEqual(set(retrieved_blogposts), set(blogposts))
with self.assertNumQueries(0):
for blogpost in retrieved_blogposts:
self.assertEqual(
blogpost.extended_object.prefetched_titles[0].title, "my title"
)
for i in range(nb_course_runs):
factories.CourseRunFactory(
__sequence=i,
languages=random.sample(
languages_subset, random.randint(1, len(languages_subset)) # nosec
),
page_in_navigation=False,
page_languages=["en", "fr"],
page_parent=course.extended_object,
should_publish=True,
)
# Create blog posts under the `News` page
blogposts = []
for _ in range(defaults.NB_OBJECTS["blogposts"]):
post = factories.BlogPostFactory.create(
page_in_navigation=True,
page_languages=["en", "fr"],
page_parent=pages_created["blogposts"],
fill_cover=pick_image("cover"),
fill_excerpt=True,
fill_body=True,
fill_categories=[
*random.sample(levels, defaults.NB_OBJECTS["blogpost_levels"]),
*random.sample(tags, defaults.NB_OBJECTS["blogpost_tags"]),
],
fill_author=random.sample(persons, 1),
should_publish=True,
)
blogposts.append(post)
# Create programs under the `Programs` page