def setUp(self): self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user # create a tutorial self.tuto = PublishableContentFactory(type="TUTORIAL") self.tuto.authors.add(self.user1) UserGalleryFactory(gallery=self.tuto.gallery, user=self.user1, mode="W") self.tuto.licence = LicenceFactory() self.tuto.subcategory.add(SubCategoryFactory()) self.tuto.save() tuto_draft = self.tuto.load_version() # then, publish it ! version = tuto_draft.current_version self.published = publish_content(self.tuto, tuto_draft, is_major_update=True) self.tuto.sha_public = version self.tuto.sha_draft = version self.tuto.public_version = self.published self.tuto.save() self.client.force_login(self.user1)
def setUp(self): self.overridden_zds_app = overridden_zds_app # don't build PDF to speed up the tests overridden_zds_app["content"]["build_pdf_when_published"] = False self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user overridden_zds_app["member"]["bot_account"] = self.mas.username self.licence = LicenceFactory() self.subcategory = SubCategoryFactory(category=CategoryFactory()) self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user self.content = PublishableContentFactory( author_list=[self.user_author], light=False) self.part_published = ContainerFactory( db_object=self.content, light=False, parent=self.content.load_version()) self.ignored_part = ContainerFactory( db_object=self.content, light=False, parent=self.content.load_version()) ExtractFactory(db_object=self.content, container=self.part_published, light=False) ExtractFactory(db_object=self.content, container=self.ignored_part, light=False)
def setUp(self): self.overridden_zds_app = overridden_zds_app # don't build PDF to speed up the tests overridden_zds_app["content"]["build_pdf_when_published"] = False self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user overridden_zds_app["member"]["bot_account"] = self.mas.username bot = Group(name=overridden_zds_app["member"]["bot_group"]) bot.save() self.external = UserFactory( username=overridden_zds_app["member"]["external_account"], password="******") self.beta_forum = ForumFactory( pk=overridden_zds_app["forum"]["beta_forum_id"], category=ForumCategoryFactory(position=1), position_in_category=1, ) # ensure that the forum, for the beta versions, is created self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.tag = TagFactory() self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user # create an article self.article = PublishableContentFactory(type="ARTICLE") self.article.authors.add(self.user_author) UserGalleryFactory(gallery=self.article.gallery, user=self.user_author, mode="W") self.article.licence = self.licence self.article.subcategory.add(self.subcategory) self.article.tags.add(self.tag) self.article.save() # fill it with one extract self.article_draft = self.article.load_version() self.extract1 = ExtractFactory(container=self.article_draft, db_object=self.article) # then, publish it ! version = self.article_draft.current_version self.published = publish_content(self.article, self.article_draft, is_major_update=True) self.article.sha_public = version self.article.sha_draft = version self.article.public_version = self.published self.article.save() self.articlefeed = LastArticlesFeedRSS()
def test_accessible_ui_for_author(self): opinion = PublishedContentFactory(author_list=[self.user_author], type="OPINION") subcategory = SubCategoryFactory() opinion.subcategory.add(subcategory) opinion.save() self.client.force_login(self.user_author) resp = self.client.get(reverse("opinion:view", kwargs={"pk": opinion.pk, "slug": opinion.slug})) self.assertContains(resp, "Version brouillon", msg_prefix="Author must access their draft directly") self.assertNotContains(resp, "{}?subcategory=".format(reverse("publication:list"))) self.assertContains(resp, "{}?category=".format(reverse("opinion:list")))
def test_list_categories(self): category_1 = CategoryFactory() subcategory_1 = SubCategoryFactory(category=category_1) subcategory_2 = SubCategoryFactory(category=category_1) # Not in context if nothing published inside this subcategory SubCategoryFactory(category=category_1) for _ in range(5): self._create_and_publish_type_in_subcategory( "TUTORIAL", subcategory_1) self._create_and_publish_type_in_subcategory( "ARTICLE", subcategory_2) self.client.logout() resp = self.client.get(reverse("publication:list")) context_categories = list(resp.context_data["categories"]) self.assertEqual(context_categories[0].contents_count, 10) self.assertEqual(context_categories[0].subcategories, [subcategory_1, subcategory_2]) self.assertIn(category_1, context_categories)
def _generate(cls, create, attrs): # These parameters are only used inside _generate() and won't be saved in the database, # which is why we use attrs.pop() (they are removed from attrs). light = attrs.pop("light", True) author_list = attrs.pop("author_list", None) add_license = attrs.pop("add_license", True) add_category = attrs.pop("add_category", True) # This parameter will be saved in the database, # which is why we use attrs.get() (it stays in attrs). licence = attrs.get("licence", None) auths = author_list or [] if add_license: given_licence = licence or Licence.objects.first() if isinstance(given_licence, str) and given_licence: given_licence = Licence.objects.filter( title=given_licence).first() or Licence.objects.first() licence = given_licence or LicenceFactory() text = text_content if not light: text = tricky_text_content intro_content = attrs.pop("intro", text) conclusion_content = attrs.pop("conclusion", text) publishable_content = super()._generate(create, attrs) publishable_content.gallery = GalleryFactory() publishable_content.licence = licence for auth in auths: publishable_content.authors.add(auth) if add_category: publishable_content.subcategory.add(SubCategoryFactory()) publishable_content.save() for author in publishable_content.authors.all(): UserGalleryFactory(user=author, gallery=publishable_content.gallery, mode="W") init_new_repo(publishable_content, intro_content, conclusion_content) return publishable_content
def setUp(self): self.staff = StaffProfileFactory().user settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" self.mas = ProfileFactory().user self.overridden_zds_app["member"]["bot_account"] = self.mas.username self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.user_author = ProfileFactory().user self.user_staff = StaffProfileFactory().user self.user_guest = ProfileFactory().user self.tuto = PublishableContentFactory(type="TUTORIAL") self.tuto.authors.add(self.user_author) UserGalleryFactory(gallery=self.tuto.gallery, user=self.user_author, mode="W") self.tuto.licence = self.licence self.tuto.subcategory.add(self.subcategory) self.tuto.save() self.beta_forum = ForumFactory( pk=self.overridden_zds_app["forum"]["beta_forum_id"], category=ForumCategoryFactory(position=1), position_in_category=1, ) # ensure that the forum, for the beta versions, is created self.tuto_draft = self.tuto.load_version() self.part1 = ContainerFactory(parent=self.tuto_draft, db_object=self.tuto) self.chapter1 = ContainerFactory(parent=self.part1, db_object=self.tuto) self.extract1 = ExtractFactory(container=self.chapter1, db_object=self.tuto) bot = Group(name=self.overridden_zds_app["member"]["bot_group"]) bot.save() self.external = UserFactory( username=self.overridden_zds_app["member"]["external_account"], password="******")
def setUp(self): self.licence = LicenceFactory() self.subcategory = SubCategoryFactory() self.author = ProfileFactory() self.user = ProfileFactory() self.staff = StaffProfileFactory() self.tuto = PublishableContentFactory(type="TUTORIAL") self.tuto.authors.add(self.author.user) self.tuto.licence = self.licence self.tuto.subcategory.add(self.subcategory) self.tuto.save() self.validation = Validation( content=self.tuto, version=self.tuto.sha_draft, comment_authors="bla", date_proposition=datetime.now(), ) self.validation.save() self.topic = send_mp(author=self.author.user, users=[], title="Title", text="Testing", subtitle="", leave=False) self.topic.add_participant(self.user.user) send_message_mp(self.user.user, self.topic, "Testing") # humane_delta test periods = ((1, 0), (2, 1), (3, 7), (4, 30), (5, 360)) cont = dict() cont["date_today"] = periods[0][0] cont["date_yesterday"] = periods[1][0] cont["date_last_week"] = periods[2][0] cont["date_last_month"] = periods[3][0] cont["date_last_year"] = periods[4][0] self.context = Context(cont)
def test_publication_and_attributes_consistency(self): pubdate = datetime.now() - timedelta(days=1) article = PublishedContentFactory(type="ARTICLE", author_list=[self.user_author]) public_version = article.public_version public_version.publication_date = pubdate public_version.save() # everything must come from database to have good datetime comparison article = PublishableContent.objects.get(pk=article.pk) article.public_version.load_public_version() old_date = article.public_version.publication_date old_title = article.public_version.title() old_description = article.public_version.description() article.licence = LicenceFactory() article.save() self.client.force_login(self.user_author) self.client.post( reverse("content:edit", args=[article.pk, article.slug]), { "title": old_title + "bla", "description": old_description + "bla", "type": "ARTICLE", "licence": article.licence.pk, "subcategory": SubCategoryFactory().pk, "last_hash": article.sha_draft, }, ) article = PublishableContent.objects.prefetch_related( "public_version").get(pk=article.pk) article.public_version.load_public_version() self.assertEqual(old_title, article.public_version.title()) self.assertEqual(old_description, article.public_version.description()) self.assertEqual(old_date, article.public_version.publication_date) publish_content(article, article.load_version(), False) article = PublishableContent.objects.get(pk=article.pk) article.public_version.load_public_version() self.assertEqual(old_date, article.public_version.publication_date) self.assertNotEqual(old_date, article.public_version.update_date)
def test_filters(self): """Test filtering by category & tag""" subcategory2 = SubCategoryFactory() subcategory3 = SubCategoryFactory() tag2 = TagFactory() tag3 = TagFactory() # Add a new tuto & publish it article2 = PublishableContentFactory(type="ARTICLE") article2.authors.add(self.user_author) article2.licence = self.licence article2.subcategory.add(subcategory2) article2.tags.add(self.tag) article2.tags.add(tag2) article2.save() article2_draft = article2.load_version() article2.sha_public = article2.sha_draft = article2_draft.current_version article2.public_version = publish_content(article2, article2_draft, is_major_update=True) article2.save() # Default view ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [article2, self.article]) # Filter by subcategory self.articlefeed.query_params = {"subcategory": self.subcategory.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [self.article]) self.articlefeed.query_params = { "subcategory": f" {self.subcategory.slug} " } ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [self.article]) self.articlefeed.query_params = {"subcategory": subcategory2.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [article2]) self.articlefeed.query_params = {"subcategory": subcategory3.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, []) self.articlefeed.query_params = {"subcategory": "invalid"} self.assertRaises(Http404, self.articlefeed.items) # Filter by tag self.articlefeed.query_params = {"tag": self.tag.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [article2, self.article]) self.articlefeed.query_params = {"tag": tag2.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [article2]) self.articlefeed.query_params = {"tag": f" {tag2.slug} "} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, [article2]) self.articlefeed.query_params = {"tag": tag3.slug} ret = [item.content for item in self.articlefeed.items()] self.assertEqual(ret, []) self.articlefeed.query_params = {"tag": "invalid"} self.assertRaises(Http404, self.articlefeed.items)
def test_validation_list(self): """ensure the behavior of the `validation:list` page (with filters)""" text = "Ceci est un éléphant" tuto_not_reserved = PublishableContentFactory( type="TUTORIAL", author_list=[self.user_author]) tuto_reserved = PublishableContentFactory( type="TUTORIAL", author_list=[self.user_author]) article_not_reserved = PublishableContentFactory( type="ARTICLE", author_list=[self.user_author]) article_reserved = PublishableContentFactory( type="ARTICLE", author_list=[self.user_author]) all_contents = [ tuto_not_reserved, tuto_reserved, article_not_reserved, article_reserved ] reserved_contents = [tuto_reserved, article_reserved] # apply a filter to test category filter subcat = SubCategoryFactory() article_reserved.subcategory.add(subcat) article_reserved.save() # send in validation for content in all_contents: v = ValidationFactory(content=content, status="PENDING") v.date_proposition = datetime.datetime.now() v.version = content.sha_draft v.comment_authors = text if content in reserved_contents: v.validator = self.user_staff v.date_reserve = datetime.datetime.now() v.status = "PENDING_V" v.save() # first, test access for public result = self.client.get(reverse("validation:list"), follow=False) self.assertEqual(result.status_code, 302) # get 302 → redirection to login # connect with author: self.client.force_login(self.user_author) result = self.client.get(reverse("validation:list"), follow=False) self.assertEqual(result.status_code, 403) # get 403 not allowed self.client.logout() # connect with staff: self.client.force_login(self.user_staff) response = self.client.get(reverse("validation:list"), follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 4) # a total of 4 contents in validation # test filters response = self.client.get(reverse("validation:list") + "?type=article", follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 2) # 2 articles response = self.client.get(reverse("validation:list") + "?type=tuto", follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 2) # 2 articles response = self.client.get(reverse("validation:list") + "?type=orphan", follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 2) # 2 not-reserved content for validation in validations: self.assertFalse(validation.content in reserved_contents) response = self.client.get(reverse("validation:list") + "?type=reserved", follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 2) # 2 reserved content for validation in validations: self.assertTrue(validation.content in reserved_contents) response = self.client.get(reverse("validation:list") + f"?subcategory={subcat.pk}", follow=False) self.assertEqual(response.status_code, 200) # OK validations = response.context["validations"] self.assertEqual(len(validations), 1) # 1 content with this category self.assertEqual(validations[0].content, article_reserved) # the right content
def setUp(self): self.author = ProfileFactory() self.staff = StaffProfileFactory() self.licence = LicenceFactory() self.subcategory = SubCategoryFactory()