Skip to content

Commit

Permalink
Properly redirect after login link
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Aug 6, 2024
1 parent a661b30 commit 8be5b15
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
14 changes: 13 additions & 1 deletion totem/email/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
73 changes: 43 additions & 30 deletions totem/users/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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

from totem.circles.tests.factories import CircleEventFactory, CircleFactory
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

Expand All @@ -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):
Expand All @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions totem/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8be5b15

Please sign in to comment.