Exemplo n.º 1
0
    def test_renewal_processed_with_no_existing_future_plan(self):
        prior_plan = SubscriptionPlanFactory()
        original_activated_licenses = [
            LicenseFactory.create(
                subscription_plan=prior_plan,
                status=constants.ACTIVATED,
                user_email='activated_user_{}@example.com'.format(i))
            for i in range(5)
        ]
        original_assigned_licenses = [
            LicenseFactory.create(
                subscription_plan=prior_plan,
                status=constants.ASSIGNED,
                user_email='assigned_user_{}@example.com'.format(i))
            for i in range(5)
        ]
        original_licenses = original_activated_licenses + original_assigned_licenses

        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            number_of_licenses=10,
            license_types_to_copy=constants.LicenseTypesToRenew.
            ASSIGNED_AND_ACTIVATED)

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

        renewal.refresh_from_db()
        original_plan = renewal.prior_subscription_plan
        future_plan = renewal.renewed_subscription_plan
        self.assertTrue(renewal.processed)
        self.assertEqual(renewal.processed_datetime, NOW)
        self.assertEqual(original_plan.product_id, future_plan.product_id)
        self.assertEqual(future_plan.num_licenses, renewal.number_of_licenses)
        self._assert_all_licenses_renewed(future_plan)
Exemplo n.º 2
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
    def test_prior_plans_in_renewal_chain_processed(
            self, mock_license_expiration_task, mock_track_event):
        """
        Verifies that previous subscriptions in a chain of renewals will also be processed when the last plan expires.
        """

        expired_subscription_plan_1 = self._create_expired_plan_with_licenses(
            start_date=localized_datetime(2014, 1, 1),
            expiration_date=localized_datetime(2015, 1, 1))
        expired_subscription_plan_2 = self._create_expired_plan_with_licenses(
            start_date=localized_datetime(2015, 1, 1),
            expiration_date=localized_datetime(2016, 1, 1))
        expired_subscription_plan_3 = self._create_expired_plan_with_licenses()

        SubscriptionPlanRenewalFactory(
            prior_subscription_plan=expired_subscription_plan_1,
            renewed_subscription_plan=expired_subscription_plan_2)

        SubscriptionPlanRenewalFactory(
            prior_subscription_plan=expired_subscription_plan_2,
            renewed_subscription_plan=expired_subscription_plan_3)

        call_command(self.command_name)

        args_1 = mock_license_expiration_task.call_args_list[0][0][0]
        args_2 = mock_license_expiration_task.call_args_list[1][0][0]
        args_3 = mock_license_expiration_task.call_args_list[2][0][0]
        assert args_1 == self._get_allocated_license_uuids(
            expired_subscription_plan_3)
        assert args_2 == self._get_allocated_license_uuids(
            expired_subscription_plan_1)
        assert args_3 == self._get_allocated_license_uuids(
            expired_subscription_plan_2)
        assert mock_license_expiration_task.call_count == 3
Exemplo n.º 4
0
 def test_is_locked_for_renewal_processing(self, is_locked_for_renewal_processing):
     today = localized_utcnow()
     with freezegun.freeze_time(today):
         renewed_subscription_plan = SubscriptionPlanFactory.create(expiration_date=today)
         renewal_kwargs = {'prior_subscription_plan': renewed_subscription_plan}
         if is_locked_for_renewal_processing:
             renewal_kwargs.update({'effective_date': renewed_subscription_plan.expiration_date})
         SubscriptionPlanRenewalFactory.create(**renewal_kwargs)
         self.assertEqual(renewed_subscription_plan.is_locked_for_renewal_processing, is_locked_for_renewal_processing)
Exemplo n.º 5
0
 def test_prior_renewals(self):
     renewed_subscription_plan_1 = SubscriptionPlanFactory.create()
     renewed_subscription_plan_2 = SubscriptionPlanFactory.create()
     renewal_1 = SubscriptionPlanRenewalFactory.create(
         prior_subscription_plan=self.subscription_plan,
         renewed_subscription_plan=renewed_subscription_plan_1
     )
     renewal_2 = SubscriptionPlanRenewalFactory.create(
         prior_subscription_plan=renewed_subscription_plan_1,
         renewed_subscription_plan=renewed_subscription_plan_2
     )
     self.assertEqual(renewed_subscription_plan_2.prior_renewals, [renewal_1, renewal_2])
Exemplo n.º 6
0
 def test_subscription_plan_renewal_factory(self):
     """
     Verify an unexpired subscription plan renewal is created by default.
     """
     subscription_renewal = SubscriptionPlanRenewalFactory()
     self.assertTrue(subscription_renewal.effective_date <
                     subscription_renewal.renewed_expiration_date)
Exemplo n.º 7
0
    def test_license_renewed_events(self):
        """ Test that our standard renewal routine triggers the right set of events
        for all licenses involved:
            - a creation event for each new license
            - NO assignment or activation event since the license comes from a renewal.
            - a renewal event for each old/new license pair
        """
        prior_plan = SubscriptionPlanFactory()
        original_activated_licenses = [
            LicenseFactory.create(
                subscription_plan=prior_plan,
                status=constants.ACTIVATED,
                user_email='activated_user_{}@example.com'.format(i))
            for i in range(2)
        ]
        original_assigned_licenses = [
            LicenseFactory.create(
                subscription_plan=prior_plan,
                status=constants.ASSIGNED,
                user_email='assigned_user_{}@example.com'.format(i))
            for i in range(2)
        ]
        original_licenses = original_activated_licenses + original_assigned_licenses

        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            number_of_licenses=len(original_licenses),
            license_types_to_copy=constants.LicenseTypesToRenew.
            ASSIGNED_AND_ACTIVATED)

        with mock.patch(
                'license_manager.apps.subscriptions.event_utils.track_event'
        ) as mock_track_event:
            with freeze_time(NOW):
                api.renew_subscription(renewal)
            renewal.refresh_from_db()

            for call in mock_track_event.call_args_list[0:4]:
                assert call[0][1] == constants.SegmentEvents.LICENSE_CREATED

            for call in mock_track_event.call_args_list[4:]:
                assert call[0][1] == constants.SegmentEvents.LICENSE_RENEWED
            assert mock_track_event.call_count == 8
Exemplo n.º 8
0
    def create_subscription_with_renewal(self,
                                         effective_date,
                                         processed=False):
        prior_subscription_plan = SubscriptionPlanFactory.create(
            start_date=self.now - timedelta(days=7),
            expiration_date=self.now,
        )

        renewed_subscription_plan = SubscriptionPlanFactory.create(
            start_date=self.now,
            expiration_date=self.now + timedelta(days=7),
        )

        SubscriptionPlanRenewalFactory.create(
            prior_subscription_plan=prior_subscription_plan,
            renewed_subscription_plan=renewed_subscription_plan,
            effective_date=effective_date,
            processed=processed)

        return (prior_subscription_plan)
    def test_subscription_with_renewal_not_processed(
            self, mock_license_expiration_task, mock_track_event):
        """
        Verifies that a subscription plan's expiration will not be processed if it has a renewal.
        """

        expired_subscription = self._create_expired_plan_with_licenses()
        SubscriptionPlanRenewalFactory(
            prior_subscription_plan=expired_subscription)
        call_command(self.command_name)

        expired_subscription.refresh_from_db()
        mock_license_expiration_task.assert_not_called()
        assert expired_subscription.expiration_processed is False
Exemplo n.º 10
0
    def test_cannot_renew_too_many_existing_unassigned_licenses(self):
        future_plan = SubscriptionPlanFactory()
        LicenseFactory.create_batch(
            50,
            subscription_plan=future_plan,
            status=constants.UNASSIGNED,
        )
        renewal = SubscriptionPlanRenewalFactory(
            renewed_subscription_plan=future_plan,
            number_of_licenses=20,
        )

        expected_message = 'More licenses exist than were requested to be renewed'
        with self.assertRaisesRegex(exceptions.RenewalProcessingError,
                                    expected_message):
            api.renew_subscription(renewal)
Exemplo n.º 11
0
    def test_cannot_renew_with_existing_assigned_future_licenses(self):
        future_plan = SubscriptionPlanFactory()
        LicenseFactory.create_batch(
            5,
            subscription_plan=future_plan,
            status=constants.ACTIVATED,
        )
        renewal = SubscriptionPlanRenewalFactory(
            renewed_subscription_plan=future_plan,
            number_of_licenses=20,
        )

        expected_message = 'there are existing licenses in the renewed plan that are activated'
        with self.assertRaisesRegex(exceptions.RenewalProcessingError,
                                    expected_message):
            api.renew_subscription(renewal)
Exemplo n.º 12
0
    def test_cannot_renew_for_fewer_licenses(self):
        prior_plan = SubscriptionPlanFactory()
        LicenseFactory.create_batch(
            5,
            subscription_plan=prior_plan,
            status=constants.ACTIVATED,
        )
        renewal = SubscriptionPlanRenewalFactory(
            number_of_licenses=2,
            prior_subscription_plan=prior_plan,
        )

        expected_message = 'Cannot renew for fewer than the number of original activated licenses.'
        with self.assertRaisesRegex(exceptions.RenewalProcessingError,
                                    expected_message):
            api.renew_subscription(renewal)
Exemplo n.º 13
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.º 14
0
    def test_renewal_processed_segment_events(self, mock_track_event):
        prior_plan = SubscriptionPlanFactory()
        [
            LicenseFactory.create(subscription_plan=prior_plan,
                                  status=constants.ACTIVATED,
                                  user_email='activated_user_{}@example.com')
        ]

        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            number_of_licenses=1,
            license_types_to_copy=constants.LicenseTypesToRenew.
            ASSIGNED_AND_ACTIVATED)
        api.renew_subscription(renewal)
        assert mock_track_event.call_count == 2
        assert (mock_track_event.call_args_list[0].args[1] ==
                constants.SegmentEvents.LICENSE_CREATED)
        assert (mock_track_event.call_args_list[1].args[1] ==
                constants.SegmentEvents.LICENSE_RENEWED)
        self.assertFalse(
            mock_track_event.call_args_list[1].args[2]['is_auto_renewed'])
Exemplo n.º 15
0
    def test_renewal_processed_with_existing_future_plan(self):
        prior_plan = SubscriptionPlanFactory()
        original_licenses = [
            LicenseFactory.create(
                subscription_plan=prior_plan,
                status=constants.ACTIVATED,
                user_email='activated_user_{}@example.com'.format(i))
            for i in range(5)
        ]

        # create some revoked original licenses that should not be renewed
        LicenseFactory.create_batch(
            67,
            subscription_plan=prior_plan,
            status=constants.REVOKED,
        )

        future_plan = SubscriptionPlanFactory()
        LicenseFactory.create_batch(
            10,
            subscription_plan=future_plan,
            status=constants.UNASSIGNED,
        )
        renewal = SubscriptionPlanRenewalFactory(
            prior_subscription_plan=prior_plan,
            renewed_subscription_plan=future_plan,
            number_of_licenses=10,
        )

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

        future_plan.refresh_from_db()
        self.assertTrue(renewal.processed)
        self.assertEqual(renewal.processed_datetime, NOW)
        self.assertEqual(future_plan.num_licenses, renewal.number_of_licenses)
        self._assert_all_licenses_renewed(future_plan)