def test_get_offers_for_multiple_courses_voucher(self): """ Verify that the course offers data is returned for a multiple courses voucher. """ self.mock_access_token_response() course, seat = self.create_course_and_seat() self.mock_course_runs_endpoint( self.site_configuration.discovery_api_url, query='*:*', course_run=course ) new_range, __ = Range.objects.get_or_create(catalog_query='*:*', course_seat_types='verified') new_range.add_product(seat) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual(first_offer, { 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer(seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })
def get_course_offer_data(self, benefit, course, course_info, credit_provider_price, is_verified, multiple_credit_providers, product, stock_record, voucher): """ Gets course offer data. Arguments: benefit (Benefit): Benefit associated with a voucher course (Course): Course associated with a voucher course_info (dict): Course info fetched from an API (LMS or Course Catalog) is_verified (bool): Indicated whether or not the voucher's range of products contains a verified course seat stock_record (StockRecord): Stock record associated with the course seat voucher (Voucher): Voucher for which the course offer data is being fetched Returns: dict: Course offer data """ try: image = course_info['image']['src'] except (KeyError, TypeError): image = '' return { 'benefit': serializers.BenefitSerializer(benefit).data, 'contains_verified': is_verified, 'course_start_date': course_info.get('start', ''), 'id': course.id, 'image_url': image, 'multiple_credit_providers': multiple_credit_providers, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': credit_provider_price, 'seat_type': product.attr.certificate_type, 'stockrecords': serializers.StockRecordSerializer(stock_record).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime }
def test_get_offers_for_single_course_voucher(self): """ Verify that the course offers data is returned for a single course voucher. """ self.mock_access_token_response() course, seat = self.create_course_and_seat() new_range = RangeFactory(products=[seat, ]) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) self.mock_course_run_detail_endpoint( course, discovery_api_url=self.site_configuration.discovery_api_url ) offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual(first_offer, { 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2013-02-05T05:00:00Z', 'id': course.id, 'image_url': '/path/to/image.jpg', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer(seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })
def test_get_offers_for_course_catalog_voucher(self): """ Verify that the course offers data is returned for a course catalog voucher. """ catalog_id = 1 catalog_query = '*:*' # Populate database for the test case. course, seat = self.create_course_and_seat() new_range, __ = Range.objects.get_or_create( course_catalog=catalog_id, course_seat_types='verified') new_range.add_product(seat) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) # Mock network calls self.mock_dynamic_catalog_course_runs_api( query=catalog_query, course_run=course, discovery_api_url=self.site_configuration.discovery_api_url) self.mock_fetch_course_catalog( catalog_id=catalog_id, expected_query=catalog_query, discovery_api_url=self.site_configuration.discovery_api_url) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] first_offer = offers[0] # Verify that offers are returned when voucher is created using course catalog self.assertEqual(len(offers), 1) self.assertDictEqual( first_offer, { 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer( seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })
def get_offers(self, products, request, voucher): """ Get the course offers associated with the voucher. Arguments: products (List): List of Products associated with the voucher request (HttpRequest): Request data voucher (Voucher): Oscar Voucher for which the offers are returned Returns: List: List of course offers where each offer is represented by a dictionary """ benefit = voucher.offers.first().benefit offers = [] query_results = request.site.siteconfiguration.course_catalog_api_client.course_runs.get( q=benefit.range.catalog_query)['results'] course_ids = [product.course_id for product in products] courses = Course.objects.filter(id__in=course_ids) contains_verified_course = next( (False for course in courses if course.type != 'verified'), True) for product in products: course_catalog_data = next((result for result in query_results if result['key'] == product.course_id), None) try: course = courses.get(id=product.course_id) except Course.DoesNotExist: logger.error('Course %s not found.', product.course_id) if course_catalog_data and course: stock_record = StockRecord.objects.get(product__id=product.id) offers.append({ 'benefit': serializers.BenefitSerializer(benefit).data, 'contains_verified': contains_verified_course, 'course_start_date': course_catalog_data['start'], 'id': course.id, 'image_url': course_catalog_data['image']['src'], 'organization': CourseKey.from_string(course.id).org, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer(stock_record).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, }) return offers
def test_get_offers_for_enterprise_catalog_voucher(self): """ Verify that the course offers data is returned for an enterprise catalog voucher. """ Switch.objects.update_or_create( name=ENTERPRISE_OFFERS_FOR_COUPONS_SWITCH, defaults={'active': False}) self.mock_access_token_response() course, seat = self.create_course_and_seat() enterprise_catalog_id = str(uuid4()) self.mock_enterprise_catalog_course_endpoint( self.site_configuration.enterprise_api_url, enterprise_catalog_id, course_run=course) new_range, __ = Range.objects.get_or_create( catalog_query='*:*', course_seat_types='verified', enterprise_customer=str(uuid4()), enterprise_customer_catalog=enterprise_catalog_id, ) new_range.add_product(seat) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual( first_offer, { 'benefit': { 'type': get_benefit_type(benefit), 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer( seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })
def test_get_offers_for_enterprise_offer(self): """ Verify that the course offers data is returned for an enterprise catalog voucher. """ self.mock_access_token_response() course, seat = self.create_course_and_seat() enterprise_customer_id = str(uuid4()) enterprise_catalog_id = str(uuid4()) self.mock_enterprise_catalog_course_endpoint( self.site_configuration.enterprise_api_url, enterprise_catalog_id, course_run=course) voucher = prepare_enterprise_voucher( benefit_value=10, enterprise_customer=enterprise_customer_id, enterprise_customer_catalog=enterprise_catalog_id) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) offers = VoucherViewSet().get_offers(request=request, voucher=voucher)['results'] first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual( first_offer, { 'benefit': { 'type': get_benefit_type(benefit), 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': seat.attr.certificate_type, 'stockrecords': serializers.StockRecordSerializer( seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })
def test_offers_api_endpoint_for_course_catalog_voucher(self): """ Verify that the course offers data is returned for a course catalog voucher. """ catalog_id = 1 catalog_query = '*:*' self.mock_access_token_response() # Populate database for the test case. course, seat = self.create_course_and_seat() new_range, __ = Range.objects.get_or_create(course_catalog=catalog_id, course_seat_types='verified') new_range.add_product(seat) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) # Mock network calls self.mock_catalog_detail_endpoint( catalog_id=catalog_id, expected_query=catalog_query, discovery_api_url=self.site_configuration.discovery_api_url ) self.mock_course_runs_endpoint( discovery_api_url=self.site_configuration.discovery_api_url, query=catalog_query, course_run=course ) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) response = self.endpointView(request) # Verify that offers are returned when voucher is created using course catalog self.assertEqual(response.status_code, 200) self.assertListEqual( response.data['results'], [{ 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer(seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, }], )
def test_get_offers_for_single_course_voucher(self): """ Verify that the course offers data is returned for a single course voucher. """ course, seat = self.create_course_and_seat() new_range = RangeFactory(products=[ seat, ]) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) voucher, products = get_voucher_and_products_from_code(voucher.code) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) self.mock_course_api_response(course=course) offers = VoucherViewSet().get_offers(products=products, request=request, voucher=voucher) first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual( first_offer, { 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2013-02-05T05:00:00Z', 'id': course.id, 'image_url': get_lms_url( '/asset-v1:test+test+test+type@asset+block@images_course_image.jpg' ), 'organization': CourseKey.from_string(course.id).org, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer( seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime })
def test_get_offers(self): """ Verify that the course offers data is returned. """ course, seat = self.create_course_and_seat() new_range = RangeFactory(products=[ seat, ]) voucher, __ = prepare_voucher(_range=new_range, benefit_value=10) voucher, products = get_voucher_and_products_from_code(voucher.code) benefit = voucher.offers.first().benefit request = self.prepare_offers_listing_request(voucher.code) self.mock_dynamic_catalog_course_runs_api( course_run=course, query=new_range.catalog_query) offers = VoucherViewSet().get_offers(products=products, request=request, voucher=voucher) first_offer = offers[0] self.assertEqual(len(offers), 1) self.assertDictEqual( first_offer, { 'benefit': { 'type': benefit.type, 'value': benefit.value }, 'contains_verified': True, 'course_start_date': '2016-05-01T00:00:00Z', 'id': course.id, 'image_url': 'path/to/the/course/image', 'organization': CourseKey.from_string(course.id).org, 'seat_type': course.type, 'stockrecords': serializers.StockRecordSerializer( seat.stockrecords.first()).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime })
def test_get_course_offer_data(self): """ Verify that the course offers data is properly formatted. """ benefit = BenefitFactory() course, seat = self.create_course_and_seat() course_info = { 'start': '2016-05-01T00:00:00Z', 'image': { 'src': 'path/to/the/course/image' } } stock_record = seat.stockrecords.first() voucher = VoucherFactory() offer = VoucherViewSet().get_course_offer_data( benefit=benefit, course=course, course_info=course_info, credit_provider_price=None, multiple_credit_providers=False, is_verified=True, product=seat, stock_record=stock_record, voucher=voucher) self.assertDictEqual( offer, { 'benefit': { 'type': get_benefit_type(benefit), 'value': benefit.value }, 'contains_verified': True, 'course_start_date': course_info['start'], 'id': course.id, 'image_url': course_info['image']['src'], 'multiple_credit_providers': False, 'organization': CourseKey.from_string(course.id).org, 'credit_provider_price': None, 'seat_type': seat.attr.certificate_type, 'stockrecords': serializers.StockRecordSerializer(stock_record).data, 'title': course.name, 'voucher_end_date': voucher.end_datetime, })