From 607bd538e635a956c53f9a0071230e93af175bda Mon Sep 17 00:00:00 2001 From: Peter Fogg Date: Fri, 4 Sep 2015 09:39:09 -0400 Subject: [PATCH] Update discussion events to include team_id when appropriate. TNL-3196 --- .../django_comment_client/base/tests.py | 52 ++++++++++++++++++- .../django_comment_client/base/views.py | 4 ++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/django_comment_client/base/tests.py b/lms/djangoapps/django_comment_client/base/tests.py index b99182b58e..568b866ab4 100644 --- a/lms/djangoapps/django_comment_client/base/tests.py +++ b/lms/djangoapps/django_comment_client/base/tests.py @@ -24,6 +24,7 @@ from django_comment_client.tests.unicode import UnicodeTestMixin from django_comment_common.models import Role from django_comment_common.utils import seed_permissions_roles, ThreadContext from student.tests.factories import CourseEnrollmentFactory, UserFactory, CourseAccessRoleFactory +from teams.tests.factories import CourseTeamFactory, CourseTeamMembershipFactory from util.testing import UrlResetMixin from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -1226,7 +1227,7 @@ class CreateSubCommentUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, Moc request = RequestFactory().post("dummy_url", {"body": text}) request.user = self.student request.view_name = "create_sub_comment" - Thread.commentable_id = Mock() + Thread.commentable_id = "test_commentable" try: response = views.create_sub_comment( request, course_id=unicode(self.course.id), comment_id="dummy_comment_id" @@ -1496,7 +1497,11 @@ class TeamsPermissionsTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSe self.assertEqual(response.status_code, status_code) +TEAM_COMMENTABLE_ID = 'test-team-discussion' + + @disable_signal(views, 'comment_created') +@ddt.ddt class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): """ Forum actions are expected to launch analytics events. Test these here. @@ -1592,6 +1597,51 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): self.assertEqual(event['user_course_roles'], ['Wizard']) self.assertEqual(event['options']['followed'], False) + @patch('eventtracking.tracker.emit') + @patch('lms.lib.comment_client.utils.requests.request') + @ddt.data(( + 'create_thread', + 'edx.forum.thread.created', { + 'thread_type': 'discussion', + 'body': 'Test text', + 'title': 'Test', + 'auto_subscribe': True + }, + {'commentable_id': TEAM_COMMENTABLE_ID} + ), ( + 'create_comment', + 'edx.forum.response.created', + {'body': 'Test comment', 'auto_subscribe': True}, + {'thread_id': 'test_thread_id'} + ), ( + 'create_sub_comment', + 'edx.forum.comment.created', + {'body': 'Another comment'}, + {'comment_id': 'dummy_comment_id'} + )) + @ddt.unpack + def test_team_events(self, view_name, event_name, view_data, view_kwargs, mock_request, mock_emit): + user = self.student + team = CourseTeamFactory.create(discussion_topic_id=TEAM_COMMENTABLE_ID) + CourseTeamMembershipFactory.create(team=team, user=user) + + mock_request.return_value.status_code = 200 + self._set_mock_request_data(mock_request, { + 'closed': False, + 'commentable_id': TEAM_COMMENTABLE_ID, + 'thread_id': 'test_thread_id', + }) + + request = RequestFactory().post('dummy_url', view_data) + request.user = user + request.view_name = view_name + + getattr(views, view_name)(request, course_id=unicode(self.course.id), **view_kwargs) + + name, event = mock_emit.call_args[0] + self.assertEqual(name, event_name) + self.assertEqual(event['team_id'], team.team_id) + class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin): diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index b24409112c..84b494eb37 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -94,6 +94,10 @@ def track_forum_event(request, event_name, course, obj, data, id_map=None): data['id'] = obj.id commentable_id = data['commentable_id'] + team = get_team(commentable_id) + if team is not None: + data.update(team_id=team.team_id) + if id_map is None: id_map = get_cached_discussion_id_map(course, [commentable_id], user)