INCR-306 Run python-modernize on lms/djangoapps/discussion/settings and lms/djangoapps/discussion/tests (#20619)
* run python modernize * run isort * Fix quality
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
from django.test import TestCase
|
||||
import mock
|
||||
"""
|
||||
Tests the forum notification signals.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import mock
|
||||
from django.test import TestCase
|
||||
from edx_django_utils.cache import RequestCache
|
||||
|
||||
from lms.djangoapps.discussion.signals.handlers import ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY
|
||||
from openedx.core.djangoapps.django_comment_common import signals, models
|
||||
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory, SiteConfigurationFactory
|
||||
from openedx.core.djangoapps.django_comment_common import models, signals
|
||||
from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory, SiteFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
|
||||
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
"""
|
||||
Tests the execution of forum notification tasks.
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
from __future__ import absolute_import
|
||||
|
||||
import json
|
||||
import math
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import ddt
|
||||
from django.contrib.sites.models import Site
|
||||
import mock
|
||||
|
||||
import six
|
||||
from django.contrib.sites.models import Site
|
||||
from edx_ace.channel import ChannelType, get_channel_for_message
|
||||
from edx_ace.recipient import Recipient
|
||||
from edx_ace.renderers import EmailRenderer
|
||||
from edx_ace.channel import ChannelType, get_channel_for_message
|
||||
from edx_ace.utils import date
|
||||
|
||||
import openedx.core.djangoapps.django_comment_common.comment_client as cc
|
||||
from lms.djangoapps.discussion.signals.handlers import ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY
|
||||
from lms.djangoapps.discussion.tasks import _should_send_message, _track_notification_sent
|
||||
from openedx.core.djangoapps.ace_common.template_context import get_base_template_context
|
||||
from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory
|
||||
import openedx.core.djangoapps.django_comment_common.comment_client as cc
|
||||
from openedx.core.djangoapps.django_comment_common.models import ForumsConfig
|
||||
from openedx.core.djangoapps.django_comment_common.signals import comment_created
|
||||
from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory
|
||||
@@ -25,7 +28,6 @@ from openedx.core.lib.celery.task_utils import emulate_http_request
|
||||
from student.tests.factories import CourseEnrollmentFactory, UserFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
|
||||
|
||||
NOW = datetime.utcnow()
|
||||
ONE_HOUR_AGO = NOW - timedelta(hours=1)
|
||||
TWO_HOURS_AGO = NOW - timedelta(hours=2)
|
||||
@@ -107,7 +109,7 @@ class TaskTestCase(ModuleStoreTestCase):
|
||||
def create_thread_and_comments(cls):
|
||||
cls.thread = {
|
||||
'id': cls.discussion_id,
|
||||
'course_id': unicode(cls.course.id),
|
||||
'course_id': six.text_type(cls.course.id),
|
||||
'created_at': date.serialize(TWO_HOURS_AGO),
|
||||
'title': 'thread-title',
|
||||
'user_id': cls.thread_author.id,
|
||||
@@ -145,7 +147,7 @@ class TaskTestCase(ModuleStoreTestCase):
|
||||
cls.comment['child_count'] = 1
|
||||
cls.thread2 = {
|
||||
'id': cls.discussion_id,
|
||||
'course_id': unicode(cls.course.id),
|
||||
'course_id': six.text_type(cls.course.id),
|
||||
'created_at': date.serialize(TWO_HOURS_AGO),
|
||||
'title': 'thread-title',
|
||||
'user_id': cls.thread_author.id,
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
"""
|
||||
Tests the forum notification views.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import ddt
|
||||
from django.urls import reverse
|
||||
import six
|
||||
from django.http import Http404
|
||||
from django.test.client import Client, RequestFactory
|
||||
from django.test.utils import override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import translation
|
||||
from edx_django_utils.cache import RequestCache
|
||||
from mock import ANY, Mock, call, patch
|
||||
from six import text_type
|
||||
from six.moves import range
|
||||
|
||||
from course_modes.models import CourseMode
|
||||
from course_modes.tests.factories import CourseModeFactory
|
||||
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
|
||||
from lms.djangoapps.discussion import views
|
||||
from lms.djangoapps.discussion.django_comment_client.constants import TYPE_ENTRY, TYPE_SUBCATEGORY
|
||||
from lms.djangoapps.discussion.django_comment_client.permissions import get_team
|
||||
from lms.djangoapps.discussion.django_comment_client.tests.group_id import (
|
||||
@@ -29,19 +38,16 @@ from lms.djangoapps.discussion.django_comment_client.tests.utils import (
|
||||
topic_name_to_id
|
||||
)
|
||||
from lms.djangoapps.discussion.django_comment_client.utils import strip_none
|
||||
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
|
||||
from lms.djangoapps.discussion import views
|
||||
from lms.djangoapps.discussion.views import _get_discussion_default_topic_id
|
||||
from lms.djangoapps.discussion.views import course_discussions_settings_handler
|
||||
from lms.djangoapps.discussion.views import _get_discussion_default_topic_id, course_discussions_settings_handler
|
||||
from lms.djangoapps.teams.tests.factories import CourseTeamFactory, CourseTeamMembershipFactory
|
||||
from openedx.core.djangoapps.course_groups.models import CourseUserGroup
|
||||
from openedx.core.djangoapps.course_groups.tests.helpers import config_course_cohorts
|
||||
from openedx.core.djangoapps.course_groups.tests.test_views import CohortViewsTestCase
|
||||
from openedx.core.djangoapps.django_comment_common.comment_client.utils import CommentClientPaginatedResult
|
||||
from openedx.core.djangoapps.django_comment_common.models import (
|
||||
CourseDiscussionSettings,
|
||||
ForumsConfig,
|
||||
FORUM_ROLE_STUDENT,
|
||||
CourseDiscussionSettings,
|
||||
ForumsConfig
|
||||
)
|
||||
from openedx.core.djangoapps.django_comment_common.utils import ThreadContext, seed_permissions_roles
|
||||
from openedx.core.djangoapps.util.testing import ContentGroupTestCase
|
||||
@@ -292,7 +298,7 @@ class PartialDictMatcher(object):
|
||||
def __eq__(self, other):
|
||||
return all([
|
||||
key in other and other[key] == value
|
||||
for key, value in self.expected_values.iteritems()
|
||||
for key, value in six.iteritems(self.expected_values)
|
||||
])
|
||||
|
||||
|
||||
@@ -545,7 +551,7 @@ class SingleCohortedThreadTestCase(CohortedTestCase):
|
||||
self.client.login(username=self.student.username, password='test')
|
||||
response = self.client.get(
|
||||
reverse('single_thread', kwargs={
|
||||
'course_id': unicode(self.course.id),
|
||||
'course_id': six.text_type(self.course.id),
|
||||
'discussion_id': "cohorted_topic",
|
||||
'thread_id': self.mock_thread_id,
|
||||
})
|
||||
@@ -666,7 +672,7 @@ class SingleThreadGroupIdTestCase(CohortedTestCase, GroupIdAssertionMixin):
|
||||
self.client.login(username=user.username, password='test')
|
||||
|
||||
return self.client.get(
|
||||
reverse('single_thread', args=[unicode(self.course.id), commentable_id, "dummy_thread_id"]),
|
||||
reverse('single_thread', args=[six.text_type(self.course.id), commentable_id, "dummy_thread_id"]),
|
||||
data=request_data,
|
||||
**headers
|
||||
)
|
||||
@@ -729,7 +735,7 @@ class ForumFormDiscussionContentGroupTestCase(ForumsEnableMixin, ContentGroupTes
|
||||
)
|
||||
self.client.login(username=user.username, password='test')
|
||||
return self.client.get(
|
||||
reverse("forum_form_discussion", args=[unicode(self.course.id)]),
|
||||
reverse("forum_form_discussion", args=[six.text_type(self.course.id)]),
|
||||
HTTP_X_REQUESTED_WITH="XMLHttpRequest"
|
||||
)
|
||||
|
||||
@@ -794,7 +800,7 @@ class SingleThreadContentGroupTestCase(ForumsEnableMixin, UrlResetMixin, Content
|
||||
def call_single_thread():
|
||||
self.client.login(username=user.username, password='test')
|
||||
return self.client.get(
|
||||
reverse('single_thread', args=[unicode(self.course.id), discussion_id, thread_id])
|
||||
reverse('single_thread', args=[six.text_type(self.course.id), discussion_id, thread_id])
|
||||
)
|
||||
|
||||
if should_have_access:
|
||||
@@ -917,7 +923,7 @@ class InlineDiscussionContextTestCase(ForumsEnableMixin, ModuleStoreTestCase):
|
||||
|
||||
response = views.inline_discussion(
|
||||
request,
|
||||
unicode(self.course.id),
|
||||
six.text_type(self.course.id),
|
||||
self.discussion_topic_id,
|
||||
)
|
||||
|
||||
@@ -995,7 +1001,7 @@ class ForumFormDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdT
|
||||
|
||||
self.client.login(username=user.username, password='test')
|
||||
return self.client.get(
|
||||
reverse("forum_form_discussion", args=[unicode(self.course.id)]),
|
||||
reverse("forum_form_discussion", args=[six.text_type(self.course.id)]),
|
||||
data=request_data,
|
||||
**headers
|
||||
)
|
||||
@@ -1047,7 +1053,7 @@ class UserProfileDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupI
|
||||
|
||||
self.client.login(username=requesting_user.username, password='test')
|
||||
return self.client.get(
|
||||
reverse('user_profile', args=[unicode(self.course.id), profiled_user.id]),
|
||||
reverse('user_profile', args=[six.text_type(self.course.id), profiled_user.id]),
|
||||
data=request_data,
|
||||
**headers
|
||||
)
|
||||
@@ -1290,7 +1296,7 @@ class UserProfileTestCase(ForumsEnableMixin, UrlResetMixin, ModuleStoreTestCase)
|
||||
|
||||
response = self.client.get(
|
||||
reverse('user_profile', kwargs={
|
||||
'course_id': unicode(self.course.id),
|
||||
'course_id': six.text_type(self.course.id),
|
||||
'user_id': self.profiled_user.id,
|
||||
}),
|
||||
data=params,
|
||||
@@ -1552,7 +1558,7 @@ class ForumDiscussionXSSTestCase(ForumsEnableMixin, UrlResetMixin, ModuleStoreTe
|
||||
mock_user.return_value.to_dict.return_value = {}
|
||||
reverse_url = "%s%s" % (reverse(
|
||||
"forum_form_discussion",
|
||||
kwargs={"course_id": unicode(self.course.id)}), '/forum_form_discussion')
|
||||
kwargs={"course_id": six.text_type(self.course.id)}), '/forum_form_discussion')
|
||||
# Test that malicious code does not appear in html
|
||||
url = "%s?%s=%s" % (reverse_url, 'sort_key', malicious_code)
|
||||
resp = self.client.get(url)
|
||||
@@ -1575,7 +1581,7 @@ class ForumDiscussionXSSTestCase(ForumsEnableMixin, UrlResetMixin, ModuleStoreTe
|
||||
mock_request.side_effect = make_mock_request_impl(course=self.course, text='dummy')
|
||||
|
||||
url = reverse('user_profile',
|
||||
kwargs={'course_id': unicode(self.course.id), 'user_id': str(self.student.id)})
|
||||
kwargs={'course_id': six.text_type(self.course.id), 'user_id': str(self.student.id)})
|
||||
# Test that malicious code does not appear in html
|
||||
url_string = "%s?%s=%s" % (url, 'page', malicious_code)
|
||||
resp = self.client.get(url_string)
|
||||
@@ -1772,7 +1778,7 @@ class EnterpriseConsentTestCase(EnterpriseTestConsentRequired, ForumsEnableMixin
|
||||
mock_enterprise_customer_for_request.return_value = None
|
||||
|
||||
thread_id = 'dummy'
|
||||
course_id = unicode(self.course.id)
|
||||
course_id = six.text_type(self.course.id)
|
||||
mock_request.side_effect = make_mock_request_impl(course=self.course, text='dummy', thread_id=thread_id)
|
||||
|
||||
for url in (
|
||||
@@ -1830,7 +1836,7 @@ class CourseDiscussionTopicsTestCase(DividedDiscussionsTestCase):
|
||||
"""
|
||||
Verify that we cannot access divide_discussion_topics if we're a non-staff user.
|
||||
"""
|
||||
self._verify_non_staff_cannot_access(views.discussion_topics, "GET", [unicode(self.course.id)])
|
||||
self._verify_non_staff_cannot_access(views.discussion_topics, "GET", [six.text_type(self.course.id)])
|
||||
|
||||
def test_get_discussion_topics(self):
|
||||
"""
|
||||
@@ -1899,10 +1905,10 @@ class CourseDiscussionsHandlerTestCase(DividedDiscussionsTestCase):
|
||||
Verify that we cannot access course_discussions_settings_handler if we're a non-staff user.
|
||||
"""
|
||||
self._verify_non_staff_cannot_access(
|
||||
course_discussions_settings_handler, "GET", [unicode(self.course.id)]
|
||||
course_discussions_settings_handler, "GET", [six.text_type(self.course.id)]
|
||||
)
|
||||
self._verify_non_staff_cannot_access(
|
||||
course_discussions_settings_handler, "PATCH", [unicode(self.course.id)]
|
||||
course_discussions_settings_handler, "PATCH", [six.text_type(self.course.id)]
|
||||
)
|
||||
|
||||
def test_update_always_divide_inline_discussion_settings(self):
|
||||
@@ -2120,7 +2126,7 @@ class ThreadViewedEventTestCase(EventTestMixin, ForumsEnableMixin, UrlResetMixin
|
||||
commentable_id=self.category.discussion_id,
|
||||
)
|
||||
url = '/courses/{0}/discussion/forum/{1}/threads/{2}'.format(
|
||||
unicode(self.course.id),
|
||||
six.text_type(self.course.id),
|
||||
self.category.discussion_id,
|
||||
self.DUMMY_THREAD_ID
|
||||
)
|
||||
@@ -2138,9 +2144,9 @@ class ThreadViewedEventTestCase(EventTestMixin, ForumsEnableMixin, UrlResetMixin
|
||||
'team_id': self.team.id,
|
||||
'url': self.DUMMY_URL,
|
||||
}
|
||||
expected_event_items = expected_event.items()
|
||||
expected_event_items = list(expected_event.items())
|
||||
|
||||
self.assert_event_emission_count('edx.forum.thread.viewed', 1)
|
||||
_, event = self.get_latest_call_args()
|
||||
event_items = event.items()
|
||||
event_items = list(event.items())
|
||||
self.assertTrue(kv_pair in event_items for kv_pair in expected_event_items)
|
||||
|
||||
Reference in New Issue
Block a user