From 66ffaaf5d323e1d8d2607844d8136a349b6dc651 Mon Sep 17 00:00:00 2001 From: Artem Muterko Date: Wed, 11 Jan 2017 11:42:25 +0200 Subject: [PATCH] Add more tests for Base and SimpleRate throttles (#4802) --- tests/test_throttling.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/test_throttling.py b/tests/test_throttling.py index 20a6f21d7..267020f52 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -358,18 +358,42 @@ class XffUniqueMachinesTest(XffTestingBase): assert self.view(self.request).status_code == 200 +class BaseThrottleTests(TestCase): + + def test_allow_request_raises_not_implemented_error(self): + with pytest.raises(NotImplementedError): + BaseThrottle().allow_request(request={}, view={}) + + class SimpleRateThrottleTests(TestCase): - def test_throttle_raises_error_if_scope_is_missing(self): + def setUp(self): + SimpleRateThrottle.scope = 'anon' + + def test_get_rate_raises_error_if_scope_is_missing(self): + throttle = SimpleRateThrottle() with pytest.raises(ImproperlyConfigured): - SimpleRateThrottle() + throttle.scope = None + throttle.get_rate() def test_throttle_raises_error_if_rate_is_missing(self): - SimpleRateThrottle.scope = 'test' + SimpleRateThrottle.scope = 'invalid scope' with pytest.raises(ImproperlyConfigured): SimpleRateThrottle() def test_parse_rate_returns_tuple_with_none_if_rate_not_provided(self): - SimpleRateThrottle.scope = 'anon' rate = SimpleRateThrottle().parse_rate(None) assert rate == (None, None) + + def test_allow_request_returns_true_if_rate_is_none(self): + assert SimpleRateThrottle().allow_request(request={}, view={}) is True + + def test_get_cache_key_raises_not_implemented_error(self): + with pytest.raises(NotImplementedError): + SimpleRateThrottle().get_cache_key({}, {}) + + def test_allow_request_returns_true_if_key_is_none(self): + throttle = SimpleRateThrottle() + throttle.rate = 'some rate' + throttle.get_cache_key = lambda *args: None + assert throttle.allow_request(request={}, view={}) is True