def setUp(self): # don't build PDF to speed up the tests 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 test_opinion_publication_staff(self): """ Test the publication of PublishableContent where type is OPINION (with staff). """ text_publication = "Aussi tôt dit, aussi tôt fait !" opinion = PublishableContentFactory(type="OPINION") opinion.authors.add(self.user_author) UserGalleryFactory(gallery=opinion.gallery, user=self.user_author, mode="W") opinion.licence = self.licence opinion.save() opinion_draft = opinion.load_version() ExtractFactory(container=opinion_draft, db_object=opinion) ExtractFactory(container=opinion_draft, db_object=opinion) self.client.force_login(self.user_staff) result = self.client.post( reverse("validation:publish-opinion", kwargs={"pk": opinion.pk, "slug": opinion.slug}), {"text": text_publication, "source": "", "version": opinion_draft.current_version}, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(PublishedContent.objects.count(), 1) opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertIsNotNone(opinion.public_version) self.assertEqual(opinion.public_version.sha_public, opinion_draft.current_version)
def setUp(self): 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.user_author = ProfileFactory().user self.staff = StaffProfileFactory().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.save() 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)
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_fail_gallery_details_no_permission(self): """fail when a user has no permission at all""" gallery = GalleryFactory() UserGalleryFactory(gallery=gallery, user=self.profile1.user) self.client.force_login(self.profile2.user) response = self.client.get(reverse("gallery-details", args=[gallery.pk, gallery.slug])) self.assertEqual(403, response.status_code)
def test_get_list_of_gallery(self): gallery = GalleryFactory() UserGalleryFactory(user=self.profile.user, gallery=gallery) response = self.client.get(reverse("api:gallery:list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get("count"), 1) self.assertIsNone(response.data.get("next")) self.assertIsNone(response.data.get("previous"))
def test_publish_content_medium_tuto(self): # 3. Medium-size tutorial midsize_tuto = PublishableContentFactory(type="TUTORIAL") midsize_tuto.authors.add(self.user_author) UserGalleryFactory(gallery=midsize_tuto.gallery, user=self.user_author, mode="W") midsize_tuto.licence = self.licence midsize_tuto.save() # populate with 2 chapters (1 extract each) midsize_tuto_draft = midsize_tuto.load_version() chapter1 = ContainerFactory(parent=midsize_tuto_draft, db_object=midsize_tuto) ExtractFactory(container=chapter1, db_object=midsize_tuto) chapter2 = ContainerFactory(parent=midsize_tuto_draft, db_object=midsize_tuto) ExtractFactory(container=chapter2, db_object=midsize_tuto) # publish it midsize_tuto = PublishableContent.objects.get(pk=midsize_tuto.pk) published = publish_content(midsize_tuto, midsize_tuto_draft) self.assertEqual(published.content, midsize_tuto) self.assertEqual(published.content_pk, midsize_tuto.pk) self.assertEqual(published.content_type, midsize_tuto.type) self.assertEqual(published.content_public_slug, midsize_tuto_draft.slug) self.assertEqual(published.sha_public, midsize_tuto.sha_draft) public = midsize_tuto.load_version(sha=published.sha_public, public=published) self.assertIsNotNone(public) self.assertTrue(public.PUBLIC) # it's a PublicContent object self.assertEqual(public.type, published.content_type) self.assertEqual(public.current_version, published.sha_public) # test creation of files: self.assertTrue(Path(published.get_prod_path()).is_dir()) self.assertTrue( Path(published.get_prod_path(), "manifest.json").is_file()) self.assertTrue( Path(public.get_prod_path(), public.introduction).is_file()) self.assertTrue( Path(public.get_prod_path(), public.conclusion).is_file()) self.assertEqual(len(public.children), 2) for child in public.children: self.assertTrue(os.path.isfile( child.get_prod_path())) # an HTML file for each chapter self.assertIsNone(child.introduction) self.assertIsNone(child.conclusion)
def test_delete_fail_no_right(self): UserGalleryFactory(user=self.other.user, gallery=self.gallery) response = self.client.delete( reverse("api:gallery:detail", kwargs={"pk": self.gallery.pk})) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(Gallery.objects.filter(pk=self.gallery.pk).count(), 1) self.assertEqual( UserGallery.objects.filter(gallery=self.gallery).count(), 1)
def test_delete(self): UserGalleryFactory(user=self.profile.user, gallery=self.gallery) response = self.client.delete( reverse("api:gallery:detail", kwargs={"pk": self.gallery.pk})) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Gallery.objects.filter(pk=self.gallery.pk).count(), 0) self.assertEqual( UserGallery.objects.filter(gallery=self.gallery).count(), 0)
def get_small_opinion(self): """ Returns a published opinion without extract. """ opinion = PublishableContentFactory(type="OPINION") opinion.authors.add(self.user_author) UserGalleryFactory(gallery=opinion.gallery, user=self.user_author, mode="W") opinion.licence = LicenceFactory() opinion.save() opinion_draft = opinion.load_version() return publish_content(opinion, opinion_draft)
def setUp(self): self.profile = ProfileFactory() self.other = ProfileFactory() self.client = APIClient() client_oauth2 = create_oauth2_client(self.profile.user) authenticate_client(self.client, client_oauth2, self.profile.user.username, "hostel77") self.gallery = GalleryFactory() UserGalleryFactory(user=self.profile.user, gallery=self.gallery) self.image = ImageFactory(gallery=self.gallery) self.gallery_other = GalleryFactory() UserGalleryFactory(user=self.other.user, gallery=self.gallery_other) self.image_other = ImageFactory(gallery=self.gallery_other) self.gallery_shared = GalleryFactory() UserGalleryFactory(user=self.other.user, gallery=self.gallery_shared) UserGalleryFactory(user=self.profile.user, gallery=self.gallery_shared, mode=GALLERY_READ) self.image_shared = ImageFactory(gallery=self.gallery_shared)
def test_permanently_unpublish_opinion(self): opinion = PublishableContentFactory(type="OPINION") opinion.authors.add(self.user_author) UserGalleryFactory(gallery=opinion.gallery, user=self.user_author, mode="W") opinion.licence = self.licence opinion.save() opinion_draft = opinion.load_version() ExtractFactory(container=opinion_draft, db_object=opinion) ExtractFactory(container=opinion_draft, db_object=opinion) self.client.force_login(self.user_author) # publish result = self.client.post( reverse("validation:publish-opinion", kwargs={"pk": opinion.pk, "slug": opinion.slug}), {"source": "", "version": opinion_draft.current_version}, follow=False, ) self.assertEqual(result.status_code, 302) # login as staff self.client.force_login(self.user_staff) # unpublish opinion result = self.client.post( reverse("validation:ignore-opinion", kwargs={"pk": opinion.pk, "slug": opinion.slug}), { "operation": "REMOVE_PUB", }, follow=False, ) self.assertEqual(result.status_code, 200) # refresh opinion = PublishableContent.objects.get(pk=opinion.pk) # check that the opinion is not published self.assertFalse(opinion.in_public()) # check that it's impossible to publish the opinion again result = self.client.get(opinion.get_absolute_url()) self.assertContains(result, _("Billet modéré")) # front result = self.client.post( reverse("validation:publish-opinion", kwargs={"pk": opinion.pk, "slug": opinion.slug}), {"source": "", "version": opinion_draft.current_version}, follow=False, ) self.assertEqual(result.status_code, 403) # back
def test_get_gallery(self): UserGalleryFactory(user=self.profile.user, gallery=self.gallery) response = self.client.get( reverse("api:gallery:detail", kwargs={"pk": self.gallery.pk})) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.get("id"), self.gallery.pk) self.assertEqual(response.data.get("title"), self.gallery.title) self.assertEqual(response.data.get("subtitle"), self.gallery.subtitle) self.assertIsNone(response.data.get("linked_content")) self.assertEqual(response.data.get("image_count"), 0) self.assertEqual(response.data.get("permissions"), { "read": True, "write": True })
def test_list_galeries_belong_to_member(self): profile = ProfileFactory() gallery = GalleryFactory() GalleryFactory() UserGalleryFactory(user=profile.user, gallery=gallery) self.client.force_login(profile.user) response = self.client.get(reverse("gallery-list"), follow=True) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context["galleries"])) self.assertEqual( UserGallery.objects.filter(user=profile.user).first().gallery, response.context["galleries"].first() )
def get_published_content(self, author, user_staff, nb_part=1, nb_chapter=1, nb_extract=1): bigtuto = PublishableContentFactory(type="TUTORIAL") bigtuto.authors.add(author) UserGalleryFactory(gallery=bigtuto.gallery, user=author, mode="W") bigtuto.licence = LicenceFactory() bigtuto.save() # populate the bigtuto bigtuto_draft = bigtuto.load_version() for i in range(nb_part): part = ContainerFactory(parent=bigtuto_draft, db_object=bigtuto) for j in range(nb_chapter): chapter = ContainerFactory(parent=part, db_object=bigtuto) for k in range(nb_extract): ExtractFactory(container=chapter, db_object=bigtuto) # connect with author: self.client.force_login(author) # ask validation self.client.post( reverse("validation:ask", kwargs={"pk": bigtuto.pk, "slug": bigtuto.slug}), {"text": "ask for validation", "source": "", "version": bigtuto_draft.current_version}, follow=False, ) # login with staff and publish self.client.force_login(user_staff) validation = Validation.objects.filter(content=bigtuto).last() self.client.post( reverse("validation:reserve", kwargs={"pk": validation.pk}), {"version": validation.version}, follow=False ) # accept self.client.post( reverse("validation:accept", kwargs={"pk": validation.pk}), {"text": "accept validation", "is_major": True, "source": ""}, follow=False, ) self.client.logout() published = PublishedContent.objects.filter(content=bigtuto).first() self.assertIsNotNone(published) return published
def create_content(self): """ Returns a content and its draft used in following tests. """ tuto = PublishableContentFactory(type="TUTORIAL", intro="Intro tuto", conclusion="Conclusion tuto") tuto.authors.add(self.user_author) UserGalleryFactory(gallery=tuto.gallery, user=self.user_author, mode="W") tuto.licence = self.licence tuto.save() tuto_draft = tuto.load_version() return tuto, tuto_draft
def test_put_fail_no_right(self): title = "Ma super galerie" subtitle = "... A été mise à jour !" UserGalleryFactory(user=self.other.user, gallery=self.gallery) response = self.client.put( reverse("api:gallery:detail", kwargs={"pk": self.gallery.pk}), { "title": title, "subtitle": subtitle }) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) gallery = Gallery.objects.get(pk=self.gallery.pk) self.assertNotEqual(gallery.title, title) self.assertNotEqual(gallery.subtitle, subtitle)
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 test_delete_participant(self): UserGalleryFactory(user=self.new_participant.user, gallery=self.gallery) response = self.client.delete( reverse( "api:gallery:detail-participant", kwargs={ "pk_gallery": self.gallery.pk, "user__pk": self.new_participant.user.pk }, )) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual( UserGallery.objects.filter(gallery=self.gallery, user=self.new_participant.user).count(), 0)
def test_delete_participant_fail_no_permissions(self): UserGalleryFactory(user=self.new_participant.user, gallery=self.gallery_other) response = self.client.delete( reverse( "api:gallery:detail-participant", kwargs={ "pk_gallery": self.gallery_other.pk, "user__pk": self.new_participant.user.pk }, )) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual( UserGallery.objects.filter(gallery=self.gallery_other, user=self.new_participant.user).count(), 1)
def test_delete_image_from_other_user(self): """if user try to remove images from another user without permission""" profile4 = ProfileFactory() gallery4 = GalleryFactory() image4 = ImageFactory(gallery=gallery4) UserGalleryFactory(user=profile4.user, gallery=gallery4) self.assertEqual(1, Image.objects.filter(pk=image4.pk).count()) self.client.force_login(self.profile1.user) self.client.post( reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}), {"gallery": self.gallery1.pk, "delete": "", "image": image4.pk}, follow=True, ) self.assertEqual(1, Image.objects.filter(pk=image4.pk).count()) image4.delete()
def test_post_fail_add_participant_already_in(self): user_gallery = UserGalleryFactory(user=self.new_participant.user, gallery=self.gallery, mode=GALLERY_READ) response = self.client.post( reverse("api:gallery:list-participants", kwargs={"pk_gallery": self.gallery.pk}), { "id": self.new_participant.user.pk, "can_write": True }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) user_gallery = UserGallery.objects.get(pk=user_gallery.pk) self.assertEqual(user_gallery.mode, GALLERY_READ)
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 test_put_update_gallery(self): title = "Ma super galerie" subtitle = "... A été mise à jour !" UserGalleryFactory(user=self.profile.user, gallery=self.gallery) response = self.client.put( reverse("api:gallery:detail", kwargs={"pk": self.gallery.pk}), { "title": title, "subtitle": subtitle }) self.assertEqual(response.status_code, status.HTTP_200_OK) gallery = Gallery.objects.get(pk=self.gallery.pk) self.assertEqual(gallery.title, title) self.assertEqual(gallery.subtitle, subtitle) self.assertEqual(response.data.get("title"), gallery.title) self.assertEqual(response.data.get("subtitle"), gallery.subtitle)
def test_put_fail_modify_participant_read_permissions(self): user_gallery = UserGalleryFactory(user=self.new_participant.user, gallery=self.gallery_shared, mode=GALLERY_READ) response = self.client.put( reverse( "api:gallery:detail-participant", kwargs={ "pk_gallery": self.gallery_shared.pk, "user__pk": self.new_participant.user.pk }, ), {"can_write": True}, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) user_gallery = UserGallery.objects.get(pk=user_gallery.pk) self.assertEqual(user_gallery.mode, GALLERY_READ)
def test_put_modify_participant(self): user_gallery = UserGalleryFactory(user=self.new_participant.user, gallery=self.gallery, mode=GALLERY_READ) response = self.client.put( reverse( "api:gallery:detail-participant", kwargs={ "pk_gallery": self.gallery.pk, "user__pk": self.new_participant.user.pk }, ), {"can_write": True}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) user_gallery = UserGallery.objects.get(pk=user_gallery.pk) self.assertEqual(user_gallery.mode, GALLERY_WRITE)
def load_gallery(cli, size, fake, *_, **__): """ Load galleries """ nb_galleries = size * 1 nb_images = size * 3 cli.stdout.write(f"Nombres de galéries à créer par utilisateur: {nb_galleries}") cli.stdout.write(f"Nombres d'images à créer par gallerie: {nb_images}") tps1 = time.time() nb_users = User.objects.count() if nb_users == 0: cli.stdout.write( "Il n'y a aucun membre actuellement. " "Vous devez rajouter les membres dans vos fixtures (member)" ) return profiles = list(Profile.objects.all()) for user_index in range(0, nb_users): for gallery_index in range(0, nb_galleries): gal = GalleryFactory(title=fake.text(max_nb_chars=80), subtitle=fake.text(max_nb_chars=200)) UserGalleryFactory(user=profiles[user_index].user, gallery=gal) __push_images_into_gallery(gal, user_index, gallery_index, nb_galleries, nb_images, nb_users) tps2 = time.time() cli.stdout.write(f"\nFait en {tps2 - tps1} sec")
def setUp(self): self.mas = ProfileFactory().user self.overridden_zds_app["member"]["bot_account"] = self.mas.username self.licence = LicenceFactory() self.user_author = ProfileFactory().user self.staff = StaffProfileFactory().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.save() 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.old_registry = { key: value for key, value in PublicatorRegistry.get_all_registered() } class TestPdfPublicator(Publicator): def publish(self, md_file_path, base_name, **kwargs): with Path(base_name + ".pdf").open("w") as f: f.write("bla") shutil.copy2( str(Path(base_name + ".pdf")), str(Path(md_file_path.replace("__building", "")).parent)) PublicatorRegistry.registry["pdf"] = TestPdfPublicator()
def test_cancel_pick_operation(self): opinion = PublishableContentFactory(type="OPINION") opinion.authors.add(self.user_author) UserGalleryFactory(gallery=opinion.gallery, user=self.user_author, mode="W") opinion.licence = self.licence opinion.save() opinion_draft = opinion.load_version() ExtractFactory(container=opinion_draft, db_object=opinion) ExtractFactory(container=opinion_draft, db_object=opinion) self.client.force_login(self.user_author) # publish result = self.client.post( reverse("validation:publish-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "source": "", "version": opinion_draft.current_version }, follow=False, ) self.assertEqual(result.status_code, 302) # login as staff self.client.force_login(self.user_staff) # PICK result = self.client.post( reverse("validation:pick-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "source": "", "version": opinion_draft.current_version }, follow=False, ) self.assertEqual(result.status_code, 302) # cancel the operation operation = PickListOperation.objects.latest("operation_date") result = self.client.post(reverse("validation:revoke-ignore-opinion", kwargs={"pk": operation.pk}), follow=False) self.assertEqual(result.status_code, 200) # refresh operation = PickListOperation.objects.get(pk=operation.pk) opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertFalse(operation.is_effective) self.assertEqual(self.user_staff, operation.canceler_user) self.assertIsNone(opinion.sha_picked) # NO_PICK result = self.client.post( reverse("validation:ignore-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "operation": "NO_PICK", }, follow=False, ) self.assertEqual(result.status_code, 200) # cancel the operation operation = PickListOperation.objects.latest("operation_date") result = self.client.post(reverse("validation:revoke-ignore-opinion", kwargs={"pk": operation.pk}), follow=False) self.assertEqual(result.status_code, 200) # check that the opinion is displayed on validation page result = self.client.get(reverse("validation:list-opinion")) self.assertContains(result, opinion.title) # REMOVE_PUB result = self.client.post( reverse("validation:ignore-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "operation": "REMOVE_PUB", }, follow=False, ) self.assertEqual(result.status_code, 200) # cancel the operation operation = PickListOperation.objects.latest("operation_date") result = self.client.post(reverse("validation:revoke-ignore-opinion", kwargs={"pk": operation.pk}), follow=False) self.assertEqual(result.status_code, 200) # check that the opinion can be published again result = self.client.post( reverse("validation:publish-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "source": "", "version": opinion_draft.current_version }, follow=False, ) self.assertEqual(result.status_code, 302)
def test_publish_content_change_title_before_watchdog( self, opinions_management): """ Test we can publish a content, change its title and publish it again right away, before the publication watchdog processed the first publication. """ previous_extra_content_generation_policy = self.overridden_zds_app[ "content"]["extra_content_generation_policy"] self.overridden_zds_app["content"][ "extra_content_generation_policy"] = "WATCHDOG" # Create a content: opinion = PublishableContentFactory(type="OPINION") opinion.authors.add(self.user_author) UserGalleryFactory(gallery=opinion.gallery, user=self.user_author, mode="W") opinion.licence = self.licence opinion.save() opinion_draft = opinion.load_version() # Publish it a first time: self.client.force_login(self.user_author) result = self.client.post( reverse("validation:publish-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "text": "Blabla", "source": "", "version": opinion_draft.current_version }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(PublishedContent.objects.count(), 1) opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertIsNotNone(opinion.public_version) self.assertEqual(opinion.public_version.sha_public, opinion_draft.current_version) # Change the title: random = "Whatever, we don't care about the details" result = self.client.post( reverse("content:edit", args=[opinion.pk, opinion.slug]), { "title": "{} ({})".format(opinion.title, "modified"), "description": random, "introduction": random, "conclusion": random, "type": "OPINION", "licence": opinion.licence.pk, "subcategory": opinion.subcategory.first().pk, "last_hash": opinion.load_version().compute_hash(), "image": (settings.BASE_DIR / "fixtures" / "logo.png").open("rb"), }, follow=False, ) self.assertEqual(result.status_code, 302) self.assertEqual(PublishedContent.objects.count(), 1) self.assertEqual(opinions_management.send.call_count, 1) self.assertEqual(opinions_management.send.call_args[1]["action"], "publish") opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertIsNotNone(opinion.public_version) self.assertEqual(opinion.public_version.sha_public, opinion_draft.current_version) # and publish it a second time now it has a new title: result = self.client.post( reverse("validation:publish-opinion", kwargs={ "pk": opinion.pk, "slug": opinion.slug }), { "text": "Blabla", "source": "", "version": opinion_draft.current_version }, follow=False, ) self.assertEqual(result.status_code, 302) # There are two PublishedContent: one with the old title and the old slug # redirecting to the current version of the content with the new title: self.assertEqual(PublishedContent.objects.count(), 2) opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertIsNotNone(opinion.public_version) opinion_draft = opinion.load_version() self.assertEqual(opinion.public_version.sha_public, opinion_draft.current_version) requested_events = PublicationEvent.objects.filter( state_of_processing="REQUESTED") self.assertEqual(requested_events.count(), 4) # Now, call the watchdog: call_command("publication_watchdog", "--once") requested_events = PublicationEvent.objects.filter( state_of_processing="REQUESTED") self.assertEqual(requested_events.count(), 0) self.overridden_zds_app["content"][ "extra_content_generation_policy"] = previous_extra_content_generation_policy