Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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
edx-platform
Commits
bb1631b2
Commit
bb1631b2
authored
Aug 31, 2015
by
Clinton Blackburn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9545 from edx/clintonb/csrf-test
Added test for CreditCourse endpoint CSRF validation
parents
40e38829
c6c897e2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
1 deletions
+29
-1
openedx/core/djangoapps/credit/tests/test_views.py
+29
-1
No files found.
openedx/core/djangoapps/credit/tests/test_views.py
View file @
bb1631b2
...
...
@@ -8,7 +8,7 @@ import unittest
import
ddt
from
django.conf
import
settings
from
django.core.urlresolvers
import
reverse
from
django.test
import
TestCase
from
django.test
import
TestCase
,
Client
from
django.test.utils
import
override_settings
from
mock
import
patch
from
oauth2_provider.tests.factories
import
AccessTokenFactory
,
ClientFactory
...
...
@@ -380,6 +380,34 @@ class CreditCourseViewSetTests(TestCase):
response
=
self
.
client
.
get
(
self
.
path
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_session_auth_post_requires_csrf_token
(
self
):
""" Verify non-GET requests require a CSRF token be attached to the request. """
user
=
UserFactory
(
password
=
self
.
password
,
is_staff
=
True
)
client
=
Client
(
enforce_csrf_checks
=
True
)
self
.
assertTrue
(
client
.
login
(
username
=
user
.
username
,
password
=
self
.
password
))
data
=
{
'course_key'
:
'a/b/c'
,
'enabled'
:
True
}
# POSTs without a CSRF token should fail.
response
=
client
.
post
(
self
.
path
,
data
=
json
.
dumps
(
data
),
content_type
=
JSON
)
# NOTE (CCB): Ordinarily we would expect a 403; however, since the CSRF validation and session authentication
# fail, DRF considers the request to be unauthenticated.
self
.
assertEqual
(
response
.
status_code
,
401
)
self
.
assertIn
(
'CSRF'
,
response
.
content
)
# Retrieve a CSRF token
response
=
client
.
get
(
'/dashboard'
)
csrf_token
=
response
.
cookies
[
settings
.
CSRF_COOKIE_NAME
]
.
value
# pylint: disable=no-member
self
.
assertGreater
(
len
(
csrf_token
),
0
)
# Ensure POSTs made with the token succeed.
response
=
client
.
post
(
self
.
path
,
data
=
json
.
dumps
(
data
),
content_type
=
JSON
,
HTTP_X_CSRFTOKEN
=
csrf_token
)
self
.
assertEqual
(
response
.
status_code
,
201
)
def
test_oauth
(
self
):
""" Verify the endpoint supports OAuth, and only allows authorization for staff users. """
user
=
UserFactory
(
is_staff
=
False
)
...
...
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