implemented per user throttling

(commit to permissions.py fixes a bug that came out of the unittest)
This commit is contained in:
markotibold 2011-06-11 01:49:22 +02:00
parent 8ef333a29f
commit 87db5fbda5

View File

@ -4,6 +4,7 @@ from django.conf.urls.defaults import patterns
from django.test import TestCase from django.test import TestCase
from django.utils import simplejson as json from django.utils import simplejson as json
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.cache import cache
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
from djangorestframework.views import View from djangorestframework.views import View
@ -25,7 +26,8 @@ class ThrottlingTests(TestCase):
urls = 'djangorestframework.tests.throttling' urls = 'djangorestframework.tests.throttling'
def setUp(self): def setUp(self):
time.sleep(1) # make sure throttle is expired before next test """Reset the cache so that no throttles will be active"""
cache.clear()
def test_requests_are_throttled(self): def test_requests_are_throttled(self):
"""Ensure request rate is limited""" """Ensure request rate is limited"""
@ -34,17 +36,19 @@ class ThrottlingTests(TestCase):
response = self.client.get('/') response = self.client.get('/')
self.assertEqual(503, response.status_code) self.assertEqual(503, response.status_code)
def DISABLEDtest_request_throttling_is_per_user(self): def test_request_throttling_is_per_user(self):
#Can not login user.....Dunno why... #Can not login user.....Dunno why...
"""Ensure request rate is only limited per user, not globally""" """Ensure request rate is only limited per user, not globally"""
User.objects.create_user('testuser', 'test', 'foo@bar.baz').save() for username in ('testuser', 'another_testuser'):
User.objects.create_user('another_testuser', 'test', 'foo@bar.baz').save() user = User.objects.create(username=username)
user.set_password('test')
self.assertTrue(self.client.login(username='testuser', password='test')) user.save()
self.assertTrue(self.client.login(username='testuser', password='test'), msg='Login Failed')
for dummy in range(3): for dummy in range(3):
response = self.client.get('/') response = self.client.get('/')
self.client.logout() self.client.logout()
self.assertTrue(self.client.login(username='another_testuser', password='test')) self.assertTrue(self.client.login(username='another_testuser', password='test'), msg='Login failed')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_request_throttling_expires(self): def test_request_throttling_expires(self):