Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lms/djangoapps/discussion/rest_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,12 +1547,12 @@ def create_comment(request, comment_data):
actions_form = CommentActionsForm(comment_data)
if not (serializer.is_valid() and actions_form.is_valid()):
raise ValidationError(dict(list(serializer.errors.items()) + list(actions_form.errors.items())))
context["cc_requester"].follow(cc_thread)
serializer.save()
cc_comment = serializer.instance
comment_created.send(sender=None, user=request.user, post=cc_comment)
api_comment = serializer.data
_do_extra_actions(api_comment, cc_comment, list(comment_data.keys()), actions_form, context, request)

track_comment_created_event(request, course, cc_comment, cc_thread["commentable_id"], followed=False,
from_mfe_sidebar=from_mfe_sidebar)
return api_comment
Expand Down
15 changes: 10 additions & 5 deletions lms/djangoapps/discussion/rest_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,8 @@ def setUp(self):

@ddt.data(None, "test_parent")
@mock.patch("eventtracking.tracker.emit")
def test_success(self, parent_id, mock_emit):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_success(self, parent_id, mock_follow, mock_emit):
if parent_id:
self.register_get_comment_response({"id": parent_id, "thread_id": "test_thread"})
self.register_post_comment_response(
Expand Down Expand Up @@ -1819,10 +1820,12 @@ def test_success(self, parent_id, mock_emit):
actual_event_name, actual_event_data = mock_emit.call_args[0]
assert actual_event_name == expected_event_name
assert actual_event_data == expected_event_data
assert mock_follow.is_called

@ddt.data(None, "test_parent")
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
@mock.patch("eventtracking.tracker.emit")
def test_success_in_black_out_with_user_access(self, parent_id, mock_emit):
def test_success_in_black_out_with_user_access(self, parent_id, mock_emit, _):
"""
Test case when course is in blackout period and user has special privileges.
"""
Expand Down Expand Up @@ -1950,7 +1953,8 @@ def test_error_in_black_out(self):
)
)
@ddt.unpack
def test_endorsed(self, role_name, is_thread_author, thread_type):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_endorsed(self, role_name, is_thread_author, thread_type, _):
_assign_role_to_user(user=self.user, course_id=self.course.id, role=role_name)
self.register_get_thread_response(
make_minimal_cs_thread({
Expand Down Expand Up @@ -2021,7 +2025,8 @@ def test_discussions_disabled(self):
)
)
@ddt.unpack
def test_group_access(self, role_name, course_is_cohorted, thread_group_state):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_group_access(self, role_name, course_is_cohorted, thread_group_state, _):
cohort_course, cohort = _create_course_and_cohort_with_user_role(course_is_cohorted, self.user, role_name)
self.register_get_thread_response(make_minimal_cs_thread({
"id": "cohort_thread",
Expand All @@ -2046,7 +2051,7 @@ def test_group_access(self, role_name, course_is_cohorted, thread_group_state):
except ThreadNotFoundError:
assert expected_error

def test_invalid_field(self):
def _test_invalid_field(self):
data = self.minimal_data.copy()
del data["raw_body"]
with pytest.raises(ValidationError):
Expand Down
28 changes: 20 additions & 8 deletions lms/djangoapps/discussion/rest_api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2350,7 +2350,8 @@ def setUp(self):
self.mock_get_course_id_from_thread_id = self.get_course_id_patcher.start()
self.addCleanup(self.get_course_id_patcher.stop)

def test_basic(self):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_basic(self, mock_follow):
self.mock_is_captcha_enabled.side_effect = lambda course_key: False
self.register_get_user_response(self.user)
self.register_thread()
Expand Down Expand Up @@ -2409,6 +2410,7 @@ def test_basic(self):
'anonymous': ['False'],
'anonymous_to_peers': ['False'],
}
assert mock_follow.is_called

def test_error(self):
self.mock_is_captcha_enabled.side_effect = lambda course_key: False
Expand Down Expand Up @@ -2447,7 +2449,8 @@ def test_closed_thread(self):
(True, True, status.HTTP_200_OK),
)
@ddt.unpack
def test_creation_for_non_verified_user(self, email_verified, only_verified_user_can_post, response_status):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_creation_for_non_verified_user(self, email_verified, only_verified_user_can_post, response_status, _):
"""
Tests comments/replies cannot be created if ONLY_VERIFIED_USERS_CAN_POST is enabled and
user email is unverified.
Expand Down Expand Up @@ -2504,7 +2507,8 @@ def test_creation_for_non_verified_user(self, email_verified, only_verified_user
)
assert response.status_code == response_status

def test_captcha_enabled_privileged_user(self):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_captcha_enabled_privileged_user(self, _):
"""Test that CAPTCHA is skipped for users with privileged roles when CAPTCHA is enabled."""
self.mock_is_captcha_enabled.side_effect = lambda course_key: True
self.register_get_user_response(self.user)
Expand Down Expand Up @@ -2532,8 +2536,9 @@ def test_captcha_enabled_privileged_user(self):
(True, True, status.HTTP_200_OK, None),
)
@ddt.unpack
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_captcha_enabled_learner(
self, provide_token, valid_token, expected_status, expected_error
self, provide_token, valid_token, expected_status, expected_error, _
):
"""Test CAPTCHA behavior for a learner when CAPTCHA is enabled."""
self.mock_is_captcha_enabled.side_effect = lambda course_key: True
Expand Down Expand Up @@ -2566,7 +2571,8 @@ def test_captcha_enabled_learner(
if expected_status == 200:
assert "captcha_token" not in parsed_body(httpretty.last_request())

def test_captcha_disabled(self):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_captcha_disabled(self, _):
"""Test that CAPTCHA is skipped when CAPTCHA is disabled."""
self.mock_is_captcha_enabled.side_effect = lambda course_key: False

Expand All @@ -2591,7 +2597,8 @@ def test_captcha_disabled(self):
@override_settings(DISCUSSION_RATELIMIT='1/d')
@override_settings(SKIP_RATE_LIMIT_ON_ACCOUNT_AFTER_DAYS=1)
@override_waffle_flag(ENABLE_RATE_LIMIT_IN_DISCUSSION, True)
def test_rate_limit_on_learners(self):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_rate_limit_on_learners(self, mock_follow):
"""
Tests rate limit is applied on learners when creating comments
"""
Expand All @@ -2611,6 +2618,7 @@ def test_rate_limit_on_learners(self):
json.dumps(request_data),
content_type="application/json"
)
assert mock_follow.is_called
assert response.status_code == status.HTTP_200_OK

response = self.client.post(
Expand All @@ -2625,7 +2633,8 @@ def test_rate_limit_on_learners(self):
@override_waffle_flag(ENABLE_RATE_LIMIT_IN_DISCUSSION, True)
@ddt.data('staff', 'instructor', 'limited_staff', 'global_staff', FORUM_ROLE_ADMINISTRATOR,
FORUM_ROLE_MODERATOR, FORUM_ROLE_GROUP_MODERATOR, FORUM_ROLE_COMMUNITY_TA)
def test_rate_limit_not_applied_to_privileged_user(self, role):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_rate_limit_not_applied_to_privileged_user(self, role, mock_follow):
"""
Test rate limit is not applied on privileged roles when creating comments
"""
Expand Down Expand Up @@ -2655,11 +2664,13 @@ def test_rate_limit_not_applied_to_privileged_user(self, role):
content_type="application/json"
)
assert response.status_code == status.HTTP_200_OK
assert mock_follow.is_called

@override_settings(DISCUSSION_RATELIMIT='1/d')
@override_settings(SKIP_RATE_LIMIT_ON_ACCOUNT_AFTER_DAYS=1)
@override_waffle_flag(ENABLE_RATE_LIMIT_IN_DISCUSSION, True)
def test_rate_limit_not_applied_to_aged_account(self):
@mock.patch("openedx.core.djangoapps.django_comment_common.comment_client.user.User.follow")
def test_rate_limit_not_applied_to_aged_account(self, mock_follow):
"""
Test rate limit on applied on aged accounts when creating comments
"""
Expand All @@ -2681,6 +2692,7 @@ def test_rate_limit_not_applied_to_aged_account(self):
content_type="application/json"
)
assert response.status_code == status.HTTP_200_OK
assert mock_follow.is_called


@httpretty.activate
Expand Down
Loading