Commit bb9ac1a3 by Michael Frey Committed by GitHub

Merge pull request #918 from edx/mjfrey/account-for-null-data

Check existence of JSON data from both catalog and LMS course API
parents 57b9f504 df4d82d1
......@@ -352,3 +352,24 @@ class VoucherViewOffersEndpointTests(
'title': course.name,
'voucher_end_date': voucher.end_datetime
})
def test_get_course_offer_verify_null_fields(self):
""" Verify that the course offers data is properly formatted. """
benefit = BenefitFactory()
course, seat = self.create_course_and_seat()
course_info = {
}
stock_record = seat.stockrecords.first()
voucher = VoucherFactory()
offer = VoucherViewSet().get_course_offer_data(
benefit=benefit,
course=course,
course_info=course_info,
is_verified=True,
stock_record=stock_record,
voucher=voucher
)
self.assertEqual(offer['image_url'], '')
self.assertEqual(offer['course_start_date'], '')
......@@ -191,9 +191,9 @@ class VoucherViewSet(NonDestroyableModelViewSet):
return {
'benefit': serializers.BenefitSerializer(benefit).data,
'contains_verified': is_verified,
'course_start_date': course_info['start'],
'course_start_date': course_info.get('start', ''),
'id': course.id,
'image_url': course_info['image']['src'],
'image_url': course_info.get('image', {}).get('src', ''),
'organization': CourseKey.from_string(course.id).org,
'seat_type': course.type,
'stockrecords': serializers.StockRecordSerializer(stock_record).data,
......
......@@ -397,3 +397,18 @@ class BasketSummaryViewTests(CourseCatalogTestMixin, LmsApiMockMixin, ApiMockMix
self.assertEqual(response.status_code, 200)
cached_course_after = cache.get(cache_hash)
self.assertEqual(cached_course_after['name'], self.course.name)
def test_empty_course_api_response(self):
""" Check to see if we can handle empty response from the course api """
seat = self.create_seat(self.course)
self.create_basket_and_add_product(seat)
course_id = 'course-v1:test+test+test'
course_url = get_lms_url('api/courses/v1/courses/{}/'.format(course_id))
course_info_json = json.dumps('{}')
httpretty.register_uri(httpretty.GET, course_url, body=course_info_json, content_type='application/json')
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
line_data = response.context['formset_lines_data'][0][1]
self.assertEqual(line_data.get('image_url'), None)
self.assertEqual(line_data.get('course_short_description'), None)
......@@ -107,8 +107,8 @@ class BasketSummaryView(BasketView):
short_description = None
try:
course = get_course_info_from_lms(course_key)
image_url = get_lms_url(course['media']['course_image']['uri'])
short_description = course['short_description']
image_url = course.get('media', '{}').get('image', '{}').get('raw', '')
short_description = course.get('short_description', '')
course_name = course['name']
except (ConnectionError, SlumberBaseException, Timeout):
logger.exception('Failed to retrieve data from Course API for course [%s].', course_key)
......
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