From 34dd0b20d988f6403a3f43fb29fcc06204eea3f3 Mon Sep 17 00:00:00 2001 From: higs4281 Date: Tue, 27 Jul 2021 22:17:21 -0400 Subject: [PATCH] Sidestep API throttling when in DEBUG mode Integration testing with Cypress is difficult when testing on a server, because many of the test actions generate API calls that violate throttling rules, and the tests have to be slowed down to work. This change allows throttling to be sidestepped when DEBUG is True, which can be set locally and on servers where CI testing could be run. --- complaint_search/tests/test_views_document.py | 21 ++++++++++++------ complaint_search/tests/test_views_search.py | 22 +++++++++++++++++++ complaint_search/throttling.py | 4 ++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/complaint_search/tests/test_views_document.py b/complaint_search/tests/test_views_document.py index 9112e62..fcd7c59 100644 --- a/complaint_search/tests/test_views_document.py +++ b/complaint_search/tests/test_views_document.py @@ -1,4 +1,5 @@ from django.core.cache import cache +from django.test import override_settings import mock from elasticsearch7 import TransportError @@ -37,6 +38,7 @@ def test_document__valid(self, mock_esdocument): mock_esdocument.assert_called_once_with("123456") self.assertEqual('OK', response.data) + @override_settings(DEBUG=False) @mock.patch('complaint_search.es_interface.document') def test_document_with_document_anon_rate_throttle(self, mock_esdocument): url = reverse('complaint_search:complaint', kwargs={"id": "123456"}) @@ -48,14 +50,19 @@ def test_document_with_document_anon_rate_throttle(self, mock_esdocument): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('OK', response.data) + @override_settings(DEBUG=True) + @mock.patch('complaint_search.es_interface.document') + def test_document_with_anon_rate_throttle_debug(self, mock_esdocument): + url = reverse('complaint_search:complaint', kwargs={"id": "123456"}) + mock_esdocument.return_value = 'OK' + DocumentAnonRateThrottle.rate = self.orig_document_anon_rate + limit = int(self.orig_document_anon_rate.split('/')[0]) + for i in range(limit): + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual('OK', response.data) response = self.client.get(url) - self.assertEqual( - response.status_code, status.HTTP_429_TOO_MANY_REQUESTS - ) - self.assertIsNotNone(response.data.get('detail')) - self.assertIn("Request was throttled", response.data.get('detail')) - self.assertEqual(limit, mock_esdocument.call_count) - self.assertEqual(5, limit) + self.assertEqual(response.status_code, status.HTTP_200_OK) @mock.patch('complaint_search.es_interface.document') def test_document_with_document_ui_rate_throttle(self, mock_esdocument): diff --git a/complaint_search/tests/test_views_search.py b/complaint_search/tests/test_views_search.py index 5211204..ba559ee 100644 --- a/complaint_search/tests/test_views_search.py +++ b/complaint_search/tests/test_views_search.py @@ -4,6 +4,7 @@ from django.conf import settings from django.core.cache import cache from django.http import StreamingHttpResponse +from django.test import override_settings import mock from elasticsearch7 import TransportError @@ -759,6 +760,7 @@ def test_search_with_no_highlight__invalid_type(self, mock_essearch): ]}, response.data) + @override_settings(DEBUG=False) @mock.patch('complaint_search.es_interface.search') def test_search_with_search_anon_rate_throttle(self, mock_essearch): url = reverse('complaint_search:search') @@ -781,6 +783,25 @@ def test_search_with_search_anon_rate_throttle(self, mock_essearch): self.assertEqual(limit, mock_essearch.call_count) self.assertEqual(20, limit) + @override_settings(DEBUG=True) + @mock.patch('complaint_search.es_interface.search') + def test_search_with_anon_rate_throttle_debug(self, mock_essearch): + url = reverse('complaint_search:search') + mock_essearch.return_value = 'OK' + SearchAnonRateThrottle.rate = self.orig_search_anon_rate + ExportUIRateThrottle.rate = self.orig_export_ui_rate + ExportAnonRateThrottle.rate = self.orig_export_anon_rate + limit = int(self.orig_search_anon_rate.split('/')[0]) + for _ in range(limit): + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual('OK', response.data) + + response = self.client.get(url) + self.assertEqual( + response.status_code, status.HTTP_200_OK + ) + @mock.patch('complaint_search.es_interface.search') def test_search_with_search_ui_rate_throttle(self, mock_essearch): url = reverse('complaint_search:search') @@ -801,6 +822,7 @@ def test_search_with_search_ui_rate_throttle(self, mock_essearch): self.assertEqual(limit + 1, mock_essearch.call_count) self.assertEqual(20, limit) + @override_settings(DEBUG=False) @mock.patch('complaint_search.es_interface.search') def test_search_with_export_anon_rate_throttle(self, mock_essearch): url = reverse('complaint_search:search') diff --git a/complaint_search/throttling.py b/complaint_search/throttling.py index b0a8ee7..33fa8ac 100644 --- a/complaint_search/throttling.py +++ b/complaint_search/throttling.py @@ -1,5 +1,7 @@ import os +from django.conf import settings + from rest_framework.throttling import AnonRateThrottle from complaint_search.defaults import EXPORT_FORMATS @@ -27,6 +29,8 @@ class CCDBAnonRateThrottle(CCDBRateThrottle): scope = 'ccdb_anon' def allow_request(self, request, view): + if settings.DEBUG is True: + return True if not self.is_referred_from_ui(request, view): return super(CCDBAnonRateThrottle, self).allow_request( request, view