def _simulate_downgrade(self, days_overdue, is_customer_billing_account=False): domain, latest_invoice = _generate_invoice_and_subscription( days_overdue, is_customer_billing_account=is_customer_billing_account ) self.domains.append(domain) downgrade_eligible_domains(only_downgrade_domain=domain.name) return domain, latest_invoice
def test_overdue_notification(self): domain, latest_invoice = self._simulate_downgrade( DAYS_PAST_DUE_TO_TRIGGER_OVERDUE_NOTICE) # confirm communication was initiated self.assertTrue( InvoiceCommunicationHistory.objects.filter( invoice=latest_invoice, communication_type=CommunicationType.OVERDUE_INVOICE, ).exists()) # try to trigger another communication (it should fail), and make sure # only one communication was ever sent downgrade_eligible_domains(only_downgrade_domain=domain.name) self.assertTrue( InvoiceCommunicationHistory.objects.filter( invoice=latest_invoice, ).count(), 1)
def test_downgrade_warning(self): domain, latest_invoice = self._simulate_downgrade( DAYS_PAST_DUE_TO_TRIGGER_DOWNGRADE_WARNING ) # confirm communication was initiated self.assertTrue(InvoiceCommunicationHistory.objects.filter( invoice=latest_invoice, communication_type=CommunicationType.DOWNGRADE_WARNING, ).exists()) # make sure a downgrade warning isn't sent again downgrade_eligible_domains(only_downgrade_domain=domain.name) self.assertTrue(InvoiceCommunicationHistory.objects.filter( invoice=latest_invoice, communication_type=CommunicationType.DOWNGRADE_WARNING, ).count(), 1)
def post(self, request, *args, **kwargs): if self.async_response is not None: return self.async_response if self.trigger_form.is_valid(): domain = self.trigger_form.cleaned_data['domain'] overdue_invoice, _ = get_oldest_unpaid_invoice_over_threshold( datetime.date.today(), domain) if not overdue_invoice: messages.error( request, f'No overdue invoices were found for project "{domain}"', ) else: downgrade_eligible_domains(only_downgrade_domain=domain) messages.success( request, f'Successfully triggered the downgrade process ' f'for project "{domain}".') return HttpResponseRedirect(reverse(self.urlname)) return self.get(request, *args, **kwargs)
def test_downgrade(self): domain, latest_invoice = self._simulate_downgrade( DAYS_PAST_DUE_TO_TRIGGER_DOWNGRADE ) # confirm a downgrade wasn't actually initiated because a warning # email has not been sent subscription = Subscription.get_active_subscription_by_domain(domain) self.assertNotEqual(subscription.plan_version.plan.edition, SoftwarePlanEdition.PAUSED) # fake the warning to have been triggered a few days ago warning_days_ago = DAYS_PAST_DUE_TO_TRIGGER_DOWNGRADE - DAYS_PAST_DUE_TO_TRIGGER_DOWNGRADE_WARNING history = InvoiceCommunicationHistory.objects.filter( invoice=latest_invoice ).latest('date_created') history.date_created = datetime.date.today() - datetime.timedelta(days=warning_days_ago) history.save() # now trigger a successful downgrade downgrade_eligible_domains(only_downgrade_domain=domain.name) subscription = Subscription.get_active_subscription_by_domain(domain) self.assertEqual(subscription.plan_version.plan.edition, SoftwarePlanEdition.PAUSED)
def run_downgrade_process(): downgrade_eligible_domains()