def test_course_name_missing(self): """Verify the Course Structure API is queried if the Commerce API doesn't return a course name.""" # Mock the Commerce API so that it does not return a name body = { 'name': None, 'verification_deadline': EXPIRES_STRING, } httpretty.register_uri(httpretty.GET, self.commerce_api_url, body=json.dumps(body), content_type=JSON) # Mock the Course Structure API httpretty.register_uri(httpretty.GET, self.course_structure_url, body='{}', content_type=JSON) # Try migrating the course, which should fail. try: migrated_course = MigratedCourse(self.course_id, self.site.domain) migrated_course.load_from_lms(ACCESS_TOKEN) except Exception as ex: # pylint: disable=broad-except self.assertEqual( ex.message, 'Aborting migration. No name is available for {}.'.format( self.course_id)) # Verify the Course Structure API was called. last_request = httpretty.last_request() self.assertEqual(last_request.path, urlparse(self.course_structure_url).path)
def test_fall_back_to_course_structure(self): """ Verify that migration falls back to the Course Structure API when data is unavailable from the Commerce API. """ self._mock_lms_apis() body = {'detail': 'Not found'} httpretty.register_uri( httpretty.GET, self.commerce_api_url, status=404, body=json.dumps(body), content_type=JSON ) migrated_course = MigratedCourse(self.course_id, self.site.domain) migrated_course.load_from_lms() course = migrated_course.course # Verify that created objects match mocked data. parent_seat = course.parent_seat_product self.assertEqual(parent_seat.title, 'Seat in {}'.format(self.course_name)) # Confirm that there is no verification deadline set for the course. self.assertEqual(course.verification_deadline, None) for seat in course.seat_products: mode = mode_for_product(seat) self.assert_stock_record_valid(seat.stockrecords.first(), seat, Decimal(self.prices[mode]))
def test_whitespace_stripped(self): """Verify that whitespace in course names is stripped during migration.""" self._mock_lms_apis() body = { # Wrap the course name with whitespace 'name': ' {} '.format(self.course_name), 'verification_deadline': EXPIRES_STRING, } httpretty.register_uri(httpretty.GET, self.commerce_api_url, body=json.dumps(body), content_type=JSON) migrated_course = MigratedCourse(self.course_id, self.partner.short_code) migrated_course.load_from_lms(ACCESS_TOKEN) course = migrated_course.course # Verify that whitespace has been stripped from the course name. self.assertEqual(course.name, self.course_name) parent_seat = course.parent_seat_product self.assertEqual(parent_seat.title, 'Seat in {}'.format(self.course_name))
def _migrate_course_from_lms(self): """ Create a new MigratedCourse and simulate the loading of data from LMS. """ self._mock_lms_apis() migrated_course = MigratedCourse(self.course_id, self.partner.short_code) migrated_course.load_from_lms(ACCESS_TOKEN) return migrated_course
def test_course_name_missing(self): """Verify the Course Structure API is queried if the Commerce API doesn't return a course name.""" # Mock the Commerce API so that it does not return a name body = { 'name': None, 'verification_deadline': EXPIRES_STRING, } httpretty.register_uri(httpretty.GET, self.commerce_api_url, body=json.dumps(body), content_type=JSON) # Mock the Course Structure API httpretty.register_uri(httpretty.GET, self.course_structure_url, body='{}', content_type=JSON) # Try migrating the course, which should fail. try: migrated_course = MigratedCourse(self.course_id, self.partner.short_code) migrated_course.load_from_lms(ACCESS_TOKEN) except Exception as ex: # pylint: disable=broad-except self.assertEqual(ex.message, 'Aborting migration. No name is available for {}.'.format(self.course_id)) # Verify the Course Structure API was called. last_request = httpretty.last_request() self.assertEqual(last_request.path, urlparse(self.course_structure_url).path)
def test_fall_back_to_course_structure(self): """ Verify that migration falls back to the Course Structure API when data is unavailable from the Commerce API. """ self._mock_lms_apis() body = {'detail': 'Not found'} httpretty.register_uri(httpretty.GET, self.commerce_api_url, status=404, body=json.dumps(body), content_type=JSON) migrated_course = MigratedCourse(self.course_id, self.partner.short_code) migrated_course.load_from_lms(ACCESS_TOKEN) course = migrated_course.course # Ensure that the LMS was called with the correct headers. course_structure_path = urlparse(self.course_structure_url).path for request in httpretty.httpretty.latest_requests: if request.path == course_structure_path: self.assert_lms_api_headers(request, bearer=True) else: self.assert_lms_api_headers(request) # Verify that created objects match mocked data. parent_seat = course.parent_seat_product self.assertEqual(parent_seat.title, 'Seat in {}'.format(self.course_name)) # Confirm that there is no verification deadline set for the course. self.assertEqual(course.verification_deadline, None) for seat in course.seat_products: mode = mode_for_seat(seat) self.assert_stock_record_valid(seat.stockrecords.first(), seat, Decimal(self.prices[mode]))
def test_fall_back_to_course_structure(self): """ Verify that migration falls back to the Course Structure API when data is unavailable from the Commerce API. """ self._mock_lms_apis() body = {'detail': 'Not found'} httpretty.register_uri( httpretty.GET, self.commerce_api_url, status=404, body=json.dumps(body), content_type=JSON ) migrated_course = MigratedCourse(self.course_id, self.partner.short_code) migrated_course.load_from_lms(ACCESS_TOKEN) course = migrated_course.course # Ensure that the LMS was called with the correct headers. course_structure_path = urlparse(self.course_structure_url).path for request in httpretty.httpretty.latest_requests: if request.path == course_structure_path: self.assert_lms_api_headers(request, bearer=True) else: self.assert_lms_api_headers(request) # Verify that created objects match mocked data. parent_seat = course.parent_seat_product self.assertEqual(parent_seat.title, 'Seat in {}'.format(self.course_name)) # Confirm that there is no verification deadline set for the course. self.assertEqual(course.verification_deadline, None) for seat in course.seat_products: mode = mode_for_seat(seat) self.assert_stock_record_valid(seat.stockrecords.first(), seat, Decimal(self.prices[mode]))
def _migrate_course_from_lms(self): """ Create a new MigratedCourse and simulate the loading of data from LMS. """ self._mock_lms_apis() migrated_course = MigratedCourse(self.course_id, self.site.domain) migrated_course.load_from_lms() return migrated_course