Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
d901b1d3
Commit
d901b1d3
authored
10 years ago
by
Stephen Sanchez
Browse files
Options
Downloads
Patches
Plain Diff
Allow services to ignore user throttling.
parent
c8825652
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/djangoapps/enrollment/tests/test_views.py
+36
-0
36 additions, 0 deletions
common/djangoapps/enrollment/tests/test_views.py
common/djangoapps/enrollment/views.py
+8
-6
8 additions, 6 deletions
common/djangoapps/enrollment/views.py
with
44 additions
and
6 deletions
common/djangoapps/enrollment/tests/test_views.py
+
36
−
0
View file @
d901b1d3
...
...
@@ -12,6 +12,8 @@ from rest_framework import status
from
django.conf
import
settings
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
xmodule.modulestore.tests.factories
import
CourseFactory
from
course_modes.models
import
CourseMode
from
util.models
import
RateLimitConfiguration
from
util.testing
import
UrlResetMixin
from
enrollment
import
api
from
enrollment.errors
import
CourseEnrollmentError
...
...
@@ -37,6 +39,11 @@ class EnrollmentTest(ModuleStoreTestCase, APITestCase):
def
setUp
(
self
):
"""
Create a course and user, then log in.
"""
super
(
EnrollmentTest
,
self
).
setUp
()
rate_limit_config
=
RateLimitConfiguration
.
current
()
rate_limit_config
.
enabled
=
False
rate_limit_config
.
save
()
self
.
course
=
CourseFactory
.
create
()
self
.
user
=
UserFactory
.
create
(
username
=
self
.
USERNAME
,
email
=
self
.
EMAIL
,
password
=
self
.
PASSWORD
)
self
.
other_user
=
UserFactory
.
create
()
...
...
@@ -276,6 +283,35 @@ class EnrollmentTest(ModuleStoreTestCase, APITestCase):
self
.
assertEqual
(
resp
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
self
.
assertIn
(
"
No course
"
,
resp
.
content
)
def
test_enrollment_throttle_for_user
(
self
):
"""
Make sure a user requests do not exceed the maximum number of requests
"""
rate_limit_config
=
RateLimitConfiguration
.
current
()
rate_limit_config
.
enabled
=
True
rate_limit_config
.
save
()
CourseModeFactory
.
create
(
course_id
=
self
.
course
.
id
,
mode_slug
=
CourseMode
.
HONOR
,
mode_display_name
=
CourseMode
.
HONOR
,
)
for
attempt
in
range
(
0
,
50
):
expected_status
=
status
.
HTTP_429_TOO_MANY_REQUESTS
if
attempt
>=
40
else
status
.
HTTP_200_OK
self
.
_create_enrollment
(
expected_status
=
expected_status
)
def
test_enrollment_throttle_for_service
(
self
):
"""
Make sure a service can call the enrollment API as many times as needed.
"""
rate_limit_config
=
RateLimitConfiguration
.
current
()
rate_limit_config
.
enabled
=
True
rate_limit_config
.
save
()
CourseModeFactory
.
create
(
course_id
=
self
.
course
.
id
,
mode_slug
=
CourseMode
.
HONOR
,
mode_display_name
=
CourseMode
.
HONOR
,
)
for
attempt
in
range
(
0
,
50
):
self
.
_create_enrollment
(
as_server
=
True
)
def
_create_enrollment
(
self
,
course_id
=
None
,
username
=
None
,
expected_status
=
status
.
HTTP_200_OK
,
email_opt_in
=
None
,
as_server
=
False
):
"""
Enroll in the course and verify the URL we are sent to.
"""
course_id
=
unicode
(
self
.
course
.
id
)
if
course_id
is
None
else
course_id
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/enrollment/views.py
+
8
−
6
View file @
d901b1d3
...
...
@@ -24,12 +24,6 @@ from enrollment.errors import (
)
class
EnrollmentUserThrottle
(
UserRateThrottle
):
"""
Limit the number of requests users can make to the enrollment API.
"""
# TODO Limit significantly after performance testing. # pylint: disable=fixme
rate
=
'
50/second
'
class
ApiKeyPermissionMixIn
(
object
):
"""
This mixin is used to provide a convenience function for doing individual permission checks
...
...
@@ -49,6 +43,14 @@ class ApiKeyPermissionMixIn(object):
return
ApiKeyHeaderPermission
().
has_permission
(
request
,
self
)
class
EnrollmentUserThrottle
(
UserRateThrottle
,
ApiKeyPermissionMixIn
):
"""
Limit the number of requests users can make to the enrollment API.
"""
rate
=
'
40/minute
'
def
allow_request
(
self
,
request
,
view
):
return
self
.
has_api_key_permissions
(
request
)
or
super
(
EnrollmentUserThrottle
,
self
).
allow_request
(
request
,
view
)
@can_disable_rate_limit
class
EnrollmentView
(
APIView
,
ApiKeyPermissionMixIn
):
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment