|
| 1 | +import runpy |
| 2 | +from pathlib import Path |
1 | 3 | from unittest.mock import patch |
| 4 | +from urllib.parse import urlparse |
2 | 5 |
|
3 | | -from django.test import TestCase |
| 6 | +from django.conf import settings |
| 7 | +from django.test import SimpleTestCase, TestCase, override_settings |
4 | 8 | from django.urls import reverse |
5 | 9 | from rest_framework.test import APIClient |
| 10 | +from rest_framework_simplejwt.tokens import RefreshToken |
6 | 11 |
|
7 | 12 | from tests.constants import USER_CREATE_DATA |
8 | 13 | from users.models import CustomUser |
@@ -48,6 +53,21 @@ def test_user_registration_rejects_invalid_payload(self): |
48 | 53 |
|
49 | 54 | self.assertEqual(response.status_code, 400) |
50 | 55 |
|
| 56 | + @override_settings(EMAIL_BACKEND="django.core.mail.backends.console.EmailBackend") |
| 57 | + @patch( |
| 58 | + "django.core.mail.backends.console.EmailBackend.write_message", |
| 59 | + return_value=1, |
| 60 | + ) |
| 61 | + def test_user_registration_works_with_console_email_backend(self, write_message_mock): |
| 62 | + response = self.client.post("/auth/users/", USER_CREATE_DATA, format="json") |
| 63 | + |
| 64 | + self.assertEqual(response.status_code, 201) |
| 65 | + self.assertEqual(response.data["email"], USER_CREATE_DATA["email"]) |
| 66 | + self.assertEqual(response.data["is_active"], False) |
| 67 | + user = CustomUser.objects.get(email=USER_CREATE_DATA["email"]) |
| 68 | + self.assertFalse(user.is_active) |
| 69 | + write_message_mock.assert_called_once() |
| 70 | + |
51 | 71 | def test_token_obtain_pair_updates_last_login(self): |
52 | 72 | user = build_user(email="login@example.com") |
53 | 73 |
|
@@ -83,3 +103,49 @@ def test_removed_legacy_routes_return_404(self): |
83 | 103 | self.assertEqual(self.client.get("/auth/users/clone-data").status_code, 404) |
84 | 104 | self.assertEqual(self.client.get("/auth/subscription/").status_code, 404) |
85 | 105 | self.assertEqual(self.client.post("/auth/subscription/buy/").status_code, 404) |
| 106 | + |
| 107 | + |
| 108 | +class EmailSettingsDefaultsTests(SimpleTestCase): |
| 109 | + def test_email_settings_keep_existing_defaults_without_env(self): |
| 110 | + settings_path = Path(__file__).resolve().parents[2] / "procollab/settings.py" |
| 111 | + |
| 112 | + def return_default(_name, default=None, cast=None): |
| 113 | + return cast(default) if cast else default |
| 114 | + |
| 115 | + with patch("decouple.config", side_effect=return_default): |
| 116 | + loaded_settings = runpy.run_path(settings_path) |
| 117 | + |
| 118 | + self.assertEqual( |
| 119 | + loaded_settings["EMAIL_BACKEND"], |
| 120 | + "anymail.backends.unisender_go.EmailBackend", |
| 121 | + ) |
| 122 | + self.assertEqual( |
| 123 | + loaded_settings["VERIFY_EMAIL_REDIRECT_URL"], |
| 124 | + "https://app.procollab.ru/auth/verification/", |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +class VerifyEmailRedirectTests(TestCase): |
| 129 | + def setUp(self): |
| 130 | + self.client = APIClient() |
| 131 | + |
| 132 | + @override_settings( |
| 133 | + VERIFY_EMAIL_REDIRECT_URL=("https://react-dev.procollab.ru/auth/verification") |
| 134 | + ) |
| 135 | + def test_confirmation_handler_uses_configured_redirect(self): |
| 136 | + user = build_user(email="confirm@example.com", is_active=False) |
| 137 | + token = str(RefreshToken.for_user(user).access_token) |
| 138 | + |
| 139 | + response = self.client.get( |
| 140 | + "/auth/account-confirm-email/", |
| 141 | + {"token": token}, |
| 142 | + ) |
| 143 | + |
| 144 | + self.assertEqual(response.status_code, 302) |
| 145 | + redirect = urlparse(response["Location"]) |
| 146 | + self.assertEqual( |
| 147 | + redirect._replace(query="", fragment="").geturl(), |
| 148 | + settings.VERIFY_EMAIL_REDIRECT_URL, |
| 149 | + ) |
| 150 | + self.assertTrue(redirect.query.startswith("access_token=")) |
| 151 | + self.assertIn("&refresh_token=", redirect.query) |
0 commit comments