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
  • Loading branch information
iloveagent57 committed Dec 5, 2024
1 parent 1b1679f commit c2863a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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 c2863a8

Please sign in to comment.