Ejemplo n.º 1
0
    def test_is_satisfied_program_without_entitlements(self):
        """
        User entitlements should not be retrieved if no course in the program has a course entitlement product
        """
        offer = factories.ProgramOfferFactory(partner=self.partner,
                                              condition=self.condition)
        basket = factories.BasketFactory(site=self.site,
                                         owner=factories.UserFactory())
        program = self.mock_program_detail_endpoint(
            self.condition.program_uuid,
            self.site_configuration.discovery_api_url,
            include_entitlements=False)
        enrollments = [{
            'mode': 'verified',
            'course_details': {
                'course_id': 'course-v1:test-org+course+1'
            }
        }, {
            'mode': 'verified',
            'course_details': {
                'course_id': 'course-v1:test-org+course+2'
            }
        }]
        entitlements_response = {
            "count":
            0,
            "num_pages":
            1,
            "current_page":
            1,
            "results": [{
                'mode': 'verified',
                'course_uuid': '268afbfc-cc1e-415b-a5d8-c58d955bcfc3'
            }, {
                'mode': 'verified',
                'course_uuid': '268afbfc-cc1e-415b-a5d8-c58d955bcfc4'
            }],
            "next":
            None,
            "start":
            0,
            "previous":
            None
        }
        self.mock_user_data(basket.owner.username, owned_products=enrollments)
        self.mock_user_data(basket.owner.username,
                            mocked_api='entitlements',
                            owned_products=entitlements_response)

        for course in program['courses'][2:len(program['courses']) - 1]:
            course_run = Course.objects.get(id=course['course_runs'][0]['key'])
            for seat in course_run.seat_products:
                if seat.attr.id_verification_required:
                    basket.add_product(seat)

        with mock.patch(
                'ecommerce.programs.conditions.deprecated_traverse_pagination'
        ) as mock_processing_entitlements:
            self.assertFalse(self.condition.is_satisfied(offer, basket))
            mock_processing_entitlements.assert_not_called()
Ejemplo n.º 2
0
    def test_get_lms_resource_for_user_caching_none(self):
        """
        LMS resource should be properly cached when enrollments is None.
        """
        basket = factories.BasketFactory(site=self.site,
                                         owner=factories.UserFactory())
        resource_name = 'test_resource_name'
        mock_endpoint = mock.Mock()
        mock_endpoint.get.return_value = None

        return_value = self.condition._get_lms_resource_for_user(
            basket, resource_name, mock_endpoint)  # pylint: disable=protected-access

        self.assertEqual(return_value, [])
        self.assertEquals(mock_endpoint.get.call_count, 1,
                          'Endpoint should be called before caching.')

        mock_endpoint.reset_mock()

        return_value = self.condition._get_lms_resource_for_user(
            basket, resource_name, mock_endpoint)  # pylint: disable=protected-access

        self.assertEqual(return_value, [])
        self.assertEquals(mock_endpoint.get.call_count, 0,
                          'Endpoint should NOT be called after caching.')
Ejemplo n.º 3
0
    def assert_condition(self, voucher_type, assignments, expected_condition_result):
        """
        Verify that condition works as expected for different vouchers and assignments.
        """
        for assignment in assignments:
            code = assignment['code']
            email = assignment['user_email']
            # In some cases individual assignments have their own expected result
            expected_condition_result = assignment.get('result', expected_condition_result)

            voucher = Voucher.objects.get(usage=voucher_type, code=code)
            basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory(email=email))
            basket.vouchers.add(voucher)

            is_condition_satisfied = self.condition.is_satisfied(voucher.enterprise_offer, basket)
            assert is_condition_satisfied == expected_condition_result

            # update the `num_orders` so that we can also verify the redemptions check
            # also update the offer assignment status
            if expected_condition_result:
                voucher.num_orders += 1
                voucher.save()
                assignment = OfferAssignment.objects.filter(
                    offer=voucher.enterprise_offer, code=code, user_email=email
                ).exclude(
                    status__in=[OFFER_REDEEMED, OFFER_ASSIGNMENT_REVOKED]
                ).first()
                if assignment:
                    assignment.status = OFFER_REDEEMED
                    assignment.save()
Ejemplo n.º 4
0
    def setup_enterprise_coupon_data(self, mock_learner_api=True, use_new_enterprise=False):
        offer = factories.EnterpriseOfferFactory(
            partner=self.partner,
            condition=self.condition,
            offer_type=ConditionalOffer.VOUCHER
        )
        basket = factories.BasketFactory(site=self.site, owner=self.user)
        basket.add_product(self.course_run.seat_products[0])
        enterprise_id = self.condition.enterprise_customer_uuid
        if use_new_enterprise:
            enterprise_id = uuid4()  # pylint: disable=redefined-variable-type
        if mock_learner_api:
            self.mock_enterprise_learner_api(
                learner_id=self.user.id,
                enterprise_customer_uuid=str(enterprise_id),
                course_run_id=self.course_run.id,
            )
        else:
            self.mock_enterprise_learner_api_for_learner_with_no_enterprise()

        self.mock_catalog_contains_course_runs(
            [self.course_run.id],
            enterprise_id,
            enterprise_customer_catalog_uuid=self.condition.enterprise_customer_catalog_uuid,
        )
        return offer, basket
Ejemplo n.º 5
0
 def test_is_satisfied_program_retrieval_failure(self):
     """ The method should return False if no program is retrieved """
     offer = factories.ProgramOfferFactory(site=self.site, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory())
     basket.add_product(self.test_product)
     self.condition.program_uuid = None
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 6
0
 def test_is_satisfied_enterprise_learner_error(self):
     """ Ensure the condition returns false if the enterprise learner data cannot be retrieved. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api_raise_exception()
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 7
0
 def test_is_satisfied_no_enterprise_learner(self):
     """ Ensure the condition returns false if the learner is not linked to an EnterpriseCustomer. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api_for_learner_with_no_enterprise()
     self.assertFalse(self.condition.is_satisfied(offer, basket))
 def test_is_satisfied_empty_basket(self):
     """ Ensure the condition returns False if the basket is empty. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner,
                                              condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     self.assertTrue(basket.is_empty)
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 9
0
    def test_is_satisfied_when_owner_has_no_assignment(self):
        """
        Ensure that condition returns expected result the basket owner has no assignments.

        # voucher has free slots(3) available, no offer assignment for basket owner,
        # assignments(2) exist for other users, voucher has some redemptions(num_orders = 2)
        # basket owner is allowed to redeem the voucher
        """
        code = 'TA7WCQD3T4C7GHZ4'
        num_orders = 2
        max_global_applications = 7

        enterprise_offer = factories.EnterpriseOfferFactory(
            max_global_applications=max_global_applications)
        voucher = factories.VoucherFactory(usage=Voucher.MULTI_USE,
                                           code=code,
                                           num_orders=num_orders)
        voucher.offers.add(enterprise_offer)

        factories.OfferAssignmentFactory(offer=enterprise_offer,
                                         code=code,
                                         user_email='*****@*****.**')
        factories.OfferAssignmentFactory(offer=enterprise_offer,
                                         code=code,
                                         user_email='*****@*****.**')

        basket = factories.BasketFactory(
            site=self.site,
            owner=factories.UserFactory(email='*****@*****.**'))
        basket.vouchers.add(voucher)

        assert self.condition.is_satisfied(enterprise_offer, basket) is True
Ejemplo n.º 10
0
    def test_is_satisfied_with_different_users(self):
        """
        Ensure that condition returns expected result when wrong user is try to redeem the voucher.

        # code = 'ASD' assigned_to = '*****@*****.**'
        # code = 'ZXC' assigned_to = '*****@*****.**'
        # [email protected] try to redeem `ASD` code
        # `is_satisfied` should return False
        """
        voucher1 = factories.VoucherFactory(usage=Voucher.SINGLE_USE,
                                            code='ASD')
        voucher2 = factories.VoucherFactory(usage=Voucher.SINGLE_USE,
                                            code='ZXC')

        enterprise_offers = factories.EnterpriseOfferFactory.create_batch(2)
        voucher1.offers.add(enterprise_offers[0])
        voucher2.offers.add(enterprise_offers[1])

        basket = factories.BasketFactory(
            site=self.site,
            owner=factories.UserFactory(email='*****@*****.**'))
        basket.vouchers.add(voucher1)

        factories.OfferAssignmentFactory(offer=enterprise_offers[0],
                                         code=voucher1.code,
                                         user_email='*****@*****.**')
        factories.OfferAssignmentFactory(offer=enterprise_offers[1],
                                         code=voucher2.code,
                                         user_email='*****@*****.**')

        assert self.condition.is_satisfied(enterprise_offers[1],
                                           basket) is False
Ejemplo n.º 11
0
    def test_offer(self):
        # Our offer is for 100%, so all lines should end up with a price of 0.
        offer = factories.ProgramOfferFactory(benefit=factories.PercentageDiscountBenefitWithoutRangeFactory(value=100))
        basket = factories.BasketFactory(site=self.site, owner=self.create_user())

        program_uuid = offer.condition.program_uuid
        program = self.mock_program_detail_endpoint(program_uuid, self.site_configuration.discovery_api_url)

        # Add one course run seat from each course to the basket.
        for course in program['courses']:
            course_run = Course.objects.get(id=course['course_runs'][0]['key'])
            for seat in course_run.seat_products:
                if seat.attr.id_verification_required:
                    basket.add_product(seat)

        # No discounts should be applied, and each line should have a price of 100.00.
        self.assertEqual(len(basket.offer_applications), 0)
        self.assertEqual(basket.total_discount, 0)
        for line in basket.all_lines():
            self.assertEqual(line.line_price_incl_tax_incl_discounts, Decimal(100))

        # Apply the offers as Oscar will in a request
        basket.strategy = DefaultStrategy()
        Applicator().apply(basket, basket.owner)

        # Our discount should be applied, and each line should have a price of 0
        lines = basket.all_lines()
        self.assertEqual(len(basket.offer_applications), 1)
        self.assertEqual(basket.total_discount, Decimal(100) * len(lines))
        for line in lines:
            self.assertEqual(line.line_price_incl_tax_incl_discounts, 0)
Ejemplo n.º 12
0
 def test_is_satisfied_site_mismatch(self):
     """ Ensure the condition returns False if the offer site does not match the basket site. """
     offer = factories.EnterpriseOfferFactory(
         site=SiteConfigurationFactory().site, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.test_product)
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 13
0
 def test_is_satisfied_free_basket(self):
     """ Ensure the basket returns False if the basket total is zero. """
     offer = factories.ProgramOfferFactory(site=self.site, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory())
     test_product = factories.ProductFactory(stockrecords__price_excl_tax=0,
                                             stockrecords__partner__short_code='test')
     basket.add_product(test_product)
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 14
0
    def test_offer(self):
        # Our offer is for 100%, so all lines should end up with a price of 0.
        offer = factories.ProgramOfferFactory(
            site=self.site,
            benefit=factories.PercentageDiscountBenefitWithoutRangeFactory(value=100)
        )
        basket = factories.BasketFactory(site=self.site, owner=self.create_user())

        program_uuid = offer.condition.program_uuid
        program = self.mock_program_detail_endpoint(program_uuid, self.site_configuration.discovery_api_url)
        self.mock_user_data(basket.owner.username)

        # Add one course run seat from each course to the basket.
        products = []
        for course in program['courses']:
            course_run = Course.objects.get(id=course['course_runs'][0]['key'])
            for seat in course_run.seat_products:
                if seat.attr.id_verification_required:
                    products.append(seat)
                    basket.add_product(seat)

        # No discounts should be applied, and each line should have a price of 100.00.
        self.assertEqual(len(basket.offer_applications), 0)
        self.assertEqual(basket.total_discount, 0)
        for line in basket.all_lines():
            self.assertEqual(line.line_price_incl_tax_incl_discounts, Decimal(100))

        # Apply the offers as Oscar will in a request
        basket.strategy = DefaultStrategy()
        Applicator().apply(basket, basket.owner)

        # Our discount should be applied, and each line should have a price of 0
        lines = basket.all_lines()
        self.assertEqual(len(basket.offer_applications), 1)
        self.assertEqual(basket.total_discount, Decimal(100) * len(lines))
        for line in lines:
            self.assertEqual(line.line_price_incl_tax_incl_discounts, 0)

        # Reset the basket and add a voucher.
        basket.reset_offer_applications()
        product_range = RangeFactory(products=products)
        voucher, __ = factories.prepare_voucher(_range=product_range, benefit_value=50)
        self.mock_account_api(self.request, basket.owner.username, data={'is_active': True})
        self.client.login(username=basket.owner.username, password=self.password)
        self.client.post(reverse('basket:vouchers-add'), data={'code': voucher.code})
        response = self.client.get(reverse('basket:summary'))
        basket = response.context['basket']

        # Verify that voucher-based offer takes precedence over program offer.
        actual_offer_discounts = [discount['offer'] for discount in basket.offer_discounts]
        actual_voucher_discounts = [discount['offer'] for discount in basket.voucher_discounts]
        self.assertEqual(actual_offer_discounts, [])
        self.assertEqual(actual_voucher_discounts, [voucher.offers.first()])
        lines = basket.all_lines()
        self.assertEqual(len(basket.offer_applications), 1)
        self.assertEqual(basket.total_discount, Decimal(50) * len(lines))
        for line in lines:
            self.assertEqual(line.line_price_incl_tax_incl_discounts, 50)
Ejemplo n.º 15
0
 def test_is_satisfied_site_mismatch(self):
     """ Ensure the condition returns False if the offer partner does not match the basket site partner. """
     offer = factories.ProgramOfferFactory(
         partner=SiteConfigurationFactory().partner,
         condition=self.condition)
     basket = factories.BasketFactory(site=self.site,
                                      owner=factories.UserFactory())
     basket.add_product(self.test_product)
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 16
0
    def test_is_satisfied_with_exception_for_programs(self, value):
        """ The method should return False if there is an exception when trying to get program details. """
        offer = factories.ProgramOfferFactory(site=self.site, condition=self.condition)
        basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory())
        basket.add_product(self.test_product)

        with mock.patch('ecommerce.programs.conditions.get_program',
                        side_effect=value):
            self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 17
0
    def test_is_satisfied_with_enrollments(self):
        """ The condition should be satisfied if one valid course run from each course is in either the
        basket or the user's enrolled courses and the site has enabled partial program offers. """
        offer = factories.ProgramOfferFactory(partner=self.partner,
                                              condition=self.condition)
        basket = factories.BasketFactory(site=self.site,
                                         owner=factories.UserFactory())
        program = self.mock_program_detail_endpoint(
            self.condition.program_uuid,
            self.site_configuration.discovery_api_url)

        # Extract one verified seat for each course
        verified_seats = []
        for course in program['courses']:
            course_run = Course.objects.get(id=course['course_runs'][0]['key'])
            for seat in course_run.seat_products:
                if seat.attr.id_verification_required:
                    verified_seats.append(seat)

        # Add verified enrollments for the first two program courses to the mock user data
        enrollments = [{
            'mode': 'verified',
            'course_details': {
                'course_id': program['courses'][0]['course_runs'][0]['key']
            }
        }, {
            'mode': 'verified',
            'course_details': {
                'course_id': program['courses'][1]['course_runs'][0]['key']
            }
        }]
        self.mock_user_data(basket.owner.username, owned_products=enrollments)

        # If the user has not added all of the remaining courses in the program to their basket,
        # the condition should not be satisfied
        basket.flush()
        for seat in verified_seats[2:len(verified_seats) - 1]:
            basket.add_product(seat)
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # When all courses in the program that the user is not already enrolled in are in their basket
        # and the site allows partial program completion, the condition should be satisfied
        basket.add_product(verified_seats[-1])
        self.assertTrue(self.condition.is_satisfied(offer, basket))

        # If the site does not allow partial program completion and the user does not have all of the program
        # courses in their basket, the condition should not be satisfied
        basket.site.siteconfiguration.enable_partial_program = False
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # Verify the user enrollments are cached
        basket.site.siteconfiguration.enable_partial_program = True
        httpretty.disable()
        with mock.patch('ecommerce.programs.conditions.get_program',
                        return_value=program):
            self.assertTrue(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 18
0
 def test_is_satisfied_true_for_enterprise_catalog_in_basket_attribute(self):
     """
     Ensure that condition returns true for valid enterprise catalog uuid in basket attribute.
     """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     enterprise_catalog_uuid = str(self.condition.enterprise_customer_catalog_uuid)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     request_data = {'catalog': enterprise_catalog_uuid}
     basket_add_enterprise_catalog_attribute(basket, request_data)
     self._check_condition_is_satisfied(offer, basket, is_satisfied=True)
Ejemplo n.º 19
0
 def test_is_satisfied_true_for_enterprise_catalog_in_get_request(self):
     """
     Ensure that condition returns true for valid enterprise catalog uuid in GET request.
     """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     enterprise_catalog_uuid = str(self.condition.enterprise_customer_catalog_uuid)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.strategy.request = self.request
     basket.strategy.request.GET = {'catalog': enterprise_catalog_uuid}
     self._check_condition_is_satisfied(offer, basket, is_satisfied=True)
Ejemplo n.º 20
0
 def test_is_satisfied_wrong_enterprise(self):
     """ Ensure the condition returns false if the learner is associated with a different EnterpriseCustomer. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api(
         learner_id=self.user.id,
         course_run_id=self.course_run.id,
     )
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 21
0
 def test_is_satisfied_no_course_product(self):
     """ Ensure the condition returns false if the basket contains a product not associated with a course run. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.test_product)
     self.mock_enterprise_learner_api(
         learner_id=self.user.id,
         enterprise_customer_uuid=str(self.condition.enterprise_customer_uuid),
         course_run_id=self.course_run.id,
     )
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 22
0
    def test_is_satisfied_false_for_invalid_enterprise_catalog(self, invalid_enterprise_catalog_uuid):
        """
        Ensure the condition returns false if provided enterprise catalog UUID is invalid.
        """
        offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)

        basket = factories.BasketFactory(site=self.site, owner=self.user)
        basket.strategy.request = self.request
        basket.strategy.request.GET = {'catalog': invalid_enterprise_catalog_uuid}
        self._check_condition_is_satisfied(offer, basket, is_satisfied=False)
        assert invalid_enterprise_catalog_uuid != offer.condition.enterprise_customer_catalog_uuid
Ejemplo n.º 23
0
    def setUp(self):
        super(JournalBundleConditionTests, self).setUp()
        user = self.create_user(is_staff=True)
        self.client.login(username=user.username, password=self.password)

        self.condition = factories.JournalConditionFactory()
        self.offer = factories.JournalBundleOfferFactory(
            site=self.site, condition=self.condition)
        self.basket = factories.BasketFactory(site=self.site,
                                              owner=factories.UserFactory())
        self.basket.add_product(self.create_product(self.client), 1)
Ejemplo n.º 24
0
    def test_is_satisfied_when_no_code_assignments_exists(self, num_orders, redemptions_available):
        """
        Ensure that condition returns expected result when code has no assignments.
        """
        enterprise_offer = factories.EnterpriseOfferFactory()
        voucher = factories.VoucherFactory(usage=Voucher.SINGLE_USE, code='AAA', num_orders=num_orders)
        voucher.offers.add(enterprise_offer)

        basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory(email='*****@*****.**'))
        basket.vouchers.add(voucher)

        assert self.condition.is_satisfied(enterprise_offer, basket) == redemptions_available
Ejemplo n.º 25
0
    def test_is_satisfied_with_entitlements(self):
        """
        The condition should be satisfied if, for each course in the program, their is either an entitlement sku in the
        basket or the user already has an entitlement for the course and the site has enabled partial program offers.
        """
        offer = factories.ProgramOfferFactory(site=self.site, condition=self.condition)
        basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory())
        program = self.mock_program_detail_endpoint(
            self.condition.program_uuid, self.site_configuration.discovery_api_url
        )
        entitlements_response = {
            "count": 0, "num_pages": 1, "current_page": 1, "results": [
                {'mode': 'verified', 'course_uuid': '268afbfc-cc1e-415b-a5d8-c58d955bcfc3'},
                {'mode': 'verified', 'course_uuid': '268afbfc-cc1e-415b-a5d8-c58d955bcfc4'}
            ], "next": None, "start": 0, "previous": None
        }

        # Extract one verified seat for each course
        verified_entitlements = []
        course_uuids = set([course['uuid'] for course in program['courses']])
        for parent_entitlement in Product.objects.filter(
                product_class__name=COURSE_ENTITLEMENT_PRODUCT_CLASS_NAME, structure=Product.PARENT
        ):
            for entitlement in Product.objects.filter(parent=parent_entitlement):
                if entitlement.attr.UUID in course_uuids and entitlement.attr.certificate_type == 'verified':
                    verified_entitlements.append(entitlement)

        self.mock_user_data(basket.owner.username, mocked_api='entitlements', owned_products=entitlements_response)
        self.mock_user_data(basket.owner.username)
        # If the user has not added all of the remaining courses in program to their basket,
        # the condition should not be satisfied
        basket.flush()
        for entitlement in verified_entitlements[2:len(verified_entitlements) - 1]:
            basket.add_product(entitlement)
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # When all courses in the program that the user is not already enrolled in are in their basket
        # and the site allows partial program completion, the condition should be satisfied
        basket.add_product(verified_entitlements[-1])
        self.assertTrue(self.condition.is_satisfied(offer, basket))

        # If the site does not allow partial program completion and the user does not have all of the program
        # courses in their basket, the condition should not be satisfied
        basket.site.siteconfiguration.enable_partial_program = False
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # Verify the user enrollments are cached
        basket.site.siteconfiguration.enable_partial_program = True
        httpretty.disable()
        with mock.patch('ecommerce.programs.conditions.get_program',
                        return_value=program):
            self.assertTrue(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 26
0
    def test_is_satisfied_no_enrollments(self):
        """ The method should return True if the basket contains one course run seat corresponding to each
        course in the program. """
        offer = factories.ProgramOfferFactory(partner=self.partner,
                                              condition=self.condition)
        basket = factories.BasketFactory(site=self.site,
                                         owner=factories.UserFactory())
        program = self.mock_program_detail_endpoint(
            self.condition.program_uuid,
            self.site_configuration.discovery_api_url)

        # Extract one audit and one verified seat for each course
        audit_seats = []
        verified_seats = []

        for course in program['courses']:
            course_run = Course.objects.get(id=course['course_runs'][0]['key'])
            for seat in course_run.seat_products:
                if seat.attr.id_verification_required:
                    verified_seats.append(seat)
                else:
                    audit_seats.append(seat)

        self.mock_user_data(basket.owner.username)
        # Empty baskets should never be satisfied
        basket.flush()
        self.assertTrue(basket.is_empty)
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # Adding seats of all the courses with the wrong seat type should NOT satisfy the condition.
        basket.flush()
        for seat in audit_seats:
            basket.add_product(seat)
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # All courses must be represented in the basket.
        # NOTE: We add all but the first verified seat to ensure complete branch coverage of the method.
        basket.flush()
        for verified_seat in verified_seats[1:len(verified_seats)]:
            basket.add_product(verified_seat)
        self.assertFalse(self.condition.is_satisfied(offer, basket))

        # The condition should be satisfied if one valid course run from each course is in the basket.
        basket.add_product(verified_seats[0])
        self.assertTrue(self.condition.is_satisfied(offer, basket))

        # If the user is enrolled with the wrong seat type for courses missing from their basket that are
        # needed for the program, the condition should NOT be satisfied
        basket.flush()
        for verified_seat in verified_seats[1:len(verified_seats)]:
            basket.add_product(verified_seat)
        self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 27
0
 def test_is_satisfied_true(self):
     """ Ensure the condition returns true if all basket requirements are met. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api(
         learner_id=self.user.id,
         enterprise_customer_uuid=str(self.condition.enterprise_customer_uuid),
         course_run_id=self.course_run.id,
     )
     self.mock_catalog_contains_course_runs(
         [self.course_run.id],
         self.condition.enterprise_customer_uuid,
         enterprise_customer_catalog_uuid=self.condition.enterprise_customer_catalog_uuid,
     )
     self.assertTrue(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 28
0
 def test_is_satisfied_for_anonymous_user(self):
     """ Ensure the condition returns false for an anonymous user. """
     offer = factories.EnterpriseOfferFactory(partner=self.partner, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=None)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api(
         learner_id=self.user.id,
         enterprise_customer_uuid=str(self.condition.enterprise_customer_uuid),
         course_run_id=self.course_run.id,
     )
     self.mock_catalog_contains_course_runs(
         [self.course_run.id],
         self.condition.enterprise_customer_uuid,
         enterprise_customer_catalog_uuid=self.condition.enterprise_customer_catalog_uuid,
     )
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 29
0
 def test_is_satisfied_course_run_not_in_catalog(self):
     """ Ensure the condition returns false if the course run is not in the Enterprise catalog. """
     offer = factories.EnterpriseOfferFactory(site=self.site, condition=self.condition)
     basket = factories.BasketFactory(site=self.site, owner=self.user)
     basket.add_product(self.course_run.seat_products[0])
     self.mock_enterprise_learner_api(
         learner_id=self.user.id,
         enterprise_customer_uuid=str(self.condition.enterprise_customer_uuid),
         course_run_id=self.course_run.id,
     )
     self.mock_catalog_contains_course_runs(
         [self.course_run.id],
         self.condition.enterprise_customer_uuid,
         enterprise_customer_catalog_uuid=self.condition.enterprise_customer_catalog_uuid,
         contains_content=False
     )
     self.assertFalse(self.condition.is_satisfied(offer, basket))
Ejemplo n.º 30
0
    def test_is_satisfied(self, num_orders, email, offer_status, condition_result):
        """
        Ensure that condition returns expected result.
        """
        voucher = factories.VoucherFactory(usage=Voucher.SINGLE_USE, num_orders=num_orders)
        enterprise_offer = factories.EnterpriseOfferFactory(max_global_applications=None)
        voucher.offers.add(enterprise_offer)
        basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory(email=email))
        basket.vouchers.add(voucher)
        factories.OfferAssignmentFactory(
            offer=enterprise_offer,
            code=voucher.code,
            user_email=email,
            status=offer_status,
        )

        assert self.condition.is_satisfied(enterprise_offer, basket) == condition_result