Add tests for AnonRateThrottle

This commit is contained in:
Artem Muterko 2017-01-12 17:39:05 +02:00
parent 8fcb8d6350
commit 8b30218d15

View File

@ -7,13 +7,16 @@ import pytest
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.test import TestCase from django.test import TestCase
from rest_framework.request import Request
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory, force_authenticate
from rest_framework.throttling import ( from rest_framework.throttling import (
BaseThrottle, ScopedRateThrottle, SimpleRateThrottle, UserRateThrottle AnonRateThrottle, BaseThrottle, ScopedRateThrottle, SimpleRateThrottle,
UserRateThrottle
) )
from rest_framework.views import APIView from rest_framework.views import APIView
@ -414,3 +417,22 @@ class SimpleRateThrottleTests(TestCase):
throttle.now = throttle.timer() throttle.now = throttle.timer()
throttle.history = [throttle.timer() for _ in range(3)] throttle.history = [throttle.timer() for _ in range(3)]
assert throttle.wait() is None assert throttle.wait() is None
class AnonRateThrottleTests(TestCase):
def setUp(self):
self.throttle = AnonRateThrottle()
def test_authenticated_user_not_affected(self):
request = Request(HttpRequest())
user = User.objects.create(username='test')
force_authenticate(request, user, token='test_token')
request.user = user
assert self.throttle.get_cache_key(request, view={}) is None
def test_get_cache_key_returns_correct_value(self):
request = Request(HttpRequest())
cache_key = self.throttle.get_cache_key(request, view={})
assert cache_key == 'throttle_anon_None'