From 79df5456068eca60e76cb2e0971fea691faf21a4 Mon Sep 17 00:00:00 2001 From: Syed Hassan Raza Date: Thu, 21 Jul 2016 16:18:57 +0500 Subject: [PATCH] Mongo courses wrong course_key restriction --- .../commands/tests/test_migrate_to_split.py | 16 ++++++++++++++-- .../xmodule/xmodule/modulestore/mongo/base.py | 5 +++++ .../xmodule/modulestore/tests/test_mongo.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py index 780b96e99c..64df42d502 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py @@ -9,6 +9,7 @@ from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.django import modulestore +from xmodule.modulestore.exceptions import ItemNotFoundError class TestArgParsing(unittest.TestCase): @@ -107,6 +108,17 @@ class TestMigrateToSplit(ModuleStoreTestCase): "org.dept", "name", "run", ) split_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.split) - locator = split_store.make_course_key(self.course.id.org, self.course.id.course, self.course.id.run) - course_from_split = modulestore().get_course(locator) + locator = split_store.make_course_key("org.dept", "name", "run") + course_from_split = split_store.get_course(locator) self.assertIsNotNone(course_from_split) + + # Getting the original course with mongo course_id + mongo_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo) + mongo_locator = mongo_store.make_course_key(self.course.id.org, self.course.id.course, self.course.id.run) + course_from_mongo = mongo_store.get_course(mongo_locator) + self.assertIsNotNone(course_from_mongo) + + # Throws ItemNotFoundError when try to access original course with split course_id + split_locator = split_store.make_course_key(self.course.id.org, self.course.id.course, self.course.id.run) + with self.assertRaises(ItemNotFoundError): + mongo_store.get_course(split_locator) diff --git a/common/lib/xmodule/xmodule/modulestore/mongo/base.py b/common/lib/xmodule/xmodule/modulestore/mongo/base.py index 7000e1e777..2b0e366f13 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo/base.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo/base.py @@ -1088,6 +1088,11 @@ class MongoModuleStore(ModuleStoreDraftAndPublished, ModuleStoreWriteBase, Mongo Get the course with the given courseid (org/course/run) """ assert isinstance(course_key, CourseKey) + + if not course_key.deprecated: # split course_key + # The supplied CourseKey is of the wrong type, so it can't possibly be stored in this modulestore. + raise ItemNotFoundError(course_key) + course_key = self.fill_in_run(course_key) location = course_key.make_usage_key('course', course_key.run) try: diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py index be8e6c2f12..c55d63d935 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py @@ -24,6 +24,7 @@ from xblock.runtime import KeyValueStore from xblock.exceptions import InvalidScopeError from xmodule.tests import DATA_DIR +from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locations import Location from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.mongo import MongoKeyValueStore @@ -290,6 +291,15 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): assert_false(self.draft_store.has_course(mix_cased)) assert_false(self.draft_store.has_course(mix_cased, ignore_case=True)) + def test_get_mongo_course_with_split_course_key(self): + """ + Test mongo course using split course_key will not able to access it. + """ + course_key = CourseKey.from_string('course-v1:edX+simple+2012_Fall') + + with self.assertRaises(ItemNotFoundError): + self.draft_store.get_course(course_key) + def test_has_course_with_library(self): """ Test that has_course() returns False when called with a LibraryLocator.