Commit 5841d460 by Greg Price

Clean up usage of CS API key

parent a02beb6d
...@@ -102,7 +102,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -102,7 +102,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
'anonymous': False, 'course_id': u'MITx/999/Robot_Super_Course', 'anonymous': False, 'course_id': u'MITx/999/Robot_Super_Course',
}, },
params={'request_id': ANY}, params={'request_id': ANY},
headers={'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, headers=ANY,
timeout=5 timeout=5
) )
assert_equal(response.status_code, 200) assert_equal(response.status_code, 200)
...@@ -139,7 +139,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -139,7 +139,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'mark_as_read': True, 'request_id': ANY}, 'params': {'mark_as_read': True, 'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -148,7 +148,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -148,7 +148,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': {'user_id': '1'}, 'data': {'user_id': '1'},
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -157,7 +157,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -157,7 +157,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'mark_as_read': True, 'request_id': ANY}, 'params': {'mark_as_read': True, 'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
) )
...@@ -199,7 +199,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -199,7 +199,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'mark_as_read': True, 'request_id': ANY}, 'params': {'mark_as_read': True, 'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -208,7 +208,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -208,7 +208,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': {'user_id': '1'}, 'data': {'user_id': '1'},
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -217,7 +217,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -217,7 +217,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'mark_as_read': True, 'request_id': ANY}, 'params': {'mark_as_read': True, 'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
) )
...@@ -255,7 +255,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -255,7 +255,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -264,7 +264,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -264,7 +264,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': {'user_id': '1'}, 'data': {'user_id': '1'},
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -273,7 +273,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -273,7 +273,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
) )
...@@ -311,7 +311,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -311,7 +311,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -320,7 +320,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -320,7 +320,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': {'user_id': '1'}, 'data': {'user_id': '1'},
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
), ),
...@@ -329,7 +329,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): ...@@ -329,7 +329,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase):
{ {
'data': None, 'data': None,
'params': {'request_id': ANY}, 'params': {'request_id': ANY},
'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, 'headers': ANY,
'timeout': 5 'timeout': 5
} }
) )
......
...@@ -11,7 +11,7 @@ from django_comment_client.forum import views ...@@ -11,7 +11,7 @@ from django_comment_client.forum import views
from courseware.tests.modulestore_config import TEST_DATA_MIXED_MODULESTORE from courseware.tests.modulestore_config import TEST_DATA_MIXED_MODULESTORE
from nose.tools import assert_true # pylint: disable=E0611 from nose.tools import assert_true # pylint: disable=E0611
from mock import patch, Mock, ANY from mock import patch, Mock, ANY, call
import logging import logging
...@@ -243,6 +243,48 @@ class SingleThreadTestCase(ModuleStoreTestCase): ...@@ -243,6 +243,48 @@ class SingleThreadTestCase(ModuleStoreTestCase):
@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE)
@patch('requests.request')
class CommentsServiceRequestHeadersTestCase(UrlResetMixin, ModuleStoreTestCase):
@patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
def setUp(self):
username = "foo"
password = "bar"
# Invoke UrlResetMixin
super(CommentsServiceRequestHeadersTestCase, self).setUp()
self.course = CourseFactory.create()
self.student = UserFactory.create(username=username, password=password)
CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
self.assertTrue(
self.client.login(username=username, password=password)
)
def assert_all_calls_have_header(self, mock_request, key, value):
expected = call(
ANY, # method
ANY, # url
data=ANY,
params=ANY,
headers=PartialDictMatcher({key: value}),
timeout=ANY
)
for actual in mock_request.call_args_list:
self.assertEqual(expected, actual)
@override_settings(COMMENTS_SERVICE_KEY="test_api_key")
def test_api_key(self, mock_request):
mock_request.side_effect = make_mock_request_impl("dummy", "dummy")
self.client.get(
reverse(
"django_comment_client.forum.views.forum_form_discussion",
kwargs={"course_id": self.course.id}
),
)
self.assert_all_calls_have_header(mock_request, "X-Edx-Api-Key", "test_api_key")
@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE)
class InlineDiscussionUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): class InlineDiscussionUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin):
def setUp(self): def setUp(self):
self.course = CourseFactory.create() self.course = CourseFactory.create()
......
...@@ -6,8 +6,3 @@ else: ...@@ -6,8 +6,3 @@ else:
SERVICE_HOST = 'http://localhost:4567' SERVICE_HOST = 'http://localhost:4567'
PREFIX = SERVICE_HOST + '/api/v1' PREFIX = SERVICE_HOST + '/api/v1'
if hasattr(settings, "COMMENTS_SERVICE_KEY"):
API_KEY = settings.COMMENTS_SERVICE_KEY
else:
API_KEY = "PUT_YOUR_API_KEY_HERE"
...@@ -3,9 +3,10 @@ from dogapi import dog_stats_api ...@@ -3,9 +3,10 @@ from dogapi import dog_stats_api
import json import json
import logging import logging
import requests import requests
import settings from django.conf import settings
from time import time from time import time
from uuid import uuid4 from uuid import uuid4
from django.utils.translation import get_language
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -52,7 +53,7 @@ def request_timer(request_id, method, url): ...@@ -52,7 +53,7 @@ def request_timer(request_id, method, url):
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 = {}
headers = {'X-Edx-Api-Key': settings.API_KEY} headers = {'X-Edx-Api-Key': getattr(settings, "COMMENTS_SERVICE_KEY", None)}
request_id = uuid4() request_id = uuid4()
request_id_dict = {'request_id': request_id} request_id_dict = {'request_id': request_id}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment