|
| 1 | +# pylint: skip-file |
| 2 | +"""Tests for django comment client views.""" |
| 3 | + |
| 4 | +import json |
| 5 | +import logging |
| 6 | +from contextlib import contextmanager |
| 7 | +from unittest import mock |
| 8 | +from unittest.mock import ANY, Mock, patch |
| 9 | + |
| 10 | +import ddt |
| 11 | +from django.contrib.auth.models import User |
| 12 | +from django.core.management import call_command |
| 13 | +from django.test.client import RequestFactory |
| 14 | +from django.urls import reverse |
| 15 | +from eventtracking.processors.exceptions import EventEmissionExit |
| 16 | +from opaque_keys.edx.keys import CourseKey |
| 17 | +from openedx_events.learning.signals import ( |
| 18 | + FORUM_THREAD_CREATED, |
| 19 | + FORUM_THREAD_RESPONSE_CREATED, |
| 20 | + FORUM_RESPONSE_COMMENT_CREATED, |
| 21 | +) |
| 22 | + |
| 23 | +from common.djangoapps.course_modes.models import CourseMode |
| 24 | +from common.djangoapps.course_modes.tests.factories import CourseModeFactory |
| 25 | +from common.djangoapps.student.roles import CourseStaffRole, UserBasedRole |
| 26 | +from common.djangoapps.student.tests.factories import ( |
| 27 | + CourseAccessRoleFactory, |
| 28 | + CourseEnrollmentFactory, |
| 29 | + UserFactory, |
| 30 | +) |
| 31 | +from common.djangoapps.track.middleware import TrackMiddleware |
| 32 | +from common.djangoapps.track.views import segmentio |
| 33 | +from common.djangoapps.track.views.tests.base import ( |
| 34 | + SEGMENTIO_TEST_USER_ID, |
| 35 | + SegmentIOTrackingTestCaseBase, |
| 36 | +) |
| 37 | +from common.djangoapps.util.testing import UrlResetMixin |
| 38 | +from common.test.utils import MockSignalHandlerMixin, disable_signal |
| 39 | +from lms.djangoapps.discussion.django_comment_client.base import views |
| 40 | +from lms.djangoapps.discussion.django_comment_client.tests.group_id import ( |
| 41 | + CohortedTopicGroupIdTestMixin, |
| 42 | + GroupIdAssertionMixin, |
| 43 | + NonCohortedTopicGroupIdTestMixin, |
| 44 | +) |
| 45 | +from lms.djangoapps.discussion.django_comment_client.tests.unicode import ( |
| 46 | + UnicodeTestMixin, |
| 47 | +) |
| 48 | +from lms.djangoapps.discussion.django_comment_client.tests.utils import ( |
| 49 | + CohortedTestCase, |
| 50 | + ForumsEnableMixin, |
| 51 | +) |
| 52 | +from lms.djangoapps.teams.tests.factories import ( |
| 53 | + CourseTeamFactory, |
| 54 | + CourseTeamMembershipFactory, |
| 55 | +) |
| 56 | +from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted |
| 57 | +from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory |
| 58 | +from openedx.core.djangoapps.django_comment_common.comment_client import Thread |
| 59 | +from openedx.core.djangoapps.django_comment_common.models import ( |
| 60 | + FORUM_ROLE_STUDENT, |
| 61 | + CourseDiscussionSettings, |
| 62 | + Role, |
| 63 | + assign_role, |
| 64 | +) |
| 65 | +from openedx.core.djangoapps.django_comment_common.utils import ( |
| 66 | + ThreadContext, |
| 67 | + seed_permissions_roles, |
| 68 | +) |
| 69 | +from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES |
| 70 | +from openedx.core.lib.teams_config import TeamsConfig |
| 71 | +from xmodule.modulestore import ModuleStoreEnum |
| 72 | +from xmodule.modulestore.django import modulestore |
| 73 | +from xmodule.modulestore.tests.django_utils import ( |
| 74 | + TEST_DATA_SPLIT_MODULESTORE, |
| 75 | + ModuleStoreTestCase, |
| 76 | + SharedModuleStoreTestCase, |
| 77 | +) |
| 78 | +from xmodule.modulestore.tests.factories import ( |
| 79 | + CourseFactory, |
| 80 | + BlockFactory, |
| 81 | + check_mongo_calls, |
| 82 | +) |
| 83 | + |
| 84 | +from .event_transformers import ForumThreadViewedEventTransformer |
| 85 | +from lms.djangoapps.discussion.django_comment_client.tests.mixins import ( |
| 86 | + MockForumApiMixin, |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | +@disable_signal(views, "thread_edited") |
| 91 | +@disable_signal(views, "thread_voted") |
| 92 | +@disable_signal(views, "thread_deleted") |
| 93 | +class ThreadActionGroupIdTestCase( |
| 94 | + CohortedTestCase, GroupIdAssertionMixin, MockForumApiMixin |
| 95 | +): |
| 96 | + """Test case for thread actions with group ID assertions.""" |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def setUpClass(cls): |
| 100 | + """Set up class and forum mock.""" |
| 101 | + super().setUpClass() |
| 102 | + super().setUpClassAndForumMock() |
| 103 | + |
| 104 | + @classmethod |
| 105 | + def tearDownClass(cls): |
| 106 | + """Stop patches after tests complete.""" |
| 107 | + super().tearDownClass() |
| 108 | + super().disposeForumMocks() |
| 109 | + |
| 110 | + def call_view( |
| 111 | + self, view_name, mock_function, user=None, post_params=None, view_args=None |
| 112 | + ): |
| 113 | + """Call a view with the given parameters.""" |
| 114 | + thread_response = { |
| 115 | + "user_id": str(self.student.id), |
| 116 | + "group_id": self.student_cohort.id, |
| 117 | + "closed": False, |
| 118 | + "type": "thread", |
| 119 | + "commentable_id": "non_team_dummy_id", |
| 120 | + "body": "test body", |
| 121 | + } |
| 122 | + |
| 123 | + self.set_mock_return_value("get_course_id_by_thread", self.course.id) |
| 124 | + self.set_mock_return_value("get_thread", thread_response) |
| 125 | + self.set_mock_return_value(mock_function, thread_response) |
| 126 | + |
| 127 | + request = RequestFactory().post("dummy_url", post_params or {}) |
| 128 | + request.user = user or self.student |
| 129 | + request.view_name = view_name |
| 130 | + |
| 131 | + return getattr(views, view_name)( |
| 132 | + request, |
| 133 | + course_id=str(self.course.id), |
| 134 | + thread_id="dummy", |
| 135 | + **(view_args or {}) |
| 136 | + ) |
| 137 | + |
| 138 | + def test_pin_thread(self): |
| 139 | + """Test pinning a thread.""" |
| 140 | + response = self.call_view("pin_thread", "pin_thread", user=self.moderator) |
| 141 | + assert response.status_code == 200 |
| 142 | + self._assert_json_response_contains_group_info(response) |
| 143 | + |
| 144 | + response = self.call_view("un_pin_thread", "unpin_thread", user=self.moderator) |
| 145 | + assert response.status_code == 200 |
| 146 | + self._assert_json_response_contains_group_info(response) |
| 147 | + |
| 148 | + |
| 149 | +@disable_signal(views, "comment_endorsed") |
| 150 | +class ViewPermissionsTestCase( |
| 151 | + ForumsEnableMixin, |
| 152 | + UrlResetMixin, |
| 153 | + SharedModuleStoreTestCase, |
| 154 | + MockForumApiMixin, |
| 155 | +): |
| 156 | + """Test case for view permissions.""" |
| 157 | + |
| 158 | + @classmethod |
| 159 | + def setUpClass(cls): # pylint: disable=super-method-not-called |
| 160 | + """Set up class and forum mock.""" |
| 161 | + super().setUpClassAndForumMock() |
| 162 | + |
| 163 | + with super().setUpClassAndTestData(): |
| 164 | + cls.course = CourseFactory.create() |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def tearDownClass(cls): |
| 168 | + """Stop patches after tests complete.""" |
| 169 | + super().tearDownClass() |
| 170 | + super().disposeForumMocks() |
| 171 | + |
| 172 | + @classmethod |
| 173 | + def setUpTestData(cls): |
| 174 | + """Set up test data.""" |
| 175 | + super().setUpTestData() |
| 176 | + |
| 177 | + seed_permissions_roles(cls.course.id) |
| 178 | + |
| 179 | + cls.password = "test password" |
| 180 | + cls.student = UserFactory.create(password=cls.password) |
| 181 | + cls.moderator = UserFactory.create(password=cls.password) |
| 182 | + |
| 183 | + CourseEnrollmentFactory(user=cls.student, course_id=cls.course.id) |
| 184 | + CourseEnrollmentFactory(user=cls.moderator, course_id=cls.course.id) |
| 185 | + |
| 186 | + cls.moderator.roles.add( |
| 187 | + Role.objects.get(name="Moderator", course_id=cls.course.id) |
| 188 | + ) |
| 189 | + |
| 190 | + @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) |
| 191 | + def setUp(self): |
| 192 | + """Set up the test case.""" |
| 193 | + super().setUp() |
| 194 | + |
| 195 | + # Set return values dynamically using the mixin method |
| 196 | + self.set_mock_return_value("get_course_id_by_comment", self.course.id) |
| 197 | + self.set_mock_return_value("get_course_id_by_thread", self.course.id) |
| 198 | + self.set_mock_return_value("get_thread", {}) |
| 199 | + self.set_mock_return_value("pin_thread", {}) |
| 200 | + self.set_mock_return_value("unpin_thread", {}) |
| 201 | + |
| 202 | + def test_pin_thread_as_student(self): |
| 203 | + """Test pinning a thread as a student.""" |
| 204 | + self.client.login(username=self.student.username, password=self.password) |
| 205 | + response = self.client.post( |
| 206 | + reverse( |
| 207 | + "pin_thread", |
| 208 | + kwargs={"course_id": str(self.course.id), "thread_id": "dummy"}, |
| 209 | + ) |
| 210 | + ) |
| 211 | + assert response.status_code == 401 |
| 212 | + |
| 213 | + def test_pin_thread_as_moderator(self): |
| 214 | + """Test pinning a thread as a moderator.""" |
| 215 | + self.client.login(username=self.moderator.username, password=self.password) |
| 216 | + response = self.client.post( |
| 217 | + reverse( |
| 218 | + "pin_thread", |
| 219 | + kwargs={"course_id": str(self.course.id), "thread_id": "dummy"}, |
| 220 | + ) |
| 221 | + ) |
| 222 | + assert response.status_code == 200 |
| 223 | + |
| 224 | + def test_un_pin_thread_as_student(self): |
| 225 | + """Test unpinning a thread as a student.""" |
| 226 | + self.client.login(username=self.student.username, password=self.password) |
| 227 | + response = self.client.post( |
| 228 | + reverse( |
| 229 | + "un_pin_thread", |
| 230 | + kwargs={"course_id": str(self.course.id), "thread_id": "dummy"}, |
| 231 | + ) |
| 232 | + ) |
| 233 | + assert response.status_code == 401 |
| 234 | + |
| 235 | + def test_un_pin_thread_as_moderator(self): |
| 236 | + """Test unpinning a thread as a moderator.""" |
| 237 | + self.client.login(username=self.moderator.username, password=self.password) |
| 238 | + response = self.client.post( |
| 239 | + reverse( |
| 240 | + "un_pin_thread", |
| 241 | + kwargs={"course_id": str(self.course.id), "thread_id": "dummy"}, |
| 242 | + ) |
| 243 | + ) |
| 244 | + assert response.status_code == 200 |
0 commit comments