<commit_before>from datetime import date, timedelta

from django.test import TestCase

from corehq.apps.accounting.models import SoftwarePlanEdition, Subscription, DefaultProductPlan, BillingAccount
from corehq.apps.domain.models import Domain
from corehq.apps.export.utils import get_or_create_default_export_settings_for_domain
from corehq.util.test_utils import flag_enabled


class TestExportUtils(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestExportUtils, cls).setUpClass()
        cls.domain = Domain.get_or_create_with_name('test-export-utils', is_active=True)
        cls.account, _ = BillingAccount.get_or_create_account_by_domain(
            cls.domain.name,
            created_by='<EMAIL>'
        )
        cls.account.save()
        cls.subscription = Subscription.new_domain_subscription(
            cls.account, cls.domain.name,
            DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ENTERPRISE),
            date_start=date.today() - timedelta(days=3)
        )
        cls.subscription.is_active = True
        cls.subscription.save()

    @classmethod
    def tearDownClass(cls):
        cls.domain.delete()
        super(TestExportUtils, cls).tearDownClass()

    def update_subscription(self, plan):
        if self.subscription.plan_version.plan.edition != plan:
            self.subscription.change_plan(DefaultProductPlan.get_default_plan_version(plan))
            self.subscription.save()

    def test_default_export_settings_no_ff_enabled(self):
        """
        Default export settings are only available if the feature flag is enabled
        NOTE: no decorator to enable DEFAULT_EXPORT_SETTINGS feature flag
        """
        self.update_subscription(SoftwarePlanEdition.ENTERPRISE)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_non_enterprise_domain(self):
        """
        Verify software plan editions that do not have access to default export settings
        are not able to create a DefaultExportSettings instance
        """
        def test(plan_edition):
            self.update_subscription(plan_edition)
            settings = get_or_create_default_export_settings_for_domain(self.domain)
            self.assertIsNone(settings)

        yield test, SoftwarePlanEdition.COMMUNITY
        yield test, SoftwarePlanEdition.STANDARD
        yield test, SoftwarePlanEdition.PRO
        yield test, SoftwarePlanEdition.ADVANCED
        yield test, SoftwarePlanEdition.RESELLER
        yield test, SoftwarePlanEdition.MANAGED_HOSTING

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_enterprise_domain(self):
        """
        Verify software plan editions that do have access to default export settings
        are able to create a DefaultExportSettings instance<commit_msg>finally fixed tests<commit_after>from datetime import date, timedelta

from django.test import TestCase

from corehq.apps.accounting.models import SoftwarePlanEdition, Subscription, DefaultProductPlan, BillingAccount, \
    SubscriptionAdjustment
from corehq.apps.domain.models import Domain
from corehq.apps.export.utils import get_or_create_default_export_settings_for_domain
from corehq.util.test_utils import flag_enabled, generate_cases


class TestExportUtils(TestCase):

    def setUp(self):
        super(TestExportUtils, self).setUp()
        self.domain = Domain.get_or_create_with_name('test-export-utils', is_active=True)
        self.account, _ = BillingAccount.get_or_create_account_by_domain(
            self.domain.name,
            created_by='<EMAIL>'
        )
        self.account.save()
        subscription = Subscription.new_domain_subscription(
            self.account, self.domain.name,
            DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ENTERPRISE),
            date_start=date.today() - timedelta(days=3)
        )
        subscription.is_active = True
        subscription.save()

    def tearDown(self):
        self.domain.delete()
        SubscriptionAdjustment.objects.all().delete()
        Subscription.visible_and_suppressed_objects.all().delete()
        self.account.delete()
        super(TestExportUtils, self).tearDown()

    def update_subscription(self, plan):
        current_subscription = Subscription.get_active_subscription_by_domain(self.domain)
        if current_subscription.plan_version.plan.edition != plan:
            current_subscription.change_plan(DefaultProductPlan.get_default_plan_version(plan))

    def test_default_export_settings_no_ff_enabled(self):
        """
        Default export settings are only available if the feature flag is enabled
        NOTE: no decorator to enable DEFAULT_EXPORT_SETTINGS feature flag
        """
        self.update_subscription(SoftwarePlanEdition.ENTERPRISE)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_community_domain(self):
        """
        Verify COMMUNITY software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.COMMUNITY)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_standard_domain(self):
        """
        Verify STANDARD software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.STANDARD)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_pro_domain(self):
        """
        Verify PRO software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.PRO)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_advanced_domain(self):
        """
        Verify ADVANCED software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.ADVANCED)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_reseller_domain(self):
        """
        Verify RESELLER software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.RESELLER)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_managed_hosting_domain(self):
        """
        Verify MANAGED_HOSTING software plans do not have access to default export settings
        """
        self.update_subscription(SoftwarePlanEdition.MANAGED_HOSTING)
        settings = get_or_create_default_export_settings_for_domain(self.domain)
        self.assertIsNone(settings)

    @flag_enabled('DEFAULT_EXPORT_SETTINGS')
    def test_default_export_settings_enterprise_domain(self):
        """
        Verify software plan editions that do have access to default export settings
        are able to create a DefaultExportSettings instance
{'fraction_non_alphanumeric': 0.06787564766839378, 'fraction_numerical': 0.00025906735751295336, 'mean_word_length': 3.5930993456276026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '124566', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2110, 'n_words': 397}