From 6d1a5f7326fcffbe200fc7855ba70c64d8b62f70 Mon Sep 17 00:00:00 2001 From: tata Date: Thu, 10 Aug 2023 18:19:45 +0200 Subject: [PATCH] For #26 add extra tests subscr\unsubscr for exceptions --- src/contacts/exceptions.py | 16 ++++ src/contacts/tests/test_view.py | 75 +++++++++++++++++++ src/contacts/views.py | 59 ++++++--------- .../contacts/subscription/unsubscribe.html | 2 +- 4 files changed, 116 insertions(+), 36 deletions(-) create mode 100644 src/contacts/tests/test_view.py diff --git a/src/contacts/exceptions.py b/src/contacts/exceptions.py index b5ae822..4639ef9 100644 --- a/src/contacts/exceptions.py +++ b/src/contacts/exceptions.py @@ -22,3 +22,19 @@ class JobError(Exception): """ pass + + +class FailedNewsSubscription(Exception): + """ + get triggered if subscription to news letter failed + """ + + pass + + +class UnsubcribeFail(Exception): + """ + get triggered if subscription to news letter failed + """ + + pass diff --git a/src/contacts/tests/test_view.py b/src/contacts/tests/test_view.py new file mode 100644 index 0000000..3afc2aa --- /dev/null +++ b/src/contacts/tests/test_view.py @@ -0,0 +1,75 @@ +from django.contrib.auth import get_user_model +from django.test import TestCase, override_settings +from django.urls import reverse +from django.utils.translation import gettext_lazy as _ + +from src.accounts.tests.factories import UserFactory +from src.contacts.exceptions import FailedNewsSubscription, UnsubcribeFail +from src.profiles.models import Profile +from src.profiles.tests.factories.profile_factory import ProfileFactory + +User = get_user_model() + + +@override_settings(LANGUAGE_CODE="en", LANGUAGES=(("en", "English"),)) +class SubscribeTestCase(TestCase): + def test_ok_subscription_news(self): + """Auth-ed user can subscribe for a news letter""" + user = UserFactory() + self.client.force_login(user) + profile = Profile.objects.get(user=user) + headers = {"HTTP_HX-Request": "true"} + url = reverse("contacts:subscribe") + resp = self.client.post(url, **headers) + profile.refresh_from_db() + + self.assertEqual(resp.status_code, 200) + self.assertIsNotNone(resp.headers["HX-Redirect"]) + self.assertTrue(profile.want_news) + + def test_failed_subscription_news(self): + """ + Failed subscription if not htmx request + (no header htmx) + """ + user = UserFactory() + profile = user.profile + self.client.force_login(user) + url = reverse("contacts:subscribe") + with self.assertRaises(FailedNewsSubscription) as e: + self.client.post(url) # noqa + + profile.refresh_from_db() + + self.assertFalse(profile.want_news) + self.assertEqual(str(e.exception), (_("Subscription failed"))) + + +@override_settings(LANGUAGE_CODE="ru", LANGUAGES=(("ru", "Russian"),)) +class UnSubscribeTestCase(TestCase): + def test_ok_unsubscribe(self): + """if profile exists -> via htmx unsubscribe successfull""" + profile = ProfileFactory(want_news=True) + headers = {"HTTP_HX-Request": "true"} + url = reverse("contacts:end_news", kwargs={"uuid": profile.uuid}) + resp = self.client.post(url, **headers) + profile.refresh_from_db() + print(resp.items()) + self.assertEqual(resp.status_code, 200) + self.assertIsNotNone(resp.headers["HX-Redirect"]) + self.assertFalse(profile.want_news) + + def test_failed_unsubscribe_no_htmx(self): + """ + Failed un-subscribe if not htmx request + (no header htmx) + """ + profile = ProfileFactory(want_news=True) + url = reverse("contacts:end_news", kwargs={"uuid": profile.uuid}) + with self.assertRaises(UnsubcribeFail) as e: + self.client.post(url) # noqa + + self.assertTrue(profile.want_news) + self.assertEqual( + str(e.exception), (_("Something went wrong.Can't unsubscribe.")) + ) diff --git a/src/contacts/views.py b/src/contacts/views.py index 6b8d0dc..99ee83e 100644 --- a/src/contacts/views.py +++ b/src/contacts/views.py @@ -7,6 +7,8 @@ from src.profiles.models import Profile +from .exceptions import FailedNewsSubscription, UnsubcribeFail + class Subscribe(LRM, View): def get(self, request): @@ -24,35 +26,28 @@ def post(self, request, **kwargs): htmx_based request with redirect to home page and flash msg as a feedback """ - try: - htmx_req = request.headers.get("hx-request") - if request.user.is_authenticated and htmx_req is not None: - _id = request.user.profile.id - profile = get_object_or_404(Profile, id=_id) - messages.success(request, _("You have subscribed to the news letter")) - profile.want_news = True - profile.save() - return HttpResponse( - headers={ - "HX-Redirect": "/", - }, - ) - except Profile.DoesNotExist(): - messages.warning(request, _("Subscription failed")) + htmx_req = request.headers.get("hx-request") + + if request.user.is_authenticated and htmx_req is not None: + _id = request.user.profile.id + profile = get_object_or_404(Profile, id=_id) + profile.want_news = True + profile.save() + messages.success(request, _("You have subscribed to the news letter")) return HttpResponse( - headers={ - "HX-Redirect": "/", - }, + headers={"HX-Redirect": "/"}, ) + elif htmx_req is None: + raise FailedNewsSubscription(_("Subscription failed")) class UnSubscribe(View): def get(self, request, **kwargs): """ - link in newsletter contains profile uuid + get request via link in a newsletter + contains profile uuid """ uuid = kwargs.get("uuid") - profile = get_object_or_404(Profile, uuid=uuid) ctx = {"uuid": profile.uuid} print("ctx", ctx) @@ -60,23 +55,17 @@ def get(self, request, **kwargs): def post(self, request, **kwargs): # htmx_based - try: - htmx_req = request.headers.get("hx-request") - uuid = kwargs.get("uuid") - profile = get_object_or_404(Profile, uuid=uuid) - if htmx_req and profile: - messages.success(request, _("You have unsubscribed to the news letter")) - profile.want_news = False - profile.save() - return HttpResponse( - headers={ - "HX-Redirect": "/", - }, - ) - except Profile.DoesNotExist(): - messages.warning(request, _("Something went wrong.Subscription failed")) + htmx_req = request.headers.get("hx-request") + uuid = kwargs.get("uuid") + profile = get_object_or_404(Profile, uuid=uuid) + if htmx_req and profile.want_news: + profile.want_news = False + profile.save() + messages.success(request, _("You have unsubscribed to the news letter")) return HttpResponse( headers={ "HX-Redirect": "/", }, ) + elif htmx_req is None: + raise UnsubcribeFail(_("Something went wrong.Can't unsubscribe.")) diff --git a/src/templates/contacts/subscription/unsubscribe.html b/src/templates/contacts/subscription/unsubscribe.html index d298042..0ecbf72 100644 --- a/src/templates/contacts/subscription/unsubscribe.html +++ b/src/templates/contacts/subscription/unsubscribe.html @@ -8,7 +8,7 @@

{% trans "Do you really wish to unsubscribe from the newsletter?" %}