diff --git a/totem/email/tests.py b/totem/email/tests.py index 1e9a6763..a94285ee 100644 --- a/totem/email/tests.py +++ b/totem/email/tests.py @@ -3,7 +3,7 @@ from django.urls import reverse from totem.circles.tests.factories import CircleEventFactory -from totem.email.emails import send_notify_circle_advertisement +from totem.email.emails import send_notify_circle_advertisement, send_returning_login_email from totem.users.tests.factories import UserFactory from .views import get_templates @@ -59,3 +59,15 @@ def test_advert_email(self, client, db): assert event.circle.title in message assert "http://testserver/circles/subscribe" in message assert event.circle.slug in message + + +class TestReturningUsers: + def test_returning_users(self, client, db): + user = UserFactory() + user.save() + send_returning_login_email(user.email, reverse("pages:home")) + assert len(mail.outbox) == 1 + email = mail.outbox[0] + assert email.to == [user.email] + message = str(email.message()) + assert "http://testserver/" in message diff --git a/totem/users/tests/test_views.py b/totem/users/tests/test_views.py index c897d8be..50bb2b2d 100644 --- a/totem/users/tests/test_views.py +++ b/totem/users/tests/test_views.py @@ -1,8 +1,7 @@ import pytest -from django.contrib.auth.models import AnonymousUser from django.contrib.messages import get_messages from django.http import HttpResponseRedirect -from django.test import RequestFactory, TestCase +from django.test import TestCase from django.urls import reverse from sesame.utils import get_query_string @@ -10,9 +9,7 @@ from totem.onboard.models import OnboardModel from totem.users.models import Feedback, User from totem.users.tests.factories import KeeperProfileFactory, UserFactory -from totem.users.views import FEEDBACK_SUCCESS_MESSAGE, user_redirect_view - -from ..views import user_index_view +from totem.users.views import FEEDBACK_SUCCESS_MESSAGE pytestmark = pytest.mark.django_db @@ -35,18 +32,34 @@ def test_user_update_view(client): class TestUserRedirectView: - def test_get_redirect_url(self, user: User, rf: RequestFactory): - request = rf.get("/fake-url") - request.user = user - response = user_redirect_view(request) + def test_get_redirect_url(self, client, db): + user = UserFactory() + client.force_login(user) + url = reverse("users:redirect") + response = client.get(url) assert response.status_code == 302 assert response.url == reverse("onboard:index") - OnboardModel.objects.create(user=user) - user.onboard.onboarded = True - response = user_redirect_view(request) + onboard = OnboardModel.objects.create(user=user) + onboard.onboarded = True + onboard.save() + response = client.get(url) assert response.status_code == 302 assert response.url == reverse("users:dashboard") + def test_user_index_view_after_login_with_next(self, client, db): + url = reverse("users:profile") + user = UserFactory() + client.force_login(user) + onboard = OnboardModel.objects.create(user=user) + onboard.onboarded = True + onboard.save() + s = client.session + s["next"] = url + s.save() + response = client.get(reverse("users:index")) + assert isinstance(response, HttpResponseRedirect) + assert response.url == reverse("users:profile") + class TestUserDetailView: def test_authenticated(self, user: User, client): @@ -61,26 +74,26 @@ def test_authenticated(self, user: User, client): assert response.status_code == 200 -def test_user_index_view(): - factory = RequestFactory() - request = factory.get("/someroute") - request.user = AnonymousUser() - response = user_index_view(request) - assert isinstance(response, HttpResponseRedirect) - assert response.url == reverse("users:signup") + "?next=/someroute" +class TestUserIndexView: + def test_user_index_view(self, client, db): + url = reverse("users:profile") + response = client.get(url) + assert isinstance(response, HttpResponseRedirect) + assert response.url == reverse("users:signup") + "?next=" + url - user = UserFactory() - request.user = user - response = user_index_view(request) - assert response.status_code == 302 - assert response.url == "/onboard/" + url = reverse("users:index") + user = UserFactory() + client.force_login(user) + response = client.get(url) + assert response.status_code == 302 + assert response.url == "/onboard/" - OnboardModel.objects.create(user=user) - user.onboard.onboarded = True - user.onboard.save() - response = user_index_view(request) - assert response.status_code == 302 - assert response.url == "/users/dashboard/" + OnboardModel.objects.create(user=user) + user.onboard.onboarded = True + user.onboard.save() + response = client.get(url) + assert response.status_code == 302 + assert response.url == "/users/dashboard/" def test_magic_login_view_verify_email(client): diff --git a/totem/users/views.py b/totem/users/views.py index f3a3b0b8..d4dfa21d 100644 --- a/totem/users/views.py +++ b/totem/users/views.py @@ -70,6 +70,11 @@ def user_redirect_view(request, *args, **kwargs): user = request.user try: if user.onboard and user.onboard.onboarded: + next = request.session.get("next") + if next: + del request.session["next"] + assert next[0] == "/" + return redirect(next) return redirect("users:dashboard") except ObjectDoesNotExist: pass