Don't throttle API requests on dev to ease development (#9567)
Don't throttle API requests on dev to ease development
This commit is contained in:
Родитель
aa9bdce82a
Коммит
2445f3602f
|
@ -4,7 +4,6 @@ from rest_framework import status
|
|||
from rest_framework.exceptions import ParseError
|
||||
from rest_framework.mixins import CreateModelMixin
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.throttling import UserRateThrottle
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
|
||||
from olympia.abuse.models import AbuseReport
|
||||
|
@ -12,9 +11,10 @@ from olympia.abuse.serializers import (
|
|||
AddonAbuseReportSerializer, UserAbuseReportSerializer)
|
||||
from olympia.accounts.views import AccountViewSet
|
||||
from olympia.addons.views import AddonViewSet
|
||||
from olympia.api.throttling import GranularUserRateThrottle
|
||||
|
||||
|
||||
class AbuseThrottle(UserRateThrottle):
|
||||
class AbuseThrottle(GranularUserRateThrottle):
|
||||
rate = '20/day'
|
||||
scope = 'user_abuse'
|
||||
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
import mock
|
||||
|
||||
from django.conf import settings
|
||||
from django.test.client import RequestFactory
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from olympia.amo.tests import TestCase
|
||||
from olympia.api.throttling import GranularUserRateThrottle
|
||||
|
||||
|
@ -16,3 +22,29 @@ class TestGranularUserRateThrottle(TestCase):
|
|||
assert self.throttle.parse_rate('1/5minute') == (1, 60 * 5)
|
||||
assert self.throttle.parse_rate('24/1s') == (24, 1)
|
||||
assert self.throttle.parse_rate('456/7hour') == (456, 7 * 3600)
|
||||
|
||||
@mock.patch('rest_framework.throttling.UserRateThrottle.allow_request')
|
||||
def test_allow_request_if_api_throttling_setting_is_false(
|
||||
self, allow_request_mock):
|
||||
request = RequestFactory().get('/test')
|
||||
view = object()
|
||||
|
||||
# Pretend the parent class would always throttle requests if called.
|
||||
allow_request_mock.return_value = False
|
||||
|
||||
# With the setting set to True (the default), throttle as normal.
|
||||
assert settings.API_THROTTLING is True
|
||||
assert self.throttle.allow_request(request, view) is False
|
||||
assert allow_request_mock.call_count == 1
|
||||
|
||||
# With the setting set to False, ignore throttling.
|
||||
with override_settings(API_THROTTLING=False):
|
||||
assert settings.API_THROTTLING is False
|
||||
assert self.throttle.allow_request(request, view) is True
|
||||
# The parent class hasn't been called an additional time.
|
||||
assert allow_request_mock.call_count == 1
|
||||
|
||||
# And again set to True to be sure.
|
||||
assert settings.API_THROTTLING is True
|
||||
assert self.throttle.allow_request(request, view) is False
|
||||
assert allow_request_mock.call_count == 2
|
||||
|
|
|
@ -1,11 +1,25 @@
|
|||
import re
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from rest_framework.throttling import UserRateThrottle
|
||||
|
||||
|
||||
# Note: all classes defined in this module should obey API_THROTTLING and
|
||||
# deactivate throttling when this setting is False. This allows us to
|
||||
# deactivate throttling on dev easily.
|
||||
|
||||
|
||||
class GranularUserRateThrottle(UserRateThrottle):
|
||||
RATE_REGEX = r'(?P<num>\d+)\/(?P<period_num>\d{0,2})(?P<period>\w)'
|
||||
|
||||
def allow_request(self, request, view):
|
||||
if settings.API_THROTTLING:
|
||||
return super(
|
||||
GranularUserRateThrottle, self).allow_request(request, view)
|
||||
else:
|
||||
return True
|
||||
|
||||
def parse_rate(self, rate):
|
||||
if rate is None:
|
||||
return (None, None)
|
||||
|
|
|
@ -29,7 +29,7 @@ EMAIL_HOST_PASSWORD = EMAIL_URL['EMAIL_HOST_PASSWORD']
|
|||
ENV = env('ENV')
|
||||
RAISE_ON_SIGNAL_ERROR = True
|
||||
|
||||
API_THROTTLE = False
|
||||
API_THROTTLING = False
|
||||
|
||||
DOMAIN = env('DOMAIN', default='addons-dev.allizom.org')
|
||||
SERVER_EMAIL = 'zdev@addons.mozilla.org'
|
||||
|
|
|
@ -17,7 +17,7 @@ SEND_REAL_EMAIL = True
|
|||
|
||||
ENV = env('ENV')
|
||||
|
||||
API_THROTTLE = False
|
||||
API_THROTTLING = True
|
||||
|
||||
CDN_HOST = 'https://addons.cdn.mozilla.net'
|
||||
DOMAIN = env('DOMAIN', default='addons.mozilla.org')
|
||||
|
|
|
@ -27,7 +27,7 @@ EMAIL_HOST_PASSWORD = EMAIL_URL['EMAIL_HOST_PASSWORD']
|
|||
|
||||
ENV = env('ENV')
|
||||
|
||||
API_THROTTLE = False
|
||||
API_THROTTLING = True
|
||||
|
||||
DOMAIN = env('DOMAIN', default='addons.allizom.org')
|
||||
SERVER_EMAIL = 'zstage@addons.mozilla.org'
|
||||
|
|
|
@ -1798,6 +1798,10 @@ DRF_API_GATES = {
|
|||
)
|
||||
}
|
||||
|
||||
# Change this to deactivate API throttling for views using a throttling class
|
||||
# depending on the one defined in olympia.api.throttling.
|
||||
API_THROTTLING = True
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
# Set this because the default is to also include:
|
||||
# 'rest_framework.renderers.BrowsableAPIRenderer'
|
||||
|
|
Загрузка…
Ссылка в новой задаче