class TestRenewSubscriptions(BaseAccountingTest):

    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 test_simple_renewal(self):
        today = datetime.date.today()
        new_end_date = today + datetime.timedelta(days=9)

        renewed_subscription = self.subscription.renew_subscription(
            date_end=new_end_date
        )

        self.assertEqual(renewed_subscription.date_end, new_end_date)
        self.assertEqual(renewed_subscription.date_start, self.subscription.date_end)
        self.assertEqual(renewed_subscription.plan_version, self.subscription.plan_version)

    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)
class TestRenewSubscriptions(BaseAccountingTest):
    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 test_simple_renewal(self):
        today = datetime.date.today()
        new_end_date = today + datetime.timedelta(days=9)

        renewed_subscription = self.subscription.renew_subscription(
            date_end=new_end_date)

        self.assertEqual(renewed_subscription.date_end, new_end_date)
        self.assertEqual(renewed_subscription.date_start,
                         self.subscription.date_end)
        self.assertEqual(renewed_subscription.plan_version,
                         self.subscription.plan_version)

    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)
示例#3
0
class TestDeleteDomain(TestCase):

    def _create_data(self, domain_name):
        product = Product(domain=domain_name, name='test-product')
        product.save()

        location = Location(
            domain=domain_name,
            site_code='testcode',
            name='test1',
            location_type='facility'
        )
        location.save()
        self.locations[domain_name] = location.sql_location

        user = CommCareUser.create(
            domain=domain_name,
            username='******'.format(domain_name),
            password='******'
        )

        FacilityInCharge.objects.create(
            user_id=user.get_id,
            location=location.sql_location
        )

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        self.locations = {}
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )

        self._create_data('test')
        self._create_data('test2')

    def test_pre_delete_signal_receiver(self):
        self.domain.delete()

        self.assertEqual(FacilityInCharge.objects.filter(location=self.locations['test']).count(), 0)
        self.assertEqual(FacilityInCharge.objects.filter(location=self.locations['test2']).count(), 1)

    def tearDown(self):
        self.domain2.delete()
示例#4
0
class TestSubscription(BaseAccountingTest):

    def setUp(self):
        super(TestSubscription, self).setUp()
        self.billing_contact = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.domain = Domain(name='test')
        self.domain.save()
        self.currency = generator.init_default_currency()
        self.account = generator.billing_account(self.dimagi_user, self.billing_contact)
        self.subscription, self.subscription_length = generator.generate_domain_subscription_from_date(
            datetime.date.today(), self.account, self.domain.name
        )

    def test_creation(self):
        self.assertIsNotNone(self.subscription)

    def test_no_activation(self):
        tasks.activate_subscriptions(based_on_date=self.subscription.date_start - datetime.timedelta(30))
        subscription = Subscription.objects.get(id=self.subscription.id)
        self.assertFalse(subscription.is_active)

    def test_activation(self):
        tasks.activate_subscriptions(based_on_date=self.subscription.date_start)
        subscription = Subscription.objects.get(id=self.subscription.id)
        self.assertTrue(subscription.is_active)

    def test_no_deactivation(self):
        tasks.activate_subscriptions(based_on_date=self.subscription.date_start)
        tasks.deactivate_subscriptions(based_on_date=self.subscription.date_end - datetime.timedelta(30))
        subscription = Subscription.objects.get(id=self.subscription.id)
        self.assertTrue(subscription.is_active)

    def test_deactivation(self):
        tasks.deactivate_subscriptions(based_on_date=self.subscription.date_end)
        subscription = Subscription.objects.get(id=self.subscription.id)
        self.assertFalse(subscription.is_active)

    def test_deletions(self):
        self.assertRaises(models.ProtectedError, self.account.delete)
        self.assertRaises(models.ProtectedError, self.subscription.plan_version.delete)
        self.assertRaises(models.ProtectedError, self.subscription.subscriber.delete)

    def tearDown(self):
        self.billing_contact.delete()
        self.dimagi_user.delete()
        self.domain.delete()

        generator.delete_all_subscriptions()
        generator.delete_all_accounts()
        super(TestSubscription, self).tearDown()
示例#5
0
class TestDeleteDomain(TestCase):
    def _create_data(self, domain_name):
        product = Product(domain=domain_name, name='test-product')
        product.save()

        location = Location(domain=domain_name,
                            site_code='testcode',
                            name='test1',
                            location_type='facility')
        location.save()
        self.locations[domain_name] = location.sql_location

        user = CommCareUser.create(domain=domain_name,
                                   username='******'.format(domain_name),
                                   password='******')

        FacilityInCharge.objects.create(user_id=user.get_id,
                                        location=location.sql_location)

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        self.locations = {}
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )

        self._create_data('test')
        self._create_data('test2')

    def test_pre_delete_signal_receiver(self):
        self.domain.delete()

        self.assertEqual(
            FacilityInCharge.objects.filter(
                location=self.locations['test']).count(), 0)
        self.assertEqual(
            FacilityInCharge.objects.filter(
                location=self.locations['test2']).count(), 1)

    def tearDown(self):
        self.domain2.delete()
示例#6
0
class TestDeleteDomain(TestCase):

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()

        StockDataCheckpoint.objects.create(
            domain='test',
            api='test',
            limit=100,
            offset=0
        )

        StockDataCheckpoint.objects.create(
            domain='test2',
            api='test',
            limit=100,
            offset=0
        )

        MigrationCheckpoint.objects.create(
            domain='test',
            api='test',
            limit=100,
            offset=0
        )

        MigrationCheckpoint.objects.create(
            domain='test2',
            api='test',
            limit=100,
            offset=0
        )

    def test_pre_delete_signal_receiver(self):
        self.domain.delete()

        self.assertEqual(MigrationCheckpoint.objects.filter(domain='test').count(), 0)
        self.assertEqual(StockDataCheckpoint.objects.filter(domain='test').count(), 0)

        self.assertEqual(MigrationCheckpoint.objects.filter(domain='test2').count(), 1)
        self.assertEqual(StockDataCheckpoint.objects.filter(domain='test2').count(), 1)

    def tearDown(self):
        self.domain2.delete()
示例#7
0
class TestBillingRecord(BaseAccountingTest):

    def setUp(self):
        super(TestBillingRecord, self).setUp()
        self.billing_contact = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.domain = Domain(name='test')
        self.domain.save()
        self.invoice_start, self.invoice_end = get_previous_month_date_range()
        self.currency = generator.init_default_currency()
        self.account = generator.billing_account(self.dimagi_user, self.billing_contact)
        self.subscription, self.subscription_length = generator.generate_domain_subscription_from_date(
            datetime.date.today(), self.account, self.domain.name
        )
        self.invoice = Invoice(
            subscription=self.subscription,
            date_start=self.invoice_start,
            date_end=self.invoice_end,
            is_hidden=False,
        )
        self.billing_record = BillingRecord(invoice=self.invoice)

    def test_should_send_email(self):
        self.assertTrue(self.billing_record.should_send_email)

    def test_should_send_email_contracted(self):
        self.subscription.service_type = SubscriptionType.CONTRACTED
        self.assertFalse(self.billing_record.should_send_email)

        self.invoice.balance = Decimal(SMALL_INVOICE_THRESHOLD - 1)
        self.assertFalse(self.billing_record.should_send_email)

        self.invoice.balance = Decimal(SMALL_INVOICE_THRESHOLD + 1)
        self.assertTrue(self.billing_record.should_send_email)

    def test_should_send_email_autogenerate_credits(self):
        self.subscription.auto_generate_credits = True
        self.assertFalse(self.billing_record.should_send_email)

        self.invoice.balance = Decimal(SMALL_INVOICE_THRESHOLD + 1)
        self.assertTrue(self.billing_record.should_send_email)

    def test_should_send_email_hidden(self):
        self.assertTrue(self.billing_record.should_send_email)

        self.invoice.is_hidden = True
        self.assertFalse(self.billing_record.should_send_email)
class TestDeleteDomain(TestCase):

    def _create_data(self, domain_name, i):
        product = Product(domain=domain_name, name='test-{}'.format(i))
        product.save()

        location = Location(
            domain=domain_name,
            site_code='testcode-{}'.format(i),
            name='test-{}'.format(i),
            location_type='facility'
        )
        location.save()
        SupplyPointCase.create_from_location(domain_name, location)
        report = StockReport.objects.create(
            type='balance',
            domain=domain_name,
            form_id='fake',
            date=datetime.utcnow()
        )

        StockTransaction.objects.create(
            report=report,
            product_id=product.get_id,
            sql_product=SQLProduct.objects.get(product_id=product.get_id),
            section_id='stock',
            type='stockonhand',
            case_id=location.linked_supply_point().get_id,
            stock_on_hand=100
        )

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )
        LocationType.objects.create(
            domain='test',
            name='facility2',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility2',
        )
        for i in xrange(2):
            self._create_data('test', i)
            self._create_data('test2', i)

    def _assert_sql_counts(self, domain, number):
        self.assertEqual(StockTransaction.objects.filter(report__domain=domain).count(), number)
        self.assertEqual(StockReport.objects.filter(domain=domain).count(), number)
        self.assertEqual(SQLLocation.objects.filter(domain=domain).count(), number)
        self.assertEqual(SQLProduct.objects.filter(domain=domain).count(), number)
        self.assertEqual(DocDomainMapping.objects.filter(domain_name=domain).count(), number)
        self.assertEqual(LocationType.objects.filter(domain=domain).count(), number)

    def test_sql_objects_deletion(self):
        self._assert_sql_counts('test', 2)
        self.domain.delete()
        self._assert_sql_counts('test', 0)
        self._assert_sql_counts('test2', 2)

    def tearDown(self):
        self.domain2.delete()
示例#9
0
class TestNewDomainSubscription(BaseAccountingTest):
    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_new_susbscription_in_future(self):
        """
        Test covers issue that came up with commcare-hq/PR#3725.
        """
        today = datetime.date.today()
        in_30_days = today + datetime.timedelta(days=30)
        week_after_30 = in_30_days + datetime.timedelta(days=7)
        next_year = week_after_30 + datetime.timedelta(days=400)

        # mimic domain signing up for trial
        trial_subscription = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.advanced_plan,
            date_end=in_30_days,
            adjustment_method=SubscriptionAdjustmentMethod.TRIAL,
            is_trial=True,
        )
        trial_subscription.is_active = True
        trial_subscription.save()

        subscription = Subscription.new_domain_subscription(
            self.account2,
            self.domain.name,
            self.standard_plan,
            web_user=self.admin_user.username,
            date_start=week_after_30,
            date_end=next_year,
        )

        final_sub = Subscription.objects.get(pk=subscription.id)

        self.assertEqual(final_sub.date_start, week_after_30)
        self.assertEqual(final_sub.date_end, next_year)

    def test_conflicting_dates(self):
        """
        Tests creating a subscription with conflicting dates with an existing
        subscription
        """
        today = datetime.date.today()
        one_week = today + datetime.timedelta(days=7)
        one_month = today + datetime.timedelta(days=30)
        Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.advanced_plan,
            date_start=one_week,
            date_end=one_month,
        )

        # conflicting subscription with no date end.
        self.assertRaises(
            NewSubscriptionError, lambda: Subscription.new_domain_subscription(
                self.account,
                self.domain.name,
                self.standard_plan,
            ))

        # conflicting subscription with overlapping end date
        self.assertRaises(
            NewSubscriptionError, lambda: Subscription.new_domain_subscription(
                self.account,
                self.domain.name,
                self.standard_plan,
                date_end=one_week + datetime.timedelta(days=1)))

        # conflicting subscription with overlapping start date
        self.assertRaises(
            NewSubscriptionError, lambda: Subscription.new_domain_subscription(
                self.account,
                self.domain.name,
                self.standard_plan,
                date_start=one_month - datetime.timedelta(days=1)))

        # subscription without overlapping dates before
        # bound future subscription
        sub_before = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.standard_plan,
            date_end=one_week,
        )

        # subscription without overlapping dates after
        # bound future subscription
        sub_after = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.standard_plan,
            date_start=one_month,
        )
class TestNewDomainSubscription(BaseAccountingTest):

    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_new_susbscription_in_future(self):
        """
        Test covers issue that came up with commcare-hq/PR#3725.
        """
        today = datetime.date.today()
        in_30_days = today + datetime.timedelta(days=30)
        week_after_30 = in_30_days + datetime.timedelta(days=7)
        next_year = week_after_30 + datetime.timedelta(days=400)

        # mimic domain signing up for trial
        trial_subscription = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.advanced_plan,
            date_end=in_30_days,
            adjustment_method=SubscriptionAdjustmentMethod.TRIAL,
            is_trial=True,
        )
        trial_subscription.is_active = True
        trial_subscription.save()

        subscription = Subscription.new_domain_subscription(
            self.account2, self.domain.name, self.standard_plan,
            web_user=self.admin_user.username,
            date_start=week_after_30, date_end=next_year,
        )

        final_sub = Subscription.objects.get(pk=subscription.id)

        self.assertEqual(final_sub.date_start, week_after_30)
        self.assertEqual(final_sub.date_end, next_year)

    def test_conflicting_dates(self):
        """
        Tests creating a subscription with conflicting dates with an existing
        subscription
        """
        today = datetime.date.today()
        one_week = today + datetime.timedelta(days=7)
        one_month = today + datetime.timedelta(days=30)
        Subscription.new_domain_subscription(
            self.account, self.domain.name, self.advanced_plan,
            date_start=one_week,
            date_end=one_month,
        )

        # conflicting subscription with no date end.
        self.assertRaises(NewSubscriptionError, lambda: Subscription.new_domain_subscription(
            self.account, self.domain.name, self.standard_plan,
        ))

        # conflicting subscription with overlapping end date
        self.assertRaises(NewSubscriptionError, lambda: Subscription.new_domain_subscription(
            self.account, self.domain.name, self.standard_plan,
            date_end=one_week + datetime.timedelta(days=1)
        ))

        # conflicting subscription with overlapping start date
        self.assertRaises(NewSubscriptionError, lambda: Subscription.new_domain_subscription(
            self.account, self.domain.name, self.standard_plan,
            date_start=one_month - datetime.timedelta(days=1)
        ))

        # subscription without overlapping dates before
        # bound future subscription
        sub_before = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.standard_plan,
            date_end=one_week,
        )

        # subscription without overlapping dates after
        # bound future subscription
        sub_after = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.standard_plan,
            date_start=one_month,
        )
class TestUserRoleSubscriptionChanges(BaseAccountingTest):
    min_subscription_length = 3

    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 test_cancellation(self):
        subscription = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.advanced_plan,
            web_user=self.admin_user.username
        )
        self._change_std_roles()
        subscription.cancel_subscription(web_user=self.admin_user.username)

        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertTrue(custom_role.is_archived)

        # disable this part of the test until we improve the UX for notifying
        # downgraded users of their privilege changes
        # custom_web_user = WebUser.get(self.web_users[0].get_id)
        # custom_commcare_user = CommCareUser.get(self.commcare_users[0].get_id)
        # self.assertEqual(
        #     custom_web_user.get_domain_membership(self.domain.name).role_id,
        #     self.read_only_role.get_id
        # )
        # self.assertIsNone(
        #     custom_commcare_user.get_domain_membership(self.domain.name).role_id
        # )
        
        self._assertInitialRoles()
        self._assertStdUsers()

    def test_resubscription(self):
        subscription = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.advanced_plan,
            web_user=self.admin_user.username
        )
        self._change_std_roles()
        subscription.cancel_subscription(web_user=self.admin_user.username)
        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertTrue(custom_role.is_archived)
        subscription = Subscription.new_domain_subscription(
            self.account, self.domain.name, self.advanced_plan,
            web_user=self.admin_user.username
        )
        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertFalse(custom_role.is_archived)

        # disable this part of the test until we improve the UX for notifying
        # downgraded users of their privilege changes
        # custom_web_user = WebUser.get(self.web_users[0].get_id)
        # custom_commcare_user = CommCareUser.get(self.commcare_users[0].get_id)
        # self.assertEqual(
        #     custom_web_user.get_domain_membership(self.domain.name).role_id,
        #     self.read_only_role.get_id
        # )
        # self.assertIsNone(
        #     custom_commcare_user.get_domain_membership(self.domain.name).role_id
        # )

        self._assertInitialRoles()
        self._assertStdUsers()
        subscription.cancel_subscription(web_user=self.admin_user.username)

    def _change_std_roles(self):
        for u in self.user_roles:
            user_role = UserRole.get(u.get_id)
            user_role.permissions = Permissions(
                view_reports=True, edit_commcare_users=True, edit_apps=True,
                edit_data=True
            )
            user_role.save()

    def _assertInitialRoles(self):
        for u in self.user_roles:
            user_role = UserRole.get(u.get_id)
            self.assertEqual(
                user_role.permissions,
                UserRolePresets.get_permissions(user_role.name)
            )

    def _assertStdUsers(self):
        for ind, wu in enumerate(self.web_users[1:]):
            web_user = WebUser.get(wu.get_id)
            self.assertEqual(
                web_user.get_domain_membership(self.domain.name).role_id,
                self.user_roles[ind].get_id
            )

        for ind, cc in enumerate(self.commcare_users[1:]):
            commcare_user = CommCareUser.get(cc.get_id)
            self.assertEqual(
                commcare_user.get_domain_membership(self.domain.name).role_id,
                self.user_roles[ind].get_id
            )

    def tearDown(self):
        self.domain.delete()
        self.admin_user.delete()
        generator.delete_all_subscriptions()
        generator.delete_all_accounts()
class TestDeleteDomain(TestCase):

    def _create_data(self, domain_name):
        product = Product(domain=domain_name, name='test-product')
        product.save()

        location = Location(
            domain=domain_name,
            site_code='testcode',
            name='test1',
            location_type='facility'
        )
        location.save()
        self.locations[domain_name] = location.get_id

        DeliveryGroupReport.objects.create(
            location_id=location.get_id,
            quantity=1,
            message='test',
            delivery_group='A'
        )

        SupplyPointWarehouseRecord.objects.create(
            supply_point=location.get_id,
            create_date=datetime.utcnow()
        )

        Alert.objects.create(
            text='test',
            expires=datetime.utcnow(),
            date=datetime.utcnow(),
            location_id=location.get_id
        )

        organization_summary = OrganizationSummary.objects.create(
            date=datetime.utcnow(),
            location_id=location.get_id
        )

        GroupSummary.objects.create(
            org_summary=organization_summary
        )

        ProductAvailabilityData.objects.create(
            product=product.get_id,
            date=datetime.utcnow(),
            location_id=location.get_id
        )

        SupplyPointStatus.objects.create(
            location_id=location.get_id,
            status_type='del_fac',
            status_value='received'
        )

        HistoricalLocationGroup.objects.create(
            location_id=location.sql_location,
            group='A',
            date=datetime.utcnow().date()
        )

        ReportRun.objects.create(
            domain=domain_name,
            start=datetime.utcnow(),
            end=datetime.utcnow(),
            start_run=datetime.utcnow()
        )

        ILSNotes.objects.create(
            location=location.sql_location,
            domain=domain_name,
            user_name='test',
            date=datetime.utcnow(),
            text='test'
        )

        SupervisionDocument.objects.create(
            domain=domain_name,
            document='test',
            data_type='test',
            name='test'
        )

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        self.locations = {}
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )

        self._create_data('test')
        self._create_data('test2')

    def test_pre_delete_signal_receiver(self):
        self.domain.delete()

        self.assertEqual(ProductAvailabilityData.objects.filter(location_id=self.locations['test']).count(), 0)
        self.assertEqual(ProductAvailabilityData.objects.filter(location_id=self.locations['test2']).count(), 1)

        self.assertEqual(OrganizationSummary.objects.filter(location_id=self.locations['test']).count(), 0)
        self.assertEqual(OrganizationSummary.objects.filter(location_id=self.locations['test2']).count(), 1)

        self.assertEqual(GroupSummary.objects.filter(org_summary__location_id=self.locations['test']).count(), 0)
        self.assertEqual(GroupSummary.objects.filter(org_summary__location_id=self.locations['test2']).count(), 1)

        self.assertEqual(Alert.objects.filter(location_id=self.locations['test']).count(), 0)
        self.assertEqual(Alert.objects.filter(location_id=self.locations['test2']).count(), 1)

        self.assertEqual(DeliveryGroupReport.objects.filter(location_id=self.locations['test']).count(), 0)
        self.assertEqual(DeliveryGroupReport.objects.filter(location_id=self.locations['test2']).count(), 1)

        self.assertEqual(SupplyPointWarehouseRecord.objects.filter(supply_point=self.locations['test']).count(), 0)
        self.assertEqual(SupplyPointWarehouseRecord.objects.filter(
            supply_point=self.locations['test2']
        ).count(), 1)

        self.assertEqual(SupplyPointStatus.objects.filter(location_id=self.locations['test']).count(), 0)
        self.assertEqual(SupplyPointStatus.objects.filter(location_id=self.locations['test2']).count(), 1)

        self.assertEqual(HistoricalLocationGroup.objects.filter(
            location_id__location_id=self.locations['test']
        ).count(), 0)
        self.assertEqual(HistoricalLocationGroup.objects.filter(
            location_id__location_id=self.locations['test2']
        ).count(), 1)

        self.assertEqual(ReportRun.objects.filter(domain='test').count(), 0)
        self.assertEqual(ReportRun.objects.filter(domain='test2').count(), 1)

        self.assertEqual(ILSNotes.objects.filter(domain='test').count(), 0)
        self.assertEqual(ILSNotes.objects.filter(domain='test2').count(), 1)

        self.assertEqual(SupervisionDocument.objects.filter(domain='test').count(), 0)
        self.assertEqual(SupervisionDocument.objects.filter(domain='test2').count(), 1)

    def tearDown(self):
        self.domain2.delete()
示例#13
0
class TestUserRoleSubscriptionChanges(BaseAccountingTest):
    min_subscription_length = 3

    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 test_cancellation(self):
        subscription = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.advanced_plan,
            web_user=self.admin_user.username)
        self._change_std_roles()
        subscription.cancel_subscription(web_user=self.admin_user.username)

        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertTrue(custom_role.is_archived)

        # disable this part of the test until we improve the UX for notifying
        # downgraded users of their privilege changes
        # custom_web_user = WebUser.get(self.web_users[0].get_id)
        # custom_commcare_user = CommCareUser.get(self.commcare_users[0].get_id)
        # self.assertEqual(
        #     custom_web_user.get_domain_membership(self.domain.name).role_id,
        #     self.read_only_role.get_id
        # )
        # self.assertIsNone(
        #     custom_commcare_user.get_domain_membership(self.domain.name).role_id
        # )

        self._assertInitialRoles()
        self._assertStdUsers()

    def test_resubscription(self):
        subscription = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.advanced_plan,
            web_user=self.admin_user.username)
        self._change_std_roles()
        subscription.cancel_subscription(web_user=self.admin_user.username)
        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertTrue(custom_role.is_archived)
        subscription = Subscription.new_domain_subscription(
            self.account,
            self.domain.name,
            self.advanced_plan,
            web_user=self.admin_user.username)
        custom_role = UserRole.get(self.custom_role.get_id)
        self.assertFalse(custom_role.is_archived)

        # disable this part of the test until we improve the UX for notifying
        # downgraded users of their privilege changes
        # custom_web_user = WebUser.get(self.web_users[0].get_id)
        # custom_commcare_user = CommCareUser.get(self.commcare_users[0].get_id)
        # self.assertEqual(
        #     custom_web_user.get_domain_membership(self.domain.name).role_id,
        #     self.read_only_role.get_id
        # )
        # self.assertIsNone(
        #     custom_commcare_user.get_domain_membership(self.domain.name).role_id
        # )

        self._assertInitialRoles()
        self._assertStdUsers()
        subscription.cancel_subscription(web_user=self.admin_user.username)

    def _change_std_roles(self):
        for u in self.user_roles:
            user_role = UserRole.get(u.get_id)
            user_role.permissions = Permissions(view_reports=True,
                                                edit_commcare_users=True,
                                                edit_apps=True,
                                                edit_data=True)
            user_role.save()

    def _assertInitialRoles(self):
        for u in self.user_roles:
            user_role = UserRole.get(u.get_id)
            self.assertEqual(user_role.permissions,
                             UserRolePresets.get_permissions(user_role.name))

    def _assertStdUsers(self):
        for ind, wu in enumerate(self.web_users[1:]):
            web_user = WebUser.get(wu.get_id)
            self.assertEqual(
                web_user.get_domain_membership(self.domain.name).role_id,
                self.user_roles[ind].get_id)

        for ind, cc in enumerate(self.commcare_users[1:]):
            commcare_user = CommCareUser.get(cc.get_id)
            self.assertEqual(
                commcare_user.get_domain_membership(self.domain.name).role_id,
                self.user_roles[ind].get_id)

    def tearDown(self):
        self.domain.delete()
        self.admin_user.delete()
        generator.delete_all_subscriptions()
        generator.delete_all_accounts()
示例#14
0
class TestDeleteDomain(TestCase):
    def _create_data(self, domain_name):
        product = Product(domain=domain_name, name='test-product')
        product.save()

        location = Location(domain=domain_name,
                            site_code='testcode',
                            name='test1',
                            location_type='facility')
        location.save()
        self.locations[domain_name] = location.get_id

        DeliveryGroupReport.objects.create(location_id=location.get_id,
                                           quantity=1,
                                           message='test',
                                           delivery_group='A')

        SupplyPointWarehouseRecord.objects.create(
            supply_point=location.get_id, create_date=datetime.utcnow())

        Alert.objects.create(text='test',
                             expires=datetime.utcnow(),
                             date=datetime.utcnow(),
                             location_id=location.get_id)

        organization_summary = OrganizationSummary.objects.create(
            date=datetime.utcnow(), location_id=location.get_id)

        GroupSummary.objects.create(org_summary=organization_summary)

        ProductAvailabilityData.objects.create(product=product.get_id,
                                               date=datetime.utcnow(),
                                               location_id=location.get_id)

        SupplyPointStatus.objects.create(location_id=location.get_id,
                                         status_type='del_fac',
                                         status_value='received')

        HistoricalLocationGroup.objects.create(
            location_id=location.sql_location,
            group='A',
            date=datetime.utcnow().date())

        ReportRun.objects.create(domain=domain_name,
                                 start=datetime.utcnow(),
                                 end=datetime.utcnow(),
                                 start_run=datetime.utcnow())

        ILSNotes.objects.create(location=location.sql_location,
                                domain=domain_name,
                                user_name='test',
                                date=datetime.utcnow(),
                                text='test')

        SupervisionDocument.objects.create(domain=domain_name,
                                           document='test',
                                           data_type='test',
                                           name='test')

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        self.locations = {}
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )

        self._create_data('test')
        self._create_data('test2')

    def test_pre_delete_signal_receiver(self):
        self.domain.delete()

        self.assertEqual(
            ProductAvailabilityData.objects.filter(
                location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            ProductAvailabilityData.objects.filter(
                location_id=self.locations['test2']).count(), 1)

        self.assertEqual(
            OrganizationSummary.objects.filter(
                location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            OrganizationSummary.objects.filter(
                location_id=self.locations['test2']).count(), 1)

        self.assertEqual(
            GroupSummary.objects.filter(
                org_summary__location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            GroupSummary.objects.filter(
                org_summary__location_id=self.locations['test2']).count(), 1)

        self.assertEqual(
            Alert.objects.filter(location_id=self.locations['test']).count(),
            0)
        self.assertEqual(
            Alert.objects.filter(location_id=self.locations['test2']).count(),
            1)

        self.assertEqual(
            DeliveryGroupReport.objects.filter(
                location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            DeliveryGroupReport.objects.filter(
                location_id=self.locations['test2']).count(), 1)

        self.assertEqual(
            SupplyPointWarehouseRecord.objects.filter(
                supply_point=self.locations['test']).count(), 0)
        self.assertEqual(
            SupplyPointWarehouseRecord.objects.filter(
                supply_point=self.locations['test2']).count(), 1)

        self.assertEqual(
            SupplyPointStatus.objects.filter(
                location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            SupplyPointStatus.objects.filter(
                location_id=self.locations['test2']).count(), 1)

        self.assertEqual(
            HistoricalLocationGroup.objects.filter(
                location_id__location_id=self.locations['test']).count(), 0)
        self.assertEqual(
            HistoricalLocationGroup.objects.filter(
                location_id__location_id=self.locations['test2']).count(), 1)

        self.assertEqual(ReportRun.objects.filter(domain='test').count(), 0)
        self.assertEqual(ReportRun.objects.filter(domain='test2').count(), 1)

        self.assertEqual(ILSNotes.objects.filter(domain='test').count(), 0)
        self.assertEqual(ILSNotes.objects.filter(domain='test2').count(), 1)

        self.assertEqual(
            SupervisionDocument.objects.filter(domain='test').count(), 0)
        self.assertEqual(
            SupervisionDocument.objects.filter(domain='test2').count(), 1)

    def tearDown(self):
        self.domain2.delete()
示例#15
0
class TestDeleteDomain(TestCase):
    def _create_data(self, domain_name, i):
        product = Product(domain=domain_name, name='test-{}'.format(i))
        product.save()

        location = Location(domain=domain_name,
                            site_code='testcode-{}'.format(i),
                            name='test-{}'.format(i),
                            location_type='facility')
        location.save()
        SupplyPointCase.create_from_location(domain_name, location)
        report = StockReport.objects.create(type='balance',
                                            domain=domain_name,
                                            form_id='fake',
                                            date=datetime.utcnow())

        StockTransaction.objects.create(
            report=report,
            product_id=product.get_id,
            sql_product=SQLProduct.objects.get(product_id=product.get_id),
            section_id='stock',
            type='stockonhand',
            case_id=location.linked_supply_point().get_id,
            stock_on_hand=100)

    def setUp(self):
        self.domain = Domain(name="test", is_active=True)
        self.domain.save()
        self.domain2 = Domain(name="test2", is_active=True)
        self.domain2.save()
        LocationType.objects.create(
            domain='test',
            name='facility',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility',
        )
        LocationType.objects.create(
            domain='test',
            name='facility2',
        )
        LocationType.objects.create(
            domain='test2',
            name='facility2',
        )
        for i in xrange(2):
            self._create_data('test', i)
            self._create_data('test2', i)

    def _assert_sql_counts(self, domain, number):
        self.assertEqual(
            StockTransaction.objects.filter(report__domain=domain).count(),
            number)
        self.assertEqual(
            StockReport.objects.filter(domain=domain).count(), number)
        self.assertEqual(
            SQLLocation.objects.filter(domain=domain).count(), number)
        self.assertEqual(
            SQLProduct.objects.filter(domain=domain).count(), number)
        self.assertEqual(
            DocDomainMapping.objects.filter(domain_name=domain).count(),
            number)
        self.assertEqual(
            LocationType.objects.filter(domain=domain).count(), number)

    def test_sql_objects_deletion(self):
        self._assert_sql_counts('test', 2)
        self.domain.delete()
        self._assert_sql_counts('test', 0)
        self._assert_sql_counts('test2', 2)

    def tearDown(self):
        self.domain2.delete()