|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from django.conf import settings |
| 4 | +from django.core.cache import cache |
| 5 | +from django.test import TestCase, override_settings |
| 6 | +from rest_framework.permissions import AllowAny |
| 7 | +from rest_framework.response import Response |
| 8 | +from rest_framework.test import APIClient, APIRequestFactory |
| 9 | +from rest_framework.views import APIView |
| 10 | + |
| 11 | +from core.throttling import PostOnlyScopedRateThrottle |
| 12 | +from tests.constants import USER_CREATE_DATA |
| 13 | +from users.tests.helpers import build_user |
| 14 | + |
| 15 | + |
| 16 | +def throttle_settings(**rates): |
| 17 | + rest_framework = dict(settings.REST_FRAMEWORK) |
| 18 | + rest_framework["DEFAULT_THROTTLE_RATES"] = { |
| 19 | + **rest_framework.get("DEFAULT_THROTTLE_RATES", {}), |
| 20 | + **rates, |
| 21 | + } |
| 22 | + return rest_framework |
| 23 | + |
| 24 | + |
| 25 | +class DummyPostOnlyThrottleView(APIView): |
| 26 | + permission_classes = [AllowAny] |
| 27 | + throttle_classes = [PostOnlyScopedRateThrottle] |
| 28 | + throttle_scope = "auth_register" |
| 29 | + |
| 30 | + def get(self, _request): |
| 31 | + return Response({"ok": True}) |
| 32 | + |
| 33 | + def post(self, _request): |
| 34 | + return Response({"ok": True}) |
| 35 | + |
| 36 | + |
| 37 | +class AuthThrottleTests(TestCase): |
| 38 | + def setUp(self): |
| 39 | + cache.clear() |
| 40 | + self.client = APIClient() |
| 41 | + self.factory = APIRequestFactory() |
| 42 | + |
| 43 | + @override_settings( |
| 44 | + REST_FRAMEWORK=throttle_settings(auth_register="1/min") |
| 45 | + ) |
| 46 | + @patch("users.views.verify_email") |
| 47 | + def test_user_registration_post_is_scoped_throttled(self, verify_email_mock): |
| 48 | + first_response = self.client.post( |
| 49 | + "/auth/users/", |
| 50 | + USER_CREATE_DATA, |
| 51 | + format="json", |
| 52 | + REMOTE_ADDR="203.0.113.10", |
| 53 | + ) |
| 54 | + second_payload = { |
| 55 | + **USER_CREATE_DATA, |
| 56 | + "email": "second-user@example.com", |
| 57 | + } |
| 58 | + second_response = self.client.post( |
| 59 | + "/auth/users/", |
| 60 | + second_payload, |
| 61 | + format="json", |
| 62 | + REMOTE_ADDR="203.0.113.10", |
| 63 | + ) |
| 64 | + |
| 65 | + self.assertEqual(first_response.status_code, 201) |
| 66 | + self.assertEqual(second_response.status_code, 429) |
| 67 | + verify_email_mock.assert_called_once() |
| 68 | + |
| 69 | + @override_settings( |
| 70 | + REST_FRAMEWORK=throttle_settings(auth_resend_email="1/min") |
| 71 | + ) |
| 72 | + @patch("users.views.verify_email") |
| 73 | + def test_resend_verify_email_post_is_scoped_throttled(self, verify_email_mock): |
| 74 | + user = build_user(email="inactive-throttle@example.com", is_active=False) |
| 75 | + |
| 76 | + first_response = self.client.post( |
| 77 | + "/auth/resend_email/", |
| 78 | + {"email": user.email}, |
| 79 | + format="json", |
| 80 | + REMOTE_ADDR="203.0.113.11", |
| 81 | + ) |
| 82 | + second_response = self.client.post( |
| 83 | + "/auth/resend_email/", |
| 84 | + {"email": user.email}, |
| 85 | + format="json", |
| 86 | + REMOTE_ADDR="203.0.113.11", |
| 87 | + ) |
| 88 | + |
| 89 | + self.assertEqual(first_response.status_code, 200) |
| 90 | + self.assertEqual(second_response.status_code, 429) |
| 91 | + verify_email_mock.assert_called_once() |
| 92 | + |
| 93 | + @override_settings( |
| 94 | + REST_FRAMEWORK=throttle_settings(token_obtain="1/min") |
| 95 | + ) |
| 96 | + def test_token_obtain_post_is_scoped_throttled(self): |
| 97 | + user = build_user(email="token-throttle@example.com") |
| 98 | + payload = {"email": user.email, "password": "very_strong_password"} |
| 99 | + |
| 100 | + first_response = self.client.post( |
| 101 | + "/api/token/", |
| 102 | + payload, |
| 103 | + format="json", |
| 104 | + REMOTE_ADDR="203.0.113.12", |
| 105 | + ) |
| 106 | + second_response = self.client.post( |
| 107 | + "/api/token/", |
| 108 | + payload, |
| 109 | + format="json", |
| 110 | + REMOTE_ADDR="203.0.113.12", |
| 111 | + ) |
| 112 | + |
| 113 | + self.assertEqual(first_response.status_code, 200) |
| 114 | + self.assertEqual(second_response.status_code, 429) |
| 115 | + |
| 116 | + @override_settings( |
| 117 | + REST_FRAMEWORK=throttle_settings(auth_reset_password="1/min") |
| 118 | + ) |
| 119 | + @patch("users.signals.EmailMultiAlternatives") |
| 120 | + def test_password_reset_request_post_is_scoped_throttled(self, _email_message): |
| 121 | + user = build_user(email="reset-throttle@example.com") |
| 122 | + |
| 123 | + first_response = self.client.post( |
| 124 | + "/auth/reset_password/", |
| 125 | + {"email": user.email}, |
| 126 | + format="json", |
| 127 | + REMOTE_ADDR="203.0.113.13", |
| 128 | + ) |
| 129 | + second_response = self.client.post( |
| 130 | + "/auth/reset_password/", |
| 131 | + {"email": user.email}, |
| 132 | + format="json", |
| 133 | + REMOTE_ADDR="203.0.113.13", |
| 134 | + ) |
| 135 | + |
| 136 | + self.assertNotEqual(first_response.status_code, 429) |
| 137 | + self.assertEqual(second_response.status_code, 429) |
| 138 | + |
| 139 | + @override_settings( |
| 140 | + REST_FRAMEWORK=throttle_settings(auth_register="1/min") |
| 141 | + ) |
| 142 | + def test_post_only_throttle_allows_get_and_options(self): |
| 143 | + view = DummyPostOnlyThrottleView.as_view() |
| 144 | + |
| 145 | + for method in ("get", "options"): |
| 146 | + for _ in range(2): |
| 147 | + response = view( |
| 148 | + getattr(self.factory, method)( |
| 149 | + "/unused/", |
| 150 | + REMOTE_ADDR="203.0.113.14", |
| 151 | + ) |
| 152 | + ) |
| 153 | + self.assertNotEqual(response.status_code, 429) |
| 154 | + |
| 155 | + first_response = view( |
| 156 | + self.factory.post( |
| 157 | + "/unused/", |
| 158 | + {}, |
| 159 | + format="json", |
| 160 | + REMOTE_ADDR="203.0.113.14", |
| 161 | + ) |
| 162 | + ) |
| 163 | + second_response = view( |
| 164 | + self.factory.post( |
| 165 | + "/unused/", |
| 166 | + {}, |
| 167 | + format="json", |
| 168 | + REMOTE_ADDR="203.0.113.14", |
| 169 | + ) |
| 170 | + ) |
| 171 | + |
| 172 | + self.assertEqual(first_response.status_code, 200) |
| 173 | + self.assertEqual(second_response.status_code, 429) |
0 commit comments