Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
b4a9a1b9
Commit
b4a9a1b9
authored
Jan 12, 2017
by
Artem Muterko
Committed by
Tom Christie
Jan 12, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Throttle tests (#4807)
parent
8fcb8d63
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
2 deletions
+38
-2
tests/test_throttling.py
+38
-2
No files found.
tests/test_throttling.py
View file @
b4a9a1b9
...
...
@@ -7,13 +7,16 @@ import pytest
from
django.contrib.auth.models
import
User
from
django.core.cache
import
cache
from
django.core.exceptions
import
ImproperlyConfigured
from
django.http
import
HttpRequest
from
django.test
import
TestCase
from
rest_framework.request
import
Request
from
rest_framework.response
import
Response
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
(
BaseThrottle
,
ScopedRateThrottle
,
SimpleRateThrottle
,
UserRateThrottle
AnonRateThrottle
,
BaseThrottle
,
ScopedRateThrottle
,
SimpleRateThrottle
,
UserRateThrottle
)
from
rest_framework.views
import
APIView
...
...
@@ -414,3 +417,36 @@ class SimpleRateThrottleTests(TestCase):
throttle
.
now
=
throttle
.
timer
()
throttle
.
history
=
[
throttle
.
timer
()
for
_
in
range
(
3
)]
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
)
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'
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment