Skip to content

Commit c1df8db

Browse files
Taimoor  AhmedTaimoor  Ahmed
authored andcommitted
fix pytest
1 parent d949247 commit c1df8db

2 files changed

Lines changed: 76 additions & 65 deletions

File tree

lms/djangoapps/discussion/rest_api/tests/test_tasks.py

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
FORUM_ROLE_STUDENT,
3131
CourseDiscussionSettings
3232
)
33-
from openedx.core.djangoapps.notifications.config.waffle import ENABLE_NOTIFICATIONS, ENABLE_NOTIFY_ALL_LEARNERS
33+
from openedx.core.djangoapps.notifications.config.waffle import ENABLE_NOTIFICATIONS
3434
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
3535
from xmodule.modulestore.tests.factories import CourseFactory
3636

37-
from .test_views import DiscussionAPIViewTestMixin
37+
from .test_views_v2 import DiscussionAPIViewTestMixin
3838

3939

4040
def _get_mfe_url(course_id, post_id):
@@ -58,12 +58,6 @@ def setUp(self):
5858
Setup test case
5959
"""
6060
super().setUp()
61-
patcher = mock.patch(
62-
'openedx.core.djangoapps.discussions.config.waffle.ENABLE_FORUM_V2.is_enabled',
63-
return_value=False
64-
)
65-
patcher.start()
66-
self.addCleanup(patcher.stop)
6761
# Creating a course
6862
self.course = CourseFactory.create()
6963

@@ -190,14 +184,12 @@ def test_not_authenticated(self):
190184
"""
191185

192186
@ddt.data(
193-
('new_question_post', False, False),
194-
('new_discussion_post', False, False),
195-
('new_discussion_post', True, True),
196-
('new_discussion_post', True, False),
187+
('new_question_post',),
188+
('new_discussion_post',),
197189
)
198190
@ddt.unpack
199191
def test_notification_is_send_to_all_enrollments(
200-
self, notification_type, notify_all_learners, waffle_flag_enabled
192+
self, notification_type
201193
):
202194
"""
203195
Tests notification is sent to all users if course is not cohorted
@@ -207,29 +199,20 @@ def test_notification_is_send_to_all_enrollments(
207199
"discussion" if notification_type == "new_discussion_post" else "question"
208200
)
209201

210-
with override_waffle_flag(ENABLE_NOTIFY_ALL_LEARNERS, active=waffle_flag_enabled):
211-
thread = self._create_thread(thread_type=thread_type)
212-
handler = mock.Mock()
213-
COURSE_NOTIFICATION_REQUESTED.connect(handler)
214-
215-
send_thread_created_notification(
216-
thread['id'],
217-
str(self.course.id),
218-
self.author.id,
219-
notify_all_learners
220-
)
221-
expected_handler_calls = 0 if notify_all_learners and not waffle_flag_enabled else 1
222-
self.assertEqual(handler.call_count, expected_handler_calls)
223-
224-
if handler.call_count:
225-
course_notification_data = handler.call_args[1]['course_notification_data']
226-
expected_type = (
227-
'new_instructor_all_learners_post'
228-
if notify_all_learners and waffle_flag_enabled
229-
else notification_type
230-
)
231-
self.assertEqual(course_notification_data.notification_type, expected_type)
232-
self.assertEqual(course_notification_data.audience_filters, {})
202+
thread = self._create_thread(thread_type=thread_type)
203+
handler = mock.Mock()
204+
COURSE_NOTIFICATION_REQUESTED.connect(handler)
205+
206+
send_thread_created_notification(
207+
thread['id'],
208+
str(self.course.id),
209+
self.author.id
210+
)
211+
self.assertEqual(handler.call_count, 1)
212+
213+
course_notification_data = handler.call_args[1]['course_notification_data']
214+
self.assertEqual(course_notification_data.notification_type, notification_type)
215+
self.assertEqual(course_notification_data.audience_filters, {})
233216

234217
@ddt.data(
235218
('cohort_1', 'new_question_post'),
@@ -283,12 +266,6 @@ def setUp(self):
283266
super().setUp()
284267
httpretty.reset()
285268
httpretty.enable()
286-
patcher = mock.patch(
287-
'openedx.core.djangoapps.discussions.config.waffle.ENABLE_FORUM_V2.is_enabled',
288-
return_value=False
289-
)
290-
patcher.start()
291-
self.addCleanup(patcher.stop)
292269

293270
self.course = CourseFactory.create()
294271
patcher = mock.patch(

lms/djangoapps/discussion/tests/test_tasks.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from edx_ace.recipient import Recipient
1515
from edx_ace.renderers import EmailRenderer
1616
from edx_ace.utils import date
17+
from lms.djangoapps.discussion.django_comment_client.tests.mixins import (
18+
MockForumApiMixin,
19+
)
1720
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
1821

1922
import openedx.core.djangoapps.django_comment_common.comment_client as cc
@@ -69,13 +72,39 @@ def mock_request(method, url, **kwargs):
6972
return mock_request
7073

7174

75+
def make_subscribed_threads_callback(subscribed_thread_ids, per_page=1):
76+
"""
77+
Creates a callback function for simulating user data.
78+
"""
79+
80+
def callback(*args, **kwargs): # lint-amnesty, pylint: disable=unused-argument
81+
subscribed_thread_collection = [
82+
{"id": thread_id} for thread_id in subscribed_thread_ids
83+
]
84+
page = kwargs.get("page", 1)
85+
start_index = per_page * (page - 1)
86+
end_index = per_page * page
87+
data = {
88+
"collection": subscribed_thread_collection[start_index:end_index],
89+
"page": page,
90+
"num_pages": int(
91+
math.ceil(len(subscribed_thread_collection) / float(per_page))
92+
),
93+
"thread_count": len(subscribed_thread_collection),
94+
}
95+
return data
96+
97+
return callback
98+
99+
72100
@ddt.ddt
73-
class TaskTestCase(ModuleStoreTestCase): # lint-amnesty, pylint: disable=missing-class-docstring
101+
class TaskTestCase(ModuleStoreTestCase, MockForumApiMixin): # lint-amnesty, pylint: disable=missing-class-docstring
74102

75103
@classmethod
76104
@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
77105
def setUpClass(cls):
78106
super().setUpClass()
107+
super().setUpClassAndForumMock()
79108
cls.discussion_id = 'dummy_discussion_id'
80109
cls.question_id = 'dummy_question_id'
81110
cls.course = CourseOverviewFactory.create(language='fr')
@@ -109,6 +138,29 @@ def setUpClass(cls):
109138

110139
cls.create_threads_and_comments()
111140

141+
@classmethod
142+
def tearDownClass(cls):
143+
super().tearDownClass()
144+
super().disposeForumMocks()
145+
146+
def _set_forum_mocks(
147+
self,
148+
subscribed_thread_ids=None,
149+
thread_data=None,
150+
comment_data=None,
151+
per_page=1,
152+
):
153+
"""mock threads and comments"""
154+
if subscribed_thread_ids is not None:
155+
self.set_mock_side_effect(
156+
"get_user_subscriptions",
157+
make_subscribed_threads_callback(subscribed_thread_ids, per_page),
158+
)
159+
if thread_data:
160+
self.set_mock_return_value("get_thread", thread_data)
161+
if comment_data:
162+
self.set_mock_return_value("get_parent_comment", comment_data)
163+
112164
@classmethod
113165
def create_threads_and_comments(cls): # lint-amnesty, pylint: disable=missing-function-docstring
114166
# Regular discussion threads and comments.
@@ -221,9 +273,6 @@ def create_threads_and_comments(cls): # lint-amnesty, pylint: disable=missing-f
221273

222274
def setUp(self):
223275
super().setUp()
224-
self.request_patcher = mock.patch('requests.request')
225-
self.mock_request = self.request_patcher.start()
226-
227276
self.ace_send_patcher = mock.patch('edx_ace.ace.send')
228277
self.mock_ace_send = self.ace_send_patcher.start()
229278
self.mock_message_patcher = mock.patch('lms.djangoapps.discussion.tasks.ResponseNotification')
@@ -232,26 +281,11 @@ def setUp(self):
232281
thread_permalink = '/courses/discussion/dummy_discussion_id'
233282
self.permalink_patcher = mock.patch('lms.djangoapps.discussion.tasks.permalink', return_value=thread_permalink)
234283
self.mock_permalink = self.permalink_patcher.start()
235-
patcher = mock.patch(
236-
'openedx.core.djangoapps.discussions.config.waffle.ENABLE_FORUM_V2.is_enabled',
237-
return_value=False
238-
)
239-
patcher.start()
240-
self.addCleanup(patcher.stop)
241-
patcher = mock.patch(
242-
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
243-
)
244-
self.mock_get_course_id_by_comment = patcher.start()
245-
self.addCleanup(patcher.stop)
246-
patcher = mock.patch(
247-
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
248-
)
249-
self.mock_get_course_id_by_thread = patcher.start()
250-
self.addCleanup(patcher.stop)
284+
self.set_mock_return_value("get_course_id_by_thread", str(self.course.id))
285+
self.set_mock_return_value("get_course_id_by_comment", str(self.course.id))
251286

252287
def tearDown(self):
253288
super().tearDown()
254-
self.request_patcher.stop()
255289
self.ace_send_patcher.stop()
256290
self.mock_message_patcher.stop()
257291
self.permalink_patcher.stop()
@@ -279,7 +313,7 @@ def test_send_discussion_email_notification(self, user_subscribed):
279313
]
280314
for thread, comment in examples:
281315
self.mock_ace_send.reset_mock()
282-
self.mock_request.side_effect = make_mock_responder(
316+
self._set_forum_mocks(
283317
subscribed_thread_ids=subscribed_thread_ids,
284318
comment_data=comment,
285319
thread_data=thread,
@@ -344,7 +378,7 @@ def run_should_not_send_email_test(self, thread, comment_dict):
344378
"""
345379
assert email is not sent
346380
"""
347-
self.mock_request.side_effect = make_mock_responder(
381+
self._set_forum_mocks(
348382
subscribed_thread_ids=[self.discussion_id, self.question_id],
349383
comment_data=comment_dict,
350384
thread_data=thread,

0 commit comments

Comments
 (0)