def test_get_enterprise_offers(self, enterprise_id, num_expected_offers): """ Verify get_enterprise_offers returns correct objects based on filter""" uuid = enterprise_id or uuid4() for _ in range(2): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=uuid) ConditionalOfferFactory(condition=condition) for _ in range(3): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=None) ConditionalOfferFactory(condition=condition) # Make some condition offers with a uuid other than ours for _ in range(4): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=uuid4()) ConditionalOfferFactory(condition=condition) with mock.patch( 'ecommerce.extensions.offer.applicator.get_enterprise_id_for_user' ) as mock_ent_id: mock_ent_id.return_value = enterprise_id enterprise_offers = self.applicator.get_enterprise_offers( 'some-site', self.user) if num_expected_offers == 0: assert not enterprise_offers else: assert enterprise_offers.count() == num_expected_offers
def test_get_offers_with_bundle(self): """ Verify that only offers related to the bundle id are returned. """ program_offers = [ProgramOfferFactory()] bundle_id = program_offers[0].condition.program_uuid self.create_bundle_attribute(bundle_id) ConditionalOfferFactory.create_batch(2) # Unrelated offers that should not be returned self.applicator.get_site_offers = mock.Mock() self.assert_correct_offers(program_offers) self.assertFalse(self.applicator.get_site_offers.called) # Verify there was no attempt to get all site offers
def test_get_site_offers(self): """ Verify get_site_offers returns correct objects based on filter""" uuid = uuid4() for _ in range(2): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=uuid) ConditionalOfferFactory(condition=condition) for _ in range(3): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=None) ConditionalOfferFactory(condition=condition) assert self.applicator.get_site_offers().count() == 3
def test_get_site_offers(self): """ Verify get_site_offers returns correct objects based on filter""" existing_offers = list( ConditionalOffer.active.filter(offer_type=ConditionalOffer.SITE)) uuid = uuid4() for _ in range(2): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=uuid) ConditionalOfferFactory(condition=condition) for _ in range(3): condition = ConditionFactory(program_uuid=None, enterprise_customer_uuid=None) ConditionalOfferFactory(condition=condition) assert self.applicator.get_site_offers().count( ) == 3 + len(existing_offers)
def _create_enterprise_offer(): """ Return the enterprise offer. """ return ConditionalOfferFactory.create( benefit_id=EnterprisePercentageDiscountBenefitFactory.create().id, condition_id=EnterpriseCustomerConditionFactory.create().id, )
def test_get_offers_without_bundle(self): """ Verify that all non bundle offers are returned if no bundle id is given. """ offers_in_db = list( ConditionalOffer.active.filter(offer_type=ConditionalOffer.SITE)) site_offers = ConditionalOfferFactory.create_batch(3) + offers_in_db ProgramOfferFactory() # Verify that program offer was not returned without bundle_id self.assert_correct_offers(site_offers)
def test_get_offers_without_bundle(self): """ Verify that all offers are returned if no bundle id is given. """ site_offers = ConditionalOfferFactory.create_batch(3) self.applicator.get_program_offers = mock.Mock() self.assert_correct_offers(site_offers) self.assertFalse(self.applicator.get_program_offers.called ) # Verify there was no attempt to match off a bundle
def create_vouchers(self, partner=None, count=1): """Helper function that creates vouchers with a mocked coupon relation.""" vouchers = VoucherFactory.create_batch(count) partner = partner or self.partner coupon_vouchers = CouponVouchers.objects.create(coupon=ProductFactory( stockrecords__partner=partner)) for voucher in vouchers: voucher.offers.add(ConditionalOfferFactory()) coupon_vouchers.vouchers.add(voucher) return vouchers
def test_list_with_code_filter(self): """ Verify the endpoint list all vouchers, filtered by the specified code. """ voucher = VoucherFactory() voucher.offers.add(ConditionalOfferFactory()) url = '{path}?code={code}'.format(path=self.path, code=voucher.code) response = self.client.get(url) self.assertEqual(response.data['count'], 1) self.assertEqual(response.data['results'][0]['code'], voucher.code)
def test_save_edit(self, mock_discovery_call): """ Previously-created ConditionalOffer, Benefit, and Condition instances should be updated. """ mock_discovery_call.return_value = {"title": "test-journal"} journal_offer = ConditionalOfferFactory( benefit=PercentageDiscountBenefitWithoutRangeFactory(), condition=JournalConditionFactory()) data = self.generate_data( journal_bundle_uuid=journal_offer.condition.journal_bundle_uuid, benefit_type=Benefit.FIXED) form = JournalBundleOfferForm(request=self.request, data=data, instance=journal_offer) form.is_valid() form.save() journal_offer.refresh_from_db() self._assert_journal_bundle_offer_conditions( journal_offer, data['journal_bundle_uuid'], data['benefit_value'], data['benefit_type'], 'Journal Bundle Offer: test-journal')
def test_create_when_conditional_offer_with_uuid_exists(self): """ Verify a journal bundle offer can be created if a conditional offer with different type and same uuid already exists. """ data = self.generate_data() ConditionalOfferFactory( benefit=PercentageDiscountBenefitWithoutRangeFactory(), condition__journal_bundle_uuid=data['journal_bundle_uuid'], offer_type=ConditionalOffer.VOUCHER, ) form = JournalBundleOfferForm(request=self.request, data=data) self.assertTrue(form.is_valid())
def test_get_offers_without_bundle(self): """ Verify that all non bundle offers are returned if no bundle id is given. """ offers_in_db = list( ConditionalOffer.active.filter(offer_type=ConditionalOffer.SITE)) ProgramOfferFactory() site_offers = ConditionalOfferFactory.create_batch(3) + offers_in_db self.applicator.get_program_offers = mock.Mock() self.assert_correct_offers(site_offers) self.assertFalse(self.applicator.get_program_offers.called ) # Verify there was no attempt to match off a bundle
def test_init(self): """ Assert the init data from instance. """ journal_offer = ConditionalOfferFactory( benefit=PercentageDiscountBenefitWithoutRangeFactory(), condition=JournalConditionFactory()) form = JournalBundleOfferForm(instance=journal_offer) self.assertEqual(form['journal_bundle_uuid'].value(), journal_offer.condition.journal_bundle_uuid.hex) self.assertEqual(form['benefit_type'].value(), journal_offer.benefit.proxy().benefit_class_type) self.assertEqual(form['benefit_value'].value(), journal_offer.benefit.value)
def test_clean_with_conflicting_journal_uuid(self): """ Assert that an error should be raised if an offer already exists with same uuid and type. """ journal_offer = ConditionalOfferFactory( benefit=PercentageDiscountBenefitWithoutRangeFactory(), condition=JournalConditionFactory()) data = self.generate_data( journal_bundle_uuid=journal_offer.condition.journal_bundle_uuid) self._assert_form_errors( data, { 'journal_bundle_uuid': ['An offer already exists for this journal bundle'] })
def test_list(self): """ Verify the endpoint lists all vouchers. """ vouchers = VoucherFactory.create_batch(3) for voucher in vouchers: voucher.offers.add(ConditionalOfferFactory()) response = self.client.get(self.path) self.assertEqual(response.data['count'], len(vouchers)) actual_codes = [datum['code'] for datum in response.data['results']] expected_codes = [voucher.code for voucher in vouchers] self.assertEqual(actual_codes, expected_codes)
def test_log_is_fired_when_get_offers_without_bundle(self): """ Verify that logs are fired when no bundle id is given but offers are being applied""" site_offers = ConditionalOfferFactory.create_batch(3) self.applicator.get_program_offers = mock.Mock() with LogCapture(LOGGER_NAME) as l: self.assert_correct_offers(site_offers) l.check(( LOGGER_NAME, 'WARNING', 'CustomApplicator processed Basket [{}] from Request [{}] and User [{}] without a bundle.' .format(self.basket, None, self.user), )) self.assertFalse(self.applicator.get_program_offers.called ) # Verify there was no attempt to match off a bundle
def test_log_is_fired_when_get_offers_without_bundle(self): """ Verify that logs are fired when no bundle id is given but offers are being applied""" existing_offers = list( ConditionalOffer.active.filter(offer_type=ConditionalOffer.SITE)) site_offers = ConditionalOfferFactory.create_batch(3) + existing_offers self.applicator._get_program_offers = mock.Mock() # pylint: disable=protected-access with LogCapture(LOGGER_NAME) as logger: self.assert_correct_offers(site_offers) logger.check(( LOGGER_NAME, 'WARNING', 'CustomApplicator processed Basket [{}] from Request [{}] and User [{}] without a bundle.' .format(self.basket, None, self.user), )) # Verify there was no attempt to match off a bundle self.assertFalse(self.applicator._get_program_offers.called) # pylint: disable=protected-access