Ejemplo n.º 1
0
    def test_no_product(self):
        """ Verify that an exception is raised if there is no product. """
        voucher = VoucherFactory(code='NOPRODUCT')
        offer = ConditionalOfferFactory()
        voucher.offers.add(offer)

        with self.assertRaises(exceptions.ProductNotFoundError):
            get_voucher_from_code(code='NOPRODUCT')
Ejemplo n.º 2
0
    def test_no_product(self):
        """ Verify that an exception is raised if there is no product. """
        voucher = VoucherFactory(code='NOPRODUCT')
        offer = ConditionalOfferFactory()
        voucher.offers.add(offer)

        with self.assertRaises(exceptions.ProductNotFoundError):
            get_voucher_from_code(code='NOPRODUCT')
Ejemplo n.º 3
0
    def get(self, request):
        partner = get_partner_for_site(request)

        sku = request.GET.get('sku', None)
        code = request.GET.get('code', None)

        if not sku:
            return HttpResponseBadRequest(_('No SKU provided.'))

        if code:
            voucher, __ = get_voucher_from_code(code=code)
        else:
            voucher = None

        try:
            product = StockRecord.objects.get(partner=partner, partner_sku=sku).product
        except StockRecord.DoesNotExist:
            return HttpResponseBadRequest(_('SKU [{sku}] does not exist.'.format(sku=sku)))

        purchase_info = request.strategy.fetch_for_product(product)
        if not purchase_info.availability.is_available_to_buy:
            return HttpResponseBadRequest(_('Product [{product}] not available to buy.'.format(product=product.title)))

        prepare_basket(request, product, voucher)
        return HttpResponseRedirect(reverse('basket:summary'), status=303)
Ejemplo n.º 4
0
    def get(self, request):
        partner = get_partner_for_site(request)

        sku = request.GET.get('sku', None)
        code = request.GET.get('code', None)

        if not sku:
            return HttpResponseBadRequest(_('No SKU provided.'))

        if code:
            voucher, __ = get_voucher_from_code(code=code)
        else:
            voucher = None

        try:
            product = StockRecord.objects.get(partner=partner, partner_sku=sku).product
            course_key = product.attr.course_key

            api = EdxRestApiClient(
                get_lms_enrollment_base_api_url(),
                oauth_access_token=request.user.access_token,
                append_slash=False
            )
            logger.debug(
                'Getting enrollment information for [%s] in [%s].',
                request.user.username,
                course_key
            )
            status = api.enrollment(','.join([request.user.username, course_key])).get()
            username = request.user.username
            seat_type = mode_for_seat(product)
            if status and status.get('mode') == seat_type and status.get('is_active'):
                logger.warning(
                    'User [%s] attempted to repurchase the [%s] seat of course [%s]',
                    username,
                    seat_type,
                    course_key
                )
                return HttpResponseBadRequest(_('You are already enrolled in {course}.').format(
                    course=product.course.name))
        except StockRecord.DoesNotExist:
            return HttpResponseBadRequest(_('SKU [{sku}] does not exist.').format(sku=sku))
        except (ConnectionError, SlumberBaseException, Timeout) as ex:
            logger.exception(
                'Failed to retrieve enrollment details for [%s] in course [%s], Because of [%s]',
                request.user.username,
                course_key,
                ex,
            )
            return HttpResponseBadRequest(_('An error occurred while retrieving enrollment details. Please try again.'))
        purchase_info = request.strategy.fetch_for_product(product)
        if not purchase_info.availability.is_available_to_buy:
            return HttpResponseBadRequest(_('Product [{product}] not available to buy.').format(product=product.title))

        prepare_basket(request, product, voucher)
        return HttpResponseRedirect(reverse('basket:summary'), status=303)
Ejemplo n.º 5
0
    def test_get_voucher_from_code(self):
        """ Verify that get_voucher_from_code() returns product and voucher. """
        original_voucher, original_product = prepare_voucher(code=COUPON_CODE)
        voucher, product = get_voucher_from_code(code=COUPON_CODE)

        self.assertIsNotNone(voucher)
        self.assertEqual(voucher, original_voucher)
        self.assertEqual(voucher.code, COUPON_CODE)
        self.assertIsNotNone(product)
        self.assertEqual(product, original_product)
Ejemplo n.º 6
0
    def test_no_product(self):
        """ Verify that None is returned if there is no product. """
        voucher = VoucherFactory(code='NOPRODUCT')
        offer = ConditionalOfferFactory()
        voucher.offers.add(offer)
        voucher, product = get_voucher_from_code(code='NOPRODUCT')

        self.assertIsNotNone(voucher)
        self.assertEqual(voucher.code, 'NOPRODUCT')
        self.assertIsNone(product)
Ejemplo n.º 7
0
    def test_get_voucher_from_code(self):
        """ Verify that get_voucher_from_code() returns product and voucher. """
        original_voucher, original_product = prepare_voucher(code=COUPON_CODE)
        voucher, product = get_voucher_from_code(code=COUPON_CODE)

        self.assertIsNotNone(voucher)
        self.assertEqual(voucher, original_voucher)
        self.assertEqual(voucher.code, COUPON_CODE)
        self.assertIsNotNone(product)
        self.assertEqual(product, original_product)
Ejemplo n.º 8
0
 def test_get_non_existing_voucher(self):
     """ Verify that get_voucher_from_code() raises exception for a non-existing voucher. """
     with self.assertRaises(Voucher.DoesNotExist):
         get_voucher_from_code(code='INVALID')
Ejemplo n.º 9
0
 def test_get_non_existing_voucher(self):
     """ Verify that get_voucher_from_code() returns None for a non-existing voucher. """
     voucher, product = get_voucher_from_code(code='INVALID')
     self.assertIsNone(voucher)
     self.assertIsNone(product)
Ejemplo n.º 10
0
 def test_get_non_existing_voucher(self):
     """ Verify that get_voucher_from_code() raises exception for a non-existing voucher. """
     with self.assertRaises(Voucher.DoesNotExist):
         get_voucher_from_code(code='INVALID')