Skip to content

Commit

Permalink
Corrige une erreur lors du retrait d'un bot des auteurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D authored and Situphen committed Oct 26, 2022
1 parent 445993f commit 8c6a633
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 70 deletions.
70 changes: 70 additions & 0 deletions zds/tutorialv2/tests/tests_views/tests_addauthor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from unittest.mock import patch

from django.conf import settings
from django.contrib.auth.models import Group
from django.test import TestCase
from django.urls import reverse

from zds.gallery.models import UserGallery, GALLERY_WRITE
from zds.gallery.tests.factories import UserGalleryFactory
from zds.member.tests.factories import ProfileFactory, UserFactory
from zds.tutorialv2.models.database import PublishableContent
from zds.tutorialv2.tests import override_for_contents, TutorialTestMixin
from zds.tutorialv2.tests.factories import PublishableContentFactory


@override_for_contents()
class AddAuthorTest(TutorialTestMixin, TestCase):
def setUp(self):
self.bot_group = Group(name=settings.ZDS_APP["member"]["bot_group"])
self.bot_group.save()

self.overridden_zds_app["member"]["bot_account"] = ProfileFactory().user.username

self.user_author = ProfileFactory().user
self.user_guest = ProfileFactory().user
self.external = UserFactory(username=self.overridden_zds_app["member"]["external_account"])
self.external.groups.add(self.bot_group)
self.external.save()

self.tuto = PublishableContentFactory(type="TUTORIAL", author_list=[self.user_author])
UserGalleryFactory(gallery=self.tuto.gallery, user=self.user_author, mode="W")

self.client.force_login(self.user_author)

@patch("zds.tutorialv2.signals.authors_management")
def test_nominal(self, authors_management):

result = self.client.post(
reverse("content:add-author", args=[self.tuto.pk]), {"username": self.user_guest.username}, follow=False
)

self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 2)
gallery = UserGallery.objects.filter(gallery=self.tuto.gallery, user=self.user_guest).first()
self.assertIsNotNone(gallery)
self.assertEqual(GALLERY_WRITE, gallery.mode)
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "add")

@patch("zds.tutorialv2.signals.authors_management")
def test_not_existing_user(self, authors_management):

result = self.client.post(
reverse("content:add-author", args=[self.tuto.pk]), {"username": "unknown"}, follow=False
)

self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 0)

@patch("zds.tutorialv2.signals.authors_management")
def test_bot(self, authors_management):

result = self.client.post(
reverse("content:add-author", args=[self.tuto.pk]), {"username": self.external.username}, follow=False
)

self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 0)
68 changes: 1 addition & 67 deletions zds/tutorialv2/tests/tests_views/tests_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from zds.forum.tests.factories import ForumFactory, ForumCategoryFactory
from zds.forum.models import Topic, Post, TopicRead
from zds.gallery.tests.factories import UserGalleryFactory
from zds.gallery.models import GALLERY_WRITE, UserGallery, Gallery
from zds.gallery.models import UserGallery, Gallery
from zds.gallery.models import Image
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory, UserFactory
from zds.mp.models import PrivateTopic, PrivatePost
Expand Down Expand Up @@ -2441,72 +2441,6 @@ def test_help_tutorials_are_sorted_by_update_date(self):
self.assertEqual(contents[0], tutoriel_1)
self.assertEqual(contents[1], tutoriel_2)

@patch("zds.tutorialv2.signals.authors_management")
def test_add_author(self, authors_management):
self.client.force_login(self.user_author)
result = self.client.post(
reverse("content:add-author", args=[self.tuto.pk]), {"username": self.user_guest.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 2)
gallery = UserGallery.objects.filter(gallery=self.tuto.gallery, user=self.user_guest).first()
self.assertIsNotNone(gallery)
self.assertEqual(GALLERY_WRITE, gallery.mode)
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "add")
# add unexisting user
result = self.client.post(
reverse("content:add-author", args=[self.tuto.pk]), {"username": "unknown"}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 2)
self.assertEqual(authors_management.send.call_count, 1)

@patch("zds.tutorialv2.signals.authors_management")
def test_remove_author(self, authors_management):
self.client.force_login(self.user_author)
tuto = PublishableContentFactory(author_list=[self.user_author, self.user_guest])
result = self.client.post(
reverse("content:remove-author", args=[tuto.pk]), {"username": self.user_guest.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "remove")

self.assertIsNone(UserGallery.objects.filter(gallery=self.tuto.gallery, user=self.user_guest).first())
# remove unexisting user
result = self.client.post(
reverse("content:remove-author", args=[tuto.pk]), {"username": "unknown"}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 1)

# remove last author must lead to no change
result = self.client.post(
reverse("content:remove-author", args=[tuto.pk]), {"username": self.user_author.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 1)

# re-add guest
result = self.client.post(
reverse("content:add-author", args=[tuto.pk]), {"username": self.user_guest.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.count(), 2)

# then, make `user_author` remove himself
result = self.client.post(
reverse("content:remove-author", args=[tuto.pk]), {"username": self.user_author.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertTrue(reverse("tutorial:find-tutorial", args=[self.user_author.username]) in result.url)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.count(), 1)
self.assertEqual(PublishableContent.objects.get(pk=tuto.pk).authors.filter(pk=self.user_author.pk).count(), 0)

def test_warn_typo(self):
"""
Add a non-regression test about warning the author(s) of a typo in tutorial
Expand Down
105 changes: 105 additions & 0 deletions zds/tutorialv2/tests/tests_views/tests_removeauthor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from unittest.mock import patch

from django.conf import settings
from django.contrib.auth.models import Group
from django.test import TestCase
from django.urls import reverse

from zds.gallery.models import UserGallery
from zds.member.tests.factories import ProfileFactory
from zds.tutorialv2.models.database import PublishableContent
from zds.tutorialv2.tests import override_for_contents, TutorialTestMixin
from zds.tutorialv2.tests.factories import PublishableContentFactory


@override_for_contents()
class RemoveAuthor(TutorialTestMixin, TestCase):
def setUp(self):
self.bot_group = Group(name=settings.ZDS_APP["member"]["bot_group"])
self.bot_group.save()

self.overridden_zds_app["member"]["bot_account"] = ProfileFactory().user.username

self.author = ProfileFactory().user
self.other_author = ProfileFactory().user

self.external = ProfileFactory().user
self.external.username = self.overridden_zds_app["member"]["external_account"]
self.external.save()
self.external.groups.add(self.bot_group)

self.tuto = PublishableContentFactory(type="TUTORIAL", author_list=[self.author, self.other_author])

self.client.force_login(self.author)

@patch("zds.tutorialv2.signals.authors_management")
def test_nominal(self, authors_management):
"""Removing a member effectively being an author."""
result = self.client.post(
reverse("content:remove-author", args=[self.tuto.pk]),
{"username": self.other_author.username},
follow=False,
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "remove")

self.assertIsNone(UserGallery.objects.filter(gallery=self.tuto.gallery, user=self.other_author).first())

@patch("zds.tutorialv2.signals.authors_management")
def test_not_existing(self, authors_management):
"""Attempting to remove a user who is not author shall do nothing."""
result = self.client.post(
reverse("content:remove-author", args=[self.tuto.pk]), {"username": "unknown"}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 2)
self.assertEqual(authors_management.send.call_count, 0)

@patch("zds.tutorialv2.signals.authors_management")
def test_last_remaining(self, authors_management):
"""Attempting to remove the last author shall lead to no change."""
self.tuto.authors.remove(self.other_author)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 1)

result = self.client.post(
reverse("content:remove-author", args=[self.tuto.pk]), {"username": self.author.username}, follow=False
)
self.assertEqual(result.status_code, 302)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 1)
self.assertEqual(authors_management.send.call_count, 0)

@patch("zds.tutorialv2.signals.authors_management")
def test_self(self, authors_management):
"""An author removes themselves."""
result = self.client.post(
reverse("content:remove-author", args=[self.tuto.pk]), {"username": self.author.username}, follow=False
)

self.assertEqual(result.status_code, 302)
tuto = PublishableContent.objects.get(pk=self.tuto.pk)
self.assertEqual(tuto.authors.count(), 1)
self.assertFalse(tuto.authors.filter(pk=self.author.pk).exists())
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "remove")

@patch("zds.tutorialv2.signals.authors_management")
def test_bot(self, authors_management):
"""
Bots can be removed from the authors.
Check a non-regression on a bug where exceptions were raised when removing "Auteur externe" from the authors.
"""
self.tuto.authors.add(self.external)
self.assertEqual(PublishableContent.objects.get(pk=self.tuto.pk).authors.count(), 3)

result = self.client.post(
reverse("content:remove-author", args=[self.tuto.pk]), {"username": self.external.username}, follow=False
)

self.assertEqual(result.status_code, 302)
tuto = PublishableContent.objects.get(pk=self.tuto.pk)
self.assertEqual(tuto.authors.count(), 2)
self.assertFalse(tuto.authors.filter(pk=self.external.pk).exists())
self.assertEqual(authors_management.send.call_count, 1)
self.assertEqual(authors_management.send.call_args[1]["action"], "remove")
6 changes: 3 additions & 3 deletions zds/tutorialv2/views/authors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, redirect
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.text import format_lazy
Expand All @@ -10,6 +9,7 @@
from zds.gallery.models import UserGallery, GALLERY_WRITE
from zds.member.decorator import LoggedWithReadWriteHability
from zds.member.utils import get_bot_account
from zds.mp.models import is_reachable
from zds.tutorialv2 import signals

from zds.tutorialv2.forms import AuthorForm, RemoveAuthorForm
Expand Down Expand Up @@ -127,7 +127,7 @@ def form_valid(self, form):
if RemoveAuthorFromContent.remove_author(self.object, user):
if user.pk == self.request.user.pk:
current_user = True
else:
elif is_reachable(user):
send_mp(
bot,
[user],
Expand Down

0 comments on commit 8c6a633

Please sign in to comment.