Skip to content

Commit

Permalink
Sidestep API throttling when in DEBUG mode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
higs4281 committed Aug 3, 2021
1 parent 639e65f commit 34dd0b2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
21 changes: 14 additions & 7 deletions complaint_search/tests/test_views_document.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.cache import cache
from django.test import override_settings

import mock
from elasticsearch7 import TransportError
Expand Down Expand Up @@ -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"})
Expand All @@ -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):
Expand Down
22 changes: 22 additions & 0 deletions complaint_search/tests/test_views_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions complaint_search/throttling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from django.conf import settings

from rest_framework.throttling import AnonRateThrottle

from complaint_search.defaults import EXPORT_FORMATS
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 34dd0b2

Please sign in to comment.