Example #1
0
 def setUp(self):
     super(VoucherViewSetTests, self).setUp()
     self.user = self.create_user(is_staff=True)
     self.client.login(username=self.user.username, password=self.password)
     voucher1 = VoucherFactory()
     voucher1.offers.add(ConditionalOfferFactory())
     self.voucher = VoucherFactory(code=COUPON_CODE)
     self.voucher.offers.add(ConditionalOfferFactory(name='test2'))
Example #2
0
    def test_multiple_vouchers(self):
        """ Verify only the last entered voucher is contained in the basket. """
        product = ProductFactory()
        voucher1 = VoucherFactory(code='FIRST')
        basket = prepare_basket(self.request, product, voucher1)
        self.assertEqual(basket.vouchers.count(), 1)
        self.assertEqual(basket.vouchers.first(), voucher1)

        voucher2 = VoucherFactory(code='SECOND')
        new_basket = prepare_basket(self.request, product, voucher2)
        self.assertEqual(basket, new_basket)
        self.assertEqual(new_basket.vouchers.count(), 1)
        self.assertEqual(new_basket.vouchers.first(), voucher2)
Example #3
0
 def test_get_enterprise_customer_uuid_non_existing_conditional_offer(self):
     """
     Verify that None is returned if voucher exists but conditional offer
     does not exist.
     """
     voucher = VoucherFactory()
     self.assertIsNone(get_enterprise_customer_uuid(voucher.code))
Example #4
0
    def test_no_product(self):
        """ Verify that an exception is raised if there is no product. """
        voucher = VoucherFactory()
        offer = ConditionalOfferFactory()
        voucher.offers.add(offer)

        with self.assertRaises(exceptions.ProductNotFoundError):
            get_voucher_and_products_from_code(code=voucher.code)
Example #5
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(code='NOPRODUCT')
     self.assertIsNotNone(voucher)
     self.assertEqual(voucher.code, 'NOPRODUCT')
     self.assertIsNone(product)
Example #6
0
 def test_successful_response(self):
     """ Verify a successful response is returned. """
     voucher = VoucherFactory(code='ENROLLMENT')
     order = OrderFactory(user=self.user)
     line = OrderLineFactory(order=order)
     order_line_vouchers = OrderLineVouchers.objects.create(line=line)
     order_line_vouchers.vouchers.add(voucher)
     response = self.client.get(reverse(self.path, args=[order.number]))
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response['content-type'], 'text/csv')
 def setUp(self):
     self.basket = create_basket(empty=True)
     self.product = create_product(price=100)
     range = RangeFactory(products=[self.product])
     condition = ConditionFactory(range=range, value=2)
     self.voucher = VoucherFactory()
     self.voucher.offers.add(
         create_offer(offer_type='Voucher',
                      range=range,
                      condition=condition))
Example #8
0
    def test_set_enterprise_cookie_no_enterprise_customer(self):
        """
        Validate that no cookie is set if no enterprise customer is
        associated with the voucher of given code.
        """
        voucher = VoucherFactory()
        request = RequestFactory().get('/', data={'code': voucher.code})
        request.site = self.site
        response = decorators.set_enterprise_cookie(self._mock_view)(request)

        self.assertNotIn(settings.ENTERPRISE_CUSTOMER_COOKIE_NAME, response.cookies)
    def test_multiple_related_offers_not_deleted(self):
        # Voucher with already used offer
        voucher_name = "Voucher 1"
        offer = create_offer(name=_("Offer for voucher '%s'") % voucher_name,
                             offer_type='Voucher',
                             range=self.offer_range,
                             condition=self.offer_condition)

        voucher1 = VoucherFactory(name=voucher_name, code="VOUCHER1")
        voucher1.offers.add(offer)

        voucher2 = VoucherFactory(name="Voucher 2", code="VOUCHER2")
        voucher2.offers.add(offer)

        offer_ids = list(voucher1.offers.all().values_list('pk', flat=True))

        voucher1.delete()
        count_offers = ConditionalOffer.objects.filter(
            id__in=offer_ids).count()
        assert len(offer_ids) == count_offers
Example #10
0
 def test_changing_offer_type_for_voucher_offer_with_vouchers(self):
     offer = ConditionalOfferFactory(offer_type=ConditionalOffer.VOUCHER)
     VoucherFactory().offers.add(offer)
     data = {
         'name': offer.name,
         'description': offer.description,
         'offer_type': ConditionalOffer.SITE,
     }
     form = forms.MetaDataForm(data, instance=offer)
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors['offer_type'][0],
                      "This can only be changed if it has no vouchers attached to it")
Example #11
0
    def test_successful_response(self, product_title):
        """ Verify a successful response is returned. """
        voucher = VoucherFactory()
        order = OrderFactory(user=self.user)
        product = ProductFactory(title=product_title, categories=[])
        line = OrderLineFactory(order=order, product=product)
        order_line_vouchers = OrderLineVouchers.objects.create(line=line)
        order_line_vouchers.vouchers.add(voucher)

        response = self.client.get(reverse(self.path, args=[order.number]))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-type'], 'text/csv')
Example #12
0
 def test_prepare_basket_with_bundle_voucher(self):
     """
     Test prepare_basket clears vouchers for a bundle
     """
     product = ProductFactory()
     voucher = VoucherFactory(code='FIRST')
     request = self.request
     basket = prepare_basket(request, [product], voucher)
     self.assertTrue(basket.vouchers.all())
     request.GET = {'bundle': 'test_bundle'}
     basket = prepare_basket(request, [product])
     self.assertFalse(basket.vouchers.all())
    def test_is_voucher_applied(self):
        """
        Verify is_voucher_applied return correct value.
        """
        product = ProductFactory(stockrecords__price_excl_tax=100)
        voucher, product = prepare_voucher(
            _range=RangeFactory(products=[product]), benefit_value=10)
        basket = prepare_basket(self.request, [product], voucher)

        # Verify is_voucher_applied returns True when voucher is applied to the basket.
        self.assertTrue(is_voucher_applied(basket, voucher))

        # Verify is_voucher_applied returns False when voucher can not be applied to the basket.
        self.assertFalse(is_voucher_applied(basket, VoucherFactory()))
    def test_post_valid(self):
        basket = BasketFactory()
        voucher = VoucherFactory(num_basket_additions=5)
        basket.vouchers.add(voucher)

        data = {'code': voucher.code}
        request = RequestFactory().post('/', data=data, basket=basket)

        view = views.VoucherRemoveView.as_view()
        response = view(request, pk=voucher.pk)
        self.assertEqual(response.status_code, 302)

        voucher = voucher.__class__.objects.get(pk=voucher.pk)
        self.assertEqual(voucher.num_basket_additions, 4)
    def test_related_offer_different_name_not_deleted(self):
        # Voucher with offer named differently
        voucher = VoucherFactory(name="Voucher", code="VOUCHER")
        voucher.offers.add(
            create_offer(name="Different name test",
                         offer_type='Voucher',
                         range=self.offer_range,
                         condition=self.offer_condition))

        offer_ids = list(voucher.offers.all().values_list('pk', flat=True))

        voucher.delete()
        count_offers = ConditionalOffer.objects.filter(
            id__in=offer_ids).count()
        assert len(offer_ids) == count_offers
    def test_related_offer_deleted(self):
        # Voucher with offer name corresponding to it as used in the dashboard
        voucher_name = "Voucher"
        voucher = VoucherFactory(name=voucher_name, code="VOUCHER")
        voucher.offers.add(
            create_offer(name=_("Offer for voucher '%s'") % voucher_name,
                         offer_type='Voucher',
                         range=self.offer_range,
                         condition=self.offer_condition))

        voucher.delete()
        self.assertFalse(
            ConditionalOffer.objects.filter(
                name=_("Offer for voucher '%s'") % voucher_name,
                offer_type=ConditionalOffer.VOUCHER).exists())
Example #17
0
    def prepare_voucher(self, range_=None, start_datetime=None, benefit_value=100, benefit_type='Percentage'):
        """ Create a voucher and add an offer to it that contains a created product. """
        if range_ is None:
            product = ProductFactory(title='Test product')
            range_ = RangeFactory(products=[product, ])
        else:
            product = range_.all_products()[0]

        if start_datetime is None:
            start_datetime = now() - datetime.timedelta(days=1)

        voucher = VoucherFactory(code='COUPONTEST', start_datetime=start_datetime, usage=Voucher.SINGLE_USE)
        benefit = BenefitFactory(type=benefit_type, range=range_, value=benefit_value)
        offer = ConditionalOfferFactory(benefit=benefit)
        voucher.offers.add(offer)
        return voucher, product
Example #18
0
    def test_post_valid(self):
        basket = BasketFactory()
        voucher = VoucherFactory()
        self.assertTrue(voucher.is_active())

        data = {
            'code': voucher.code
        }
        request = RequestFactory().post('/', data=data, basket=basket)

        view = views.VoucherAddView.as_view()
        response = view(request)
        self.assertEqual(response.status_code, 302)

        voucher = voucher.__class__.objects.get(pk=voucher.pk)
        self.assertEqual(voucher.num_basket_additions, 1, msg=self._get_voucher_message(request))
    def test_related_offer_different_type_not_deleted(self):
        # Voucher with offer not of type "Voucher"
        voucher_name = "Voucher"
        voucher = VoucherFactory(name=voucher_name, code="VOUCHER")
        voucher.offers.add(
            create_offer(name=_("Offer for voucher '%s'") % voucher_name,
                         offer_type='Site',
                         range=self.offer_range,
                         condition=self.offer_condition))

        offer_ids = list(voucher.offers.all().values_list('pk', flat=True))

        voucher.delete()
        count_offers = ConditionalOffer.objects.filter(
            id__in=offer_ids).count()
        assert len(offer_ids) == count_offers
Example #20
0
    def test_post_valid(self):
        basket = BasketFactory()
        voucher = VoucherFactory()
        assert voucher.is_active()

        data = {
            'code': voucher.code
        }
        request = RequestFactory().post('/', data=data, basket=basket)

        view = views.VoucherAddView.as_view()
        response = view(request)
        assert response.status_code == 302

        voucher = voucher.__class__.objects.get(pk=voucher.pk)
        assert voucher.num_basket_additions == 1, (
            [unicode(m.message) for m in get_messages(request)])
Example #21
0
    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': benefit.type,
                '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,
        })
Example #22
0
    def test_grouped_voucher_discounts(self):
        voucher = VoucherFactory()
        offer1 = ConditionalOfferFactory(name='offer1')
        offer1.set_voucher(voucher)
        result1 = models.BasketDiscount(D('2.00'))

        offer2 = ConditionalOfferFactory(name='offer2')
        offer2.set_voucher(voucher)
        result2 = models.BasketDiscount(D('1.00'))

        self.applications.add(offer1, result1)
        self.applications.add(offer2, result2)

        assert len(self.applications) == 2

        discounts = self.applications.grouped_voucher_discounts
        discounts = [x for x in discounts]
        assert len(discounts) == 1
        assert discounts[0]['voucher'] == voucher
        assert discounts[0]['discount'] == D('3.00')
Example #23
0
    def test_coupon_product(self):
        """Test if a coupon product is properly created."""
        coupon_product_class, _ = ProductClass.objects.get_or_create(
            name='coupon')
        coupon_product = ProductFactory(product_class=coupon_product_class,
                                        title='Test product')
        voucher = VoucherFactory(code='MYVOUCHER')
        voucherList = CouponVouchers.objects.create(coupon=coupon_product)
        voucherList.vouchers.add(voucher)
        coupon_product.attr.coupon_voucher = voucherList

        # clean() is an Oscar validation method for products
        self.assertIsNone(coupon_product.clean())
        self.assertIsInstance(coupon_product, Product)
        self.assertEqual(coupon_product.title, 'Test product')
        self.assertEqual(coupon_product.attr.coupon_voucher.vouchers.count(),
                         1)
        self.assertEqual(
            coupon_product.attr.coupon_voucher.vouchers.first().code,
            'MYVOUCHER')
Example #24
0
    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 = {
            'start': None,
            'image': None,
        }
        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'], None)
Example #25
0
 def test_get_enterprise_customer_uuid_non_existing_voucher(self):
     """
     Verify that None is returned when voucher with given code does not exist.
     """
     voucher = VoucherFactory()
     self.assertIsNone(get_enterprise_customer_uuid(voucher.code))
 def setUp(self):
     self.voucher = VoucherFactory(usage=Voucher.ONCE_PER_CUSTOMER)
 def setUp(self):
     self.voucher = VoucherFactory(usage=Voucher.MULTI_USE)
 def setUp(self):
     self.voucher = VoucherFactory(start_datetime=START_DATETIME,
                                   end_datetime=END_DATETIME)
 def test_saves_code_as_uppercase(self):
     voucher = VoucherFactory(code='lower',
                              start_datetime=START_DATETIME,
                              end_datetime=END_DATETIME)
     self.assertEqual('LOWER', voucher.code)
    def setUp(self):
        """
        Create test data.
        """
        super(MigrateEnterpriseConditionalOffersTests, self).setUp()

        # Set up vouchers that relate to a range with a enterprise_customer
        uuid = '123e4567-e89b-12d3-a456-426655440000'
        range_with_ent_customer = RangeFactory(enterprise_customer=uuid)
        condition = ConditionFactory(range=range_with_ent_customer)
        benefit_percent = BenefitFactory(
            range=range_with_ent_customer,
            type='Percentage',
            value=10.00,
        )
        benefit_absolute = BenefitFactory(
            range=range_with_ent_customer,
            type='Absolute',
            value=47,
        )

        for i in range(2):
            code = '{}EntUserPercentBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk,
                                                    benefit_percent.type,
                                                    benefit_percent.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit_percent,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        for i in range(2):
            code = '{}EntUserAbsoluteBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk,
                                                    benefit_absolute.type,
                                                    benefit_absolute.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit_absolute,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        # Set up vouchers that do not relate to a range with an enterprise_customer
        range_no_ent_customer = RangeFactory()
        condition = ConditionFactory(range=range_no_ent_customer)
        benefit = BenefitFactory(
            range=range_no_ent_customer,
            type='Percentage',
            value=10.00,
        )

        for i in range(3):
            code = '{}NoEntUserPercentBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk, benefit.type,
                                                    benefit.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        assert Voucher.objects.filter(
            offers__condition__range__enterprise_customer__isnull=False).count(
            ) == 4

        assert Voucher.objects.filter(
            offers__condition__range__enterprise_customer__isnull=True).count(
            ) == 3

        self.command = Command()