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
17e22216
Commit
17e22216
authored
Oct 16, 2013
by
Greg Price
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1326 from edx/gprice/cs-header-auth
Use HTTP header for comments service auth
parents
02acd570
62cc9e77
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
124 additions
and
31 deletions
+124
-31
lms/djangoapps/django_comment_client/base/tests.py
+117
-20
lms/djangoapps/django_comment_client/tests/mock_cs_server/mock_cs_server.py
+2
-2
lms/djangoapps/django_comment_client/tests/mock_cs_server/test_mock_cs_server.py
+2
-2
lms/lib/comment_client/utils.py
+3
-7
No files found.
lms/djangoapps/django_comment_client/base/tests.py
View file @
17e22216
...
@@ -87,14 +87,19 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
...
@@ -87,14 +87,19 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
'course_id'
:
self
.
course_id
})
'course_id'
:
self
.
course_id
})
response
=
self
.
client
.
post
(
url
,
data
=
thread
)
response
=
self
.
client
.
post
(
url
,
data
=
thread
)
assert_true
(
mock_request
.
called
)
assert_true
(
mock_request
.
called
)
mock_request
.
assert_called_with
(
'post'
,
mock_request
.
assert_called_with
(
'http://localhost:4567/api/v1/i4x-MITx-999-course-Robot_Super_Course/threads'
,
'post'
,
data
=
{
'body'
:
u'this is a post'
,
'http://localhost:4567/api/v1/i4x-MITx-999-course-Robot_Super_Course/threads'
,
'anonymous_to_peers'
:
False
,
'user_id'
:
1
,
data
=
{
'title'
:
u'Hello'
,
'body'
:
u'this is a post'
,
'commentable_id'
:
u'i4x-MITx-999-course-Robot_Super_Course'
,
'anonymous_to_peers'
:
False
,
'user_id'
:
1
,
'anonymous'
:
False
,
'course_id'
:
u'MITx/999/Robot_Super_Course'
,
'title'
:
u'Hello'
,
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
timeout
=
5
)
'commentable_id'
:
u'i4x-MITx-999-course-Robot_Super_Course'
,
'anonymous'
:
False
,
'course_id'
:
u'MITx/999/Robot_Super_Course'
,
},
headers
=
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
timeout
=
5
)
assert_equal
(
response
.
status_code
,
200
)
assert_equal
(
response
.
status_code
,
200
)
def
test_flag_thread
(
self
,
mock_request
):
def
test_flag_thread
(
self
,
mock_request
):
...
@@ -123,9 +128,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
...
@@ -123,9 +128,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
response
=
self
.
client
.
post
(
url
)
response
=
self
.
client
.
post
(
url
)
assert_true
(
mock_request
.
called
)
assert_true
(
mock_request
.
called
)
call_list
=
[((
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
,
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}),
call_list
=
[
((
'put'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d/abuse_flag'
),
{
'data'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
,
'user_id'
:
'1'
},
'timeout'
:
5
}),
(
((
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
,
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
})]
(
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'put'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d/abuse_flag'
),
{
'data'
:
{
'user_id'
:
'1'
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
)
]
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
...
@@ -157,9 +185,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
...
@@ -157,9 +185,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
response
=
self
.
client
.
post
(
url
)
response
=
self
.
client
.
post
(
url
)
assert_true
(
mock_request
.
called
)
assert_true
(
mock_request
.
called
)
call_list
=
[((
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
,
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}),
call_list
=
[
((
'put'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d/abuse_unflag'
),
{
'data'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
,
'user_id'
:
'1'
},
'timeout'
:
5
}),
(
((
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
,
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
})]
(
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'put'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d/abuse_unflag'
),
{
'data'
:
{
'user_id'
:
'1'
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'get'
,
'http://localhost:4567/api/v1/threads/518d4237b023791dca00000d'
),
{
'params'
:
{
'mark_as_read'
:
True
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
)
]
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
...
@@ -187,9 +238,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
...
@@ -187,9 +238,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
response
=
self
.
client
.
post
(
url
)
response
=
self
.
client
.
post
(
url
)
assert_true
(
mock_request
.
called
)
assert_true
(
mock_request
.
called
)
call_list
=
[((
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}),
call_list
=
[
((
'put'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d/abuse_flag'
),
{
'data'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
,
'user_id'
:
'1'
},
'timeout'
:
5
}),
(
((
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
})]
(
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'put'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d/abuse_flag'
),
{
'data'
:
{
'user_id'
:
'1'
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
)
]
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
...
@@ -217,9 +291,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
...
@@ -217,9 +291,32 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
response
=
self
.
client
.
post
(
url
)
response
=
self
.
client
.
post
(
url
)
assert_true
(
mock_request
.
called
)
assert_true
(
mock_request
.
called
)
call_list
=
[((
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}),
call_list
=
[
((
'put'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d/abuse_unflag'
),
{
'data'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
,
'user_id'
:
'1'
},
'timeout'
:
5
}),
(
((
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{
'api_key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
})]
(
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'put'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d/abuse_unflag'
),
{
'data'
:
{
'user_id'
:
'1'
},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
),
(
(
'get'
,
'http://localhost:4567/api/v1/comments/518d4237b023791dca00000d'
),
{
'params'
:
{},
'headers'
:
{
'X-Edx-Api-Key'
:
'PUT_YOUR_API_KEY_HERE'
},
'timeout'
:
5
}
)
]
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
assert_equal
(
call_list
,
mock_request
.
call_args_list
)
...
...
lms/djangoapps/django_comment_client/tests/mock_cs_server/mock_cs_server.py
View file @
17e22216
...
@@ -27,7 +27,7 @@ class MockCommentServiceRequestHandler(BaseHTTPRequestHandler):
...
@@ -27,7 +27,7 @@ class MockCommentServiceRequestHandler(BaseHTTPRequestHandler):
(
json
.
dumps
(
post_dict
),
self
.
path
))
(
json
.
dumps
(
post_dict
),
self
.
path
))
# Every good post has at least an API key
# Every good post has at least an API key
if
'
api_key'
in
post_dict
:
if
'
X-Edx-Api-Key'
in
self
.
headers
:
response
=
self
.
server
.
_response_str
response
=
self
.
server
.
_response_str
# Log the response
# Log the response
logger
.
debug
(
"Comment Service: sending response
%
s"
%
json
.
dumps
(
response
))
logger
.
debug
(
"Comment Service: sending response
%
s"
%
json
.
dumps
(
response
))
...
@@ -62,7 +62,7 @@ class MockCommentServiceRequestHandler(BaseHTTPRequestHandler):
...
@@ -62,7 +62,7 @@ class MockCommentServiceRequestHandler(BaseHTTPRequestHandler):
(
json
.
dumps
(
post_dict
),
self
.
path
))
(
json
.
dumps
(
post_dict
),
self
.
path
))
# Every good post has at least an API key
# Every good post has at least an API key
if
'
api_key'
in
post_dict
:
if
'
X-Edx-Api-Key'
in
self
.
headers
:
response
=
self
.
server
.
_response_str
response
=
self
.
server
.
_response_str
# Log the response
# Log the response
logger
.
debug
(
"Comment Service: sending response
%
s"
%
json
.
dumps
(
response
))
logger
.
debug
(
"Comment Service: sending response
%
s"
%
json
.
dumps
(
response
))
...
...
lms/djangoapps/django_comment_client/tests/mock_cs_server/test_mock_cs_server.py
View file @
17e22216
...
@@ -43,10 +43,10 @@ class MockCommentServiceServerTest(unittest.TestCase):
...
@@ -43,10 +43,10 @@ class MockCommentServiceServerTest(unittest.TestCase):
of how you would create a new user
of how you would create a new user
"""
"""
# Send a request
# Send a request
values
=
{
'username'
:
u'user100'
,
'api_key'
:
'TEST_API_KEY'
,
values
=
{
'username'
:
u'user100'
,
'external_id'
:
'4'
,
'email'
:
u'user100@edx.org'
}
'external_id'
:
'4'
,
'email'
:
u'user100@edx.org'
}
data
=
json
.
dumps
(
values
)
data
=
json
.
dumps
(
values
)
headers
=
{
'Content-Type'
:
'application/json'
,
'Content-Length'
:
len
(
data
)}
headers
=
{
'Content-Type'
:
'application/json'
,
'Content-Length'
:
len
(
data
)
,
'X-Edx-Api-Key'
:
'TEST_API_KEY'
}
req
=
urllib2
.
Request
(
self
.
server_url
+
'/api/v1/users/4'
,
data
,
headers
)
req
=
urllib2
.
Request
(
self
.
server_url
+
'/api/v1/users/4'
,
data
,
headers
)
# Send the request to the mock cs server
# Send the request to the mock cs server
...
...
lms/lib/comment_client/utils.py
View file @
17e22216
...
@@ -31,18 +31,14 @@ def merge_dict(dic1, dic2):
...
@@ -31,18 +31,14 @@ def merge_dict(dic1, dic2):
def
perform_request
(
method
,
url
,
data_or_params
=
None
,
*
args
,
**
kwargs
):
def
perform_request
(
method
,
url
,
data_or_params
=
None
,
*
args
,
**
kwargs
):
if
data_or_params
is
None
:
if
data_or_params
is
None
:
data_or_params
=
{}
data_or_params
=
{}
data_or_params
[
'api_key'
]
=
settings
.
API_KEY
headers
=
{
'X-Edx-Api-Key'
:
settings
.
API_KEY
}
try
:
try
:
with
dog_stats_api
.
timer
(
'comment_client.request.time'
):
with
dog_stats_api
.
timer
(
'comment_client.request.time'
):
if
method
in
[
'post'
,
'put'
,
'patch'
]:
if
method
in
[
'post'
,
'put'
,
'patch'
]:
response
=
requests
.
request
(
method
,
url
,
data
=
data_or_params
,
timeout
=
5
)
response
=
requests
.
request
(
method
,
url
,
data
=
data_or_params
,
headers
=
headers
,
timeout
=
5
)
else
:
else
:
response
=
requests
.
request
(
method
,
url
,
params
=
data_or_params
,
timeout
=
5
)
response
=
requests
.
request
(
method
,
url
,
params
=
data_or_params
,
headers
=
headers
,
timeout
=
5
)
except
Exception
as
err
:
except
Exception
as
err
:
# remove API key if it is in the params
if
'api_key'
in
data_or_params
:
log
.
info
(
'Deleting API key from params'
)
del
data_or_params
[
'api_key'
]
log
.
exception
(
"Trying to call {method} on {url} with params {params}"
.
format
(
log
.
exception
(
"Trying to call {method} on {url} with params {params}"
.
format
(
method
=
method
,
url
=
url
,
params
=
data_or_params
))
method
=
method
,
url
=
url
,
params
=
data_or_params
))
# Reraise with a single exception type
# Reraise with a single exception type
...
...
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