Exemplo n.º 1
0
def make_bound_subscription_form(
    title=faker.pystr(min_chars=1, max_chars=127),
    start_date=date.today(),
    expiration_date=date.today() + timedelta(days=366),
    enterprise_catalog_uuid=faker.uuid4(),
    netsuite_product_id=faker.random_int(),
    salesforce_opportunity_id=get_random_salesforce_id(),
    num_licenses=0,
    is_active=False,
    for_internal_use_only=False,
    has_customer_agreement=True,
    customer_agreement_has_default_catalog=True,
):
    """
    Builds a bound SubscriptionPlanForm
    """
    if customer_agreement_has_default_catalog:
        customer_agreement = CustomerAgreementFactory()
    else:
        customer_agreement = CustomerAgreementFactory(default_enterprise_catalog_uuid=None)

    form_data = {
        'title': title,
        'start_date': start_date,
        'expiration_date': expiration_date,
        'enterprise_catalog_uuid': enterprise_catalog_uuid,
        'netsuite_product_id': netsuite_product_id,
        'salesforce_opportunity_id': salesforce_opportunity_id,
        'num_licenses': num_licenses,
        'is_active': is_active,
        'for_internal_use_only': for_internal_use_only,
        'customer_agreement': str(customer_agreement.uuid) if has_customer_agreement else None,
    }
    return SubscriptionPlanForm(form_data)
Exemplo n.º 2
0
def make_bound_subscription_form(
        title=faker.pystr(min_chars=1, max_chars=127),
        start_date=localized_utcnow(),
        expiration_date=localized_utcnow() + timedelta(days=366),
        enterprise_catalog_uuid=faker.uuid4(),
        enterprise_customer_uuid=faker.uuid4(),
        salesforce_opportunity_id=get_random_salesforce_id(),
        num_licenses=0,
        is_active=False,
        for_internal_use_only=False,
        has_product=True,
        is_sf_id_required=False,
        has_customer_agreement=True,
        customer_agreement_has_default_catalog=True,
        change_reason="new"):
    """
    Builds a bound SubscriptionPlanForm
    """
    if customer_agreement_has_default_catalog:
        customer_agreement = CustomerAgreementFactory(
            enterprise_customer_uuid=enterprise_customer_uuid)
    else:
        customer_agreement = CustomerAgreementFactory(
            enterprise_customer_uuid=enterprise_customer_uuid,
            default_enterprise_catalog_uuid=None)

    product = ProductFactory(plan_type=PlanTypeFactory(
        sf_id_required=is_sf_id_required))

    form_data = {
        'title':
        title,
        'start_date':
        start_date,
        'expiration_date':
        expiration_date,
        'enterprise_catalog_uuid':
        enterprise_catalog_uuid,
        'product':
        product.id if has_product else None,
        'salesforce_opportunity_id':
        salesforce_opportunity_id,
        'num_licenses':
        num_licenses,
        'is_active':
        is_active,
        'for_internal_use_only':
        for_internal_use_only,
        'customer_agreement':
        str(customer_agreement.uuid) if has_customer_agreement else None,
        'change_reason':
        change_reason
    }
    return SubscriptionPlanForm(form_data)
Exemplo n.º 3
0
 def test_customer_agreement_factory(self):
     """
     Verify the customer agreement factory performs a get_or_create when using the same unique identifiers
     """
     customer_agreement = CustomerAgreementFactory()
     self.assertTrue(customer_agreement)
     customer_agreement_2 = CustomerAgreementFactory(
         enterprise_customer_uuid=customer_agreement.
         enterprise_customer_uuid,
         enterprise_customer_slug=customer_agreement.
         enterprise_customer_slug,
     )
     assert customer_agreement == customer_agreement_2
Exemplo n.º 4
0
    def test_renewal_future_plan_auto_applies_licenses(
            self, should_auto_apply_licenses):
        customer_agreement = CustomerAgreementFactory.create()
        prior_plan = SubscriptionPlanFactory(
            customer_agreement=customer_agreement,
            should_auto_apply_licenses=should_auto_apply_licenses)
        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            number_of_licenses=1,
            license_types_to_copy=constants.LicenseTypesToRenew.
            ASSIGNED_AND_ACTIVATED,
            effective_date=NOW)

        with freezegun.freeze_time(NOW):
            api.renew_subscription(renewal)

        renewal.refresh_from_db()
        future_plan = renewal.renewed_subscription_plan

        if should_auto_apply_licenses:
            self.assertTrue(future_plan.should_auto_apply_licenses)
            self.assertEqual(customer_agreement.auto_applicable_subscription,
                             future_plan)
        else:
            assert future_plan.should_auto_apply_licenses is None
            assert customer_agreement.auto_applicable_subscription is None
Exemplo n.º 5
0
def test_select_subscription_for_auto_applied_licenses(
        mock_toggle_auto_apply_licenses):
    """
    Verify that selecting a SubscriptionPlan on a CustomerAgreement
    calls toggle_auto_apply_licenses.
    """
    customer_agreement_admin = CustomerAgreementAdmin(CustomerAgreement,
                                                      AdminSite())
    request = RequestFactory()
    request.user = UserFactory()
    customer_agreement = CustomerAgreementFactory()
    subscription_plan = SubscriptionPlanFactory(
        customer_agreement=customer_agreement)
    customer_agreement_uuid = str(customer_agreement.uuid)
    subscription_uuid = str(subscription_plan.uuid)
    request.resolver_match = mock.Mock(
        kwargs={'object_id': customer_agreement_uuid})

    form = make_bound_customer_agreement_form(
        customer_agreement=customer_agreement,
        subscription_for_auto_applied_licenses=subscription_uuid)
    obj = form.save(
    )  # Get the object returned from saving the form to save to the database
    change = True
    customer_agreement_admin.save_model(request, obj, form, change)

    args = mock_toggle_auto_apply_licenses.call_args[0]
    assert args[0] is customer_agreement_uuid
    assert args[1] is subscription_uuid
Exemplo n.º 6
0
    def setUp(self):
        super().setUp()
        self.activated_user = UserFactory()
        self.assigned_user = UserFactory()
        self.unlicensed_user = UserFactory()
        self.enterprise_customer_uuid = uuid4()
        self.enterprise_catalog_uuid = uuid4()
        self.course_key = 'testX'

        self.customer_agreement = CustomerAgreementFactory(
            enterprise_customer_uuid=self.enterprise_customer_uuid, )
        self.active_subscription_for_customer = SubscriptionPlanFactory.create(
            customer_agreement=self.customer_agreement,
            enterprise_catalog_uuid=self.enterprise_catalog_uuid,
            is_active=True,
        )
        self.activated_license = LicenseFactory.create(
            status=constants.ACTIVATED,
            user_email=self.activated_user.email,
            subscription_plan=self.active_subscription_for_customer,
        )
        self.assigned_license = LicenseFactory.create(
            status=constants.ASSIGNED,
            user_email=self.assigned_user.email,
            subscription_plan=self.active_subscription_for_customer,
        )
Exemplo n.º 7
0
    def test_activated_license_is_revoked(
        self,
        is_revocation_cap_enabled,
    ):
        agreement = CustomerAgreementFactory.create(
            enterprise_customer_uuid=uuid.UUID(
                '00000000-1111-2222-3333-444444444444'), )
        subscription_plan = SubscriptionPlanFactory.create(
            customer_agreement=agreement,
            is_revocation_cap_enabled=is_revocation_cap_enabled,
            num_revocations_applied=0,
            revoke_max_percentage=100,
        )
        original_license = LicenseFactory.create(
            status=constants.ACTIVATED,
            subscription_plan=subscription_plan,
            lms_user_id=123,
        )

        with freezegun.freeze_time(NOW):
            api.revoke_license(original_license)

        original_license.refresh_from_db()
        self.assertEqual(original_license.status, constants.REVOKED)
        self.assertEqual(original_license.revoked_date, NOW)

        # There should now be 1 unassigned license
        self.assertEqual(subscription_plan.unassigned_licenses.count(), 1)
Exemplo n.º 8
0
    def test_validate_enterprise_customer_uuid(
        self,
        has_http_error,
        expected_is_valid,
        mock_catalog_api_client,
        mock_enterprise_api_client,
    ):

        catalog_uuid = uuid4()
        enterprise_customer_uuid = uuid4()
        mock_catalog_api_client().get_enterprise_catalog.return_value = {
            'enterprise_customer': str(enterprise_customer_uuid)
        }

        if has_http_error:
            error = HTTPError()
            error.response = mock.MagicMock()
            error.response.status_code = 404
            mock_enterprise_api_client(
            ).get_enterprise_customer_data.side_effect = error
        else:
            mock_enterprise_api_client(
            ).get_enterprise_customer_data.return_value = {
                'slug': 'test',
                'name': 'test-enterprise'
            }

        form = make_bound_customer_agreement_form(
            customer_agreement=CustomerAgreementFactory(
                enterprise_customer_uuid=enterprise_customer_uuid),
            default_enterprise_catalog_uuid=catalog_uuid,
            subscription_for_auto_applied_licenses=None)
        assert form.is_valid() is expected_is_valid
        assert mock_enterprise_api_client().get_enterprise_customer_data.called
Exemplo n.º 9
0
 def setUp(self):
     super().setUp()
     customer_agreement = CustomerAgreementFactory()
     self.active_subscription_plans = SubscriptionPlanFactory.create_batch(
         2, customer_agreement=customer_agreement)
     self.inactive_subscription_plans = SubscriptionPlanFactory.create_batch(
         3, customer_agreement=customer_agreement, is_active=False)
     self.customer_agreement = customer_agreement
Exemplo n.º 10
0
    def setUpTestData(cls):
        super().setUpTestData()

        # technically this will be off on leap years, but we just need something later than now, so it's not a problem
        ONE_YEAR_FROM_NOW = localized_datetime_from_datetime(datetime.now() + timedelta(days=365))
        TWO_YEARS_FROM_NOW = localized_datetime_from_datetime(datetime.now() + timedelta(days=730))

        cls.user_email = '*****@*****.**'
        cls.enterprise_customer_uuid = uuid.uuid4()
        cls.customer_agreement = CustomerAgreementFactory.create(
            enterprise_customer_uuid=cls.enterprise_customer_uuid,
        )

        cls.subscription_plan = SubscriptionPlanFactory()

        cls.active_current_plan = SubscriptionPlanFactory.create(
            customer_agreement=cls.customer_agreement,
            is_active=True,
            start_date=localized_datetime(2021, 1, 1),
            expiration_date=ONE_YEAR_FROM_NOW,
        )
        cls.active_current_license = LicenseFactory.create(
            user_email=cls.user_email,
            subscription_plan=cls.active_current_plan,
        )

        cls.inactive_current_plan = SubscriptionPlanFactory.create(
            customer_agreement=cls.customer_agreement,
            is_active=False,
            start_date=localized_datetime(2021, 1, 1),
            expiration_date=ONE_YEAR_FROM_NOW,
        )
        cls.inactive_current_license = LicenseFactory.create(
            user_email=cls.user_email,
            subscription_plan=cls.inactive_current_plan,
        )

        cls.non_current_active_plan = SubscriptionPlanFactory.create(
            customer_agreement=cls.customer_agreement,
            is_active=True,
            start_date=ONE_YEAR_FROM_NOW,
            expiration_date=TWO_YEARS_FROM_NOW,
        )
        cls.non_current_active_license = LicenseFactory.create(
            user_email=cls.user_email,
            subscription_plan=cls.non_current_active_plan,
        )

        cls.non_current_inactive_plan = SubscriptionPlanFactory.create(
            customer_agreement=cls.customer_agreement,
            is_active=False,
            start_date=ONE_YEAR_FROM_NOW,
            expiration_date=TWO_YEARS_FROM_NOW,
        )
        cls.non_current_inactive_license = LicenseFactory.create(
            user_email=cls.user_email,
            subscription_plan=cls.non_current_inactive_plan,
        )
Exemplo n.º 11
0
    def test_save_without_slug_http_error(self, mock_client):
        original_slug = 'original-slug'
        original_name = 'Original Name'
        agreement = CustomerAgreementFactory()
        agreement.enterprise_customer_slug = original_slug
        agreement.enterprise_customer_name = original_name
        client = mock_client.return_value
        client.get_enterprise_customer_data.side_effect = HTTPError(
            'some error')

        with self.assertRaisesRegex(exceptions.CustomerAgreementError,
                                    'some error'):
            api.sync_agreement_with_enterprise_customer(
                customer_agreement=agreement)

        self.assertTrue(mock_client.called)
        client.get_enterprise_customer_data.assert_called_once_with(
            agreement.enterprise_customer_uuid)
        self.assertEqual(agreement.enterprise_customer_slug, original_slug)
        self.assertEqual(agreement.enterprise_customer_name, original_name)
Exemplo n.º 12
0
    def test_sync_slug(self, mock_client):
        new_slug = 'new-slug'
        new_name = 'New Name'
        agreement = CustomerAgreementFactory()
        agreement.enterprise_customer_slug = 'original-slug'
        agreement.enterprise_customer_name = 'Original Name'
        client = mock_client.return_value
        client.get_enterprise_customer_data.return_value = {
            'slug': new_slug,
            'name': new_name,
        }

        api.sync_agreement_with_enterprise_customer(
            customer_agreement=agreement)

        self.assertTrue(mock_client.called)
        client.get_enterprise_customer_data.assert_called_once_with(
            agreement.enterprise_customer_uuid)
        self.assertEqual(new_slug, agreement.enterprise_customer_slug)
        self.assertEqual(new_name, agreement.enterprise_customer_name)
Exemplo n.º 13
0
    def setUpTestData(cls):
        super().setUpTestData()

        customer_agreement = CustomerAgreementFactory()
        subscription_plan_1 = SubscriptionPlanFactory(
            customer_agreement=customer_agreement)
        subscription_plan_2 = SubscriptionPlanFactory(
            customer_agreement=customer_agreement)

        cls.customer_agreement = customer_agreement
        cls.subscription_plan_1 = subscription_plan_1
        cls.subscription_plan_2 = subscription_plan_2
Exemplo n.º 14
0
    def setUpTestData(cls):
        super().setUpTestData()

        cls.customer_agreement = CustomerAgreementFactory()
        cls.subscription_plan_a = SubscriptionPlanFactory(
            expiration_date=localized_datetime(2020, 1, 1),
            customer_agreement=cls.customer_agreement,
        )
        cls.subscription_plan_b = SubscriptionPlanFactory(
            expiration_date=localized_datetime(2021, 1, 1),
            customer_agreement=cls.customer_agreement
        )
Exemplo n.º 15
0
    def test_populate_subscription_for_auto_applied_licenses_plans_outside_agreement_not_included(
            self):
        customer_agreement_1 = CustomerAgreementFactory()
        customer_agreement_2 = CustomerAgreementFactory()

        sub_for_customer_agreement_1 = SubscriptionPlanFactory(
            customer_agreement=customer_agreement_1,
            should_auto_apply_licenses=False)
        SubscriptionPlanFactory(customer_agreement=customer_agreement_2,
                                should_auto_apply_licenses=True)

        form = make_bound_customer_agreement_form(
            customer_agreement=customer_agreement_1,
            subscription_for_auto_applied_licenses=None)

        field = form.fields['subscription_for_auto_applied_licenses']
        choices = field.choices
        self.assertEqual(len(choices), 2)
        self.assertEqual(choices[0], ('', '------'))
        self.assertEqual(choices[1], (sub_for_customer_agreement_1.uuid,
                                      sub_for_customer_agreement_1.title))
        self.assertEqual(field.initial, choices[0], ('', '------'))
Exemplo n.º 16
0
    def test_renewal_disable_auto_apply_licenses(self):
        customer_agreement = CustomerAgreementFactory.create()
        prior_plan = SubscriptionPlanFactory(
            customer_agreement=customer_agreement,
            should_auto_apply_licenses=True)
        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            number_of_licenses=1,
            license_types_to_copy=constants.LicenseTypesToRenew.
            ASSIGNED_AND_ACTIVATED,
            disable_auto_apply_licenses=True)

        with freezegun.freeze_time(NOW):
            api.renew_subscription(renewal)

        future_plan = renewal.renewed_subscription_plan
        self.assertFalse(future_plan.should_auto_apply_licenses)
        assert customer_agreement.auto_applicable_subscription is None
Exemplo n.º 17
0
    def test_populate_subscription_for_auto_applied_licenses_choices(self):
        customer_agreement = CustomerAgreementFactory()
        active_subscription_plan = SubscriptionPlanFactory(
            customer_agreement=customer_agreement)
        SubscriptionPlanFactory(customer_agreement=customer_agreement,
                                is_active=False)

        form = make_bound_customer_agreement_form(
            customer_agreement=customer_agreement,
            subscription_for_auto_applied_licenses=None)

        field = form.fields['subscription_for_auto_applied_licenses']
        choices = field.choices
        self.assertEqual(len(choices), 2)
        self.assertEqual(choices[0], ('', '------'))
        self.assertEqual(
            choices[1],
            (active_subscription_plan.uuid, active_subscription_plan.title))
        self.assertEqual(field.initial, ('', '------'))