Throttle tests fix (#4810)

This commit is contained in:
Artem Muterko 2017-01-12 19:53:48 +02:00 committed by Tom Christie
parent b4a9a1b9a3
commit c686749eef

View File

@ -194,6 +194,8 @@ class ScopedRateThrottleTests(TestCase):
""" """
def setUp(self): def setUp(self):
self.throttle = ScopedRateThrottle()
class XYScopedRateThrottle(ScopedRateThrottle): class XYScopedRateThrottle(ScopedRateThrottle):
TIMER_SECONDS = 0 TIMER_SECONDS = 0
THROTTLE_RATES = {'x': '3/min', 'y': '1/min'} THROTTLE_RATES = {'x': '3/min', 'y': '1/min'}
@ -293,6 +295,18 @@ class ScopedRateThrottleTests(TestCase):
response = self.unscoped_view(request) response = self.unscoped_view(request)
assert response.status_code == 200 assert response.status_code == 200
def test_get_cache_key_returns_correct_key_if_user_is_authenticated(self):
class DummyView(object):
throttle_scope = 'user'
request = Request(HttpRequest())
user = User.objects.create(username='test')
force_authenticate(request, user)
request.user = user
self.throttle.allow_request(request, DummyView())
cache_key = self.throttle.get_cache_key(request, view=DummyView())
assert cache_key == 'throttle_user_%s' % user.pk
class XffTestingBase(TestCase): class XffTestingBase(TestCase):
def setUp(self): def setUp(self):
@ -435,18 +449,3 @@ class AnonRateThrottleTests(TestCase):
request = Request(HttpRequest()) request = Request(HttpRequest())
cache_key = self.throttle.get_cache_key(request, view={}) cache_key = self.throttle.get_cache_key(request, view={})
assert cache_key == 'throttle_anon_None' assert cache_key == 'throttle_anon_None'
class UserRateThrottleTests(TestCase):
def setUp(self):
self.throttle = UserRateThrottle()
def test_get_cache_key_returns_correct_key_if_user_is_authenticated(self):
request = Request(HttpRequest())
user = User.objects.create(username='test')
force_authenticate(request, user)
request.user = user
cache_key = self.throttle.get_cache_key(request, view={})
assert cache_key == 'throttle_user_%s' % user.pk