Implement user service to return currently-logged-in user
returns XBlockuser with 2 data layers
This commit is contained in:
committed by
Ned Batchelder
parent
dd18a8e618
commit
7f1b60b286
@@ -24,6 +24,7 @@ from xblock.django.request import webob_to_django_response, django_to_webob_requ
|
||||
from xblock.exceptions import NoSuchHandlerError
|
||||
from xblock.fragment import Fragment
|
||||
from student.auth import has_studio_read_access, has_studio_write_access
|
||||
from xblock_django.user_service import DjangoXBlockUserService
|
||||
|
||||
from lms.djangoapps.lms_xblock.field_data import LmsFieldData
|
||||
from cms.lib.xblock.field_data import CmsFieldData
|
||||
@@ -112,20 +113,6 @@ class PreviewModuleSystem(ModuleSystem): # pylint: disable=abstract-method
|
||||
]
|
||||
|
||||
|
||||
class StudioUserService(object):
|
||||
"""
|
||||
Provides a Studio implementation of the XBlock user service.
|
||||
"""
|
||||
|
||||
def __init__(self, request):
|
||||
super(StudioUserService, self).__init__()
|
||||
self._request = request
|
||||
|
||||
@property
|
||||
def user_id(self):
|
||||
return self._request.user.id
|
||||
|
||||
|
||||
class StudioPermissionsService(object):
|
||||
"""
|
||||
Service that can provide information about a user's permissions.
|
||||
@@ -176,7 +163,6 @@ def _preview_module_system(request, descriptor, field_data):
|
||||
_studio_wrap_xblock,
|
||||
]
|
||||
|
||||
descriptor.runtime._services['user'] = StudioUserService(request) # pylint: disable=protected-access
|
||||
descriptor.runtime._services['studio_user_permissions'] = StudioPermissionsService(request) # pylint: disable=protected-access
|
||||
|
||||
return PreviewModuleSystem(
|
||||
@@ -204,6 +190,7 @@ def _preview_module_system(request, descriptor, field_data):
|
||||
"i18n": ModuleI18nService(),
|
||||
"field-data": field_data,
|
||||
"library_tools": LibraryToolsService(modulestore()),
|
||||
"user": DjangoXBlockUserService(request.user),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
from django.core.urlresolvers import reverse
|
||||
from contentstore.utils import reverse_usage_url, reverse_course_url
|
||||
from contentstore.views.preview import StudioUserService
|
||||
|
||||
from contentstore.views.component import (
|
||||
component_handler, get_component_templates
|
||||
@@ -30,6 +29,7 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import ItemFactory, LibraryFactory, check_mongo_calls
|
||||
from xmodule.x_module import STUDIO_VIEW, STUDENT_VIEW
|
||||
from xblock.exceptions import NoSuchHandlerError
|
||||
from xblock_django.user_service import DjangoXBlockUserService
|
||||
from opaque_keys.edx.keys import UsageKey, CourseKey
|
||||
from opaque_keys.edx.locations import Location
|
||||
from xmodule.partitions.partitions import Group, UserPartition
|
||||
@@ -1170,7 +1170,7 @@ class TestEditSplitModule(ItemTest):
|
||||
# (CachingDescriptorSystem is used in tests, PreviewModuleSystem in Studio).
|
||||
# CachingDescriptorSystem doesn't have user service, that's needed for
|
||||
# SplitTestModule. So, in this line of code we add this service manually.
|
||||
split_test.runtime._services['user'] = StudioUserService(self.request) # pylint: disable=protected-access
|
||||
split_test.runtime._services['user'] = DjangoXBlockUserService(self.user) # pylint: disable=protected-access
|
||||
|
||||
# Call add_missing_groups method to add the missing group.
|
||||
split_test.add_missing_groups(self.request)
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
Tests for contentstore.views.preview.py
|
||||
"""
|
||||
import re
|
||||
import ddt
|
||||
from mock import Mock
|
||||
from xblock.core import XBlock
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
@@ -10,8 +13,9 @@ from xblock.core import XBlockAside
|
||||
from student.tests.factories import UserFactory
|
||||
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
|
||||
from contentstore.views.preview import get_preview_fragment
|
||||
from contentstore.views.preview import get_preview_fragment, _preview_module_system
|
||||
from xmodule.modulestore import ModuleStoreEnum
|
||||
from xmodule.modulestore.tests.test_asides import AsideTestType
|
||||
from cms.djangoapps.xblock_config.models import StudioConfig
|
||||
@@ -101,3 +105,44 @@ class GetPreviewHtmlTestCase(TestCase):
|
||||
|
||||
self.assertNotRegexpMatches(html, r"data-block-type=[\"\']test_aside[\"\']")
|
||||
self.assertNotRegexpMatches(html, "Aside rendered")
|
||||
|
||||
|
||||
@XBlock.needs("field-data")
|
||||
@XBlock.needs("i18n")
|
||||
@XBlock.needs("user")
|
||||
class PureXBlock(XBlock):
|
||||
"""
|
||||
Pure XBlock to use in tests.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class StudioXBlockServiceBindingTest(ModuleStoreTestCase):
|
||||
"""
|
||||
Tests that the Studio Module System (XBlock Runtime) provides an expected set of services.
|
||||
"""
|
||||
def setUp(self):
|
||||
"""
|
||||
Set up the user and request that will be used.
|
||||
"""
|
||||
super(StudioXBlockServiceBindingTest, self).setUp()
|
||||
self.user = UserFactory()
|
||||
self.course = CourseFactory.create()
|
||||
self.request = Mock()
|
||||
self.field_data = Mock()
|
||||
|
||||
@XBlock.register_temp_plugin(PureXBlock, identifier='pure')
|
||||
@ddt.data("user", "i18n", "field-data")
|
||||
def test_expected_services_exist(self, expected_service):
|
||||
"""
|
||||
Tests that the 'user' and 'i18n' services are provided by the Studio runtime.
|
||||
"""
|
||||
descriptor = ItemFactory(category="pure", parent=self.course)
|
||||
runtime = _preview_module_system(
|
||||
self.request,
|
||||
descriptor,
|
||||
self.field_data,
|
||||
)
|
||||
service = runtime.service(descriptor, expected_service)
|
||||
self.assertIsNotNone(service)
|
||||
|
||||
Reference in New Issue
Block a user