Commit 2cc017a5 by Renzo Lucioni

Merge pull request #218 from edx/renzo/migration-sans-publication

Avoid publishing Courses to the LMS during migration
parents 2b57cece faae0f1f
...@@ -15,7 +15,12 @@ logger = logging.getLogger(__name__) ...@@ -15,7 +15,12 @@ logger = logging.getLogger(__name__)
class MigratedCourse(object): class MigratedCourse(object):
def __init__(self, course_id): def __init__(self, course_id):
self.course, __ = Course.objects.get_or_create(id=course_id) # Avoid use of get_or_create to prevent publication to the
# LMS when saving the newly instantiated Course.
try:
self.course = Course.objects.get(id=course_id)
except Course.DoesNotExist:
self.course = Course(id=course_id)
def load_from_lms(self, access_token): def load_from_lms(self, access_token):
""" """
...@@ -25,7 +30,7 @@ class MigratedCourse(object): ...@@ -25,7 +30,7 @@ class MigratedCourse(object):
""" """
name, modes = self._retrieve_data_from_lms(access_token) name, modes = self._retrieve_data_from_lms(access_token)
self.course.name = name self.course.name = name
self.course.save() self.course.save(publish=False)
self._get_products(modes) self._get_products(modes)
def _build_lms_url(self, path): def _build_lms_url(self, path):
......
...@@ -10,11 +10,14 @@ from django.conf import settings ...@@ -10,11 +10,14 @@ from django.conf import settings
from django.core.management import call_command from django.core.management import call_command
from django.test import TestCase from django.test import TestCase
import httpretty import httpretty
import mock
from oscar.core.loading import get_model from oscar.core.loading import get_model
import pytz import pytz
from waffle import Switch
from ecommerce.core.constants import ISO_8601_FORMAT from ecommerce.core.constants import ISO_8601_FORMAT
from ecommerce.courses.models import Course from ecommerce.courses.models import Course
from ecommerce.courses.publishers import LMSPublisher
from ecommerce.extensions.catalogue.management.commands.migrate_course import MigratedCourse from ecommerce.extensions.catalogue.management.commands.migrate_course import MigratedCourse
from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin from ecommerce.extensions.catalogue.tests.mixins import CourseCatalogTestMixin
from ecommerce.extensions.catalogue.utils import generate_sku from ecommerce.extensions.catalogue.utils import generate_sku
...@@ -102,6 +105,10 @@ class CourseMigrationTestMixin(CourseCatalogTestMixin): ...@@ -102,6 +105,10 @@ class CourseMigrationTestMixin(CourseCatalogTestMixin):
class MigratedCourseTests(CourseMigrationTestMixin, TestCase): class MigratedCourseTests(CourseMigrationTestMixin, TestCase):
def setUp(self):
super(MigratedCourseTests, self).setUp()
Switch.objects.get_or_create(name='publish_course_modes_to_lms', active=True)
def _migrate_course_from_lms(self): def _migrate_course_from_lms(self):
""" Create a new MigratedCourse and simulate the loading of data from LMS. """ """ Create a new MigratedCourse and simulate the loading of data from LMS. """
self._mock_lms_api() self._mock_lms_api()
...@@ -112,8 +119,13 @@ class MigratedCourseTests(CourseMigrationTestMixin, TestCase): ...@@ -112,8 +119,13 @@ class MigratedCourseTests(CourseMigrationTestMixin, TestCase):
@httpretty.activate @httpretty.activate
def test_load_from_lms(self): def test_load_from_lms(self):
""" Verify the method creates new objects based on data loaded from the LMS. """ """ Verify the method creates new objects based on data loaded from the LMS. """
migrated_course = self._migrate_course_from_lms() with mock.patch.object(LMSPublisher, 'publish') as mock_publish:
course = migrated_course.course mock_publish.return_value = True
migrated_course = self._migrate_course_from_lms()
course = migrated_course.course
# Verify that the migrated course was not published back to the LMS
self.assertFalse(mock_publish.called)
# Ensure LMS was called with the correct headers # Ensure LMS was called with the correct headers
for request in httpretty.httpretty.latest_requests: for request in httpretty.httpretty.latest_requests:
...@@ -132,6 +144,10 @@ class MigratedCourseTests(CourseMigrationTestMixin, TestCase): ...@@ -132,6 +144,10 @@ class MigratedCourseTests(CourseMigrationTestMixin, TestCase):
class CommandTests(CourseMigrationTestMixin, TestCase): class CommandTests(CourseMigrationTestMixin, TestCase):
def setUp(self):
super(CommandTests, self).setUp()
Switch.objects.get_or_create(name='publish_course_modes_to_lms', active=True)
@httpretty.activate @httpretty.activate
def test_handle(self): def test_handle(self):
""" Verify the management command retrieves data, but does not save it to the database. """ """ Verify the management command retrieves data, but does not save it to the database. """
...@@ -139,7 +155,13 @@ class CommandTests(CourseMigrationTestMixin, TestCase): ...@@ -139,7 +155,13 @@ class CommandTests(CourseMigrationTestMixin, TestCase):
initial_stock_record_count = StockRecord.objects.count() initial_stock_record_count = StockRecord.objects.count()
self._mock_lms_api() self._mock_lms_api()
call_command('migrate_course', self.course_id, access_token=ACCESS_TOKEN)
with mock.patch.object(LMSPublisher, 'publish') as mock_publish:
mock_publish.return_value = True
call_command('migrate_course', self.course_id, access_token=ACCESS_TOKEN)
# Verify that the migrated course was not published back to the LMS
self.assertFalse(mock_publish.called)
# Ensure LMS was called with the correct headers # Ensure LMS was called with the correct headers
for request in httpretty.httpretty.latest_requests: for request in httpretty.httpretty.latest_requests:
...@@ -153,5 +175,12 @@ class CommandTests(CourseMigrationTestMixin, TestCase): ...@@ -153,5 +175,12 @@ class CommandTests(CourseMigrationTestMixin, TestCase):
def test_handle_with_commit(self): def test_handle_with_commit(self):
""" Verify the management command retrieves data, and saves it to the database. """ """ Verify the management command retrieves data, and saves it to the database. """
self._mock_lms_api() self._mock_lms_api()
call_command('migrate_course', self.course_id, access_token=ACCESS_TOKEN, commit=True)
with mock.patch.object(LMSPublisher, 'publish') as mock_publish:
mock_publish.return_value = True
call_command('migrate_course', self.course_id, access_token=ACCESS_TOKEN, commit=True)
# Verify that the migrated course was not published back to the LMS
self.assertFalse(mock_publish.called)
self.assert_course_migrated() self.assert_course_migrated()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment