From 46032fe9c85f608870fb44d9eac46cdc1ddcb8f3 Mon Sep 17 00:00:00 2001 From: Edmundo Abreu e Lima <98054216+engedmundo@users.noreply.github.com> Date: Mon, 20 May 2024 16:47:43 -0300 Subject: [PATCH] feature/create tests for next in login --- .../core/views/custom_login_required_mixin.py | 2 +- .../tests/factories/passenger_factory.py | 6 +-- .../tests/views/custom_login_view_tests.py | 53 +++++++++++++++++++ .../tests/views/home_view_tests.py | 6 +-- apps/ride_manager/views/custom_login.py | 2 + apps/ride_manager/views/ride.py | 2 - 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/apps/core/views/custom_login_required_mixin.py b/apps/core/views/custom_login_required_mixin.py index de7391a..f6eeb7b 100644 --- a/apps/core/views/custom_login_required_mixin.py +++ b/apps/core/views/custom_login_required_mixin.py @@ -1,6 +1,6 @@ from django.contrib.auth.mixins import AccessMixin -from django.urls import reverse from django.http import HttpResponseRedirect +from django.urls import reverse class CustomLoginRequiredMixin(AccessMixin): diff --git a/apps/ride_manager/tests/factories/passenger_factory.py b/apps/ride_manager/tests/factories/passenger_factory.py index 3397740..b33e339 100644 --- a/apps/ride_manager/tests/factories/passenger_factory.py +++ b/apps/ride_manager/tests/factories/passenger_factory.py @@ -1,12 +1,12 @@ import factory from faker import Faker -from apps.ride_manager.models.passenger import Passenger -from apps.ride_manager.tests.factories.person_factory import PersonFactory -from apps.ride_manager.tests.factories.ride_factory import RideFactory from apps.ride_manager.enums.passenger_status_choices import ( PassengerStatusChoices, ) +from apps.ride_manager.models.passenger import Passenger +from apps.ride_manager.tests.factories.person_factory import PersonFactory +from apps.ride_manager.tests.factories.ride_factory import RideFactory fake = Faker("pt_BR") diff --git a/apps/ride_manager/tests/views/custom_login_view_tests.py b/apps/ride_manager/tests/views/custom_login_view_tests.py index 6eceda6..3c7e092 100644 --- a/apps/ride_manager/tests/views/custom_login_view_tests.py +++ b/apps/ride_manager/tests/views/custom_login_view_tests.py @@ -2,6 +2,8 @@ from django.urls import reverse from apps.core.tests.base_test import FAKE_CPF, BaseTest +from apps.ride_manager.tests.factories.person_factory import PersonFactory +from apps.ride_manager.tests.factories.ride_factory import RideFactory class CustomLoginViewTests(BaseTest): @@ -70,3 +72,54 @@ def test_login_invalid_password(self): # Then self.assertEqual(response.status_code, 200) self.assertIn(expected_message, response.content.decode()) + + def test_login_redirects_to_next(self): + # Given + cpf = FAKE_CPF + password = "testpassword" + next_url = reverse("my_rides") + url = f"{self.url}?next={next_url}" + + # When + response = self.view_unauth_client.post( + url, + data={"cpf": cpf, "password": password}, + ) + + # Then + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, next_url) + + def test_login_redirects_to_home(self): + # Given + cpf = FAKE_CPF + password = "testpassword" + + # When + response = self.view_unauth_client.post( + self.url, + data={"cpf": cpf, "password": password}, + ) + + # Then + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse("home")) + + def test_login_redirects_to_detail_ride_view(self): + # Given + person = PersonFactory(user=self.user) + ride = RideFactory(driver=person) + cpf = FAKE_CPF + password = "testpassword" + next_url = reverse("ride_detail", args=[ride.uuid]) + url = f"{self.url}?next={next_url}" + + # When + response = self.view_unauth_client.post( + url, + data={"cpf": cpf, "password": password}, + ) + + # Then + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, next_url) diff --git a/apps/ride_manager/tests/views/home_view_tests.py b/apps/ride_manager/tests/views/home_view_tests.py index c7f784b..8f804ea 100644 --- a/apps/ride_manager/tests/views/home_view_tests.py +++ b/apps/ride_manager/tests/views/home_view_tests.py @@ -2,12 +2,12 @@ from django.urls import reverse from apps.core.tests.base_test import BaseTest -from apps.ride_manager.tests.factories.passenger_factory import PassengerFactory -from apps.ride_manager.tests.factories.person_factory import PersonFactory -from apps.ride_manager.tests.factories.ride_factory import RideFactory from apps.ride_manager.enums.passenger_status_choices import ( PassengerStatusChoices, ) +from apps.ride_manager.tests.factories.passenger_factory import PassengerFactory +from apps.ride_manager.tests.factories.person_factory import PersonFactory +from apps.ride_manager.tests.factories.ride_factory import RideFactory class HomeViewTests(BaseTest): diff --git a/apps/ride_manager/views/custom_login.py b/apps/ride_manager/views/custom_login.py index f73006c..8af48bf 100644 --- a/apps/ride_manager/views/custom_login.py +++ b/apps/ride_manager/views/custom_login.py @@ -10,4 +10,6 @@ class CustomLoginView(LoginView): def get_success_url(self): self.request.session["show_caution_modal"] = True + if self.request.GET.get("next"): + return self.request.GET.get("next") return reverse_lazy("home") diff --git a/apps/ride_manager/views/ride.py b/apps/ride_manager/views/ride.py index 170b3b3..fd6ed6f 100644 --- a/apps/ride_manager/views/ride.py +++ b/apps/ride_manager/views/ride.py @@ -349,9 +349,7 @@ def mount_header(request): # mounting back link dynamically referer = request.META.get("HTTP_REFERER") - print(referer) if referer != request.build_absolute_uri("/") + "rides/": application_url = request.build_absolute_uri("/") referer = application_url + "ride/my-rides" - print(referer) return message, referer