def test_basket_calculate_by_staff_user_invalid_username(self, mocked_logger): """Verify that a staff user passing an invalid username gets a response about themselves and an error is logged about a non existant user """ self.site_configuration.enable_partial_program = True self.site_configuration.save() offer = ProgramOfferFactory( site=self.site, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100), condition=ProgramCourseRunSeatsConditionFactory() ) program_uuid = offer.condition.program_uuid program = self.mock_program_detail_endpoint(program_uuid, self.site_configuration.discovery_api_url) differentuser = self.create_user(username='******', is_staff=False) products = self._get_program_verified_seats(program) url = self._generate_sku_username_url(products, 'invalidusername') enrollment = [{'mode': 'verified', 'course_details': {'course_id': program['courses'][0]['key']}}] self.mock_user_data(differentuser.username, enrollment) expected = { 'total_incl_tax_excl_discounts': sum(product.stockrecords.first().price_excl_tax for product in products[1:]), 'total_incl_tax': Decimal('300.00'), 'currency': 'USD' } with self.assertRaises(Exception): response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue(mocked_logger.called) self.assertEqual(response.data, expected)
def test_basket_calculate_by_staff_user_invalid_username(self, mock_get_lms_resource_for_user, mock_logger): """Verify that a staff user passing an invalid username gets a response the anonymous basket and an error is logged about a non existent user """ self.site_configuration.enable_partial_program = True self.site_configuration.save() offer = ProgramOfferFactory( site=self.site, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100), condition=ProgramCourseRunSeatsConditionFactory() ) program_uuid = offer.condition.program_uuid program = self.mock_program_detail_endpoint(program_uuid, self.site_configuration.discovery_api_url) products = self._get_program_verified_seats(program) url = self._generate_sku_url(products, username='******') expected = { 'total_incl_tax_excl_discounts': sum(product.stockrecords.first().price_excl_tax for product in products[1:]), 'total_incl_tax': Decimal('300.00'), 'currency': 'USD' } with self.assertRaises(Exception): response = self.client.get(url) self.assertFalse( mock_get_lms_resource_for_user.called, msg='LMS calls should be skipped for anonymous case.' ) self.assertEqual(response.status_code, 200) self.assertTrue(mock_logger.called) self.assertEqual(response.data, expected)
def setup_other_user_basket_calculate(self): """ Sets up basket calculate for another user. Returns: products, url: The product list and the url for the anonymous basket calculate. """ self.site_configuration.enable_partial_program = True self.site_configuration.save() offer = ProgramOfferFactory( partner=self.partner, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100), condition=ProgramCourseRunSeatsConditionFactory()) program_uuid = offer.condition.program_uuid program = self.mock_program_detail_endpoint( program_uuid, self.site_configuration.discovery_api_url) different_user = self.create_user(username='******', is_staff=False) products = self._get_program_verified_seats(program) url = self._generate_sku_url( products, username=different_user.username) + '&bundle={}'.format( program_uuid) enrollment = [{ 'mode': 'verified', 'course_details': { 'course_id': program['courses'][0]['key'] } }] self.mock_user_data(different_user.username, owned_products=enrollment) return products, url
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 non bundle offers are returned if no bundle id is given. """ ProgramOfferFactory() 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 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_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() # pylint: disable=protected-access self.assert_correct_offers(site_offers) # Verify there was no attempt to match off a bundle self.assertFalse(self.applicator._get_program_offers.called) # pylint: disable=protected-access
def test_log_is_fired_when_get_offers_with_bundle(self): """ Verify that logs are fired when bundle id is given and offers are being applied""" program_offers = [ProgramOfferFactory()] bundle_id = program_offers[0].condition.program_uuid self.create_bundle_attribute(bundle_id) with LogCapture(LOGGER_NAME) as l: self.assert_correct_offers(program_offers) l.check(( LOGGER_NAME, 'WARNING', 'CustomApplicator processed Basket [{}] from Request [{}] and User [{}] with a bundle.' .format(self.basket, None, self.user), ))
def test_basket_calculate_program_offer(self): """ Verify successful basket calculation with a program offer """ offer = ProgramOfferFactory(benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100)) program_uuid = offer.condition.program_uuid self.mock_program_detail_endpoint(program_uuid) response = self.client.get(self.url) expected = { 'total_incl_tax_excl_discounts': self.product_total, 'total_incl_tax': self.product_total, 'currency': 'GBP' } self.assertEqual(response.status_code, 200) self.assertEqual(response.data, expected)
def _setup_anonymous_basket_calculate(self): """ Sets up anonymous basket calculate. Returns: products, url: The product list and the url for the anonymous basket calculate. """ self.site_configuration.enable_partial_program = True self.site_configuration.save() offer = ProgramOfferFactory( site=self.site, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100), condition=ProgramCourseRunSeatsConditionFactory()) program_uuid = offer.condition.program_uuid program = self.mock_program_detail_endpoint( program_uuid, self.site_configuration.discovery_api_url) products = self._get_program_verified_seats(program) url = self._generate_sku_url(products, username=None) return products, url
def test_basket_calculate_program_offer_unrelated_bundle_id(self): """ Test that if bundle id is present and skus do not belong to this bundle, program offers do not apply """ offer = ProgramOfferFactory( site=self.site, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=100)) program_uuid = offer.condition.program_uuid self.mock_program_detail_endpoint( program_uuid, self.site_configuration.discovery_api_url) self.mock_user_data(self.user.username) response = self.client.get(self.url + '&bundle={}'.format(program_uuid)) expected = { 'total_incl_tax_excl_discounts': self.product_total, 'total_incl_tax': self.product_total, 'currency': 'GBP' } self.assertEqual(response.status_code, 200) self.assertEqual(response.data, expected)
def _create_program_with_courses_and_offer(self): offer = ProgramOfferFactory( site=self.site, partner=self.site.siteconfiguration.partner, benefit=PercentageDiscountBenefitWithoutRangeFactory(value=20)) program_uuid = offer.condition.program_uuid program_data = self.mock_program_detail_endpoint( program_uuid, self.site_configuration.discovery_api_url) self.mock_user_data(self.user.username) sku_list = [] products = [] for course in program_data['courses']: sku_list.append(course['entitlements'][0]['sku']) for sku in sku_list: products.append( factories.ProductFactory( stockrecords__partner=self.partner, stockrecords__price_excl_tax=Decimal('10.00'), stockrecords__partner_sku=sku, )) return products, program_uuid