fix: fixed pylint warnings

This commit is contained in:
Usama Sadiq
2021-10-21 00:47:54 +05:00
committed by Kyle McCormick
parent 470be08a83
commit 95427251dc
16 changed files with 161 additions and 82 deletions

View File

@@ -10,7 +10,7 @@ from django.test.client import RequestFactory, Client
from django.test.utils import override_settings
from django.urls import reverse
from django.utils import timezone
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from unittest.mock import patch, Mock
from pyquery import PyQuery as pq
@@ -51,6 +51,7 @@ METADATA = {
CONTENT_GATING_PARTITION_ID: [CONTENT_TYPE_GATE_GROUP_IDS['full_access']]
}
}
User = get_user_model()
@patch("crum.get_current_request")
@@ -113,7 +114,7 @@ def _assert_block_is_gated(block, is_gated, user, course, request_factory, has_u
checkout_link = '#' if has_upgrade_link else None
for content_getter in (_get_content_from_fragment, _get_content_from_lms_index):
with patch.object(ContentTypeGatingPartition, '_get_checkout_link', return_value=checkout_link):
content = content_getter(block, user.id, course, request_factory)
content = content_getter(block, user.id, course, request_factory) # pylint: disable=no-value-for-parameter
if is_gated:
assert 'content-paywall' in content
if has_upgrade_link:
@@ -160,7 +161,7 @@ def _assert_block_is_empty(block, user_id, course, request_factory):
@override_settings(FIELD_OVERRIDE_PROVIDERS=(
'openedx.features.content_type_gating.field_override.ContentTypeGatingFieldOverride',
))
class TestProblemTypeAccess(SharedModuleStoreTestCase, MasqueradeMixin):
class TestProblemTypeAccess(SharedModuleStoreTestCase, MasqueradeMixin): # pylint: disable=missing-class-docstring
PROBLEM_TYPES = ['problem', 'openassessment', 'drag-and-drop-v2', 'done', 'edx_sga']
# 'html' is a component that just displays html, in these tests it is used to test that users who do not have access
@@ -800,9 +801,13 @@ class TestConditionalContentAccess(TestConditionalContent):
self.student_audit_b = self.student_b
# Create verified students
self.student_verified_a = UserFactory.create(username='student_verified_a', email='student_verified_a@example.com')
self.student_verified_a = UserFactory.create(
username='student_verified_a', email='student_verified_a@example.com'
)
CourseEnrollmentFactory.create(user=self.student_verified_a, course_id=self.course.id, mode='verified')
self.student_verified_b = UserFactory.create(username='student_verified_b', email='student_verified_b@example.com')
self.student_verified_b = UserFactory.create(
username='student_verified_b', email='student_verified_b@example.com'
)
CourseEnrollmentFactory.create(user=self.student_verified_b, course_id=self.course.id, mode='verified')
# Put students into content gating groups
@@ -832,7 +837,8 @@ class TestConditionalContentAccess(TestConditionalContent):
def test_access_based_on_conditional_content(self):
"""
If a user is enrolled as an audit user they should not have access to graded problems, including conditional content.
If a user is enrolled as an audit user they should not have access to graded problems,
including conditional content.
All paid type tracks should have access graded problems including conditional content.
"""
@@ -892,7 +898,7 @@ class TestMessageDeduplication(ModuleStoreTestCase):
self.request_factory = RequestFactory()
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1))
def _create_course(self):
def _create_course(self): # pylint: disable=missing-function-docstring
course = CourseFactory.create(run='test', display_name='test')
CourseModeFactory.create(course_id=course.id, mode_slug='audit')
CourseModeFactory.create(course_id=course.id, mode_slug='verified')
@@ -1101,7 +1107,7 @@ class TestContentTypeGatingService(ModuleStoreTestCase):
self.request_factory = RequestFactory()
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1))
def _create_course(self):
def _create_course(self): # pylint: disable=missing-function-docstring
course = CourseFactory.create(run='test', display_name='test')
CourseModeFactory.create(course_id=course.id, mode_slug='audit')
CourseModeFactory.create(course_id=course.id, mode_slug='verified')
@@ -1150,10 +1156,14 @@ class TestContentTypeGatingService(ModuleStoreTestCase):
)
# The method returns a content type gate for blocks that should be gated
assert 'content-paywall' in ContentTypeGatingService()._content_type_gate_for_block(self.user, blocks_dict['graded_1'], course['course'].id).content
assert 'content-paywall' in ContentTypeGatingService()._content_type_gate_for_block( # pylint: disable=protected-access
self.user, blocks_dict['graded_1'], course['course'].id
).content
# The method returns None for blocks that should not be gated
assert ContentTypeGatingService()._content_type_gate_for_block(self.user, blocks_dict['not_graded_1'], course['course'].id) is None
assert ContentTypeGatingService()._content_type_gate_for_block( # pylint: disable=protected-access
self.user, blocks_dict['not_graded_1'], course['course'].id
) is None
@patch.object(ContentTypeGatingService, '_get_user', return_value=UserFactory.build())
def test_check_children_for_content_type_gating_paywall(self, mocked_user): # pylint: disable=unused-argument
@@ -1173,7 +1183,9 @@ class TestContentTypeGatingService(ModuleStoreTestCase):
)
# The method returns a content type gate for blocks that should be gated
assert ContentTypeGatingService().check_children_for_content_type_gating_paywall(blocks_dict['vertical'], course['course'].id) is None
assert ContentTypeGatingService().check_children_for_content_type_gating_paywall(
blocks_dict['vertical'], course['course'].id
) is None
blocks_dict['graded_1'] = ItemFactory.create(
parent=blocks_dict['vertical'],
@@ -1183,4 +1195,6 @@ class TestContentTypeGatingService(ModuleStoreTestCase):
)
# The method returns None for blocks that should not be gated
assert 'content-paywall' in ContentTypeGatingService().check_children_for_content_type_gating_paywall(blocks_dict['vertical'], course['course'].id)
assert 'content-paywall' in ContentTypeGatingService().check_children_for_content_type_gating_paywall(
blocks_dict['vertical'], course['course'].id
)

View File

@@ -1,3 +1,4 @@
# pylint: disable=missing-module-docstring
import pytest
@@ -21,7 +22,7 @@ from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, U
@ddt.ddt
class TestContentTypeGatingConfig(CacheIsolationTestCase):
class TestContentTypeGatingConfig(CacheIsolationTestCase): # pylint: disable=missing-class-docstring
ENABLED_CACHES = ['default']
@@ -107,7 +108,9 @@ class TestContentTypeGatingConfig(CacheIsolationTestCase):
course_key = self.course_overview.id
assert (not before_enabled) == ContentTypeGatingConfig.enabled_for_course(course_key=course_key, target_datetime=target_datetime)
assert (not before_enabled) == ContentTypeGatingConfig.enabled_for_course(
course_key=course_key, target_datetime=target_datetime
)
@ddt.data(
# Generate all combinations of setting each configuration level to True/False/None
@@ -132,11 +135,17 @@ class TestContentTypeGatingConfig(CacheIsolationTestCase):
site_values={'course_org_filter': non_test_course_disabled.org}
)
ContentTypeGatingConfig.objects.create(course=non_test_course_enabled, enabled=True, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
course=non_test_course_enabled, enabled=True, enabled_as_of=datetime(2018, 1, 1)
)
ContentTypeGatingConfig.objects.create(course=non_test_course_disabled, enabled=False)
ContentTypeGatingConfig.objects.create(org=non_test_course_enabled.org, enabled=True, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
org=non_test_course_enabled.org, enabled=True, enabled_as_of=datetime(2018, 1, 1)
)
ContentTypeGatingConfig.objects.create(org=non_test_course_disabled.org, enabled=False)
ContentTypeGatingConfig.objects.create(site=non_test_site_cfg_enabled.site, enabled=True, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
site=non_test_site_cfg_enabled.site, enabled=True, enabled_as_of=datetime(2018, 1, 1)
)
ContentTypeGatingConfig.objects.create(site=non_test_site_cfg_disabled.site, enabled=False)
# Set up test objects
@@ -146,9 +155,15 @@ class TestContentTypeGatingConfig(CacheIsolationTestCase):
)
ContentTypeGatingConfig.objects.create(enabled=global_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(course=test_course, enabled=course_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(org=test_course.org, enabled=org_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(site=test_site_cfg.site, enabled=site_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
course=test_course, enabled=course_setting, enabled_as_of=datetime(2018, 1, 1)
)
ContentTypeGatingConfig.objects.create(
org=test_course.org, enabled=org_setting, enabled_as_of=datetime(2018, 1, 1)
)
ContentTypeGatingConfig.objects.create(
site=test_site_cfg.site, enabled=site_setting, enabled_as_of=datetime(2018, 1, 1)
)
all_settings = [global_setting, site_setting, org_setting, course_setting]
expected_global_setting = self._resolve_settings([global_setting])
@@ -169,21 +184,27 @@ class TestContentTypeGatingConfig(CacheIsolationTestCase):
test_site_cfg = SiteConfigurationFactory.create(
site_values={'course_org_filter': []}
)
ContentTypeGatingConfig.objects.create(site=test_site_cfg.site, enabled=site_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
site=test_site_cfg.site, enabled=site_setting, enabled_as_of=datetime(2018, 1, 1)
)
for org_setting in (True, False, None):
test_org = f"{test_site_cfg.id}-{org_setting}"
test_site_cfg.site_values['course_org_filter'].append(test_org)
test_site_cfg.save()
ContentTypeGatingConfig.objects.create(org=test_org, enabled=org_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
org=test_org, enabled=org_setting, enabled_as_of=datetime(2018, 1, 1)
)
for course_setting in (True, False, None):
test_course = CourseOverviewFactory.create(
org=test_org,
id=CourseLocator(test_org, 'test_course', f'run-{course_setting}')
)
ContentTypeGatingConfig.objects.create(course=test_course, enabled=course_setting, enabled_as_of=datetime(2018, 1, 1))
ContentTypeGatingConfig.objects.create(
course=test_course, enabled=course_setting, enabled_as_of=datetime(2018, 1, 1)
)
with self.assertNumQueries(4):
all_configs = ContentTypeGatingConfig.all_current_course_configs()
@@ -194,9 +215,21 @@ class TestContentTypeGatingConfig(CacheIsolationTestCase):
assert len(all_configs) == ((3 ** 4) + 1)
# Point-test some of the final configurations
assert all_configs[CourseLocator('7-True', 'test_course', 'run-None')] == {'enabled': (True, Provenance.org), 'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run), 'studio_override_enabled': (None, Provenance.default)}
assert all_configs[CourseLocator('7-True', 'test_course', 'run-False')] == {'enabled': (False, Provenance.run), 'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run), 'studio_override_enabled': (None, Provenance.default)}
assert all_configs[CourseLocator('7-None', 'test_course', 'run-None')] == {'enabled': (True, Provenance.site), 'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run), 'studio_override_enabled': (None, Provenance.default)}
assert all_configs[CourseLocator('7-True', 'test_course', 'run-None')] == {
'enabled': (True, Provenance.org),
'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run),
'studio_override_enabled': (None, Provenance.default)
}
assert all_configs[CourseLocator('7-True', 'test_course', 'run-False')] == {
'enabled': (False, Provenance.run),
'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run),
'studio_override_enabled': (None, Provenance.default)
}
assert all_configs[CourseLocator('7-None', 'test_course', 'run-None')] == {
'enabled': (True, Provenance.site),
'enabled_as_of': (datetime(2018, 1, 1, 0, tzinfo=pytz.UTC), Provenance.run),
'studio_override_enabled': (None, Provenance.default)
}
def test_caching_global(self):
global_config = ContentTypeGatingConfig(enabled=True, enabled_as_of=datetime(2018, 1, 1))

View File

@@ -1,3 +1,4 @@
# pylint: disable=missing-module-docstring
from datetime import datetime
from django.test import RequestFactory
from unittest.mock import Mock, patch
@@ -13,8 +14,8 @@ from openedx.core.djangoapps.content.course_overviews.tests.factories import Cou
from xmodule.partitions.partitions import UserPartitionError
class TestContentTypeGatingPartition(CacheIsolationTestCase):
def setUp(self):
class TestContentTypeGatingPartition(CacheIsolationTestCase): # pylint: disable=missing-class-docstring
def setUp(self): # pylint: disable=super-method-not-called
self.course_key = CourseKey.from_string('course-v1:test+course+key')
CourseOverviewFactory.create(id=self.course_key)
@@ -25,7 +26,9 @@ class TestContentTypeGatingPartition(CacheIsolationTestCase):
CourseModeFactory.create(course_id=mock_course.id, mode_slug='verified')
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1))
with patch('openedx.features.content_type_gating.partitions.ContentTypeGatingPartitionScheme.create_user_partition') as mock_create:
with patch(
'openedx.features.content_type_gating.partitions.ContentTypeGatingPartitionScheme.create_user_partition'
) as mock_create:
partition = create_content_gating_partition(mock_course)
assert partition == mock_create.return_value
@@ -47,13 +50,17 @@ class TestContentTypeGatingPartition(CacheIsolationTestCase):
mock_course = Mock(id=self.course_key, user_partitions={})
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1))
with patch('openedx.features.content_type_gating.partitions.UserPartition.get_scheme', side_effect=UserPartitionError):
with patch(
'openedx.features.content_type_gating.partitions.UserPartition.get_scheme', side_effect=UserPartitionError
):
partition = create_content_gating_partition(mock_course)
assert partition is None
def test_create_content_gating_partition_partition_id_used(self):
mock_course = Mock(id=self.course_key, user_partitions={Mock(name='partition', id=CONTENT_GATING_PARTITION_ID): object()})
mock_course = Mock(
id=self.course_key, user_partitions={Mock(name='partition', id=CONTENT_GATING_PARTITION_ID): object()}
)
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1))
with patch('openedx.features.content_type_gating.partitions.LOG') as mock_log:
@@ -111,7 +118,9 @@ class TestContentTypeGatingPartition(CacheIsolationTestCase):
):
fragment = partition.access_denied_fragment(mock_block, global_staff, FULL_ACCESS, 'test_allowed_group')
assert fragment is None
message = partition.access_denied_message(mock_block.scope_ids.usage_id, global_staff, FULL_ACCESS, 'test_allowed_group')
message = partition.access_denied_message(
mock_block.scope_ids.usage_id, global_staff, FULL_ACCESS, 'test_allowed_group'
)
assert message is None
def test_access_denied_fragment_for_null_request(self):

View File

@@ -6,7 +6,6 @@ import ddt
from django.urls import reverse
from django.utils import timezone
from unittest.mock import patch
from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
@@ -14,7 +13,6 @@ from common.djangoapps.util.testing import EventTestMixin
from lms.djangoapps.courseware.tests.helpers import MasqueradeMixin
from lms.djangoapps.course_home_api.tests.utils import BaseCourseHomeTests
from openedx.core.djangoapps.schedules.models import Schedule
from openedx.core.djangoapps.schedules.tests.factories import ScheduleFactory
from xmodule.modulestore.tests.factories import CourseFactory
@@ -23,7 +21,7 @@ class ResetCourseDeadlinesViewTests(EventTestMixin, BaseCourseHomeTests, Masquer
"""
Tests for reset deadlines endpoint.
"""
def setUp(self):
def setUp(self): # pylint: disable=arguments-differ
# Need to supply tracker name for the EventTestMixin. Also, EventTestMixin needs to come
# first in class inheritance so the setUp call here appropriately works
super().setUp('openedx.features.course_experience.api.v1.views.tracker')