From 72aeb2c8da776cae1b24cf580a888aa230ea5b15 Mon Sep 17 00:00:00 2001 From: wajeeha-khalid Date: Thu, 11 Feb 2016 21:24:04 +0500 Subject: [PATCH] implemented test event data for thread/comment vote --- lms/djangoapps/discussion_api/api.py | 9 ++++- .../discussion_api/tests/test_api.py | 39 ++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/discussion_api/api.py b/lms/djangoapps/discussion_api/api.py index d823ecb355..37646febac 100644 --- a/lms/djangoapps/discussion_api/api.py +++ b/lms/djangoapps/discussion_api/api.py @@ -9,7 +9,6 @@ from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.http import Http404 import itertools -from lms.djangoapps.django_comment_client.base.views import track_voted_event from rest_framework.exceptions import PermissionDenied @@ -26,7 +25,11 @@ from discussion_api.permissions import ( get_initializable_thread_fields, ) from discussion_api.serializers import CommentSerializer, ThreadSerializer, get_context -from django_comment_client.base.views import track_comment_created_event, track_thread_created_event +from django_comment_client.base.views import ( + track_comment_created_event, + track_thread_created_event, + track_voted_event, +) from django_comment_common.signals import ( thread_created, thread_edited, @@ -511,6 +514,8 @@ def _do_extra_actions(api_content, cc_content, request_fields, actions_form, con _handle_abuse_flagged_field(form_value, context["cc_requester"], cc_content) elif field == "voted": _handle_voted_field(form_value, cc_content, api_content, request, context) + else: + raise ValidationError({field: ["Invalid Key"]}) def _handle_following_field(form_value, user, cc_content): diff --git a/lms/djangoapps/discussion_api/tests/test_api.py b/lms/djangoapps/discussion_api/tests/test_api.py index 9ce1a9708d..5decbe898e 100644 --- a/lms/djangoapps/discussion_api/tests/test_api.py +++ b/lms/djangoapps/discussion_api/tests/test_api.py @@ -2107,7 +2107,8 @@ class UpdateThreadTest( @ddt.data(*itertools.product([True, False], [True, False])) @ddt.unpack - def test_voted(self, current_vote_status, new_vote_status): + @mock.patch("eventtracking.tracker.emit") + def test_voted(self, current_vote_status, new_vote_status, mock_emit): """ Test attempts to edit the "voted" field. @@ -2144,6 +2145,22 @@ class UpdateThreadTest( expected_request_data["value"] = ["up"] self.assertEqual(actual_request_data, expected_request_data) + event_name, event_data = mock_emit.call_args[0] + self.assertEqual(event_name, "edx.forum.thread.voted") + self.assertEqual( + event_data, + { + 'undo_vote': not new_vote_status, + 'url': '', + 'target_username': self.user.username, + 'vote_value': 'up', + 'user_forums_roles': [FORUM_ROLE_STUDENT], + 'user_course_roles': [], + 'commentable_id': 'original_topic', + 'id': 'test_thread' + } + ) + @ddt.data(*itertools.product([True, False], [True, False], [True, False])) @ddt.unpack def test_vote_count(self, current_vote_status, first_vote, second_vote): @@ -2499,7 +2516,8 @@ class UpdateCommentTest( @ddt.data(*itertools.product([True, False], [True, False])) @ddt.unpack - def test_voted(self, current_vote_status, new_vote_status): + @mock.patch("eventtracking.tracker.emit") + def test_voted(self, current_vote_status, new_vote_status, mock_emit): """ Test attempts to edit the "voted" field. @@ -2539,6 +2557,23 @@ class UpdateCommentTest( expected_request_data["value"] = ["up"] self.assertEqual(actual_request_data, expected_request_data) + event_name, event_data = mock_emit.call_args[0] + self.assertEqual(event_name, "edx.forum.response.voted") + + self.assertEqual( + event_data, + { + 'undo_vote': not new_vote_status, + 'url': '', + 'target_username': self.user.username, + 'vote_value': 'up', + 'user_forums_roles': [FORUM_ROLE_STUDENT], + 'user_course_roles': [], + 'commentable_id': 'dummy', + 'id': 'test_comment' + } + ) + @ddt.data(*itertools.product([True, False], [True, False], [True, False])) @ddt.unpack def test_vote_count(self, current_vote_status, first_vote, second_vote):