|
| 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.test import APIClient |
| 7 | + |
| 8 | +from tests.constants import USER_CREATE_DATA |
| 9 | +from users.tests.helpers import build_user |
| 10 | + |
| 11 | + |
| 12 | +def throttle_settings(**rates): |
| 13 | + rest_framework = dict(settings.REST_FRAMEWORK) |
| 14 | + rest_framework["DEFAULT_THROTTLE_RATES"] = { |
| 15 | + **rest_framework.get("DEFAULT_THROTTLE_RATES", {}), |
| 16 | + **rates, |
| 17 | + } |
| 18 | + return rest_framework |
| 19 | + |
| 20 | + |
| 21 | +class AuthThrottleTests(TestCase): |
| 22 | + def setUp(self): |
| 23 | + cache.clear() |
| 24 | + self.client = APIClient() |
| 25 | + |
| 26 | + @override_settings( |
| 27 | + REST_FRAMEWORK=throttle_settings(auth_user_create="1/min") |
| 28 | + ) |
| 29 | + @patch("users.views.verify_email") |
| 30 | + def test_user_registration_post_is_scoped_throttled(self, verify_email_mock): |
| 31 | + first_response = self.client.post( |
| 32 | + "/auth/users/", |
| 33 | + USER_CREATE_DATA, |
| 34 | + format="json", |
| 35 | + REMOTE_ADDR="203.0.113.10", |
| 36 | + ) |
| 37 | + second_payload = { |
| 38 | + **USER_CREATE_DATA, |
| 39 | + "email": "second-user@example.com", |
| 40 | + } |
| 41 | + second_response = self.client.post( |
| 42 | + "/auth/users/", |
| 43 | + second_payload, |
| 44 | + format="json", |
| 45 | + REMOTE_ADDR="203.0.113.10", |
| 46 | + ) |
| 47 | + |
| 48 | + self.assertEqual(first_response.status_code, 201) |
| 49 | + self.assertEqual(second_response.status_code, 429) |
| 50 | + verify_email_mock.assert_called_once() |
| 51 | + |
| 52 | + @override_settings( |
| 53 | + REST_FRAMEWORK=throttle_settings(auth_resend_email="1/min") |
| 54 | + ) |
| 55 | + @patch("users.views.verify_email") |
| 56 | + def test_resend_verify_email_post_is_scoped_throttled(self, verify_email_mock): |
| 57 | + user = build_user(email="inactive-throttle@example.com", is_active=False) |
| 58 | + |
| 59 | + first_response = self.client.post( |
| 60 | + "/auth/resend_email/", |
| 61 | + {"email": user.email}, |
| 62 | + format="json", |
| 63 | + REMOTE_ADDR="203.0.113.11", |
| 64 | + ) |
| 65 | + second_response = self.client.post( |
| 66 | + "/auth/resend_email/", |
| 67 | + {"email": user.email}, |
| 68 | + format="json", |
| 69 | + REMOTE_ADDR="203.0.113.11", |
| 70 | + ) |
| 71 | + |
| 72 | + self.assertEqual(first_response.status_code, 200) |
| 73 | + self.assertEqual(second_response.status_code, 429) |
| 74 | + verify_email_mock.assert_called_once() |
| 75 | + |
| 76 | + @override_settings( |
| 77 | + REST_FRAMEWORK=throttle_settings(token_obtain="1/min") |
| 78 | + ) |
| 79 | + def test_token_obtain_post_is_scoped_throttled(self): |
| 80 | + user = build_user(email="token-throttle@example.com") |
| 81 | + payload = {"email": user.email, "password": "very_strong_password"} |
| 82 | + |
| 83 | + first_response = self.client.post( |
| 84 | + "/api/token/", |
| 85 | + payload, |
| 86 | + format="json", |
| 87 | + REMOTE_ADDR="203.0.113.12", |
| 88 | + ) |
| 89 | + second_response = self.client.post( |
| 90 | + "/api/token/", |
| 91 | + payload, |
| 92 | + format="json", |
| 93 | + REMOTE_ADDR="203.0.113.12", |
| 94 | + ) |
| 95 | + |
| 96 | + self.assertEqual(first_response.status_code, 200) |
| 97 | + self.assertEqual(second_response.status_code, 429) |
| 98 | + |
| 99 | + @override_settings( |
| 100 | + REST_FRAMEWORK=throttle_settings(auth_reset_password="1/min") |
| 101 | + ) |
| 102 | + @patch("users.signals.EmailMultiAlternatives") |
| 103 | + def test_password_reset_request_post_is_scoped_throttled(self, _email_message): |
| 104 | + user = build_user(email="reset-throttle@example.com") |
| 105 | + |
| 106 | + first_response = self.client.post( |
| 107 | + "/auth/reset_password/", |
| 108 | + {"email": user.email}, |
| 109 | + format="json", |
| 110 | + REMOTE_ADDR="203.0.113.13", |
| 111 | + ) |
| 112 | + second_response = self.client.post( |
| 113 | + "/auth/reset_password/", |
| 114 | + {"email": user.email}, |
| 115 | + format="json", |
| 116 | + REMOTE_ADDR="203.0.113.13", |
| 117 | + ) |
| 118 | + |
| 119 | + self.assertNotEqual(first_response.status_code, 429) |
| 120 | + self.assertEqual(second_response.status_code, 429) |
0 commit comments