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/courseware/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from courseware.masquerade import get_masquerade_role, is_masquerading_as_student
from lms.djangoapps.ccx.custom_exception import CCXLocatorValidationException
from lms.djangoapps.ccx.models import CustomCourseForEdX
from lms.lib.access_control_backends import access_control_backends
from lms.djangoapps.courseware.access_control_backends import access_control_backends
from mobile_api.models import IgnoreMobileAvailableFlagConfig
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.external_auth.models import ExternalAuthMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def query(self, action, user, resource, default_has_access):
:param action: currently supporting the course access actions in SUPPORTED_ACTIONS.
:param user: The User model object.
:param resource: The course/resource ID.
:param default_has_access: True/False What's the default Open edX access control.
:return: True/False whether the `user` can perform the `action` on the `resource` or not.
:param default_has_access: AccessResponse The default access response object by Open edX.
:return: AccessResponse: ACCESS_GRANTED or ACCESS_DENIED whether the
`user` can perform the `action` on the `resource` or not.
"""
if action not in self.SUPPORTED_ACTIONS:
raise NotImplementedError(self.UNSUPPORTED_ERROR_FMT(action=action))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
"""
Test cases for the pluggable access control system.
"""
import datetime
import ddt
from mock import patch, Mock
import pytest
import pytz

from django.conf import settings
from django.test.utils import override_settings
from django.test import TestCase
from mock import patch, Mock
from opaque_keys.edx.locator import CourseLocator

from lms.lib.access_control_backends import AccessControlBackends
from lms.djangoapps.courseware.access_utils import (
ACCESS_DENIED,
ACCESS_GRANTED,
)
import lms.djangoapps.courseware.access as access
from lms.djangoapps.courseware.access_control_backends import (
access_control_backends,
AccessControlBackends,
)
from student.tests.factories import CourseEnrollmentAllowedFactory, UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase


@ddt.ddt
Expand Down Expand Up @@ -93,7 +106,7 @@ def test_settings_with_options(self, mock_backend):
'NAME': 'lms.lib:see_in_catalog_backend',
}
})
@patch('lms.lib.access_control_backends.log')
@patch('lms.djangoapps.courseware.access_control_backends.log')
def test_settings_with_missing_function(self, mock_log):
"""
Check that the system fails explicitly on a missing function.
Expand Down Expand Up @@ -159,11 +172,14 @@ def test_query_missing_backend(self, default_has_access):

@override_settings(ACCESS_CONTROL_BACKENDS={
'course.load': {
'NAME': 'lms.lib:load_backend',
'NAME': 'lms.djangoapps.courseware.access_control_backends:load_backend',
}
})
@patch('lms.lib.load_backend', Mock(side_effect=ArithmeticError('Dividing by zero!')), create=True)
@patch('lms.lib.access_control_backends.log')
@patch('lms.djangoapps.courseware.access_control_backends.load_backend', Mock(
side_effect=ArithmeticError('Dividing by zero!')),
create=True,
)
@patch('lms.djangoapps.courseware.access_control_backends.log')
def test_query_broken_backend(self, mock_log):
"""
Ensure a broken backend fails explicitly.
Expand All @@ -175,3 +191,38 @@ def test_query_broken_backend(self, mock_log):
mock_log.exception.assert_called_once_with(
'Something went wrong in querying the access control backend for `course.load`.'
)


@ddt.ddt
class AccessWithACLBackendsTestCase(ModuleStoreTestCase):
"""
Integration tests for `access._has_access_course`.
"""

def setUp(self):
"""
Set up tests environment.
"""
tomorrow = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=1)
self.user = UserFactory.create()
self.course = Mock(
enrollment_domain='',
enrollment_end=tomorrow,
enrollment_start=tomorrow,
id=CourseLocator('edX', 'test', '2012_Fall'),
)
CourseEnrollmentAllowedFactory(email=self.user.email, course_id=self.course.id)

def test_has_access_with_no_acl_backends(self):
"""
Ensure that the `access._has_access_course` queries the Access Control Backends.
"""
assert access._has_access_course(self.user, 'enroll', self.course).has_access

@ddt.data(ACCESS_GRANTED, ACCESS_DENIED)
def test_has_access_with_acl_backends(self, backend_access):
"""
Ensure that the `access._has_access_course` queries the Access Control Backends.
"""
with patch.object(access_control_backends, 'query', Mock(return_value=backend_access)):
assert access._has_access_course(self.user, 'enroll', self.course) == backend_access

This file was deleted.

3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ commands =
lms/djangoapps/course_api/ \
lms/djangoapps/course_blocks/transformers/tests/test_load_override_data.py \
lms/djangoapps/courseware/tests/test_access.py \
lms/djangoapps/courseware/tests/test_access_control_backends_integration.py \
lms/djangoapps/courseware/tests/test_access_control_backends.py \
lms/djangoapps/grades/tests/integration/test_events.py \
lms/djangoapps/instructor/tests/test_certificates.py::CertificatesInstructorApiTest \
lms/djangoapps/verify_student/tests/test_services.py \
lms/lib/tests/test_access_control_backends.py \
openedx/core/djangoapps/appsembler \
openedx/core/djangoapps/site_configuration/tests/test_tahoe_changes.py \
openedx/core/djangoapps/user_api/accounts/tests/test_utils.py::CompletionUtilsTestCase
Expand Down