Skip to content

Commit

Permalink
feat: pick the most recently start plan as auto-applicable
Browse files Browse the repository at this point in the history
ENT-9734
  • Loading branch information
iloveagent57 committed Dec 5, 2024
1 parent 1b1679f commit a9255d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
21 changes: 18 additions & 3 deletions license_manager/apps/subscriptions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ class SubscriptionPlanForm(forms.ModelForm):
choices=SubscriptionPlanShouldAutoApplyLicensesChoices.CHOICES,
required=False,
label="Should auto apply licenses",
help_text="Whether licenses from this Subscription Plan should be auto applied."
help_text=(
"""
Whether licenses from this Subscription Plan should be auto applied.
It it possible and acceptable for more than one plan in a single
customer agreement to have this field enabled.
"""
)
)

# Extra form field to specify the number of licenses to be associated with the subscription plan
Expand Down Expand Up @@ -292,8 +298,17 @@ def populate_subscription_for_auto_applied_licenses_choices(self, instance):
choices=choices,
required=False,
initial=empty_choice if not current_plan else (current_plan.uuid, current_plan.title),
help_text="The subscription plan to be associated with auto apply licenses. Selecting a license"
" will automatically enable the \"Should auto apply licenses\" field on the subscription plan"
help_text=(
"""
The subscription plan from which licenses will be auto-applied, if any.
If you do not manually modify this field, it will be automatically set, chosen as the
most recently started plan that is active, current, and has 'should_auto_apply_licenses'
set to true. Manually selecting/modifying the plan for this field will have two effects:
It will automatically enable the \"Should auto apply licenses\" field on the selected plan,
and it will automatically *disable* that field on all other plans
associated with this customer agreement.
"""
),
)
self.fields['subscription_for_auto_applied_licenses'] = choice_field

Expand Down
2 changes: 1 addition & 1 deletion license_manager/apps/subscriptions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def auto_applicable_subscription(self):
is_active=True,
start_date__lte=now,
expiration_date__gte=now
).first()
).order_by('-start_date').first()

return plan

Expand Down
49 changes: 49 additions & 0 deletions license_manager/apps/subscriptions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,55 @@ def test_is_locked_for_renewal_processing(self, is_locked_for_renewal_processing
renewed_subscription_plan.is_locked_for_renewal_processing, is_locked_for_renewal_processing,
)

def test_auto_applicable_subscription(self):
"""
Tests that we pick the most recent auto-applicable plan for a CustomerAgreement.
"""
agreement = CustomerAgreementFactory.create()
SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_datetime(2020, 1, 1),
should_auto_apply_licenses=True,
)
SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_datetime(2020, 4, 1),
should_auto_apply_licenses=True,
)
plan_3 = SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_datetime(2020, 12, 1),
should_auto_apply_licenses=True,
)
# make one plan that hasn't started yet
SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_utcnow() + timedelta(days=1),
should_auto_apply_licenses=True,
)
self.assertEqual(agreement.auto_applicable_subscription, plan_3)

def test_auto_applicable_subscription_none_available(self):
"""
Tests that when no current, auto-applicable plan is available,
CustomerAgreement.auto_applicable_subscription evaluates to null.
"""
agreement = CustomerAgreementFactory.create()
# make a plan that's expired
SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_datetime(2020, 1, 1),
expiration_date=localized_utcnow() + timedelta(days=-1),
should_auto_apply_licenses=True,
)
# make one plan that hasn't started yet
SubscriptionPlanFactory.create(
customer_agreement=agreement,
start_date=localized_utcnow() + timedelta(days=1),
should_auto_apply_licenses=True,
)
self.assertIsNone(agreement.auto_applicable_subscription)

def test_auto_apply_licenses_turned_on_at(self):
"""
Tests that auto_apply_licenses_turned_on_at returns the correct time.
Expand Down

0 comments on commit a9255d3

Please sign in to comment.