|
1 | 1 | import pytest |
| 2 | +from django.test import RequestFactory |
2 | 3 | from django.urls import reverse |
3 | 4 | from rest_framework import status |
4 | 5 |
|
| 6 | +from authentication.new_views import CustomLoginView |
5 | 7 | from users.api import User |
| 8 | +from users.factories import UserFactory |
6 | 9 | from users.models import MALE, UserProfile |
7 | 10 |
|
8 | 11 |
|
@@ -71,19 +74,35 @@ def test_post_user_extra_detail(mocker, client, user): |
71 | 74 |
|
72 | 75 |
|
73 | 76 | @pytest.mark.django_db |
74 | | -@pytest.mark.parametrize( |
75 | | - ("has_profile", "expected_url"), |
76 | | - [ |
77 | | - (True, "/dashboard"), |
78 | | - (False, f"{reverse('profile-details')}?next=%2Fdashboard"), |
79 | | - ], |
80 | | -) |
81 | | -def test_login_view(client, user, has_profile, expected_url): |
82 | | - """Test that the login endpoint redirects the user properly based on profile existence""" |
83 | | - if not has_profile: |
84 | | - user.user_profile.delete() |
85 | | - client.force_login(user) |
86 | | - url = reverse("gateway-login") |
87 | | - resp = client.get(url) |
88 | | - assert resp.url == expected_url |
89 | | - assert resp.status_code == status.HTTP_302_FOUND |
| 77 | +def test_custom_login_view_authenticated_user_with_onboarding(mocker): |
| 78 | + """Test CustomLoginView for an authenticated user with incomplete onboarding""" |
| 79 | + factory = RequestFactory() |
| 80 | + request = factory.get(reverse("login"), {"next": "/dashboard"}) |
| 81 | + user = UserFactory() |
| 82 | + request.user = user |
| 83 | + mocker.patch("authentication.new_views.get_redirect_url", return_value="/dashboard") |
| 84 | + mocker.patch("authentication.new_views.urlencode", return_value="next=/dashboard") |
| 85 | + mocker.patch( |
| 86 | + "authentication.new_views.settings.MITXONLINE_NEW_USER_LOGIN_URL", |
| 87 | + "/create-profile", |
| 88 | + ) |
| 89 | + |
| 90 | + response = CustomLoginView().get(request) |
| 91 | + |
| 92 | + assert response.status_code == 302 |
| 93 | + assert response.url == "/create-profile?next=/dashboard" |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.django_db |
| 97 | +def test_custom_login_view_authenticated_user_with_completed_onboarding(mocker): |
| 98 | + """Test that user who has completed onboarding is redirected to next url""" |
| 99 | + factory = RequestFactory() |
| 100 | + request = factory.get(reverse("login"), {"next": "/dashboard"}) |
| 101 | + user = UserFactory(user_profile__completed_onboarding=True) |
| 102 | + request.user = user |
| 103 | + mocker.patch("authentication.new_views.get_redirect_url", return_value="/dashboard") |
| 104 | + |
| 105 | + response = CustomLoginView().get(request) |
| 106 | + |
| 107 | + assert response.status_code == 302 |
| 108 | + assert response.url == "/dashboard" |
0 commit comments