Skip to content

Commit

Permalink
test: added and fixed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ayesha-waris committed Jan 24, 2024
1 parent d3139fb commit 27cc91a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ def send_new_response_endorsed_notification(self):
response on his thread has been endorsed
"""
context = {
"author_name": self.creator.username,
"post_title": self.thread.title
"username": self.creator.username,
}
self._send_notification([self.thread.user_id], "response_endorsed_on_thread", context)

Expand Down
52 changes: 50 additions & 2 deletions lms/djangoapps/discussion/rest_api/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.tests.factories import StaffFactory, UserFactory
from lms.djangoapps.discussion.django_comment_client.tests.factories import RoleFactory
from lms.djangoapps.discussion.rest_api.tasks import send_response_notifications, send_thread_created_notification
from lms.djangoapps.discussion.rest_api.tests.utils import ThreadMock, make_minimal_cs_thread
from lms.djangoapps.discussion.rest_api.tasks import (
send_response_notifications,
send_thread_created_notification,
send_response_endorsed_notification)
from lms.djangoapps.discussion.rest_api.tests.utils import ThreadMock, make_minimal_cs_thread, make_minimal_cs_comment
from openedx.core.djangoapps.course_groups.models import CohortMembership, CourseCohortsSettings
from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory
from openedx.core.djangoapps.discussions.models import DiscussionTopicLink
Expand Down Expand Up @@ -158,6 +161,23 @@ def _create_thread(self, thread_type="discussion", group_id=None):
self.register_get_thread_response(thread)
return thread

def _create_response(self, thread_id):
"""
Create a response
"""
response = make_minimal_cs_comment({
"id": 2,
"thread_id": thread_id,
"raw_body": "<p>response</p>",
"user_id": str(self.student_role.users.first().id),
"username": self.student_role.users.first().username,
"course_id": str(self.course.id),
"parent_id": None,
"endorsed": False,
})
self.register_get_comment_response(response)
return response

def test_basic(self):
"""
Left empty intentionally. This test case is inherited from DiscussionAPIViewTestMixin
Expand Down Expand Up @@ -193,6 +213,33 @@ def test_notification_is_send_to_all_enrollments(self, notification_type):
notification_audience_filters = {}
assert notification_audience_filters == course_notification_data.audience_filters

def test_notification_sent_to_thread_author_on_response_endorsement(self):
"""
Test that a notification is sent to the thread author when a response on their thread is endorsed.
"""
self._assign_enrollments()
thread = self._create_thread()
response = self._create_response(thread_id=thread['id'])
handler = mock.Mock()
USER_NOTIFICATION_REQUESTED.connect(handler)
send_response_endorsed_notification(thread['id'], str(self.course.id), str(self.student_role.users.first().id))
self.assertEqual(handler.call_count, 1)
notification_data = handler.call_args_list[0][1]['notification_data']
self.assertEqual([int(user_id) for user_id in notification_data.user_ids], [int(thread['user_id'])]) # Target only the thread author
self.assertEqual(notification_data.notification_type, 'response_endorsed_on_thread')

expected_context = {
'replier_name': response['username'],
'post_title': 'Test Title',
'course_name': self.course.display_name,
'sender_id': int(response['user_id']),
'username': response['username'],
}
self.assertDictEqual(notification_data.context, expected_context)
self.assertEqual(notification_data.content_url, _get_mfe_url(self.course.id, thread['id']))
self.assertEqual(notification_data.app_name, 'discussion')
self.assertEqual('response_endorsed_on_thread', notification_data.notification_type)

@ddt.data(
('cohort_1', 'new_question_post'),
('cohort_1', 'new_discussion_post'),
Expand Down Expand Up @@ -478,6 +525,7 @@ class TestSendCommentNotification(DiscussionAPIViewTestMixin, ModuleStoreTestCas
"""
Test case to send new_comment notification
"""

def setUp(self):
super().setUp()
httpretty.reset()
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/notifications/base_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
'<{strong}>{post_title}</{strong}></{p}>'),
'content_context': {
'post_title': 'Post title',
'username': 'Post author name',
'username': 'Response author name',
},
'email_template': '',
'filters': [FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE]
Expand Down
3 changes: 2 additions & 1 deletion openedx/core/djangoapps/notifications/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def _expected_api_response(self, course=None):
'new_comment',
'new_response',
'response_on_followed_post',
'comment_on_followed_post'
'comment_on_followed_post',
'response_endorsed_on_thread'
],
'notification_types': {
'core': {
Expand Down

0 comments on commit 27cc91a

Please sign in to comment.