def setUp(self): super(TestNewDomainSubscription, self).setUp() self.domain = Domain( name="test-domain-sub", is_active=True, ) self.domain.save() self.domain2 = Domain( name="test-domain-sub2", is_active=True, ) self.domain2.save() self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.account2 = BillingAccount.get_or_create_account_by_domain( self.domain2.name, created_by=self.admin_user.username)[0] self.standard_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.STANDARD) self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.ADVANCED)
def test_transfers(self): advanced_plan = DefaultProductPlan.get_default_plan_by_domain(self.domain, edition=SoftwarePlanEdition.ADVANCED) standard_plan = DefaultProductPlan.get_default_plan_by_domain(self.domain, edition=SoftwarePlanEdition.STANDARD) first_sub = Subscription.new_domain_subscription(self.account, self.domain, advanced_plan) product_credit = CreditLine.add_credit( self.product_credit_amt, subscription=first_sub, product_type=SoftwareProductType.COMMCARE ) feature_credit = CreditLine.add_credit( self.feature_credit_amt, subscription=first_sub, feature_type=FeatureType.USER ) subscription_credit = CreditLine.add_credit(self.subscription_credit_amt, subscription=first_sub) original_credits = [product_credit, feature_credit, subscription_credit] second_sub = first_sub.change_plan(standard_plan) second_credits = self._ensure_transfer(original_credits) for credit_line in second_credits: self.assertEqual(credit_line.subscription.pk, second_sub.pk) second_sub.date_end = datetime.date.today() + datetime.timedelta(days=5) second_sub.save() third_sub = second_sub.renew_subscription() third_credits = self._ensure_transfer(second_credits) for credit_line in third_credits: self.assertEqual(credit_line.subscription.pk, third_sub.pk) third_sub.cancel_subscription() account_credits = self._ensure_transfer(third_credits) for credit_line in account_credits: self.assertIsNone(credit_line.subscription) self.assertEqual(credit_line.account.pk, self.account.pk)
def test_transfers(self): advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.ADVANCED) standard_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.STANDARD) first_sub = Subscription.new_domain_subscription( self.account, self.domain, advanced_plan) product_credit = CreditLine.add_credit( self.product_credit_amt, subscription=first_sub, product_type=SoftwareProductType.COMMCARE, ) feature_credit = CreditLine.add_credit( self.feature_credit_amt, subscription=first_sub, feature_type=FeatureType.USER, ) subscription_credit = CreditLine.add_credit( self.subscription_credit_amt, subscription=first_sub, ) original_credits = [ product_credit, feature_credit, subscription_credit, ] second_sub = first_sub.change_plan(standard_plan) second_credits = self._ensure_transfer(original_credits) for credit_line in second_credits: self.assertEqual(credit_line.subscription.pk, second_sub.pk) second_sub.date_end = datetime.date.today() + datetime.timedelta( days=5) second_sub.save() third_sub = second_sub.renew_subscription() third_credits = self._ensure_transfer(second_credits) for credit_line in third_credits: self.assertEqual(credit_line.subscription.pk, third_sub.pk) third_sub.cancel_subscription() account_credits = self._ensure_transfer(third_credits) for credit_line in account_credits: self.assertIsNone(credit_line.subscription) self.assertEqual(credit_line.account.pk, self.account.pk)
def create_domain(self, domain): domain_obj = Domain(name=domain) domain_obj.use_default_sms_response = True domain_obj.default_sms_response = "Default SMS Response" domain_obj.save() # I tried making this class inherit from BaseSMSTest, but somehow # the multiple inheritance was causing the postgres connection to # get in a weird state where it wasn't commiting any changes. So # for now, keeping this subscription setup code as is. generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() return domain_obj
def setUp(self): super(OptTestCase, self).setUp() self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=self.backend._id, ) self.backend_mapping.save()
def setUp(self): super(BaseReminderTestCase, self).setUp() self.domain_obj = Domain(name="test") self.domain_obj.save() # Prevent resource conflict self.domain_obj = Domain.get(self.domain_obj._id) self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="tests" ) advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, advanced_plan_version ) self.subscription.is_active = True self.subscription.save() self.sms_backend = TestSMSBackend(named="MOBILE_BACKEND_TEST", is_global=True) self.sms_backend.save() self.sms_backend_mapping = BackendMapping(is_global=True,prefix="*",backend_id=self.sms_backend._id) self.sms_backend_mapping.save()
def setUp(self): super(TestRenewSubscriptions, self).setUp() self.domain = Domain( name="test-domain-sub", is_active=True, ) self.domain.save() self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.standard_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.STANDARD) today = datetime.date.today() yesterday = today + datetime.timedelta(days=-1) tomorrow = today + datetime.timedelta(days=1) self.subscription = Subscription.new_domain_subscription( self.account, self.domain.name, self.standard_plan, web_user=self.admin_user.username, date_start=yesterday, date_end=tomorrow, ) self.subscription.save()
def create_30_day_trial(domain_obj): from corehq.apps.accounting.models import ( DefaultProductPlan, SoftwarePlanEdition, BillingAccount, Currency, BillingAccountType, Subscription, SubscriptionAdjustmentMethod, ) # Create a 30 Day Trial subscription to the Advanced Plan advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED, is_trial=True) expiration_date = date.today() + timedelta(days=30) trial_account = BillingAccount.objects.get_or_create( name="Trial Account for %s" % domain_obj.name, currency=Currency.get_default(), created_by_domain=domain_obj.name, account_type=BillingAccountType.TRIAL, )[0] trial_subscription = Subscription.new_domain_subscription( trial_account, domain_obj.name, advanced_plan_version, date_end=expiration_date, adjustment_method=SubscriptionAdjustmentMethod.TRIAL, is_trial=True, ) trial_subscription.is_active = True trial_subscription.save()
def setUp(self): super(TestUserRoleSubscriptionChanges, self).setUp() self.domain = generator.arbitrary_domain() UserRole.init_domain_with_presets(self.domain.name) self.user_roles = UserRole.by_domain(self.domain.name) self.custom_role = UserRole.get_or_create_with_permissions( self.domain.name, Permissions(edit_apps=True, edit_web_users=True), "Custom Role" ) self.custom_role.save() self.read_only_role = UserRole.get_read_only_role_by_domain(self.domain.name) self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.web_users = [] self.commcare_users = [] for role in [self.custom_role] + self.user_roles: web_user = generator.arbitrary_web_user() web_user.add_domain_membership(self.domain.name, role_id=role.get_id) web_user.save() self.web_users.append(web_user) commcare_user = generator.arbitrary_commcare_user( domain=self.domain.name) commcare_user.set_role(self.domain.name, role.get_qualified_id()) commcare_user.save() self.commcare_users.append(commcare_user) self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name,created_by=self.admin_user.username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name,edition=SoftwarePlanEdition.ADVANCED)
def setUp(self): super(OptTestCase, self).setUp() self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan) self.subscription.is_active = True self.subscription.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=self.backend._id, ) self.backend_mapping.save()
def prepare_domain(domain_name): from corehq.apps.commtrack.tests import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False)]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered')) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name) ils_config.save() return domain
def create_30_day_trial(domain_obj): from corehq.apps.accounting.models import ( DefaultProductPlan, SoftwarePlanEdition, BillingAccount, Currency, BillingAccountType, Subscription, SubscriptionAdjustmentMethod, ) # Create a 30 Day Trial subscription to the Advanced Plan advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED, is_trial=True ) expiration_date = date.today() + timedelta(days=30) trial_account = BillingAccount.objects.get_or_create( name="Trial Account for %s" % domain_obj.name, currency=Currency.get_default(), created_by_domain=domain_obj.name, account_type=BillingAccountType.TRIAL, )[0] trial_subscription = Subscription.new_domain_subscription( trial_account, domain_obj.name, advanced_plan_version, date_end=expiration_date, adjustment_method=SubscriptionAdjustmentMethod.TRIAL, is_trial=True, ) trial_subscription.is_active = True trial_subscription.save()
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True)[0] if account.date_confirmed_extra_charges is None: subject = "[%s] Invoice Generation Issue" % self.domain.name email_content = render_to_string( 'accounting/invoice_error_email.html', { 'project': self.domain.name, 'error_msg': "This project is incurring charges on their " "Community subscription, but they haven't " "agreed to the charges yet. Someone should " "follow up with this project to see if everything " "is configured correctly or if communication " "needs to happen between Dimagi and the project's" "admins. For now, the invoices generated are " "marked as Do Not Invoice.", }) send_HTML_email(subject, settings.BILLING_EMAIL, email_content, email_from="Dimagi Billing Bot <%s>" % settings.DEFAULT_FROM_EMAIL) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise BillingContactInfoError( "Project %s has incurred charges, but does not have their " "Billing Contact Info filled out. Someone should follow up " "on this." % self.domain.name) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True)[0] if account.date_confirmed_extra_charges is None: if self.domain.is_active: subject = "[%s] Invoice Generation Issue" % self.domain.name email_content = render_to_string( 'accounting/invoice_error_email.html', { 'project': self.domain.name, 'error_msg': "This project is incurring charges on their " "Community subscription, but they haven't " "agreed to the charges yet. Someone should " "follow up with this project to see if everything " "is configured correctly or if communication " "needs to happen between Dimagi and the project's" "admins. For now, the invoices generated are " "marked as Do Not Invoice.", } ) send_HTML_email( subject, settings.BILLING_EMAIL, email_content, email_from="Dimagi Billing Bot <%s>" % settings.DEFAULT_FROM_EMAIL ) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise BillingContactInfoError( "Project %s has incurred charges, but does not have their " "Billing Contact Info filled out. Someone should follow up " "on this." % self.domain.name ) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def test_change_plan_on_renewal(self): new_edition = SoftwarePlanEdition.ADVANCED new_plan = DefaultProductPlan.get_default_plan_by_domain(self.domain.name, new_edition) self.renewed_subscription = self.subscription.renew_subscription( new_version=new_plan ) self.assertEqual(self.renewed_subscription.plan_version, new_plan)
def setUp(self): self._clear_docs() self.domain = create_domain(DOMAIN) self.account = BillingAccount.get_or_create_account_by_domain(DOMAIN, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_by_domain(DOMAIN, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription(self.account, DOMAIN, plan) self.subscription.is_active = True self.subscription.save() self.couch_user = WebUser.create(None, "test", "foobar") self.couch_user.add_domain_membership(DOMAIN, is_admin=True) self.couch_user.save()
def test_change_plan_on_renewal(self): today = datetime.date.today() new_end_date = today + datetime.timedelta(days=9) new_edition = SoftwarePlanEdition.ADVANCED new_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, new_edition) renewed_subscription = self.subscription.renew_subscription( date_end=new_end_date, new_version=new_plan) self.assertEqual(renewed_subscription.plan_version, new_plan)
def test_change_plan_on_renewal(self): today = datetime.date.today() new_end_date = today + datetime.timedelta(days=9) new_edition = SoftwarePlanEdition.ADVANCED new_plan = DefaultProductPlan.get_default_plan_by_domain(self.domain.name, new_edition) renewed_subscription = self.subscription.renew_subscription( date_end=new_end_date, new_version=new_plan ) self.assertEqual(renewed_subscription.plan_version, new_plan)
def setup_subscription(cls, domain_name, software_plan): generator.instantiate_accounting_for_tests() plan = DefaultProductPlan.get_default_plan_by_domain( domain_name, edition=software_plan ) cls.account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__ )[0] cls.subscription = Subscription.new_domain_subscription(cls.account, domain_name, plan) cls.subscription.is_active = True cls.subscription.save()
def prepare_domain(domain_name): from corehq.apps.commtrack.tests.util import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [ ("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False) ]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED ) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered') ) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True) ils_config.save() fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields') fields_definition.fields.append(CustomDataField( slug='group', label='Group', is_required=False, choices=['A', 'B', 'C'], is_multiple_choice=False )) fields_definition.save() return domain
def prepare_domain(domain_name): domain = create_domain(domain_name) domain.convert_to_commtrack() domain.default_sms_backend_id = TEST_BACKEND domain.save() def _make_loc_type(name, administrative=False, parent_type=None): return LocationType.objects.get_or_create( domain=domain_name, name=name, administrative=administrative, parent_type=parent_type, )[0] country = _make_loc_type(name="country", administrative=True) _make_loc_type(name="Central Medical Store", parent_type=country) _make_loc_type(name="Teaching Hospital", parent_type=country) region = _make_loc_type(name="region", administrative=True, parent_type=country) _make_loc_type(name="Regional Medical Store", parent_type=region) _make_loc_type(name="Regional Hospital", parent_type=region) district = _make_loc_type(name="district", administrative=True, parent_type=region) _make_loc_type(name="Clinic", parent_type=district) _make_loc_type(name="District Hospital", parent_type=district) _make_loc_type(name="Health Centre", parent_type=district) _make_loc_type(name="CHPS Facility", parent_type=district) _make_loc_type(name="Hospital", parent_type=district) _make_loc_type(name="Psychiatric Hospital", parent_type=district) _make_loc_type(name="Polyclinic", parent_type=district) _make_loc_type(name="facility", parent_type=district) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED ) subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ews_config = EWSGhanaConfig(enabled=True, domain=domain.name) ews_config.save() return domain
def create_account_and_subscription(self, domain_name): self.account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain_name, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, domain_name, plan ) self.subscription.is_active = True self.subscription.save()
def setUp(self): super(TestDomainInvoiceFactory, self).setUp() self.invoice_start, self.invoice_end = get_previous_month_date_range() self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain( domain=self.domain.name, created_by="TEST")[0] self.community = DefaultProductPlan.get_default_plan_by_domain( self.domain).plan.get_version() generator.arbitrary_commcare_users_for_domain( self.domain.name, self.community.user_limit + 1) self.invoice_factory = DomainInvoiceFactory(self.invoice_start, self.invoice_end, self.domain)
def prepare_domain(domain_name): from corehq.apps.commtrack.tests import bootstrap_domain domain = bootstrap_domain(domain_name) def _make_loc_type(name, administrative=False, parent_type=None): return LocationType.objects.get_or_create( domain=domain_name, name=name, administrative=administrative, parent_type=parent_type, )[0] country = _make_loc_type(name="country", administrative=True) _make_loc_type(name="Central Medical Store", parent_type=country) _make_loc_type(name="Teaching Hospital", parent_type=country) region = _make_loc_type(name="region", administrative=True, parent_type=country) _make_loc_type(name="Regional Medical Store", parent_type=region) _make_loc_type(name="Regional Hospital", parent_type=region) district = _make_loc_type(name="district", administrative=True, parent_type=region) _make_loc_type(name="Clinic", parent_type=district) _make_loc_type(name="District Hospital", parent_type=district) _make_loc_type(name="Health Centre", parent_type=district) _make_loc_type(name="CHPS Facility", parent_type=district) _make_loc_type(name="Hospital", parent_type=district) _make_loc_type(name="Psychiatric Hospital", parent_type=district) _make_loc_type(name="Polyclinic", parent_type=district) _make_loc_type(name="facility", parent_type=district) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED) subscription = Subscription.new_domain_subscription( account, domain.name, plan) subscription.is_active = True subscription.save() ews_config = EWSGhanaConfig(enabled=True, domain=domain.name) ews_config.save() return domain
def setUp(self): super(TestSubscriptionPermissionsChanges, self).setUp() self.domain = Domain( name="test-sub-changes", is_active=True, ) self.domain.save() self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.ADVANCED)
def _setup_subscription(cls, domain_name, software_plan): plan = DefaultProductPlan.get_default_plan_by_domain( domain_name, edition=software_plan ) account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__ )[0] subscription = Subscription.new_domain_subscription( account, domain_name, plan, date_start=datetime.date.today() - datetime.timedelta(days=1), date_end=datetime.date.today() + datetime.timedelta(days=5)) subscription.save() cls._to_delete.append(account) cls._to_delete.append(subscription)
def setUpClass(cls): super(UpdateLocationKeywordTest, cls).setUpClass() cls.domain = "opt-test" cls.domain_obj = Domain(name=cls.domain) cls.domain_obj.save() generator.instantiate_accounting_for_tests() cls.account = BillingAccount.get_or_create_account_by_domain( cls.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( cls.domain_obj, edition=SoftwarePlanEdition.ADVANCED ) cls.subscription = Subscription.new_domain_subscription( cls.account, cls.domain_obj.name, plan ) cls.subscription.is_active = True cls.subscription.save() cls.backend = TestSMSBackend(is_global=True) cls.backend.save() cls.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=cls.backend._id, ) cls.backend_mapping.save() cls.user = create_mobile_worker(cls.domain, 'test', '*****', '4444') cls.location_type = LocationType.objects.create( domain=cls.domain, name='test' ) cls.location = Location( domain=cls.domain, name='test', site_code='site_code', location_type='test' ) cls.location.save()
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True, entry_point=EntryPoint.SELF_STARTED, )[0] if account.date_confirmed_extra_charges is None: logger.info( "Did not generate invoice because date_confirmed_extra_charges " "was null for domain %s" % self.domain.name ) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise BillingContactInfoError( "Project %s has incurred charges, but does not have their " "Billing Contact Info filled out." % self.domain.name ) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def setUp(self): super(TestDomainInvoiceFactory, self).setUp() self.invoice_start, self.invoice_end = get_previous_month_date_range() self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain( domain=self.domain.name, created_by="TEST" )[0] self.community = DefaultProductPlan.get_default_plan_by_domain( self.domain).plan.get_version() generator.arbitrary_commcare_users_for_domain( self.domain.name, self.community.user_limit + 1 ) self.invoice_factory = DomainInvoiceFactory( self.invoice_start, self.invoice_end, self.domain )
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True)[0] if account.date_confirmed_extra_charges is None: logger.error( "[BILLING] " "Domain '%s' is going to get charged on " "Community, but they haven't formally acknowledged this. " "Someone on ops should reconcile this soon. To be on the " "safe side, we've marked the invoices as Do Not Invoice." % self.domain.name ) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise InvoiceError("No Billing Contact Info could be found " "for domain '%s'." % self.domain.name) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True, entry_point=EntryPoint.SELF_STARTED, )[0] if account.date_confirmed_extra_charges is None: logger.info( "Did not generate invoice because date_confirmed_extra_charges " "was null for domain %s" % self.domain.name) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise BillingContactInfoError( "Project %s has incurred charges, but does not have their " "Billing Contact Info filled out." % self.domain.name) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def test_product_plan_for_community_invoices(self): """ Make sure that the different product domains get the different community plans by product type. """ domains_by_product = generator.arbitrary_domains_by_product_type() for domain in domains_by_product.values(): generator.create_excess_community_users(domain) tasks.generate_invoices() for product_type, domain in domains_by_product.items(): subscriber = Subscriber.objects.get(domain=domain.name) self.assertEqual(subscriber.subscription_set.count(), 1) subscription = subscriber.subscription_set.get() self.assertEqual(subscription.invoice_set.count(), 1) expected_community_plan = DefaultProductPlan.get_default_plan_by_domain(domain) self.assertEqual( subscription.plan_version.plan, expected_community_plan.plan ) domain.delete()
def prepare_domain(domain_name): from corehq.apps.commtrack.tests import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [ ("MOHSW", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False) ]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED ) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered') ) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name) ils_config.save() return domain
def _ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self._get_community_ranges(subscriptions) if not community_ranges: return # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, entry_point=EntryPoint.SELF_STARTED, )[0] if account.date_confirmed_extra_charges is None: log_accounting_info( "Did not generate invoice because date_confirmed_extra_charges " "was null for domain %s" % self.domain.name ) do_not_invoice = True for start_date, end_date in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=start_date, date_end=end_date, do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def test_wrong_domain(self): """ If correct credentials for a user in a different domain are submitted, the response is forbidden """ wrong_domain = Domain.get_or_create_with_name("dvorak", is_active=True) # have to set up subscription for the bad domain or it will fail on authorization new_account = BillingAccount.get_or_create_account_by_domain(wrong_domain.name, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_by_domain(wrong_domain.name, edition=SoftwarePlanEdition.ADVANCED) new_subscription = Subscription.new_domain_subscription(new_account, wrong_domain.name, plan) new_subscription.is_active = True new_subscription.save() wrong_list_endpoint = reverse( "api_dispatch_list", kwargs=dict( domain=wrong_domain.name, api_name=self.api_name, resource_name=self.resource.Meta.resource_name ), ) response = self.client.post(wrong_list_endpoint, {"username": self.username, "password": self.password}) self.assertEqual(response.status_code, 403) wrong_domain.delete()
def create_30_day_advanced_trial(domain_obj): # Create a 30 Day Trial subscription to the Advanced Plan advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED, is_trial=True ) expiration_date = date.today() + timedelta(days=30) trial_account = BillingAccount.objects.get_or_create( name="Trial Account for %s" % domain_obj.name, currency=Currency.get_default(), created_by_domain=domain_obj.name, account_type=BillingAccountType.TRIAL, pre_or_post_pay=PreOrPostPay.POSTPAY, )[0] trial_subscription = Subscription.new_domain_subscription( trial_account, domain_obj.name, advanced_plan_version, date_end=expiration_date, adjustment_method=SubscriptionAdjustmentMethod.TRIAL, is_trial=True, service_type=SubscriptionType.TRIAL ) trial_subscription.is_active = True trial_subscription.save()
def setUp(self): super(TestUserRoleSubscriptionChanges, self).setUp() self.domain = Domain( name="test-sub-changes", is_active=True, ) self.domain.save() UserRole.init_domain_with_presets(self.domain.name) self.user_roles = UserRole.by_domain(self.domain.name) self.custom_role = UserRole.get_or_create_with_permissions( self.domain.name, Permissions(edit_apps=True, edit_web_users=True), "Custom Role") self.custom_role.save() self.read_only_role = UserRole.get_read_only_role_by_domain( self.domain.name) self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.web_users = [] self.commcare_users = [] for role in [self.custom_role] + self.user_roles: web_user = generator.arbitrary_web_user() web_user.add_domain_membership(self.domain.name, role_id=role.get_id) web_user.save() self.web_users.append(web_user) commcare_user = generator.arbitrary_commcare_user( domain=self.domain.name) commcare_user.set_role(self.domain.name, role.get_qualified_id()) commcare_user.save() self.commcare_users.append(commcare_user) self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.ADVANCED)
def create_domain(self, domain): domain_obj = Domain(name=domain) domain_obj.use_default_sms_response = True domain_obj.default_sms_response = "Default SMS Response" domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() return domain_obj
def request_new_domain(request, form, org, domain_type=None, new_user=True): now = datetime.utcnow() current_user = CouchUser.from_django_user(request.user) commtrack_enabled = domain_type == 'commtrack' dom_req = RegistrationRequest() if new_user: dom_req.request_time = now dom_req.request_ip = get_ip(request) dom_req.activation_guid = uuid.uuid1().hex new_domain = Domain( name=form.cleaned_data['domain_name'], is_active=False, date_created=datetime.utcnow(), commtrack_enabled=commtrack_enabled, creating_user=current_user.username, secure_submissions=True, ) if form.cleaned_data.get('domain_timezone'): new_domain.default_timezone = form.cleaned_data['domain_timezone'] if org: new_domain.organization = org new_domain.hr_name = request.POST.get('domain_hrname', None) or new_domain.name if not new_user: new_domain.is_active = True # ensure no duplicate domain documents get created on cloudant new_domain.save(**get_safe_write_kwargs()) if not new_domain.name: new_domain.name = new_domain._id new_domain.save() # we need to get the name from the _id # Create a 30 Day Trial subscription to the Advanced Plan advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( new_domain, edition=SoftwarePlanEdition.ADVANCED, is_trial=True ) expiration_date = date.today() + timedelta(days=30) trial_account = BillingAccount.objects.get_or_create( name="Trial Account for %s" % new_domain.name, currency=Currency.get_default(), created_by_domain=new_domain.name, account_type=BillingAccountType.TRIAL, )[0] trial_subscription = Subscription.new_domain_subscription( trial_account, new_domain.name, advanced_plan_version, date_end=expiration_date, adjustment_method=SubscriptionAdjustmentMethod.TRIAL, is_trial=True, ) trial_subscription.is_active = True trial_subscription.save() dom_req.domain = new_domain.name if request.user.is_authenticated(): if not current_user: current_user = WebUser() current_user.sync_from_django_user(request.user) current_user.save() current_user.add_domain_membership(new_domain.name, is_admin=True) current_user.save() dom_req.requesting_user_username = request.user.username dom_req.new_user_username = request.user.username if new_user: dom_req.save() send_domain_registration_email(request.user.email, dom_req.domain, dom_req.activation_guid) else: send_global_domain_registration_email(request.user, new_domain.name) send_new_request_update_email(request.user, get_ip(request), new_domain.name, is_new_user=new_user)
def setUp(self): super(BackendTestCase, self).setUp() self.domain = "test-domain" self.domain2 = "test-domain2" self.site, self.site_created = self.get_or_create_site() self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan) self.subscription.is_active = True self.subscription.save() self.domain_obj = Domain.get( self.domain_obj._id) # Prevent resource conflict self.backend1 = TestCaseBackend(name="BACKEND1", is_global=True) self.backend1.save() self.backend2 = TestCaseBackend(name="BACKEND2", is_global=True) self.backend2.save() self.backend3 = TestCaseBackend(name="BACKEND3", is_global=True) self.backend3.save() self.backend4 = TestCaseBackend(name="BACKEND4", is_global=True) self.backend4.save() self.backend5 = TestCaseBackend(name="BACKEND5", domain=self.domain, is_global=False, authorized_domains=[]) self.backend5.save() self.backend6 = TestCaseBackend(name="BACKEND6", domain=self.domain2, is_global=False, authorized_domains=[self.domain]) self.backend6.save() self.backend7 = TestCaseBackend(name="BACKEND7", domain=self.domain2, is_global=False, authorized_domains=[]) self.backend7.save() self.backend8 = TestCaseBackend(name="BACKEND", domain=self.domain, is_global=False, authorized_domains=[]) self.backend8.save() self.backend9 = TestCaseBackend(name="BACKEND", domain=self.domain2, is_global=False, authorized_domains=[self.domain]) self.backend9.save() self.backend10 = TestCaseBackend(name="BACKEND", is_global=True) self.backend10.save() self.backend_mapping1 = BackendMapping(is_global=True, prefix="*", backend_id=self.backend1._id) self.backend_mapping1.save() self.backend_mapping2 = BackendMapping(is_global=True, prefix="1", backend_id=self.backend2._id) self.backend_mapping2.save() self.backend_mapping3 = BackendMapping(is_global=True, prefix="91", backend_id=self.backend3._id) self.backend_mapping3.save() self.backend_mapping4 = BackendMapping(is_global=True, prefix="265", backend_id=self.backend4._id) self.backend_mapping4.save() self.backend_mapping5 = BackendMapping(is_global=True, prefix="256", backend_id=self.backend5._id) self.backend_mapping5.save() self.backend_mapping6 = BackendMapping(is_global=True, prefix="25670", backend_id=self.backend6._id) self.backend_mapping6.save() self.backend_mapping7 = BackendMapping(is_global=True, prefix="25675", backend_id=self.backend7._id) self.backend_mapping7.save() self.case = CommCareCase(domain=self.domain) self.case.set_case_property("contact_phone_number", "15551234567") self.case.set_case_property("contact_phone_number_is_verified", "1") self.case.save() self.contact = CommConnectCase.wrap(self.case.to_json()) settings.SMS_LOADED_BACKENDS.append( "corehq.apps.sms.tests.TestCaseBackend")
def setUpClass(cls): cls.domain = initial_bootstrap(TEST_DOMAIN) db = get_db() if db.doc_exist(DOMAIN_MODULE_KEY): module_config = db.open_doc(DOMAIN_MODULE_KEY) module_map = module_config.get('module_map') if module_map: module_map[TEST_DOMAIN] = 'custom.ewsghana' else: module_config['module_map'][TEST_DOMAIN] = 'custom.ewsghana' else: module_config = db.save_doc({ '_id': DOMAIN_MODULE_KEY, 'module_map': { 'ewsghana-test-input-stock': 'custom.ewsghana' } }) db.save_doc(module_config) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( cls.domain, edition=SoftwarePlanEdition.ENTERPRISE) subscription = Subscription.new_domain_subscription( account, cls.domain.name, plan) subscription.is_active = True subscription.save() cls.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') cls.api_object = EWSApi(TEST_DOMAIN, cls.endpoint) cls.api_object.prepare_commtrack_config() cls.api_object.prepare_custom_fields() cls.datapath = os.path.join(os.path.dirname(__file__), 'data') with open(os.path.join(cls.datapath, 'sample_products.json')) as f: for p in json.loads(f.read()): cls.api_object.product_sync(Product(p)) with open(os.path.join(cls.datapath, 'sample_locations.json')) as f: for loc in json.loads(f.read()): cls.api_object.location_sync(Location(loc)) cls.test_facility3 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='tsactive') cls.testregion2 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testregion2') cls.rsp = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='rsp') cls.test_district = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testdistrict') cls.username1 = 'ews_user1' cls.password1 = 'dummy' cls.web_user1 = WebUser.create(TEST_DOMAIN, cls.username1, cls.password1) cls.web_user1.eula.signed = True cls.web_user1.save() cls.username2 = 'ews_user2' cls.password2 = 'dummy' cls.web_user2 = WebUser.create(TEST_DOMAIN, cls.username2, cls.password2) cls.web_user2.get_domain_membership( TEST_DOMAIN).location_id = cls.test_facility3.location_id cls.web_user2.eula.signed = True cls.web_user2.save() cls.username3 = 'ews_user3' cls.password3 = 'dummy' cls.web_user3 = WebUser.create(TEST_DOMAIN, cls.username3, cls.password3) cls.web_user3.get_domain_membership( TEST_DOMAIN).location_id = cls.testregion2.location_id cls.web_user3.eula.signed = True cls.web_user3.save() cls.username4 = 'ews_user4' cls.password4 = 'dummy' cls.web_user4 = WebUser.create(TEST_DOMAIN, cls.username4, cls.password4) cls.web_user4.get_domain_membership( TEST_DOMAIN).location_id = cls.rsp.location_id cls.web_user4.eula.signed = True cls.web_user4.save() cls.username5 = 'ews_user5' cls.password5 = 'dummy' cls.web_user5 = WebUser.create(TEST_DOMAIN, cls.username5, cls.password5) domain_membership = cls.web_user5.get_domain_membership(TEST_DOMAIN) domain_membership.location_id = cls.test_district.location_id domain_membership.role_id = UserRole.get_read_only_role_by_domain( cls.domain.name).get_id cls.web_user5.eula.signed = True cls.web_user5.save() cls.ad = SQLProduct.objects.get(domain=TEST_DOMAIN, code='ad') cls.al = SQLProduct.objects.get(domain=TEST_DOMAIN, code='al') cls.client = Client()
def set_up_subscription(cls): cls.account = BillingAccount.get_or_create_account_by_domain(cls.domain.name, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_by_domain(cls.domain.name, edition=SoftwarePlanEdition.ADVANCED) cls.subscription = Subscription.new_domain_subscription(cls.account, cls.domain.name, plan) cls.subscription.is_active = True cls.subscription.save()