diff --git a/lms/djangoapps/django_comment_client/base/tests.py b/lms/djangoapps/django_comment_client/base/tests.py index 37bc7f7892..4f47f5b640 100644 --- a/lms/djangoapps/django_comment_client/base/tests.py +++ b/lms/djangoapps/django_comment_client/base/tests.py @@ -102,7 +102,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): 'anonymous': False, 'course_id': u'MITx/999/Robot_Super_Course', }, params={'request_id': ANY}, - headers={'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + headers=ANY, timeout=5 ) assert_equal(response.status_code, 200) @@ -139,7 +139,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'mark_as_read': True, 'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -148,7 +148,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': {'user_id': '1'}, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -157,7 +157,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'mark_as_read': True, 'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ) @@ -199,7 +199,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'mark_as_read': True, 'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -208,7 +208,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': {'user_id': '1'}, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -217,7 +217,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'mark_as_read': True, 'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ) @@ -255,7 +255,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -264,7 +264,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': {'user_id': '1'}, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -273,7 +273,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ) @@ -311,7 +311,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -320,7 +320,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': {'user_id': '1'}, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ), @@ -329,7 +329,7 @@ class ViewsTestCase(UrlResetMixin, ModuleStoreTestCase): { 'data': None, 'params': {'request_id': ANY}, - 'headers': {'X-Edx-Api-Key': 'PUT_YOUR_API_KEY_HERE'}, + 'headers': ANY, 'timeout': 5 } ) diff --git a/lms/djangoapps/django_comment_client/forum/tests.py b/lms/djangoapps/django_comment_client/forum/tests.py index 4425c3e23b..0bfeded536 100644 --- a/lms/djangoapps/django_comment_client/forum/tests.py +++ b/lms/djangoapps/django_comment_client/forum/tests.py @@ -11,7 +11,7 @@ from django_comment_client.forum import views from courseware.tests.modulestore_config import TEST_DATA_MIXED_MODULESTORE from nose.tools import assert_true # pylint: disable=E0611 -from mock import patch, Mock, ANY +from mock import patch, Mock, ANY, call import logging @@ -242,6 +242,67 @@ class SingleThreadTestCase(ModuleStoreTestCase): self.assertEquals(response.status_code, 405) +@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) + + def test_accept_language(self, mock_request): + lang = "eo" + text = "dummy content" + thread_id = "test_thread_id" + mock_request.side_effect = make_mock_request_impl(text, thread_id) + + self.client.get( + reverse( + "django_comment_client.forum.views.single_thread", + kwargs={ + "course_id": self.course.id, + "discussion_id": "dummy", + "thread_id": thread_id, + } + ), + HTTP_ACCEPT_LANGUAGE=lang, + ) + self.assert_all_calls_have_header(mock_request, "Accept-Language", lang) + + @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): def setUp(self): diff --git a/lms/lib/comment_client/settings.py b/lms/lib/comment_client/settings.py index 4ae697adb5..f64726335f 100644 --- a/lms/lib/comment_client/settings.py +++ b/lms/lib/comment_client/settings.py @@ -6,8 +6,3 @@ else: SERVICE_HOST = 'http://localhost:4567' 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" diff --git a/lms/lib/comment_client/utils.py b/lms/lib/comment_client/utils.py index c24dac8121..5e1eb88ebf 100644 --- a/lms/lib/comment_client/utils.py +++ b/lms/lib/comment_client/utils.py @@ -3,9 +3,10 @@ from dogapi import dog_stats_api import json import logging import requests -import settings +from django.conf import settings from time import time from uuid import uuid4 +from django.utils.translation import get_language log = logging.getLogger(__name__) @@ -52,7 +53,10 @@ def request_timer(request_id, method, url): def perform_request(method, url, data_or_params=None, *args, **kwargs): if data_or_params is None: data_or_params = {} - headers = {'X-Edx-Api-Key': settings.API_KEY} + headers = { + 'X-Edx-Api-Key': getattr(settings, "COMMENTS_SERVICE_KEY", None), + 'Accept-Language': get_language(), + } request_id = uuid4() request_id_dict = {'request_id': request_id}