def test_constructor(self, org, lib, block_type, block_id): lib_key = LibraryLocator(org=org, library=lib) lib_usage_key = LibraryUsageLocator(library_key=lib_key, block_type=block_type, block_id=block_id) lib_usage_key2 = UsageKey.from_string( u"lib-block-v1:{}+{}+{}@{}+{}@{}".format(org, lib, BLOCK_TYPE_PREFIX, block_type, BLOCK_PREFIX, block_id)) self.assertEqual(lib_usage_key, lib_usage_key2) self.assertEqual(lib_usage_key.library_key, lib_key) self.assertEqual(lib_usage_key.library_key, lib_key) self.assertEqual(lib_usage_key.branch, None) self.assertEqual(lib_usage_key.run, LibraryLocator.RUN) self.assertIsInstance(lib_usage_key2, LibraryUsageLocator) self.assertIsInstance(lib_usage_key2.library_key, LibraryLocator)
def _get_or_create_library(org, number, display_name, user): """ Create or retrieve given library and return its course-like key """ try: # Create library if it does not exist store = modulestore() with store.default_store(ModuleStoreEnum.Type.split): library = store.create_library( org=org, library=number, user_id=user.id, fields={"display_name": display_name}, ) add_instructor(library.location.library_key, user, user) return library.location.library_key, True except DuplicateCourseError: # Course exists, return its key return LibraryLocator(org=org, library=number), False
def test_create_library(self): """ From the home page: Click "New Library" Fill out the form Submit the form We should be redirected to the edit view for the library Return to the home page The newly created library should now appear in the list of libraries """ unique_suffix = uuid4().hex[:4] name = "New Library Name " + unique_suffix org = "TestOrgX" + unique_suffix number = "TESTLIB_" + unique_suffix self.auth_page.visit() self.dashboard_page.visit() self.dashboard_page.wait_for_element_visibility( '.content-primary', 'See library list.') self.assertFalse( self.dashboard_page.has_library(name=name, org=org, number=number)) self.assertTrue(self.dashboard_page.has_new_library_button()) self.dashboard_page.click_new_library() self.assertTrue(self.dashboard_page.is_new_library_form_visible()) self.dashboard_page.fill_new_library_form(name, org, number) self.assertTrue(self.dashboard_page.is_new_library_form_valid()) self.dashboard_page.submit_new_library_form() # The next page is the library edit view; make sure it loads: lib_page = LibraryEditPage(self.browser, LibraryLocator(org, number)) lib_page.wait_for_page() # Then go back to the home page and make sure the new library is listed there: self.dashboard_page.visit() self.assertTrue( self.dashboard_page.has_library(name=name, org=org, number=number))
def test_superclass_make_relative(self): lib_key = LibraryLocator(org="TestX", library="problem-bank-15") obj = BlockUsageLocator.make_relative(lib_key, "block_type", "block_id") self.assertIsInstance(obj, LibraryUsageLocator)
def get_key(self): """ Get the library locator for the current library key. """ return LibraryLocator(self.courselike_key.org, self.courselike_key.library)
def test_get_library_non_existent(self): """ Test get_library() with non-existent key """ result = self.store.get_library(LibraryLocator("non", "existent")) assert result is None
def test_lib_key_roundtrip_and_equality(self): org = 'TestX' code = 'test-problem-bank' lib_key = LibraryLocator(org=org, library=code) lib_key2 = CourseKey.from_string(str(lib_key)) self.assertEqual(lib_key, lib_key2)
class SandboxServiceTest(TestCase): """ Test SandboxService methods. """ PYTHON_LIB_FILENAME = 'test_python_lib.zip' PYTHON_LIB_SOURCE_FILE = './common/test/data/uploads/python_lib.zip' @classmethod def setUpClass(cls): """ Upload the python lib file to the test course. """ super().setUpClass() course_key = CourseLocator('test', 'sandbox_test', '2021_01') cls.sandbox_service = SandboxService(course_id=course_key, contentstore=contentstore) cls.zipfile = upload_file_to_course( course_key=course_key, contentstore=cls.sandbox_service.contentstore(), source_file=cls.PYTHON_LIB_SOURCE_FILE, target_filename=cls.PYTHON_LIB_FILENAME, ) @staticmethod def validate_can_execute_unsafe_code(context_key, expected_result): sandbox_service = SandboxService(course_id=context_key, contentstore=None) assert expected_result == sandbox_service.can_execute_unsafe_code() @ddt.data( CourseLocator('edX', 'notful', 'empty'), LibraryLocator('edY', 'test_bank'), ) @override_settings( COURSES_WITH_UNSAFE_CODE=['edX/full/.*', 'library:v1-edX+.*']) def test_sandbox_exclusion(self, context_key): """ Test to make sure that a non-match returns false """ self.validate_can_execute_unsafe_code(context_key, False) @ddt.data( CourseKey.from_string('edX/full/2012_Fall'), CourseKey.from_string('edX/full/2013_Spring'), ) @override_settings(COURSES_WITH_UNSAFE_CODE=['edX/full/.*']) def test_sandbox_inclusion(self, context_key): """ Test to make sure that a match works across course runs """ self.validate_can_execute_unsafe_code(context_key, True) self.validate_can_execute_unsafe_code( LibraryLocator('edX', 'test_bank'), False) @ddt.data( CourseLocator('edX', 'full', '2012_Fall'), CourseLocator('edX', 'full', '2013_Spring'), LibraryLocator('edX', 'test_bank'), ) def test_courselikes_with_unsafe_code_default(self, context_key): """ Test that the default setting for COURSES_WITH_UNSAFE_CODE is an empty setting, i.e., we don't use @override_settings in these tests """ self.validate_can_execute_unsafe_code(context_key, False) @override_settings(PYTHON_LIB_FILENAME=PYTHON_LIB_FILENAME) def test_get_python_lib_zip(self): assert self.sandbox_service.get_python_lib_zip() == self.zipfile def test_no_python_lib_zip(self): assert self.sandbox_service.get_python_lib_zip() is None
def test_lib_key_constructor_bad_version_guid(self): with self.assertRaises(InvalidKeyError): LibraryLocator(version_guid="012345") with self.assertRaises(InvalidKeyError): LibraryLocator(version_guid=None)
def test_lib_key_constructor_overspecified(self): with self.assertRaises(ValueError): LibraryLocator(org='TestX', library='big', course='small')
def test_lib_key_constructor_underspecified(self, constructor_kwargs): with self.assertRaises(InvalidKeyError): LibraryLocator(**constructor_kwargs)
def test_lib_key_no_offering(self): with self.assertRaises(ValueError): LibraryLocator(org='TestX', library='test', offering='tribble')
class CourseBlocksSignalTest(ModuleStoreTestCase): """ Tests for the Course Blocks signal """ ENABLED_SIGNALS = ['course_deleted', 'course_published'] def setUp(self): super(CourseBlocksSignalTest, self).setUp() self.course = CourseFactory.create() self.course_usage_key = self.store.make_course_usage_key( self.course.id) def test_course_update(self): test_display_name = "Lightsabers 101" # Course exists in cache initially bs_manager = get_block_structure_manager(self.course.id) orig_block_structure = bs_manager.get_collected() self.assertTrue( is_course_in_block_structure_cache(self.course.id, self.store)) self.assertNotEqual( test_display_name, orig_block_structure.get_xblock_field(self.course_usage_key, 'display_name')) self.course.display_name = test_display_name self.store.update_item(self.course, self.user.id) # Cached version of course has been updated updated_block_structure = bs_manager.get_collected() self.assertEqual( test_display_name, updated_block_structure.get_xblock_field(self.course_usage_key, 'display_name')) @ddt.data(True, False) @patch( 'openedx.core.djangoapps.content.block_structure.manager.BlockStructureManager.clear' ) def test_cache_invalidation(self, invalidate_cache_enabled, mock_bs_manager_clear): test_display_name = "Jedi 101" with waffle().override(INVALIDATE_CACHE_ON_PUBLISH, active=invalidate_cache_enabled): self.course.display_name = test_display_name self.store.update_item(self.course, self.user.id) self.assertEquals(mock_bs_manager_clear.called, invalidate_cache_enabled) def test_course_delete(self): bs_manager = get_block_structure_manager(self.course.id) self.assertIsNotNone(bs_manager.get_collected()) self.assertTrue( is_course_in_block_structure_cache(self.course.id, self.store)) self.store.delete_course(self.course.id, self.user.id) with self.assertRaises(ItemNotFoundError): bs_manager.get_collected() self.assertFalse( is_course_in_block_structure_cache(self.course.id, self.store)) @ddt.data( (CourseLocator(org='org', course='course', run='run'), True), (LibraryLocator(org='org', course='course'), False), ) @ddt.unpack @patch( 'openedx.core.djangoapps.content.block_structure.tasks.update_course_in_cache_v2.apply_async' ) def test_update_only_for_courses(self, key, expect_update_called, mock_update): _update_block_structure_on_course_publish(sender=None, course_key=key) self.assertEqual(mock_update.called, expect_update_called)
def test_sandbox_exclusion(self): """ Test to make sure that a non-match returns false """ self.assertFalse(can_execute_unsafe_code(CourseLocator('edX', 'notful', 'empty'))) self.assertFalse(can_execute_unsafe_code(LibraryLocator('edY', 'test_bank')))
def test_version_only_lib_key(self): version_only_lib_key = LibraryLocator(version_guid=ObjectId('519665f6223ebd6980884f2b')) self.assertEqual(version_only_lib_key.org, None) self.assertEqual(version_only_lib_key.library, None) # pylint: disable=no-member with self.assertRaises(InvalidKeyError): version_only_lib_key.for_branch("test")
def test_constructor_invalid(self, kwargs): lib_key = LibraryLocator(org="TestX", library="problem-bank-15") with self.assertRaises(InvalidKeyError): LibraryUsageLocator(library_key=lib_key, **kwargs)
def get_id(org, library, url_name): """ Create a LibraryLocator given an org and library. url_name is ignored, but left in for compatibility with the parent signature. """ return LibraryLocator(org=org, library=library)
def test_changing_course(self): lib_key = LibraryLocator(org="TestX", library="test") with self.assertRaises(AttributeError): lib_key.course = "PHYS" with self.assertRaises(KeyError): lib_key.replace(course="PHYS")
def get_id(org, library, url_name): # lint-amnesty, pylint: disable=arguments-differ """ Create a LibraryLocator given an org and library. url_name is ignored, but left in for compatibility with the parent signature. """ return LibraryLocator(org=org, library=library)
def test_invalid_run(self): with self.assertRaises(ValueError): LibraryLocator(org='TestX', library='test', run='not-library')