Exemplo n.º 1
0
    def test_dispatching(self, create_invoice_mock, apply_async_mock):
        self.assertFalse(apply_async_mock.called)
        self.assertFalse(create_invoice_mock.called)
        self.assertEqual(
            Invoice.objects.filter(
                status=Invoice.STATUS_CHOICES.PENDING).count(), 0)
        self.assertEqual(InvoiceItem.objects.count(), 0)
        self.assertEqual(
            Subscription.objects.filter(
                charged_until=Invoice.next_period()).count(), 1)

        self.task()

        self.assertTrue(apply_async_mock.called)
        self.assertTrue(create_invoice_mock.called)
        expected_count = len(self.active_subs)
        self.assertEqual(create_invoice_mock.call_count, expected_count)
        self.assertEqual(
            Invoice.objects.filter(
                status=Invoice.STATUS_CHOICES.PENDING).count(), expected_count)
        self.assertEqual(InvoiceItem.objects.count(), expected_count)
        self.assertEqual(
            Subscription.objects.filter(
                charged_until=Invoice.next_period()).count(),
            expected_count + 1)
Exemplo n.º 2
0
 def setUp(self):
     self.admin = G(Admin)
     self.invoices = [
         G(Invoice, admin=self.admin, period=Invoice.previous_period()),
         G(Invoice, admin=self.admin, period=Invoice.previous_period()),
         G(Invoice, admin=self.admin, period=Invoice.current_period()),
     ]
     self.task = InvoiceDispatcher
 def set_hard_limit_as_reached(self):
     self.admin.billing_profile.hard_limit = Decimal(20)
     self.admin.billing_profile.hard_limit_reached = Invoice.current_period(
     )
     self.admin.billing_profile.save()
     G(Invoice,
       admin=self.admin,
       period=Invoice.current_period(),
       amount=Decimal(99))
Exemplo n.º 4
0
    def run(self):
        invoices = Invoice.objects.filter(status_sent=False,
                                          period__lt=Invoice.current_period())
        invoices = invoices.select_related('admin').prefetch_related('items')
        invoices = invoices.order_by('pk')

        chunk = list(invoices[:self.chunk_size + 1])
        if not chunk:
            return

        try:
            for invoice in chunk[:self.chunk_size]:
                # Don't send summary for invoices without items with API_CALL or
                # CODEBOX_TIME. Because we prefetched items we check this in python
                if any(item for item in invoice.items.all() if not item.is_fee()):
                    analytics.track(
                        invoice.admin.id,
                        'Monthly Summary',
                        {
                            'apiCalls': invoice.get_usage(Transaction.SOURCES.API_CALL),
                            'codeboxSeconds': invoice.get_usage(
                                Transaction.SOURCES.CODEBOX_TIME),
                            'apiPlan': invoice.get_display_plan_limit(
                                Transaction.SOURCES.API_CALL),
                            'codeboxPlan': invoice.get_display_plan_limit(
                                Transaction.SOURCES.CODEBOX_TIME)
                        },
                        timestamp=self.timestamp)
        finally:
            invoices.filter(pk__lte=chunk[-1].pk).update(status_sent=True)

        if len(chunk) > self.chunk_size:
            self.delay()
Exemplo n.º 5
0
    def test_subscribing_for_the_first_time_charges_me(self,
                                                       create_charge_mock):
        # prepare subscription: start_date in the past;
        today = datetime.date.today()
        last_subscription = Subscription.objects.active_for_admin(
            admin_id=self.admin.id).get()
        last_subscription.range = DateRange(today - datetime.timedelta(days=5),
                                            None)
        last_subscription.save()

        self.assertFalse(create_charge_mock.called)
        response = self.client.post(self.url, {'commitment': self.commitment})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(create_charge_mock.called)

        plan_fee = self.plan.get_plan_fee(self.commitment,
                                          today).quantize(Decimal('.01'))
        self.assertTrue(
            Subscription.objects.filter(admin=self.admin,
                                        range__endswith=today).exists())

        self.assertTrue(
            Invoice.objects.filter(
                admin=self.admin,
                status=Invoice.STATUS_CHOICES.PAYMENT_SCHEDULED,
                plan_fee=plan_fee).exists())
        self.assertEqual(InvoiceItem.objects.count(), 1)

        current_sub = Subscription.objects.active_for_admin(self.admin).get()
        self.assertEqual(current_sub.start, today)
        self.assertIsNone(current_sub.end)
        self.assertEqual(current_sub.plan_id, self.plan.id)
        self.assertEqual(current_sub.commitment, self.commitment)
        self.assertEqual(current_sub.charged_until, Invoice.next_period())
Exemplo n.º 6
0
    def test_next_chunk_task(self, delay_mock, apply_async_mock):
        self.assertFalse(apply_async_mock.called)
        self.assertFalse(delay_mock.called)

        self.task(chunk_size=1)

        self.assertTrue(apply_async_mock.called)
        self.assertTrue(delay_mock.called)
        delay_mock.assert_called_once_with(Invoice.previous_period(), 1)
Exemplo n.º 7
0
    def test_limit_is_rechecked_on_update(self):
        self.assertFalse(Profile.is_hard_limit_reached(self.admin.id))
        G(Invoice,
          admin=self.admin,
          period=Invoice.current_period(),
          overage_amount=100)
        self.client.patch(self.url, {'hard_limit': '10'})
        self.assertTrue(Profile.is_hard_limit_reached(self.admin.id))

        self.client.patch(self.url, {'hard_limit': '100'})
        self.assertFalse(Profile.is_hard_limit_reached(self.admin.id))
Exemplo n.º 8
0
    def setUp(self):
        self.admins = [G(Admin), G(Admin), G(Admin), G(Admin), G(Admin)]

        # Admin without soft limit
        self.admins[0].billing_profile.soft_limit = Decimal(0)
        self.admins[0].billing_profile.save()
        G(Invoice,
          admin=self.admins[0],
          period=Invoice.current_period(),
          overage_amount=Decimal(99))

        # Admin with soft limit
        self.admins[1].billing_profile.soft_limit = Decimal(10)
        self.admins[1].billing_profile.save()
        G(Invoice,
          admin=self.admins[1],
          period=Invoice.current_period(),
          overage_amount=Decimal(99))

        # Admin with soft limit & already notified
        self.admins[2].billing_profile.soft_limit = Decimal(20)
        self.admins[
            2].billing_profile.soft_limit_reached = Invoice.current_period()
        self.admins[2].billing_profile.save()
        G(Invoice,
          admin=self.admins[2],
          period=Invoice.current_period(),
          overage_amount=Decimal(99))

        # Second admin with soft limit
        self.admins[3].billing_profile.soft_limit = Decimal(30)
        self.admins[3].billing_profile.save()
        G(Invoice,
          admin=self.admins[3],
          period=Invoice.current_period(),
          overage_amount=Decimal(99))

        # Admin with soft limit already notified in prev billing cycle
        self.admins[4].billing_profile.soft_limit = Decimal(40)
        self.admins[
            4].billing_profile.soft_limit_reached = Invoice.previous_period()
        self.admins[4].billing_profile.save()
        G(Invoice,
          admin=self.admins[4],
          period=Invoice.current_period(),
          overage_amount=Decimal(30))

        self.task = CheckSoftLimits
Exemplo n.º 9
0
    def test_run(self, group_delay_mock):
        notify_mock = mock.Mock()
        notify_mock.return_value = notify_mock

        self.assertFalse(group_delay_mock.called)
        self.assertFalse(notify_mock.called)
        self.assertEqual(
            Profile.objects.filter(soft_limit_reached='1970-01-01').count(), 3)

        self.task.get_notify_task = notify_mock
        self.task()

        self.assertTrue(group_delay_mock.called)
        self.assertTrue(notify_mock.called)
        self.assertEqual(notify_mock.call_count, 3)
        self.assertEqual(
            Profile.objects.filter(
                soft_limit_reached=Invoice.current_period()).count(), 4)

        notify_mock.s.assert_any_call(self.admins[0].pk)
        notify_mock.s.assert_any_call(self.admins[1].pk)
        notify_mock.s.assert_any_call(self.admins[3].pk)