Skip to content

Commit

Permalink
Refactorise la création de lien
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Jun 14, 2021
1 parent 8624c68 commit 0f2d91e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 92 deletions.
104 changes: 31 additions & 73 deletions zds/tutorialv2/tests/tests_views/tests_shareable_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _


from zds.member.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.factories import PublishableContentFactory
from zds.tutorialv2.models.database import ShareableLink
from zds.tutorialv2.tests import TutorialTestMixin


class ListShareableLinksPermissionTests(TutorialTestMixin, TestCase):
class ListShareableLinksTests(TutorialTestMixin, TestCase):
def setUp(self):
# Create users
self.author = ProfileFactory().user
Expand All @@ -22,73 +21,54 @@ def setUp(self):
self.content = PublishableContentFactory(author_list=[self.author])

# Get information to be reused in tests
self.page_url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk})
self.login_url = reverse("member-login") + "?next=" + self.page_url
self.url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk})
self.login_url = reverse("member-login") + "?next=" + self.url

def test_not_authenticated(self):
self.client.logout() # ensure no user is authenticated
response = self.client.get(self.page_url)
self.client.logout()
response = self.client.get(self.url)
self.assertRedirects(response, self.login_url)

def test_authenticated_author(self):
self.client.force_login(self.author)
response = self.client.get(self.page_url)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)

def test_authenticated_staff(self):
self.client.force_login(self.staff)
response = self.client.get(self.page_url)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)

def test_authenticated_outsider(self):
self.client.force_login(self.outsider)
response = self.client.get(self.page_url)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)


class ListShareableLinksFunctionalTests(TestCase):
def setUp(self):
# Create users
self.author = ProfileFactory().user
self.client.force_login(self.author)

# Create contents
self.content_1 = PublishableContentFactory(author_list=[self.author])
self.content_2 = PublishableContentFactory(author_list=[self.author])

# Get information to be reused in tests
self.list_url_1 = reverse("content:list-shareable-links", kwargs={"pk": self.content_1.pk})

def test_no_link(self):
response = self.client.get(self.list_url_1)
self.client.force_login(self.author)
response = self.client.get(self.url)
self.assertContains(response, _("Vous n'avez pas de liens de partage actifs."))
self.assertContains(response, _("Créer un lien de partage"))

def test_one_link(self):
ShareableLink(content=self.content_1).save()
response = self.client.get(self.list_url_1)
self.client.force_login(self.author)
ShareableLink(content=self.content).save()
response = self.client.get(self.url)
self.assertContains(response, _("Liens actifs"))
self.assertContains(response, _("Créer un lien de partage"))
self.assertContains(response, '<li class="shareable-link">', count=1)

def test_two_links(self):
ShareableLink(content=self.content_1).save()
ShareableLink(content=self.content_1).save()
response = self.client.get(self.list_url_1)
self.client.force_login(self.author)
ShareableLink(content=self.content).save()
ShareableLink(content=self.content).save()
response = self.client.get(self.url)
self.assertContains(response, _("Liens actifs"))
self.assertContains(response, _("Créer un lien de partage"))
self.assertContains(response, '<li class="shareable-link">', count=2)

def test_no_interference(self):
ShareableLink(content=self.content_1).save()
ShareableLink(content=self.content_2).save()
response = self.client.get(self.list_url_1)
self.assertContains(response, _("Liens actifs"))
self.assertContains(response, _("Créer un lien de partage"))
self.assertContains(response, '<li class="shareable-link">', count=1)


class CreateShareableLinksPermissionTests(TestCase):
class CreateShareableLinkTests(TestCase):
def setUp(self):
# Create users
self.author = ProfileFactory().user
Expand All @@ -99,57 +79,35 @@ def setUp(self):
self.content = PublishableContentFactory(author_list=[self.author])

# Get information to be reused in tests
self.form_url = reverse("content:create-shareable-link", kwargs={"pk": self.content.pk})
self.list_url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk})
self.login_url = reverse("member-login") + "?next=" + self.form_url
self.url = reverse("content:create-shareable-link", kwargs={"pk": self.content.pk})
self.redirect_url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk})
self.login_url = reverse("member-login") + "?next=" + self.url

def test_not_authenticated(self):
self.client.logout() # ensure no user is authenticated
response = self.client.post(self.form_url)
self.client.logout()
response = self.client.post(self.url)
self.assertRedirects(response, self.login_url)

def test_authenticated_author(self):
self.client.force_login(self.author)
response = self.client.post(self.form_url)
self.assertRedirects(response, self.list_url)

def test_no_get_method(self):
self.client.force_login(self.author)
response = self.client.get(self.form_url)
self.assertEqual(response.status_code, 405)
n_links_before = len(ShareableLink.objects.all())
data = {"description": "Ceci n'est pas le lien vers La Blague", "expiration": "2042-08-01", "type": "BETA"}
response = self.client.post(self.url, data=data)
self.assertRedirects(response, self.redirect_url)
n_links_after = len(ShareableLink.objects.all())
self.assertEqual(n_links_after, n_links_before + 1)

def test_authenticated_staff(self):
self.client.force_login(self.staff)
response = self.client.post(self.form_url)
response = self.client.post(self.url)
self.assertEqual(response.status_code, 403)

def test_authenticated_outsider(self):
self.client.force_login(self.outsider)
response = self.client.post(self.form_url)
response = self.client.post(self.url)
self.assertEqual(response.status_code, 403)


class CreateShareableLinksFunctionalTests(TestCase):
def setUp(self):
# Create user
self.author = ProfileFactory().user
self.client.force_login(self.author)

# Create contents
self.content_1 = PublishableContentFactory(author_list=[self.author])
self.content_2 = PublishableContentFactory(author_list=[self.author])

# Get information to be reused in tests
self.form_url = reverse("content:create-shareable-link", kwargs={"pk": self.content_1.pk})
self.list_url = reverse("content:list-shareable-links", kwargs={"pk": self.content_2.pk})

def test_create_shareable_link(self):
n_links_before = len(ShareableLink.objects.all())
self.client.post(self.form_url)
n_links_after = len(ShareableLink.objects.all())
self.assertEqual(n_links_after, n_links_before + 1)


class EditShareableLinkTests(TestCase):
def setUp(self):
# Create users
Expand Down
41 changes: 22 additions & 19 deletions zds/tutorialv2/views/shareable_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic import TemplateView, FormView, RedirectView
from django.views.generic.edit import BaseFormView
from django.views.generic import TemplateView

from zds.member.decorator import LoginRequiredMixin
from zds.tutorialv2.mixins import SingleContentDetailViewMixin, ModalFormView, SingleContentViewMixin
from zds.tutorialv2.mixins import SingleContentDetailViewMixin, ModalFormView
from zds.tutorialv2.models import SHAREABLE_LINK_TYPES
from zds.tutorialv2.models.database import ShareableLink
from zds.tutorialv2.models.database import ShareableLink, PublishableContent


class ListShareableLinks(LoginRequiredMixin, SingleContentDetailViewMixin, TemplateView):
Expand Down Expand Up @@ -54,29 +52,34 @@ class ShareableLinkForm(forms.Form):
type = forms.ChoiceField(choices=SHAREABLE_LINK_TYPES)


class CreateShareableLink(LoginRequiredMixin, SingleContentDetailViewMixin, ModalFormView):
class CreateShareableLink(ModalFormView):
http_method_names = ["post"]
template_name = "tutorialv2/view/list_shareable_links.html"
authorized_for_staff = False
form_class = ShareableLinkForm
modal_form = True

def get_success_url(self):
return reverse("content:list-shareable-links", kwargs={"pk": self.get_object().pk})
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
self.content = get_object_or_404(PublishableContent, pk=self.kwargs["pk"])
if self.request.user not in self.content.authors.all():
raise PermissionDenied
self.redirect_url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk})
return super().dispatch(*args, *kwargs)

def form_invalid(self, form):
form.previous_page_url = self.redirect_url
return super().form_invalid(form)

def form_valid(self, form):
link = ShareableLink(content=self.get_object())
link.description = form.cleaned_data["description"]
link.expiration = form.cleaned_data["expiration"]
link.type = form.cleaned_data["type"]
link.save()
ShareableLink(
content=self.content,
description=form.cleaned_data["description"],
expiration=form.cleaned_data["expiration"],
type=form.cleaned_data["type"],
).save()
self.success_url = self.redirect_url
messages.success(self.request, _("Le lien a été créé."))
return super().form_valid(form)

def form_invalid(self, form):
form.previous_page_url = reverse("content:list-shareable-links", kwargs={"pk": self.get_object().pk})
return super().form_invalid(form)


class EditShareableLink(ModalFormView):
http_method_names = ["post"]
Expand Down

0 comments on commit 0f2d91e

Please sign in to comment.