def test_track_completed_discounted_order_with_voucher_with_offer(self): with mock.patch( 'ecommerce.extensions.checkout.signals.track_segment_event' ) as mock_track: # Orders may be discounted by a fixed value fixed_benefit = 5.00 offer_discount = 6 product = ProductFactory(categories=[], stockrecords__price_currency='USD') _range = factories.RangeFactory(products=[product], ) voucher, product = prepare_voucher(_range=_range, benefit_value=fixed_benefit, benefit_type=Benefit.FIXED) factories.ConditionalOfferFactory( offer_type=ConditionalOffer.SITE, benefit=factories.BenefitFactory(range=_range, value=offer_discount), condition=factories.ConditionFactory(type=Condition.COVERAGE, value=1, range=_range)) basket = BasketFactory(owner=self.user, site=self.site) basket.add_product(product) basket.vouchers.add(voucher) Applicator().apply(basket, user=basket.owner, request=self.request) order = factories.create_order(basket=basket, user=self.user) track_completed_order(None, order) properties = self._generate_event_properties(order, voucher) mock_track.assert_called_once_with(order.site, order.user, 'Order Completed', properties)
def test_track_completed_discounted_order_with_offer(self): """ An event including a discount but no coupon should be sent to Segment""" with mock.patch( 'ecommerce.extensions.checkout.signals.track_segment_event' ) as mock_track: # Orders may be discounted by a fixed value offer_discount = 5 product = ProductFactory(categories=[], stockrecords__price_currency='USD') _range = factories.RangeFactory(products=[product], ) site_offer = factories.ConditionalOfferFactory( offer_type=ConditionalOffer.SITE, benefit=factories.BenefitFactory(range=_range, value=offer_discount), condition=factories.ConditionFactory(type=Condition.COVERAGE, value=1, range=_range)) basket = BasketFactory(owner=self.user, site=self.site) basket.add_product(product) Applicator().apply_offers(basket, [site_offer]) order = factories.create_order(basket=basket, user=self.user) track_completed_order(None, order) properties = self._generate_event_properties(order) mock_track.assert_called_once_with(order.site, order.user, 'Order Completed', properties)
def test_track_completed_discounted_order_with_voucher(self): """ An event including coupon information should be sent to Segment""" with mock.patch( 'ecommerce.extensions.checkout.signals.track_segment_event' ) as mock_track: # Orders may be discounted by percent percent_benefit = 66 product = ProductFactory(categories=[], stockrecords__price_currency='USD') _range = factories.RangeFactory(products=[product], ) voucher, product = prepare_voucher(_range=_range, benefit_value=percent_benefit) basket = factories.BasketFactory(owner=self.user, site=self.site) basket.add_product(product) basket.vouchers.add(voucher) Applicator().apply(basket, user=basket.owner, request=self.request) order = factories.create_order(basket=basket, user=self.user) track_completed_order(None, order) properties = self._generate_event_properties(order, voucher) mock_track.assert_called_once_with(order.site, order.user, 'Order Completed', properties, traits=properties)
def test_apply(self, discount_param, jwt_decode_handler, request): # pylint: disable=unused-argument request_type = discount_param[0] discount_percent = discount_param[1] discount_jwt = { 'discount_applicable': True, 'discount_percent': discount_percent } mock_kwargs = { 'method': request_type, request_type: { 'discount_jwt': discount_jwt } } request.return_value = Mock(**mock_kwargs) basket = BasketFactory(site=self.site, owner=self.create_user()) product = ProductFactory(categories=[], stockrecords__price_currency='USD') basket.add_product(product) Applicator().apply(basket) if discount_percent is not None: # Our discount should be applied self.assertEqual(len(basket.offer_applications), 1) benefit = basket.offer_discounts[0].get('offer').benefit self.assertEqual( basket.total_discount, benefit.round(discount_jwt['discount_percent'] / Decimal('100') * basket.total_incl_tax_excl_discounts)) else: self.assertEqual(len(basket.offer_applications), 0)
def test_is_satisfied_quantity_more_than_1(self, request): # pylint: disable=unused-argument """ This discount should not apply if are buying more than one of the same course. """ product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.basket.add_product(product, quantity=2) self.assertFalse(self.condition.is_satisfied(self.offer, self.basket))
def test_is_satisfied_not_seat_product(self, request): # pylint: disable=unused-argument """ This discount should not apply if are not purchasing a seat product. """ product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.basket.add_product(product) self.assertFalse(self.condition.is_satisfied(self.offer, self.basket))
def setUp(self): super(EnterpriseCustomerConditionTests, self).setUp() Switch.objects.update_or_create(name=ENTERPRISE_OFFERS_SWITCH, defaults={'active': True}) self.user = factories.UserFactory() self.condition = factories.EnterpriseCustomerConditionFactory() self.test_product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.course_run = CourseFactory(partner=self.partner) self.course_run.create_or_update_seat('verified', True, Decimal(100))
def setUp(self): super(EnterpriseCustomerConditionTests, self).setUp() self.user = UserFactory() self.condition = factories.EnterpriseCustomerConditionFactory() self.test_product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.course_run = CourseFactory(partner=self.partner) self.course_run.create_or_update_seat('verified', True, Decimal(100))
def test_restricted_course_mode(self, mode): """Test that an exception is raised when a black-listed course mode is used.""" course = CourseFactory(id='black/list/mode') seat = course.create_or_update_seat(mode, False, 0, self.partner) # Seats derived from a migrated "audit" mode do not have a certificate_type attribute. if mode == 'audit': seat = ProductFactory() self.data.update({'stock_record_ids': [StockRecord.objects.get(product=seat).id]}) self.assert_post_response_status(self.data)
def test_restricted_course_mode(self, mode): """Test that an exception is raised when a black-listed course mode is used.""" course = CourseFactory(id='black/list/mode') seat = course.create_or_update_seat(mode, False, 0, self.partner) # Seats derived from a migrated "audit" mode do not have a certificate_type attribute. if mode == 'audit': seat = ProductFactory() self.data.update({'stock_record_ids': [StockRecord.objects.get(product=seat).id]}) response = self.client.post(COUPONS_LINK, data=self.data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_is_satisfied_with_non_seat_type_product(self): """ Test `ManualEnrollmentOrderDiscountCondition.is_satisfied` works as expected when there basket contains non seat type product. """ product = ProductFactory() self.basket.add_product(product) offer = ManualEnrollmentOrderOfferFactory() status = self.condition.is_satisfied(offer, self.basket) assert not status
def test_is_satisfied_true(self, discount_jwt, jwt_decode_handler, request): # pylint: disable=unused-argument request.return_value = Mock(method='GET', GET={'discount_jwt': discount_jwt}) product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.basket.add_product(product) if discount_jwt and discount_jwt.get('discount_applicable') is True: self.assertTrue( self.condition.is_satisfied(self.offer, self.basket)) else: self.assertFalse( self.condition.is_satisfied(self.offer, self.basket))
def setUp(self): super(EnterpriseCustomerConditionTests, self).setUp() self.user = UserFactory() self.condition = factories.EnterpriseCustomerConditionFactory() self.test_product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.course_run = CourseFactory(partner=self.partner) self.course_run.create_or_update_seat('verified', True, Decimal(100)) self.entitlement = create_or_update_course_entitlement( 'verified', 100, self.partner, 'edX-DemoX', 'edX Demo Entitlement' ) self.entitlement_stock_record = StockRecord.objects.filter(product=self.entitlement).first() self.entitlement_catalog = Catalog.objects.create(partner=self.partner) self.entitlement_catalog.stock_records.add(self.entitlement_stock_record)
def setUp(self): super(ProgramCourseRunSeatsConditionTests, self).setUp() self.condition = factories.ProgramCourseRunSeatsConditionFactory() self.test_product = ProductFactory()
def test_generate_sku_with_unexpected_product_class(self): """Verify the method raises an exception for unsupported product class.""" product = ProductFactory() with self.assertRaises(Exception): generate_sku(product, self.partner)
def setUp(self): super(ProgramCourseRunSeatsConditionTests, self).setUp() self.condition = factories.ProgramCourseRunSeatsConditionFactory() self.test_product = ProductFactory(stockrecords__price_excl_tax=10, categories=[]) self.site.siteconfiguration.enable_partial_program = True
def test_no_voucher_error_msg(self): """ Verify correct error message is returned when voucher can't be found. """ self.basket.add_product(ProductFactory()) self.assert_form_valid_message( "Coupon code '{code}' does not exist.".format(code=COUPON_CODE))